Use the Content Moderation Python SDK to add face images for a person. Each person can have up to 20 faces. Two upload methods are supported: submitting image URLs directly, or uploading a local file.
Prerequisites
Before you begin, ensure that you have:
Installed the Python SDK dependencies. See Installation for the required Python version. Using an incompatible version causes API calls to fail.
Downloaded and imported the Extension.Uploader utility class into your project (required for local file uploads only)
Usage notes
A person can have a maximum of 20 faces.
Use the Content Moderation API endpoints when calling this SDK. See Endpoints.
For all request parameters, see API operation for adding faces.
Check
faceImageItem["success"]in the response to confirm whether each face was added successfully.
Add faces from image URLs
# 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
# Reuse the instantiated client across requests to improve moderation performance
# and avoid repeated connection overhead.
# Get your RAM user credentials from environment variables:
# AccessKey ID: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# AccessKey secret: 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"]):
# success is true: the face was added successfully.
print faceImageItem["faceId"]Add faces from a local file
Use the Extension.Uploader utility class to upload a local file to the server, then pass the returned URL to the AddFacesRequest.
# 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
# Reuse the instantiated client across requests to improve moderation performance
# and avoid repeated connection overhead.
# Get your RAM user credentials from environment variables:
# AccessKey ID: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# AccessKey secret: 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 and get its URL.
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 is true: the face was added successfully.
print faceImageItem["faceId"]