全部产品
云市场

Python

更新时间:2019-10-14 16:11:23

准备工作

在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问访问密钥(AccessKey)。详情参考创建AccessKey

安装Python SDK核心库。

  • 使用依赖包工具安装(推荐)。
    • 执行以下命令,安装阿里云SDK核心库:pip install aliyun-python-sdk-core
  • 自行下载安装: 您可以使用git clone或其它手段下载aliyun-python-sdk-core并自行添加解决方案。aliyun-python-sdk-core GitHub地址:aliyun-python-sdk-core

安装IVPD Python SDK。

  • 使用依赖包工具安装(推荐)。
    • 执行以下命令,安装图像搜索Python SDK:pip install aliyun-python-sdk-ivpd
  • 自行下载安装:您可以使用git clone或其它手段下载aliyun-python-sdk-imagesearch并自行添加解决方案。aliyun-python-sdk-imagesearch GitHub地址:aliyun-python-sdk-ivpd

完整代码示例

  1. # -*- coding: utf8 -*-
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkivpd.request.v20190625 import *
  4. # 创建 AcsClient 实例
  5. client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "<region>")
  6. # 抠图
  7. request = SegmentImageRequest()
  8. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/aliyun-doc/pop/images/segment-image-src.jpg")
  9. response = client.do_action_with_exception(request)
  10. print(response)
  11. # 清晰化/超分辨率
  12. request = DetectImageElementsRequest()
  13. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/MakeSuperResolution/sup-dog.png")
  14. response = client.do_action_with_exception(request)
  15. print(response)
  16. # 尺寸变换
  17. request = ChangeImageSizeRequest()
  18. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/IMG_20190515_202100.jpg")
  19. request.set_Width(500)
  20. request.set_Height(600)
  21. response = client.do_action_with_exception(request)
  22. print(response)
  23. # 图像元素检测
  24. request = DetectImageElementsRequest()
  25. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/IMG_20190515_202100.jpg")
  26. response = client.do_action_with_exception(request)
  27. print(response)
  28. # 风格迁移
  29. request = ExtendImageStyleRequest()
  30. request.set_StyleUrl("https://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/ExtendImageStyle/styleUrl.jpeg")
  31. request.set_MajorUrl("https://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/ExtendImageStyle/majorUrl.jpeg")
  32. response = client.do_action_with_exception(request)
  33. print(response)
  34. # 色板识别
  35. request = RecognizeImageColorRequest()
  36. request.set_Url("https://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/RecognizeImageColor/RecognizeImageColor.png")
  37. request.set_ColorCount(3)
  38. response = client.do_action_with_exception(request)
  39. print(response)
  40. # 风格识别
  41. request = RecognizeImageStyleRequest()
  42. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/RecognizeImageStyle/technology.png")
  43. response = client.do_action_with_exception(request)
  44. print(response)
  45. # 色彩迁移
  46. request = RecolorImageRequest()
  47. #自动拓色
  48. request.set_Mode("AUTO")
  49. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/RecolorImage/recolor-1-src.png")
  50. request.set_ColorCount(3)
  51. response = client.do_action_with_exception(request)
  52. print(response)
  53. #参考图拓色
  54. request.set_Mode("REF_PIC")
  55. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/RecolorImage/recolor-refurl-src.png")
  56. response = client.do_action_with_exception(request)
  57. print(response)
  58. #色板拓色
  59. request.setMode("TEMPLATE")
  60. request.set_Url("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/pop-sample/images/RecolorImage/recolor-2-src.png")
  61. colorTemplateList = ()
  62. colorTemplateList.add({"Color": "FF0000"}) # 16711680
  63. colorTemplateList.add({"Color": "00FF00"}) # 65280
  64. colorTemplateList.add({"Color": "0000FF"}) # 255
  65. request.set_ColorTemplates(colorTemplateList)
  66. response = client.do_action_with_exception(request)
  67. print(response)