智能标签

更新时间: 2023-11-15 13:32:10

本文介绍了Python SDK的安装方式和使用智能标签SDK的示例代码。

版本说明

媒体处理的Python SDK版本需要在2.7.6版本及以上。阿里云Python SDK推荐的pip安装方式:

  • Python 2.x

    pip install aliyun-python-sdk-core
    pip install aliyun-python-sdk-mts
  • Python 3.x

    pip install aliyun-python-sdk-core-v3
    pip install aliyun-python-sdk-mts

前提条件

使用前请先初始化客户端,详细说明请参见初始化

提交智能标签作业

调用SubmitSmarttagJob提交智能标签作业,接口字段和参数详细信息请参见提交智能标签作业,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.SubmitSmarttagJobRequest import SubmitSmarttagJobRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = SubmitSmarttagJobRequest()
request.set_accept_format('json')
request.set_Title("<yourTitle>")
request.set_Content("<yourContent>")
request.set_PipelineId("")
request.set_Input("oss://xxx.mp4")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

查询智能标签作业

调用QuerySmarttagJob查询智能标签作业,接口字段和参数详细信息请参见查询智能标签作业,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.QuerySmarttagJobRequest import QuerySmarttagJobRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = QuerySmarttagJobRequest()
request.set_accept_format('json')
request.set_JobId("<JobId>")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

添加模板

调用AddSmarttagTemplate 添加一个模板,接口字段和参数详细信息请参见添加模板,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.AddSmarttagTemplateRequest import AddSmarttagTemplateRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = AddSmarttagTemplateRequest()
request.set_accept_format('json')
request.set_Industry("media")
request.set_Scene("search")
request.set_AnalyseTypes("ocr,asr,classification,shows,face,role,object,tvstation,action,emotion,landmark,scene")
request.set_TemplateName("test")
request.set_FaceCategoryIds("politician,sensitive,celebrity")
request.set_IsDefault(False)

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

查询模板

调用QuerySmarttagTemplateList查询模板信息,接口字段和参数详细信息请参见查询模板,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.QuerySmarttagTemplateListRequest import QuerySmarttagTemplateListRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = QuerySmarttagTemplateListRequest()
request.set_accept_format('json')
request.set_TemplateId("5f951a2c3b440ebdb42****")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

更新模板

调用UpdateSmarttagTemplate更新模板信息,接口字段和参数详细信息请参见更新模板,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.UpdateSmarttagTemplateRequest import UpdateSmarttagTemplateRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = UpdateSmarttagTemplateRequest()
request.set_accept_format('json')
request.set_TemplateName("update测试")
request.set_TemplateId("5f1a2c3d6440ebdb42****")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

删除模板

调用DeleteSmarttagTemplate删除模板,接口字段和参数详细信息请参见删除模板,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.DeleteSmarttagTemplateRequest import DeleteSmarttagTemplateRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = DeleteSmarttagTemplateRequest()
request.set_accept_format('json')
request.set_TemplateId("5f9559551a2c36440ebdb42****")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

注册自定义人脸

调用RegisterCustomFace注册自定义人脸,接口字段和详细参数信息请参见注册⾃定义⼈脸,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.RegisterCustomFaceRequest import RegisterCustomFaceRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = RegisterCustomFaceRequest()
request.set_accept_format('json')
request.set_PersonId("<yourPersonId>")
request.set_ImageUrl("http://xxxxx.jpeg")
request.set_CategoryId("<yourCategoryId>")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

注销自定义人脸

调用UnregisterCustomFace注销自定义人脸,接口字段和参数详细说明请参见注销⾃定义⼈脸,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.UnregisterCustomFaceRequest import UnregisterCustomFaceRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = UnregisterCustomFaceRequest()
request.set_accept_format('json')
request.set_PersonId("<yourPersonId>")
request.set_FaceId("<FaceId>")
request.set_CategoryId("<yourCategoryId>")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

添加自定义人物库或人物标签

调用TagCustomPerson添加自定义人物库或人物标签,接口字段和参数详细信息请参见添加自定义人物库或人物标签,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.TagCustomPersonRequest import TagCustomPersonRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = TagCustomPersonRequest()
request.set_accept_format('json')
request.set_CategoryId("<yourCategoryId>")
request.set_CategoryName("<yourCategoryaName>")
request.set_CategoryDescription("<yourCategoryDescription>")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))

列出人物库所有人物和人脸信息

调用ListCustomPersons列出人物库所有人物和人脸信息,接口字段和参数详细说明请参见列出⼈物库所有⼈物和⼈脸信息,调用示例如下:

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.ListCustomPersonsRequest import ListCustomPersonsRequest

# 读取环境变量中设置的ACCESS_KEY   ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = ListCustomPersonsRequest()
request.set_accept_format('json')
request.set_CategoryId("<yourCategoryId>")
request.set_PersonId("<yourPersonId>")

response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))
阿里云首页 媒体处理 相关技术圈