This topic describes how to use the Python SDK to add faces for a person.
Usage notes
You can add a group of faces for a person. A person can have a maximum of 20 faces. For more information about the parameters, see API operation for adding faces.
You need to use the endpoints of the Content Moderation API to call this SDK. For more information about the endpoints, see Endpoints.
Prerequisites
Python dependencies are installed. For more information, see Installation.
NoteYou must use the required Python version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
The Extension.Uploader utility class is downloaded and imported into your project.
Sample code for submitting face image URLs to add faces
# coding=utf-8
# Add faces and return the face IDs.
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddFacesRequest
import json
# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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"]):
# If the value of success is true, the faces are added.
print faceImageItem["faceId"]Sample code for submitting a local file to add faces
# coding=utf-8
# Add faces and return the face IDs.
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddFacesRequest
from aliyunsdkgreenextension.request.extension import ClientUploader
import json
# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = AddFacesRequest.AddFacesRequest()
request.set_accept_format('JSON')
# Upload the local file to the server.
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"]):
# If the value of success is true, the faces are added.
print faceImageItem["faceId"]