新增人脸

本文介绍了如何使用Python SDK新增个体对应人脸图片。

功能描述

为个体新增一组人脸图片,一个个体最多允许包含20张人脸图片。关于参数的说明,请参见新增人脸API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

新增人脸图片(URL)代码示例

# coding=utf-8
# 新增个体人脸,返回人脸ID。

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddFacesRequest
import json

# 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
# 常见获取环境变量方式:
# 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("建议从环境变量中获取RAM用户AccessKey ID", "建议从环境变量中获取RAM用户AccessKey Secret", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = AddFacesRequest.AddFacesRequest()
request.set_accept_format('JSON')

request.set_content(
    bytearray(json.dumps({"personId": "python_personId_test_1",
                          "urls": ["https://example.com/tfs/xxx.jpg"]}), "utf-8"))

response = clt.do_action_with_exception(request)
print response
result = json.loads(response)
if 200 == result["code"]:
    resultObject = result["data"]
    if (200 == resultObject["code"]):
        personId = resultObject["personId"]
        faceImageItems = resultObject["faceImageItems"]
        for faceImageItem in faceImageItems:
            if (faceImageItem["success"]):
                # success为true表示添加成功。
                print faceImageItem["faceId"]

新增人脸图片(本地文件)代码示例

# coding=utf-8
# 个体新增人脸,返回人脸Id

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddFacesRequest
from aliyunsdkgreenextension.request.extension import ClientUploader
import json

# 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
# 常见获取环境变量方式:
# 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("建议从环境变量中获取RAM用户AccessKey ID", "建议从环境变量中获取RAM用户AccessKey Secret", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = AddFacesRequest.AddFacesRequest()
request.set_accept_format('JSON')

# 上传本地文件到服务端。
uploader = ClientUploader.getImageClientUploader(clt)
url = uploader.uploadFile('d:/test/test.jpg')

request.set_content(
    bytearray(json.dumps({"personId": "python_personId_test_1",
                          "urls": [url]}), "utf-8"))

response = clt.do_action_with_exception(request)
print response
result = json.loads(response)
if 200 == result["code"]:
    resultObject = result["data"]
    if (200 == resultObject["code"]):
        personId = resultObject["personId"]
        faceImageItems = resultObject["faceImageItems"]
        for faceImageItem in faceImageItems:
            if (faceImageItem["success"]):
                # success为true表示添加成功。
                print faceImageItem["faceId"]