Add a person

更新时间:
复制 MD 格式

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:

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_id

Request parameters

ParameterTypeRequiredDescription
personIdStringYesThe unique ID of the person.
groupIdsArrayYesThe IDs of the groups the person belongs to. Specify at least one group ID.
nameStringNoThe display name of the person.
noteStringNoAdditional notes about the person.

What's next