All Products
Search
Document Center

Content Moderation:Facial attribute detection

Last Updated:Aug 19, 2024

This topic describes how to use the facial attribute detection SDK for Python to perform attribute detection on specified face images. The objects of facial attribute detection must be images.

Usage notes

Facial attribute detection can identify various facial characteristics in an image, including 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 more information about the parameters, see API operation for facial attribute detection.

You need to use the endpoints of the Content Moderation API to call this SDK. For more information about the endpoints, see Endpoints.

Prerequisites

  • Python dependencies are installed. For more information, see Installation.

    Note

    You must use the required Python version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

  • The Extension.Uploader utility class is downloaded and imported into your project.

Sample code for submitting the URL of a face image

# coding=utf-8
# The following code provides an example on how to call the asynchronous image moderation operation. You need to query the moderation results based on the task ID returned by the operation. 
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import DetectFaceRequest
import json

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: 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')
# The request object cannot be reused. You must create a request object for each request. 
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)

Sample code for submitting a local file

# coding=utf-8
# The following code provides an example on how to call the asynchronous image moderation operation. You need to query the moderation results based on the task ID returned by the operation. 
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import DetectFaceRequest
from aliyunsdkgreenextension.request.extension import ClientUploader
import json

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: 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')
# The request object cannot be reused. You must create a request object for each request. 
request = DetectFaceRequest.DetectFaceRequest()
request.set_accept_format('JSON')

clientUploader = ClientUploader.getImageClientUploader(clt)
url = clientUploader.uploadFile("/Users/Documents/tmp.jpg")
request.set_content(json.dumps({"url": url}))
response = clt.do_action_with_exception(request)
print(response)
result = json.loads(response)