Rekognition client
This page contains examples with the Rekognition client. See the client introduction for a more detailed description how to use a client. You may also want to consider the authentication documentation to understand the many ways you can authenticate with AWS.
The Rekognition package could be installed with Composer.
composer require async-aws/rekognition
A new client object may be instantiated by:
use AsyncAws\Rekognition\RekognitionClient;
$rekognition = new RekognitionClient();
The authentication parameters is read from the environment by default. You can also specify a AWS access id and secret:
use AsyncAws\Rekognition\RekognitionClient;
$rekognition = new RekognitionClient([
'accessKeyId' => 'my_access_key',
'accessKeySecret' => 'my_access_secret',
'region' => 'eu-central-1',
]);
For all available options, see the configuration reference.
The client supports the basics methods for common use cases of facial recognition
Usage¶
Create Collections¶
Creating collection is a way to split your rekognition workflows into bags with is own characteristics, for easy searching:
use AsyncAws\Rekognition\RekognitionClient;
use AsyncAws\Rekognition\Input\CreateCollectionRequest;
$rekognition = new RekognitionClient();
$request = new CreateCollectionRequest([
"collectionId" => "dogs-collection",
]);
$response = $rekognition->createCollection($request);
echo 'Version: ' . $response->getFaceModelVersion();
Add faces to a collection¶
If you want to add faces from an image into a collection you will use the method indexFaces.
use AsyncAws\Rekognition\RekognitionClient;
use AsyncAws\Rekognition\Input\IndexFacesRequest;
use AsyncAws\Rekognition\ValueObject\Image;
$rekognition = new RekognitionClient();
$addFacesRequest = new IndexFacesRequest(
[
'Image' => new Image(['Bytes' => file_get_contents(__DIR__ . '/resources/snoopy.jpg')]),
'CollectionId' => 'dogs-collection',
'ExternalImageId' => 'snoopy_1',
]
);
foreach ($rekognition->indexFaces($addFacesRequest)->getFaceRecords() as $faceRecord) {
echo 'face: ' . $faceRecord->getFace()->getFaceId() . ' (' . $faceRecord->getFace()->getConfidence() . '%)';
}
Search for a face inside a collection¶
When you want to search a face inside a collection with an image you will use the method searchFacesByImage.
use AsyncAws\Rekognition\RekognitionClient;
use AsyncAws\Rekognition\Input\searchFacesByImageRequest;
use AsyncAws\Rekognition\ValueObject\Image;
$rekognition = new RekognitionClient();
$addFacesRequest = new SearchFacesByImageRequest(
[
'image' => new Image(['Bytes' => file_get_contents(__DIR__ . '/resources/snoopy.jpg')]),
'faceMatchThreshold' => 75.5,
'collectionId' => 'dogs-collection',
]
);
$response = $rekognition->searchFacesByImage($addFacesRequest)->getSearchedFaceBoundingBox();
foreach ($rekognition->searchFacesByImage($addFacesRequest)->getFaceMatches() as $faceMatch) {
echo 'face: ' . $faceMatch->getFace()->getFaceId() . ' (' . $faceMatch->getFace()->getConfidence() . '%)';
}
The source code to this page is found on GitHub.