Delete faces

更新时间:
复制 MD 格式

Use the Content Moderation Python SDK to delete one or more faces associated with a person in your face library.

Prerequisites

Before you begin, ensure that you have:

  • Installed the Python dependencies using the version specified in Installation. Using a different Python version causes subsequent API calls to fail.

  • Downloaded and imported the Extension.Uploader utility class into your project.

Usage notes

To delete faces, provide the personId of the target person and the list of faceIds to remove. For the full parameter reference, see API operation for deleting faces.

Use the Content Moderation API endpoints when calling this SDK. For a list of available endpoints, see Endpoints.

Delete faces from a person

The following example deletes faces from a person in the face library.

# coding=utf-8
import json
import os

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import DeleteFacesRequest

# Reuse the AcsClient instance across requests to improve performance
# and avoid repeated connection overhead.
clt = client.AcsClient(
    os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],      # AccessKey ID
    os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],  # AccessKey secret
    "cn-shanghai"
)

# Set the endpoint for the cn-shanghai region
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

request = DeleteFacesRequest.DeleteFacesRequest()
request.set_accept_format("JSON")

# Specify the person ID and the list of face IDs to delete
request.set_content(
    bytearray(
        json.dumps({
            "personId": "python_personId_test_1",
            "faceIds": ["31842550077981745"]
        }),
        "utf-8"
    )
)

response = clt.do_action_with_exception(request)
result = json.loads(response)

if result["code"] == 200:
    result_object = result["data"]
    if result_object["code"] == 200:
        person_id = result_object["personId"]
        print(person_id)

Request parameters

ParameterTypeRequiredDescription
personIdStringYesThe ID of the person whose faces you want to delete
faceIdsArray of stringsYesThe list of face IDs to delete from the specified person

Response structure

A successful deletion returns a two-level response:

FieldDescription
result["code"]Outer status code. 200 means the service accepted the request.
result["data"]["code"]Inner operation code. 200 means the faces were deleted.
result["data"]["personId"]The ID of the person whose faces were deleted.

Next steps

  • Add faces — Add new faces to a person in the face library.

  • Query faces — Verify that the target faces were removed.