Amazon Rekognition API’s integration Using PHP

First you need to create a account in AWS Account.
After once you finish Account creation process. Then you need to create
AWS access key ID , AWS secret access key once you have this then we can start programming.

Lets Getting Started Using the Amazon Rekognition
php API’s.

So fist we need to install Amazon Rekognition PHP SDK

You can install using GIT  Or install using composer.

Once you install Amazon Rekognition PHP SDK. Then create a file aws_tutorial.php

In this file we import required classes.

In Header of PHP file

require 'vendor/autoload.php'; 
use Aws\S3\S3Client;  
use Aws\Exception\AwsException;
use Aws\Rekognition\RekognitionClient;
use Aws\Rekognition\Exception\RekognitionException;

In order to use aws service we need to authenticate using
AWS access key ID , AWS secret access key . so for that we need to use this code

$credentials = new Aws\Credentials\Credentials('access_key', 'secret_key');
$s3Client= new Aws\S3\S3Client([
    'version'     => 'latest',
    'region'      => 'us-east-1',
    'credentials' => $credentials
]);

Once you authenticate. Then we need to create AWS Bucket.

$BUCKET_NAME="Name of bucket";
$result = $s3Client->createBucket([
        'Bucket' => $BUCKET_NAME,
    ]);

List AWS Buckets

//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
    echo $bucket['Name'] . "\n";
}

Create AWS Rekognition Client

$rekognitionClient = RekognitionClient::factory(array(
  'version'     => 'latest',
  'region'      => 'us-east-1',
  'credentials' => $credentials
  ));

Create AWS Collection

$result = $rekognitionClient->createCollection([
    'CollectionId' => 'Your Colletion ID',
]);

AWS Index Faces

$photo = 'abc.jpg';
$result = $rekognitionClient->indexFaces([
  'CollectionId' => 'collection ID',
  'DetectionAttributes' => ["DEFAULT"],
  'ExternalImageId' => 'abc',
  'Image' => [ 
    'Bytes' => file_get_contents($photo),
    
  ],
  'MaxFaces' => 2,
  'QualityFilter' => 'NONE',
]);
print_r($result);

AWS List Of faces

$result = $rekognitionClient->listFaces([
  'CollectionId' => 'collection id', 
  'MaxResults' => 20
]);
print_r($result);

AWS Search Faces

$photo = 'abc.jpg';
$result = $rekognitionClient->searchFacesByImage([
    'CollectionId' => 'Collection ID', 
    'FaceMatchThreshold' => 70.5,
    'Image' => [ 
        'Bytes' => file_get_contents($photo)
    ],
    'MaxFaces' => 2,
]);
print_r($result);

That’s it for now there are more method provide by AWS Face Rekognition click here

AWS Compare Faces

$result = $rekognition->compareFaces([
    'SimilarityThreshold' => 60,
    'SourceImage' => [
        'Bytes' => file_get_contents('temp/new/5c767b3a071e4.png'),
       
        ],
     'TargetImage' =>[
        'Bytes' => file_get_contents('temp/new/400de1fc-f313-44b4-ae20-82a3abc15104.jpg'),
        
    ],
]);

AWS Rekognition Celebrity

$photo = 'abc.jpg'; 
$result = $rekognition->recognizeCelebrities([
'Image' => ['Bytes'=>file_get_contents($photo),],
"MaxLabels"=> 10,
"MinConfidence"=>77,
]);

AWS Detect Faces

$photo = 'temp/new/abc.jpg';
$result = $rekognition->detectLabels(array(
   'Image' => array(
      'Bytes' => file_get_contents($photo),
   ),
   "MaxLabels"=> 10,
    "MinConfidence"=>77,
   )
);

Add a Comment

Your email address will not be published. Required fields are marked *