全部产品
云市场
云游戏

Python SDK

更新时间:2020-04-27 20:37:08

准备工作

  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。
    使用依赖包工具安装(推荐)。
    执行以下命令,安装视频指纹 Python SDK:
    1. pip install aliyun-python-sdk-videosearch
    自行下载安装。
    您可以使用git clone或其它手段下载aliyun-python-sdk-videosearch并自行添加解决方案。

完整代码示例

  1. # -*- coding: utf8 -*-
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkcore.profile import region_provider
  4. from aliyunsdkvideosearch.request.v20200225 import AddStorageVideoTaskRequest
  5. from aliyunsdkvideosearch.request.v20200225 import AddSearchVideoTaskRequest
  6. from aliyunsdkvideosearch.request.v20200225 import AddDeletionVideoTaskRequest
  7. from aliyunsdkvideosearch.request.v20200225 import GetTaskStatusRequest
  8. # 创建 AcsClient 实例
  9. client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "<region>")
  10. region_provider.add_endpoint("videosearch", "cn-beijing", "multisearch.cn-beijing.aliyuncs.com")
  11. # 创建视频入库任务
  12. request = AddStorageVideoTaskRequest.AddStorageVideoTaskRequest()
  13. # 指定请求方式,避免SignatureDoesNotMatch错误
  14. request.set_method("POST")
  15. # 处理任务的实例id
  16. request.set_InstanceId("videosearch")
  17. # 视频oss url
  18. request.set_VideoUrl("<yourVideoOssUrl>")
  19. # 视频的业务key
  20. request.set_VideoId("<yourVideoId>")
  21. # 视频的业务标签,此参数为可选
  22. request.set_VideoTags("<yourVideoTag>")
  23. # 入库回调地址,此参数为可选
  24. request.set_CallbackUrl("<yourCallbackUrl>")
  25. try:
  26. response = client.do_action_with_exception(request)
  27. print(response)
  28. except exceptions.ServerException as serr:
  29. print(serr.get_request_id())
  30. print(serr.get_error_code())
  31. print(serr.get_error_msg())
  32. except exceptions.ClientException as cerr:
  33. print(cerr.get_error_code())
  34. print(cerr.get_error_msg())
  35. # 创建视频检索任务
  36. request = AddSearchVideoTaskRequest.AddSearchVideoTaskRequest();
  37. # 指定请求方式,避免SignatureDoesNotMatch错误
  38. request.set_method("POST")
  39. # 处理任务的实例id
  40. request.set_InstanceId("<yourInstanceId>")
  41. # 视频oss url
  42. request.set_VideoUrl("<yourVideoOssUrl>")
  43. # 视频的业务key,当searchStorageType为1(直接入库)、2(覆盖入库)时为必填
  44. request.set_VideoId("<yourVideoId>")
  45. # 视频的业务标签,当searchStorageType为1(直接入库)、2(覆盖入库)时生效
  46. request.set_VideoTags("<yourVideoTag>")
  47. # 返回视频数量
  48. request.set_ReturnVideoNumber(10)
  49. # 检索入库类型,取值范围为1(直接入库)、2(覆盖入库)、3(不入库)
  50. request.set_StorageType(3)
  51. # 查询标签,对应视频入库时的videoTag
  52. request.set_QueryTags("<yourQueryTags>")
  53. # 覆盖入库的阈值,当searchStorageType为2(覆盖入库)时生效
  54. request.set_ReplaceStorageThreshold(0.95f)
  55. # 入库回调地址,此参数为可选
  56. request.set_CallbackUrl("<yourStorageCallback>")
  57. try:
  58. response = client.do_action_with_exception(request)
  59. print(response)
  60. except exceptions.ServerException as serr:
  61. print(serr.get_request_id())
  62. print(serr.get_error_code())
  63. print(serr.get_error_msg())
  64. except exceptions.ClientException as cerr:
  65. print(cerr.get_error_code())
  66. print(cerr.get_error_msg())
  67. # 获取任务处理状态
  68. request = GetTaskStatusRequest.GetTaskStatusRequest()
  69. # 指定请求方式,避免SignatureDoesNotMatch错误
  70. request.set_method("POST")
  71. # 任务id
  72. request.set_TaskId("<yourTaskId>")
  73. # 任务所在的实例id
  74. request.set_InstanceId("<yourInstanceId>")
  75. try:
  76. response = client.do_action_with_exception(request)
  77. print(response)
  78. except exceptions.ServerException as serr:
  79. print(serr.get_request_id())
  80. print(serr.get_error_code())
  81. print(serr.get_error_msg())
  82. except exceptions.ClientException as cerr:
  83. print(cerr.get_error_code())
  84. print(cerr.get_error_msg())
  85. # 删除视频
  86. request = AddDeletionVideoTaskRequest.AddDeletionVideoTaskRequest()
  87. # 指定请求方式,避免SignatureDoesNotMatch错误
  88. request.set_method("POST")
  89. # 视频id
  90. request.set_VideoId("<yourVideoId>")
  91. # 视频所在实例id
  92. request.set_InstanceId("<yourInstanceId>")
  93. try:
  94. response = client.do_action_with_exception(request)
  95. print(response)
  96. except exceptions.ServerException as serr:
  97. print(serr.get_request_id())
  98. print(serr.get_error_code())
  99. print(serr.get_error_msg())
  100. except exceptions.ClientException as cerr:
  101. print(cerr.get_error_code())
  102. print(cerr.get_error_msg())