Facial attribute detection
Detect facial attributes in images using the Content Moderation software development kit (SDK) for PHP. The API returns attributes such as blur level, head angle, face position, smile intensity, and whether the person is wearing glasses, a face mask, or a hat, or has a beard, bangs, or a specific hair type.
Prerequisites
Before you begin, ensure that you have:
The Content Moderation SDK for PHP installed using the PHP version required by the Installation topic. Using an unsupported PHP version causes subsequent API calls to fail.
Limitations
| Limitation | Detail |
|---|---|
| Supported input | Image URLs only. Local files and binary data are not supported. |
| Supported URL types | Public HTTP and HTTPS URLs |
| Maximum URL length | 2,048 characters |
Detect facial attributes
The following example submits a facial attribute detection task using the detectFace operation.
<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;
try {
// Initialize the client. Reuse this instance across requests to improve
// performance and avoid repeated connection overhead.
// Read credentials from environment variables to keep them out of source code.
AlibabaCloud::accessKeyClient(
getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), // AccessKey ID
getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET') // AccessKey secret
)
->timeout(10) // Default request timeout: 10 seconds
->connectTimeout(3) // Default connection timeout: 3 seconds (values < 1 are in milliseconds)
->regionId('cn-shanghai')
->asDefaultClient();
$result = Green::v20180509()->detectFace()
->timeout(10) // Per-request timeout override: 10 seconds
->connectTimeout(3) // Per-request connection timeout override: 3 seconds
->body(json_encode([
'dataId' => '<your-data-id>', // Your business data ID for tracking
'url' => '<image-url>', // Public HTTP or HTTPS URL of the image
'bizType' => '<business-scenario>' // Your business scenario identifier
]))
->request();
print_r($result->toArray());
} catch (ClientException $exception) {
echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
echo $exception->getMessage() . PHP_EOL;
echo $exception->getErrorCode() . PHP_EOL;
echo $exception->getRequestId() . PHP_EOL;
echo $exception->getErrorMessage() . PHP_EOL;
}Replace the placeholders before running the code:
| Placeholder | Description | Example |
|---|---|---|
<your-data-id> | Unique ID for tracking the request in your system | img-20240101-001 |
<image-url> | Public HTTP or HTTPS URL of the image to analyze | https://example.com/photo.jpg |
<business-scenario> | Your bizType identifier for routing results | face_check |
Request body parameters
The request body is a JSON object passed to the body() method. The following table describes the parameters.
| Parameter | Type | Description |
|---|---|---|
dataId | String | Your internal identifier for the request. Use this to correlate detection results with records in your system. |
url | String | The URL of the image to analyze. Must be a public HTTP or HTTPS URL no longer than 2,048 characters. |
bizType | String | Your business scenario identifier for routing and categorizing results. |
For all request and response parameters, see Facial attribute detection API.
What's next
Review all response fields in the Facial attribute detection API reference.
Find the API endpoint for your region in Endpoints.