Add faces

Updated at:

Add face images for a registered person using the Content Moderation SDK for PHP.

Prerequisites

Before you begin, ensure that you have:

  • Installed the Content Moderation SDK for PHP. For installation instructions and required PHP version, see Installation.

  • A person ID for the person you want to add faces to.

  • URLs for the face images you want to register.

Use the PHP version specified in the Installation guide. Using an incompatible version causes API calls to fail.

Usage notes

  • Each person can have a maximum of 20 faces.

  • Each image must contain exactly one face.

  • The personId and urls parameters are required.

  • Use the Content Moderation API endpoints to call this SDK. For endpoint details, see Endpoints.

For the full list of parameters, see API operation for adding faces.

Add faces for a person

The following example uses the addFaces() method to register face image URLs for a specified person.

<?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 connections.
    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
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    // Build the request body.
    // personId: the ID of the person to add faces to (required)
    // urls: URLs of face images (required, one face per image)
    $person = [
        'personId' => '<person-id>',
        'urls'     => ['<face-image-url>'],
    ];

    $result = Green::v20180509()->addFaces()
        ->timeout(10)        // Per-request timeout: 10 seconds
        ->connectTimeout(3)  // Per-request connection timeout: 3 seconds
        ->body(json_encode($person))
        ->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 following placeholders with actual values:

PlaceholderDescription
<person-id>The ID of the person you want to add faces to
<face-image-url>The URL of a face image. Each image must contain exactly one face

What's next