本文介绍了如何使用阿里云视觉智能开放平台相关服务的Python SDK,具体包括SDK的获取和安装方法以及SDK代码示例。

准备工作

在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问访问密钥(AccessKey)。详情请参见创建AccessKey
  • 安装Python SDK核心库。
    • 使用依赖包工具安装(推荐)。
    • 执行pip install aliyun-python-sdk-core命令,安装阿里云SDK核心库。
    • 自行下载安装:您可以使用git clone或其它方式从GitHub下载aliyun-python-sdk-core并自行添加解决方案。
  • 安装视觉智能API相关服务Python SDK。
    • 使用依赖包工具安装(推荐)。
    • 执行以下命令,安装相应的包。
      • 图像识别:pip install aliyun-python-sdk-imagerecog
      • 图像增强:pip install aliyun-python-sdk-imageenhan
      • 人脸人体:pip install aliyun-python-sdk-facebody
      • 商品理解:pip install aliyun-python-sdk-goodstech
      • 内容安全:pip install aliyun-python-sdk-imageaudit
      • 图像分割:pip install aliyun-python-sdk-imageseg
      • 目标检测:pip install aliyun-python-sdk-objectdet
      • 文字识别:pip install aliyun-python-sdk-ocr
    • 自行下载安装:您可以使用git clone或其它方式下载aliyun-python-sdk-imageenhan并自行添加解决方案。详细下载地址请参见SDK简介

代码示例

# -*- coding: utf8 -*-
from aliyunsdkcore.client import AcsClient
from aliyunsdkimageenhan.request.v20190930 import MakeSuperResolutionImageRequest
from aliyunsdkimageenhan.request.v20190930 import ExtendImageStyleRequest
from aliyunsdkimageenhan.request.v20190930 import RecolorImageRequest
from aliyunsdkimageenhan.request.v20190930 import ChangeImageSizeRequest
from aliyunsdkimagerecog.request.v20190930 import RecognizeImageColorRequest
from aliyunsdkimagerecog.request.v20190930 import RecognizeImageStyleRequest
from aliyunsdkimagerecog.request.v20190930 import DetectImageElementsRequest
from aliyunsdkimagerecog.request.v20190930 import TaggingImageRequest
from aliyunsdkimagerecog.request.v20190930 import RecognizeSceneRequest
from aliyunsdkfacebody.request.v20191230 import DetectFaceRequest
from aliyunsdkfacebody.request.v20191230 import RecognizeFaceRequest
from aliyunsdkfacebody.request.v20191230 import CompareFaceRequest
from aliyunsdkocr.request.v20191230 import RecognizeCharacterRequest
from aliyunsdkocr.request.v20191230 import RecognizeTableRequest
from aliyunsdkocr.request.v20191230 import RecognizeIdentityCardRequest
from aliyunsdkocr.request.v20191230 import RecognizeBankCardRequest
from aliyunsdkocr.request.v20191230 import RecognizeBusinessCardRequest
from aliyunsdkocr.request.v20191230 import RecognizeAccountPageRequest
from aliyunsdkocr.request.v20191230 import RecognizeDriverLicenseRequest
from aliyunsdkocr.request.v20191230 import RecognizeDrivingLicenseRequest
from aliyunsdkocr.request.v20191230 import RecognizeLicensePlateRequest
from aliyunsdkocr.request.v20191230 import RecognizeVINCodeRequest
from aliyunsdkocr.request.v20191230 import RecognizeTaxiInvoiceRequest
from aliyunsdkocr.request.v20191230 import RecognizeTrainTicketRequest
from aliyunsdkocr.request.v20191230 import RecognizeBusinessLicenseRequest
from aliyunsdkocr.request.v20191230 import RecognizeStampRequest
from aliyunsdkgoodstech.request.v20191230 import ClassifyCommodityRequest
from aliyunsdkimageaudit.request.v20191230 import ScanImageRequest
from aliyunsdkimageseg.request.v20191230 import SegmentCommonImageRequest
from aliyunsdkimageseg.request.v20191230 import SegmentBodyRequest
from aliyunsdkimageseg.request.v20191230 import SegmentCommodityRequest
from aliyunsdkobjectdet.request.v20191230 import DetectMainBodyRequest
from aliyunsdkobjectdet.request.v20191230 import DetectVehicleRequest

# 创建 AcsClient 实例
client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "cn-shanghai")

# 清晰化/超分辨率
request = MakeSuperResolutionImageRequest.MakeSuperResolutionImageRequest()
request.set_Url("http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/MakeSuperResolution/sup-dog.png")
response = client.do_action_with_exception(request)
print(response)

# 尺寸变换
request = ChangeImageSizeRequest.ChangeImageSizeRequest()
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png")
request.set_Width(500)
request.set_Height(600)
response = client.do_action_with_exception(request)
print(response)

# 图像元素检测
request = DetectImageElementsRequest.DetectImageElementsRequest()
request.set_Url("http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/DetectImageElements/detect-elements-src.png")
response = client.do_action_with_exception(request)
print(response)

