Use the Content Moderation Python SDK to add a person to a group in your face library.
Prerequisites
Before you begin, ensure that you have:
Python dependencies installed — see Installation
The Extension.Uploader utility class downloaded and imported into your project
Note: Use the Python version specified in the Installation topic. Using an unsupported Python version causes operation calls to fail.
Usage notes
Each person must belong to at least one group. Specify the target group when adding a person.
Use the Content Moderation API endpoints to call this SDK. For endpoint details, see Endpoints.
For the full list of request parameters, see API operation for adding a person.
Sample code
Both samples use AddPersonRequest to submit the request. The response contains a nested data object — check both result["code"] and result["data"]["code"] to confirm success.
Reuse the instantiated client across requests to improve moderation performance and avoid unnecessary reconnections.
Python 3
# coding=utf-8
# Add a person.
import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddPersonRequest
# Obtain credentials from environment variables.
# ALIBABA_CLOUD_ACCESS_KEY_ID — AccessKey ID of your RAM user
# ALIBABA_CLOUD_ACCESS_KEY_SECRET — AccessKey secret of your RAM user
clt = client.AcsClient(
os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
"cn-shanghai"
)
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = AddPersonRequest.AddPersonRequest()
request.set_accept_format('JSON')
# Set the person details. groupIds accepts an array of one or more group IDs.
request.set_content(bytearray(json.dumps(
{"personId": "python_personId_test_1", "groupIds": ["python_groupId_1"], "name": "Test", "note": "Remarks"}
), "utf-8"))
response = clt.do_action_with_exception(request)
print(response)
result = json.loads(response)
if result["code"] == 200:
result_object = result["data"]
if result_object["code"] == 200:
# Status code 200 indicates the person was added successfully.
person_id = result_object["personId"]
print(person_id)Python 2
# coding=utf-8
# Add a person.
import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddPersonRequest
# Obtain credentials from environment variables.
# ALIBABA_CLOUD_ACCESS_KEY_ID — AccessKey ID of your RAM user
# ALIBABA_CLOUD_ACCESS_KEY_SECRET — AccessKey secret of your RAM user
clt = client.AcsClient(
os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
"cn-shanghai"
)
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = AddPersonRequest.AddPersonRequest()
request.set_accept_format('JSON')
# Set the person details. groupIds accepts an array of one or more group IDs.
request.set_content(
bytearray(json.dumps(
{"personId": "python_personId_test_1", "groupIds": ["python_groupId_1"], "name": "Test", "note": "Remarks"}
), "utf-8"))
response = clt.do_action_with_exception(request)
print response
result = json.loads(response)
if 200 == result["code"]:
result_object = result["data"]
if 200 == result_object["code"]:
# Status code 200 indicates the person was added successfully.
person_id = result_object["personId"]
print person_idRequest parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
personId | String | Yes | The unique ID of the person. |
groupIds | Array | Yes | The IDs of the groups the person belongs to. Specify at least one group ID. |
name | String | No | The display name of the person. |
note | String | No | Additional notes about the person. |