全部产品
云市场

Python SDK

更新时间:2019-12-04 17:36:15

准备工作

  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并自行添加解决方案。

    完整代码示例

  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. # 创建 AcsClient 实例
  9. client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "<region>")
  10. # 创建结构化任务
  11. request = CreateLabelTaskRequest.CreateLabelTaskRequest()
  12. request.set_VideoUrl("<yourVideoUrl>")
  13. request.set_VideoName("<yourVideoName>")
  14. request.set_ApplicationId("<yourApplicationId>")
  15. # request.set_CallbackUrl("<yourCallbackUrl>") # 回调通知Url,此参数为可选
  16. # request.set_TemplateId("<yourTemplateId>") # 自定义模板id,此参数为可选,不传会使用默认模板
  17. response = client.do_action_with_exception(request)
  18. print(response)
  19. # 创建视频首图任务
  20. request = CreateCoverTaskRequest.CreateCoverTaskRequest()
  21. request.set_VideoUrl("<yourVideoUrl>")
  22. request.set_VideoName("<yourVideoName>")
  23. request.set_ApplicationId("<yourApplicationId>")
  24. # request.set_CallbackUrl("<yourCallbackUrl>") # 回调通知Url,此参数为可选
  25. # request.set_TemplateId("<yourTemplateId>") # 自定义模板id,此参数为可选,不传会使用默认模板
  26. response = client.do_action_with_exception(request)
  27. print(response)
  28. # 创建视频GIF任务
  29. request = CreateGifTaskRequest.CreateGifTaskRequest()
  30. request.set_VideoUrl("<yourVideoUrl>")
  31. request.set_VideoName("<yourVideoName>")
  32. request.set_ApplicationId("<yourApplicationId>")
  33. # request.set_CallbackUrl("<yourCallbackUrl>") # 回调通知Url,此参数为可选
  34. # request.set_TemplateId("<yourTemplateId>") # 自定义模板id,此参数为可选,不传会使用默认模板
  35. response = client.do_action_with_exception(request)
  36. print(response)
  37. # 获取任务处理状态
  38. request = GetTaskStatusRequest.GetTaskStatusRequest()
  39. request.set_TaskId(<yourTaskId>)
  40. response = client.do_action_with_exception(request)
  41. print(response)
  42. # 获取任务处理结果
  43. request = GetTaskResultRequest.GetTaskResultRequest()
  44. request.set_TaskId(<yourTaskId>)
  45. response = client.do_action_with_exception(request)
  46. print(response)
  47. # 创建人脸库
  48. request = CreateFaceGroupRequest.GetCreateFaceGroupRequest()
  49. request.set_FaceGroupName(<yourFaceGroupName>)
  50. request.set_FaceDescription(<yourFaceDescription>)
  51. response = client.do_action_with_exception(request)
  52. print(response)
  53. # 创建人脸库人物
  54. request = CreateFacePersonRequest.CreateFacePersonRequest()
  55. request.set_FaceGroupId(<yourFaceGroupId>)
  56. request.set_FacePersonName(<yourFacePersonName>)
  57. response = client.do_action_with_exception(request)
  58. print(response)
  59. # 注册人脸
  60. request = RegisterFaceImageRequest.RegisterFaceImageRequest()
  61. request.set_FaceGroupId(<yourFaceGroupId>)
  62. request.set_FacePersonId(<yourFacePersonId>)
  63. request.set_ImageUrl(<yourImageUrl>)
  64. response = client.do_action_with_exception(request)
  65. print(response)
  66. # 删除人脸库
  67. request = DeleteFaceGroupRequest.DeleteFaceGroupRequest()
  68. request.set_FaceGroupId(<yourFaceGroupId>)
  69. response = client.do_action_with_exception(request)
  70. print(response)
  71. # 删除人脸库人物
  72. request = DeleteFacePersonRequest.DeleteFacePersonRequest()
  73. request.set_FaceGroupId(<yourFaceGroupId>)
  74. request.set_FacePersonId(<yourFacePersonId>)
  75. response = client.do_action_with_exception(request)
  76. print(response)
  77. # 删除人脸库人物图片
  78. request = DeleteFaceImageRequest.DeleteFaceImageRequest()
  79. request.set_FaceGroupId(<yourFaceGroupId>)
  80. request.set_FacePersonId(<yourFacePersonId>)
  81. request.set_FaceImageId(<yourFaceImageId>)
  82. response = client.do_action_with_exception(request)
  83. print(response)
  84. # 获取人脸库列表
  85. request = ListFaceGroupsRequest.ListFaceGroupsRequest()
  86. request.set_PageNumber(<yourPageNumber>)
  87. request.set_PageSize(<yourPageSize>)
  88. response = client.do_action_with_exception(request)
  89. print(response)
  90. # 获取人脸库人物图片列表
  91. request = ListFaceImagesRequest.ListFaceImagesRequest()
  92. request.set_FaceGroupId(<yourFaceGroupId>)
  93. request.set_FacePersonId(<yourFacePersonId>)
  94. request.set_PageNumber(<yourPageNumber>)
  95. request.set_PageSize(<yourPageSize>)
  96. response = client.do_action_with_exception(request)
  97. print(response)