全部产品

Python SDK

更新时间:2020-10-12 17:23:54

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参考创建AccessKey
  2. 安装Python SDK核心库。
    使用依赖包工具安装(推荐)。
    执行以下命令,安装阿里云SDK核心库:
    1. pip install alibabacloud_tea_rpc
    自行下载安装。
    您可以使用git clone或其它手段下载aliyun-openapi-python-sdk并自行添加解决方案。
  3. 安装视频内容检索Python SDK。
    使用依赖包工具安装(推荐)。
    执行以下命令,安装视频指纹 Python SDK:
    1. pip install alibabacloud_videosearch20200225
    自行下载安装。
    您可以使用git clone或其它手段下载aliyun-python-sdk-videosearch并自行添加解决方案。

完整代码示例

  1. # -*- coding: utf8 -*-
  2. import json
  3. from alibabacloud_tea_rpc.models import Config
  4. from alibabacloud_videosearch20200225.client import Client
  5. from alibabacloud_videosearch20200225.models import AddStorageVideoTaskRequest, AddSearchVideoTaskRequest, \
  6. GetTaskStatusRequest, GetStorageHistoryRequest, AddDeletionVideoTaskRequest
  7. def add_storage_video_task(client):
  8. request = AddStorageVideoTaskRequest()
  9. # 实例id
  10. request.instance_id = '<yourInstanceId>'
  11. # 视频url
  12. request.video_url = '<yourVideoUrl>'
  13. # 视频id(业务key)
  14. request.video_id = '<yourVideoId>'
  15. # 视频描述
  16. request.description = '<yourDescription>'
  17. # 视频标签
  18. request.video_tags = '<yourVideoTags>'
  19. # 入库完成的回调
  20. request.callback_url = '<yourCallbackUrl>'
  21. response = client.add_storage_video_task(request)
  22. print('RequestId : ' + response.request_id)
  23. print('Data : ' + json.dumps(response.data.to_map()))
  24. def add_search_video_task(client):
  25. request = AddSearchVideoTaskRequest()
  26. # 实例id
  27. request.instance_id = '<yourInstanceId>'
  28. # 检索类型(1. 视频搜视频 2. 图片搜视频)
  29. request.search_type = 1
  30. # 视频/图片的url地址
  31. request.video_url = '<yourVideoUrl>'
  32. # 视频/图片的描述
  33. request.description = '<yourDescription>'
  34. # 检索标签
  35. request.query_tags = '<yourQueryTags>'
  36. # 返回结果数
  37. request.return_video_number = 20
  38. # 查询入库类型,当检索类型为1时生效(1. 直接入库 2. 去重入库 3. 不入库)
  39. request.storage_type = 3
  40. # 视频id,当searchType为1且storageType为1,2时生效
  41. request.video_id = '<yourVideoId>'
  42. # 视频标签,当searchType为1且storageType为1,2时生效
  43. request.video_tags = '<yourVideoTags>'
  44. # 去重入库阈值,searchType为1且storageType为2时生效
  45. request.replace_storage_threshold = 0.75
  46. # 检索完成的回调
  47. request.callback_url = '<yourCallbackUrl>'
  48. response = client.add_search_video_task(request)
  49. print('RequestId : ' + response.request_id)
  50. print('Data : ' + json.dumps(response.data.to_map()))
  51. def get_task_status(client):
  52. request = GetTaskStatusRequest()
  53. # 实例id
  54. request.instance_id = '<yourInstanceId>'
  55. # 任务id
  56. request.task_id = '<yourTaskId>'
  57. response = client.get_task_status(request)
  58. print('RequestId : ' + response.request_id)
  59. print('Data : ' + response.data.__str__())
  60. print('TaskInfo : ' + json.dumps(response.task_info.to_map()))
  61. def get_storage_history(client):
  62. request = GetStorageHistoryRequest()
  63. # 实例id
  64. request.instance_id = '<yourInstanceId>'
  65. # 视频id
  66. request.video_id = '<yourVideoId>'
  67. # 当前页数
  68. request.page_number = 1
  69. # 分页大小
  70. request.page_size = 10
  71. response = client.get_storage_history(request)
  72. print('RequestId : ' + response.request_id)
  73. print('Data : ' + json.dumps(response.data.to_map()))
  74. def add_deletion_video_task(client):
  75. request = AddDeletionVideoTaskRequest()
  76. # 实例id
  77. request.instance_id = '<yourInstanceId>'
  78. # 视频id
  79. request.video_id = '<yourVideoId>'
  80. response = client.add_deletion_video_task(request)
  81. print('RequestId : ' + response.request_id)
  82. print('Data : ' + response.data)
  83. if __name__ == '__main__':
  84. config = Config(
  85. access_key_id="<yourAccessKeyId>",
  86. access_key_secret="<yourAccessKeySecret>",
  87. region_id="<yourRegionId>",
  88. endpoint="multisearch.<regionId>.aliyuncs.com"
  89. )
  90. client = Client(config)
  91. # 创建视频入库任务
  92. add_storage_video_task(client)
  93. # 创建检索任务
  94. add_search_video_task(client)
  95. # 获取任务状态
  96. get_task_status(client)
  97. # 获取入库历史
  98. get_storage_history(client)
  99. # 删除视频
  100. add_deletion_video_task(client)