# 风格迁移
request = ExtendImageStyleRequest.ExtendImageStyleRequest()
request.set_StyleUrl("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ExtendImageStyle/styleUrl.jpeg")
request.set_MajorUrl("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ExtendImageStyle/majorUrl.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 色板识别
request = RecognizeImageColorRequest.RecognizeImageColorRequest()
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png")
request.set_ColorCount(3)
response = client.do_action_with_exception(request)
print(response)

# 风格识别
request = RecognizeImageStyleRequest.RecognizeImageStyleRequest()
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeImageStyle/technology.png")
response = client.do_action_with_exception(request)
print(response)

# 色彩迁移
request = RecolorImageRequest.RecolorImageRequest()
#自动拓色
request.set_Mode("AUTO")
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png")
request.set_ColorCount(3)
response = client.do_action_with_exception(request)
print(response)
#参考图拓色
request.set_Mode("REF_PIC")
request.set_RefUrl("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png")
response = client.do_action_with_exception(request)
print(response)
#色板拓色
request.set_Mode("TEMPLATE")
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-2-src.png")
colorTemplateList = []
colorTemplateList.append({"Color": "FF0000"})
colorTemplateList.append({"Color": "00FF00"})
colorTemplateList.append({"Color": "0000FF"})
request.set_ColorTemplates(colorTemplateList)
response = client.do_action_with_exception(request)
print(response)

# 通用图像打标
request = TaggingImageRequest.TaggingImageRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 场景识别
request = RecognizeSceneRequest.RecognizeSceneRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 人脸检测定位
request = DetectFaceRequest.DetectFaceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmengImage.png")
response = client.do_action_with_exception(request)
print(response)

# 人脸属性识别
request = RecognizeFaceRequest.RecognizeFaceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 人脸比对
request = CompareFaceRequest.CompareFaceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURLA("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
request.set_ImageURLB("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 通用识别
request = RecognizeCharacterRequest.RecognizeCharacterRequest();
request.set_MinHeight(10);
request.set_OutputProbability(True);
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai-internal.aliyuncs.com/viapi-demo/images/RecognizeCharacter/recognizeCharacter_demo.jpg")
response = client.do_action_with_exception(request)
print(response)

# 表格识别
request = RecognizeTableRequest.RecognizeTableRequest();
request.set_UseFinanceModel(False);
request.set_AssureDirection(False);
request.set_HasLine(False);
request.set_SkipDetection(False);
request.set_OutputFormat("json");
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 身份证识别
request = RecognizeIdentityCardRequest.RecognizeIdentityCardRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeIdentityCard/identityCard.jpg")
request.set_Side("face")
response = client.do_action_with_exception(request)
print(response)

# 银行卡识别
request = RecognizeBankCardRequest.RecognizeBankCardRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeBankCard/bankcard.jpg")
response = client.do_action_with_exception(request)
print(response)

# 名片识别
request = RecognizeBusinessCardRequest.RecognizeBusinessCardRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 户口页识别
request = RecognizeAccountPageRequest.RecognizeAccountPageRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 驾驶证识别
request = RecognizeDriverLicenseRequest.RecognizeDriverLicenseRequest();
request.set_Side("face")
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 行驶证识别
request = RecognizeDrivingLicenseRequest.RecognizeDrivingLicenseRequest();
request.set_Side("face")
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 车牌识别
request = RecognizeLicensePlateRequest.RecognizeLicensePlateRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# VIN码识别
request = RecognizeVINCodeRequest.RecognizeVINCodeRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 出租车发票识别
request = RecognizeTaxiInvoiceRequest.RecognizeTaxiInvoiceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 火车票识别
request = RecognizeTrainTicketRequest.RecognizeTrainTicketRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 营业执照识别
request = RecognizeBusinessLicenseRequest.RecognizeBusinessLicenseRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 公章识别
request = RecognizeStampRequest.RecognizeStampRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 商品分类
request = ClassifyCommodityRequest.ClassifyCommodityRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 图片智能鉴黄
request = ScanImageRequest.ScanImageRequest();
scenes = []
scenes.append("porn")
request.set_Scenes(scenes)
tasks = []
## 如下url替换为自有的上海region的oss文件地址
tasks.append({"DataId":"<your-data-id>", "ImageURL":"https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png"})
request.set_Tasks(tasks)
response = client.do_action_with_exception(request)
print(response)

# 通用分割
request = SegmentCommonImageRequest.SegmentCommonImageRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 人像分割
request = SegmentBodyRequest.SegmentBodyRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentBody/%E4%BA%BA%E5%83%8F%E5%88%86%E5%89%B2.png")
response = client.do_action_with_exception(request)
print(response)

# 商品分割
request = SegmentCommodityRequest.SegmentCommodityRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommodity/%E5%95%86%E5%93%81.png")
response = client.do_action_with_exception(request)
print(response)

# 主体检测
request = DetectMainBodyRequest.DetectMainBodyRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 机动车检测
request = DetectVehicleRequest.DetectVehicleRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)