Use the Content Moderation Python SDK to search a face image against a custom face group. The SDK submits a synchronous scan request and returns matched faces with a scene and suggestion for each result.
Prerequisites
Before you begin, ensure that you have:
Python dependencies installed. For installation instructions, see Installation
Use the Python version specified in the Installation topic. Using a different version causes operation calls to fail.
The Extension.Uploader utility class downloaded and imported into your project. This is required for local file submissions.
How it works
Create an
AcsClientpointed at thecn-shanghairegion and override the endpoint withregion_provider.modify_point.Build a task object with a unique
dataId, the image source (url), and agroupIdthat identifies which face group to search.Submit an
ImageSyncScanRequestwith thesface-nscene. The SDK returns results synchronously.Iterate the response to read the
sceneandsuggestionfor each matched face.
Submit an image URL for face retrieval
Replace the following values before running the code:
| Placeholder | Description | Example |
|---|---|---|
<your-access-key-id> | AccessKey ID of your RAM user | LTAI5tXxx |
<your-access-key-secret> | AccessKey secret of your RAM user | xXxXxXx |
<image-url> | Publicly accessible URL of the face image | https://example.com/face.jpg |
<group-id> | ID of the face group to search | python_groupId_1 |
Reuse the instantiated client across requests. Creating a new client for each request degrades moderation performance and causes unnecessary connection overhead.
# coding=utf-8
import json
import uuid
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import ImageSyncScanRequest
# Create a client for the cn-shanghai region.
# Load credentials from environment variables to avoid hardcoding secrets.
# AccessKey ID: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# AccessKey secret: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("<your-access-key-id>", "<your-access-key-secret>", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = ImageSyncScanRequest.ImageSyncScanRequest()
request.set_accept_format('JSON')
# Build the task. The synchronous face retrieval operation supports one image per request.
# groupId identifies the face group to search against.
task1 = {
"dataId": str(uuid.uuid1()),
"url": "<image-url>",
"extras": {"groupId": "<group-id>"}
}
request.set_content(bytearray(json.dumps({"tasks": [task1], "scenes": ["sface-n"]}), "utf-8"))
response = clt.do_action_with_exception(request)
print response
result = json.loads(response)
if 200 == result["code"]:
taskResults = result["data"]
for taskResult in taskResults:
if 200 == taskResult["code"]:
sceneResults = taskResult["results"]
for sceneResult in sceneResults:
scene = sceneResult["scene"]
suggestion = sceneResult["suggestion"]
print suggestion
print scene
# Take action based on the scene and suggestion values.The code prints the raw response JSON first, then the suggestion and scene values for each result. Check the suggestion field to determine how to handle the image.
Submit a local file for face retrieval
Replace the following values before running the code:
| Placeholder | Description | Example |
|---|---|---|
<your-access-key-id> | AccessKey ID of your RAM user | LTAI5tXxx |
<your-access-key-secret> | AccessKey secret of your RAM user | xXxXxXx |
<local-file-path> | Absolute path to the local face image | d:/test/test.jpg |
<group-id> | ID of the face group to search | python_groupId_1 |
# coding=utf-8
import json
import uuid
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import ImageSyncScanRequest
from aliyunsdkgreenextension.request.extension import ClientUploader
# Create a client for the cn-shanghai region.
# Load credentials from environment variables to avoid hardcoding secrets.
# AccessKey ID: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# AccessKey secret: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("<your-access-key-id>", "<your-access-key-secret>", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
request = ImageSyncScanRequest.ImageSyncScanRequest()
request.set_accept_format('JSON')
# Upload the local file to the server and get a temporary URL.
uploader = ClientUploader.getImageClientUploader(clt)
url = uploader.uploadFile('<local-file-path>')
# Build the task using the uploaded file URL.
# groupId identifies the face group to search against.
task1 = {
"dataId": str(uuid.uuid1()),
"url": url,
"extras": {"groupId": "<group-id>"}
}
request.set_content(bytearray(json.dumps({"tasks": [task1], "scenes": ["sface-n"]}), "utf-8"))
response = clt.do_action_with_exception(request)
print response
result = json.loads(response)
if 200 == result["code"]:
taskResults = result["data"]
for taskResult in taskResults:
if 200 == taskResult["code"]:
sceneResults = taskResult["results"]
for sceneResult in sceneResults:
scene = sceneResult["scene"]
suggestion = sceneResult["suggestion"]
print suggestion
print scene
# Take action based on the scene and suggestion values.What's next
Image moderation — synchronous — full parameter reference for
/green/image/scanImage moderation — asynchronous — parameter reference for
/green/image/asyncscanand/green/image/results