全部产品

Python SDK

更新时间:2020-04-24 19:21:49

准备工作

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

  2. 安装Python SDK核心库。 使用依赖包工具安装(推荐)。

    执行以下命令,安装阿里云SDK核心库:

    1. pip install aliyun-python-sdk-core

    自行下载安装。

    您可以使用git clone或其它手段下载aliyun-openapi-python-sdk并自行添加解决方案。

  3. 安装多媒体AI搜索Python SDK。

    使用依赖包工具安装(推荐)。

    执行以下命令,安装多媒体AI Python SDK:

    1. pip install aliyun-python-sdk-multimediaai

    自行下载安装。

    您可以使用git clone或其它手段下载aliyun-python-sdk-multimediaai并自行添加解决方案。

    注意:模板中yourContent参考 模板附录

    完整代码示例

  1. # -*- coding: utf8 -*-
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkmultimediaai.request.v20190810 import CreateLabelTaskRequest
  4. from aliyunsdkmultimediaai.request.v20190810 import CreateCoverTaskRequest
  5. from aliyunsdkmultimediaai.request.v20190810 import CreateGifTaskRequest
  6. from aliyunsdkmultimediaai.request.v20190810 import GetTaskStatusRequest
  7. from aliyunsdkmultimediaai.request.v20190810 import GetTaskResultRequest
  8. from aliyunsdkcore.acs_exception.exceptions import ClientException
  9. from aliyunsdkcore.acs_exception.exceptions import ServerException
  10. import json
  11. # 创建 AcsClient 实例
  12. client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "<region>")
  13. # 创建结构化任务
  14. request = CreateLabelTaskRequest.CreateLabelTaskRequest()
  15. request.set_VideoUrl("<yourVideoUrl>")
  16. request.set_VideoName("<yourVideoName>")
  17. request.set_ApplicationId("<yourApplicationId>")
  18. # request.set_CallbackUrl("<yourCallbackUrl>") # 回调通知Url,此参数为可选
  19. # request.set_TemplateId("<yourTemplateId>") # 自定义模板id,此参数为可选,不传会使用默认模板
  20. try:
  21. response = client.do_action_with_exception(request)
  22. print(response)
  23. except ServerException, e:
  24. print(e.get_error_code())
  25. print(e.get_error_msg())
  26. except ClientException, e:
  27. print(e.get_error_code())
  28. print(e.get_error_msg())
  29. # 创建视频首图任务
  30. request = CreateCoverTaskRequest.CreateCoverTaskRequest()
  31. request.set_VideoUrl("<yourVideoUrl>") # 视频的URL地址
  32. request.set_VideoName("<yourVideoName>") # 视频名称,建议传入视频名称,可提升视频分类的准确度
  33. request.set_ApplicationId("<yourApplicationId>")
  34. # request.set_CallbackUrl("<yourCallbackUrl>") # 回调通知Url,此参数为可选
  35. # request.set_TemplateId("<yourTemplateId>") # 自定义模板id,此参数为可选,不传会使用默认模板
  36. try:
  37. response = client.do_action_with_exception(request)
  38. print(response)
  39. except ServerException, e:
  40. print(e.get_error_code())
  41. print(e.get_error_msg())
  42. except ClientException, e:
  43. print(e.get_error_code())
  44. print(e.get_error_msg())
  45. # 创建视频GIF任务
  46. request = CreateGifTaskRequest.CreateGifTaskRequest()
  47. request.set_VideoUrl("<yourVideoUrl>")
  48. request.set_VideoName("<yourVideoName>")
  49. request.set_ApplicationId("<yourApplicationId>")
  50. # request.set_CallbackUrl("<yourCallbackUrl>") # 回调通知Url,此参数为可选
  51. # request.set_TemplateId("<yourTemplateId>") # 自定义模板id,此参数为可选,不传会使用默认模板
  52. try:
  53. response = client.do_action_with_exception(request)
  54. print(response)
  55. except ServerException, e:
  56. print(e.get_error_code())
  57. print(e.get_error_msg())
  58. except ClientException, e:
  59. print(e.get_error_code())
  60. print(e.get_error_msg())
  61. # 获取任务处理状态
  62. request = GetTaskStatusRequest.GetTaskStatusRequest()
  63. request.set_TaskId(<yourTaskId>)
  64. try:
  65. response = client.do_action_with_exception(request)
  66. print(response)
  67. except ServerException, e:
  68. print(e.get_error_code())
  69. print(e.get_error_msg())
  70. except ClientException, e:
  71. print(e.get_error_code())
  72. print(e.get_error_msg())
  73. # 获取任务处理结果
  74. request = GetTaskResultRequest.GetTaskResultRequest()
  75. request.set_TaskId(<yourTaskId>)
  76. try:
  77. response = client.do_action_with_exception(request)
  78. print(response)
  79. resp = json.loads(response)
  80. status = resp["Status"]
  81. resu = resp["Result"]
  82. #任务失败
  83. if(status == 3) :
  84. print("errorCode:" + resu["ErrorCode"])
  85. print("errorMessage:" + resu["ErrorMessage"])
  86. except ServerException, e:
  87. print(e.get_error_code())
  88. print(e.get_error_msg())
  89. except ClientException, e:
  90. print(e.get_error_code())
  91. print(e.get_error_msg())
  92. # 创建人脸库
  93. request = CreateFaceGroupRequest.GetCreateFaceGroupRequest()
  94. request.set_FaceGroupName(<yourFaceGroupName>)
  95. request.set_FaceDescription(<yourFaceDescription>)
  96. try:
  97. response = client.do_action_with_exception(request)
  98. print(response)
  99. except ServerException, e:
  100. print(e.get_error_code())
  101. print(e.get_error_msg())
  102. except ClientException, e:
  103. print(e.get_error_code())
  104. print(e.get_error_msg())
  105. # 创建人脸库人物
  106. request = CreateFacePersonRequest.CreateFacePersonRequest()
  107. request.set_FaceGroupId(<yourFaceGroupId>)
  108. request.set_FacePersonName(<yourFacePersonName>)
  109. try:
  110. response = client.do_action_with_exception(request)
  111. print(response)
  112. except ServerException, e:
  113. print(e.get_error_code())
  114. print(e.get_error_msg())
  115. except ClientException, e:
  116. print(e.get_error_code())
  117. print(e.get_error_msg())
  118. # 注册人脸
  119. request = RegisterFaceImageRequest.RegisterFaceImageRequest()
  120. request.set_FaceGroupId(<yourFaceGroupId>)
  121. request.set_FacePersonId(<yourFacePersonId>)
  122. request.set_ImageUrl(<yourImageUrl>)
  123. try:
  124. response = client.do_action_with_exception(request)
  125. print(response)
  126. except ServerException, e:
  127. print(e.get_error_code())
  128. print(e.get_error_msg())
  129. except ClientException, e:
  130. print(e.get_error_code())
  131. print(e.get_error_msg())
  132. # 删除人脸库
  133. request = DeleteFaceGroupRequest.DeleteFaceGroupRequest()
  134. request.set_FaceGroupId(<yourFaceGroupId>)
  135. try:
  136. response = client.do_action_with_exception(request)
  137. print(response)
  138. except ServerException, e:
  139. print(e.get_error_code())
  140. print(e.get_error_msg())
  141. except ClientException, e:
  142. print(e.get_error_code())
  143. print(e.get_error_msg())
  144. # 删除人脸库人物
  145. request = DeleteFacePersonRequest.DeleteFacePersonRequest()
  146. request.set_FaceGroupId(<yourFaceGroupId>)
  147. request.set_FacePersonId(<yourFacePersonId>)
  148. try:
  149. response = client.do_action_with_exception(request)
  150. print(response)
  151. except ServerException, e:
  152. print(e.get_error_code())
  153. print(e.get_error_msg())
  154. except ClientException, e:
  155. print(e.get_error_code())
  156. print(e.get_error_msg())
  157. # 删除人脸库人物图片
  158. request = DeleteFaceImageRequest.DeleteFaceImageRequest()
  159. request.set_FaceGroupId(<yourFaceGroupId>)
  160. request.set_FacePersonId(<yourFacePersonId>)
  161. request.set_FaceImageId(<yourFaceImageId>)
  162. try:
  163. response = client.do_action_with_exception(request)
  164. print(response)
  165. except ServerException, e:
  166. print(e.get_error_code())
  167. print(e.get_error_msg())
  168. except ClientException, e:
  169. print(e.get_error_code())
  170. print(e.get_error_msg())
  171. # 获取人脸库列表
  172. request = ListFaceGroupsRequest.ListFaceGroupsRequest()
  173. request.set_PageNumber(<yourPageNumber>)
  174. request.set_PageSize(<yourPageSize>)
  175. try:
  176. response = client.do_action_with_exception(request)
  177. print(response)
  178. except ServerException, e:
  179. print(e.get_error_code())
  180. print(e.get_error_msg())
  181. except ClientException, e:
  182. print(e.get_error_code())
  183. print(e.get_error_msg())
  184. # 获取人脸库人物图片列表
  185. request = ListFaceImagesRequest.ListFaceImagesRequest()
  186. request.set_FaceGroupId(<yourFaceGroupId>)
  187. request.set_FacePersonId(<yourFacePersonId>)
  188. request.set_PageNumber(<yourPageNumber>)
  189. request.set_PageSize(<yourPageSize>)
  190. try:
  191. response = client.do_action_with_exception(request)
  192. print(response)
  193. except ServerException, e:
  194. print(e.get_error_code())
  195. print(e.get_error_msg())
  196. except ClientException, e:
  197. print(e.get_error_code())
  198. print(e.get_error_msg())
  199. # 创建视频内容分析模板
  200. request = CreateTemplateRequest.CreateTemplateRequest()
  201. request.set_TemplateName(<yourTemplateName>) //模板名称
  202. request.set_Content(<yourContent>) //模板内容
  203. request.set_IsDefault(<yourIsDefault>) //是否默认模板(true false 否)
  204. request.set_Type(<yourType>) //1.LABEL (默认为1)
  205. try:
  206. response = client.do_action_with_exception(request)
  207. print(response)
  208. except ServerException, e:
  209. print(e.get_error_code())
  210. print(e.get_error_msg())
  211. except ClientException, e:
  212. print(e.get_error_code())
  213. print(e.get_error_msg())
  214. # 修改视频内容分析模板
  215. request = UpdateTemplateRequest.UpdateTemplateRequest()
  216. request.set_TemplateName(<yourTemplateName>) //模板名称
  217. request.set_TemplateId(<yourTemplateId>) //模板id
  218. request.set_Content(<yourContent>) //模板内容
  219. request.set_IsDefault(<yourIsDefault>) //是否默认模板(true false 否)
  220. request.set_Type(<yourType>) //1.LABEL (默认为1)
  221. try:
  222. response = client.do_action_with_exception(request)
  223. print(response)
  224. except ServerException, e:
  225. print(e.get_error_code())
  226. print(e.get_error_msg())
  227. except ClientException, e:
  228. print(e.get_error_code())
  229. print(e.get_error_msg())
  230. # 查询视频内容分析列表
  231. request = ListTemplatesRequest.ListTemplatesRequest()
  232. request.set_TemplateName(<yourTemplateName>) //模板名称
  233. request.set_TemplateId(<yourTemplateId>) //模板id
  234. request.set_Type(<yourType>)
  235. request.set_PageNumber(<yourPageNumber>)
  236. request.set_PageSize(<yourPageSize>)
  237. try:
  238. response = client.do_action_with_exception(request)
  239. print(response)
  240. except ServerException, e:
  241. print(e.get_error_code())
  242. print(e.get_error_msg())
  243. except ClientException, e:
  244. print(e.get_error_code())
  245. print(e.get_error_msg())
  246. # 查询视频内容分析详情
  247. request = GetTemplateRequest.GetTemplateRequest()
  248. request.set_TemplateId(<yourTemplateId>) //模板id
  249. try:
  250. response = client.do_action_with_exception(request)
  251. print(response)
  252. except ServerException, e:
  253. print(e.get_error_code())
  254. print(e.get_error_msg())
  255. except ClientException, e:
  256. print(e.get_error_code())
  257. print(e.get_error_msg())