Facial attribute detection

更新时间:
复制 MD 格式

Use the Content Moderation Python SDK to detect facial attributes in images. The SDK accepts an image by URL or local file and returns attribute data such as blur level, face angle, smile intensity, and accessory detection.

Prerequisites

Before you begin, ensure that you have:

  • Python dependencies installed. See Installation for the required Python version. Using a different version causes operation calls to fail.

  • The Extension.Uploader utility class downloaded and imported into your project (required for local file uploads only)

Usage notes

  • Images are the only supported input type. Video and audio are not supported.

  • Use the Content Moderation API endpoints when calling this SDK. See Endpoints.

  • The API can detect the following facial attributes: face blur, angle, position, smile intensity, and whether the person is wearing glasses, a mask, a hat, has a beard, bangs, or specific hair types. For full parameter definitions and response schema, see the API operation for facial attribute detection.

  • Reuse the instantiated AcsClient across requests to improve performance and avoid repeated connection overhead.

  • Create a new DetectFaceRequest object for each request. Request objects cannot be reused.

Submit a face image URL

# coding=utf-8
import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import DetectFaceRequest

# Initialize the client — reuse this instance across requests
# Load credentials from environment variables to avoid hardcoding sensitive values
clt = client.AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),      # AccessKey ID
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # AccessKey secret
    'cn-shanghai'
)
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')

# Create a new request object for each detection call
request = DetectFaceRequest.DetectFaceRequest()
request.set_accept_format('JSON')
request.set_content(json.dumps({"url": "http://example.com/xxx.jpg"}))

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

Submit a local file

To submit a local image file, use ClientUploader to upload the file and retrieve a temporary URL, then pass that URL to the detection request.

# coding=utf-8
import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import DetectFaceRequest
from aliyunsdkgreenextension.request.extension import ClientUploader

# Initialize the client — reuse this instance across requests
# Load credentials from environment variables to avoid hardcoding sensitive values
clt = client.AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),      # AccessKey ID
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # AccessKey secret
    'cn-shanghai'
)
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')

# Upload the local file and get a temporary URL
clientUploader = ClientUploader.getImageClientUploader(clt)
url = clientUploader.uploadFile("/path/to/image.jpg")

# Create a new request object for each detection call
request = DetectFaceRequest.DetectFaceRequest()
request.set_accept_format('JSON')
request.set_content(json.dumps({"url": url}))

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

Replace /path/to/image.jpg with the absolute path to your image file.

What's next