0

I have uploaded images using php on S3, now i want to compare/match given image in my S3 collection, I googled but not getting answer, if it is possible to use AWS face rekognition using PHP to search in S3 collection.

3 Answers3

0

Use the compareFaces APi to compare faces present in two images stored in S3. Keep one image as source and pass the target images in iteration.

API:

<?php
    require 'vendor/autoload.php';

    use Aws\Rekognition\RekognitionClient;

    $options = [
        'region'            => 'us-east-1',
        'version'           => '2016-06-27',
    ];

    $client = new RekognitionClient($options);

    $result = $client->compareFaces([
        'SimilarityThreshold' => <float>,
        'SourceImage' => [ 
            'Bytes' => <string || resource || Psr\Http\Message\StreamInterface>,
            'S3Object' => [
                'Bucket' => '<string>',
                'Name' => '<string>',
                'Version' => '<string>',
            ],
        ],
        'TargetImage' => [ 
            'Bytes' => <string || resource || Psr\Http\Message\StreamInterface>,
            'S3Object' => [
                'Bucket' => '<string>',
                'Name' => '<string>',
                'Version' => '<string>',
            ],
        ],
    ]);
?>

If you want to search within a collection(assuming you have already created one using CreateCollection API and have already indexed faces in it), then you can use the searchFacesByImage API. Just provide the target collection-id(which has multiple images indexed in it) along with the s3-location of the source image. The response will contain all the faces that matches, ordered by similarity score.

API:

$result = $client->searchFacesByImage([
    'CollectionId' => '<string>', 
    'FaceMatchThreshold' => <float>,
    'Image' => [ 
        'Bytes' => <string || resource || Psr\Http\Message\StreamInterface>,
        'S3Object' => [
            'Bucket' => '<string>',
            'Name' => '<string>',
            'Version' => '<string>',
        ],
    ],
    'MaxFaces' => <integer>,
]);
Madhukar Mohanraju
  • 2,793
  • 11
  • 28
  • don't forget that you need to create the collection using indexFaces: – Chris Adzima Oct 13 '17 at 16:26
  • Thank you @Madukar , Your given code look helpful but when i am trying it I don't know package name and class of $client in your given code "$result = $client->searchFacesByImage". Same question from you which i asked with Chris in his suggestion. Could you please explain me how to initialize it with it's parameter and which package should i use? Thank you in advance. – user3560746 Oct 14 '17 at 17:49
  • Thanks @madhukar, whatever code you gave it worked perfectly for me. I have a different requiremet like i have a folder in my s3 and i want to make a group e.g. "visitor". Example which you have i need to specify "SourceImage" and "TargetImage" but i want to search/find it by only specifing "SourceImage" and search it in full visitor folder. Please suggest code to save in that manner and compare as per my requirements. – user3560746 Oct 16 '17 at 14:00
  • some edition to above question. I tried to save collectionid while savinf image below is code. $response = $s3->putObject(array( 'CollectionId' => 'visitor-collection', 'Bucket' => 's3-visitor', 'Key' => 'visitors/'.$input['imageId'], 'SourceFile' => public_path('images/attachments/').$input['imageId'], )); $s3Id = $response['ObjectURL']; return $s3Id; – user3560746 Oct 16 '17 at 14:11
0

@Madhukar's answer is correct, however you need one more step to search the s3 bucket. You need to index the faces in the bucket like so:

$result = $rekog->indexFaces([ 
  'CollectionId' => '<string>', // REQUIRED 
  'ExternalImageId' => <string>, //This is important to know what image the face is in (I use the s3 key) 
  'Image' => [ 
      'S3Object' => [ 
        'Bucket' => '<string>', 
        'Name' => <string>, 
    ], 
  ], 
]);

You will need to do this for every image in the collection

Chris Adzima
  • 113
  • 9
0

Thanks @Madhukar, your suggestion worked for me to compare 2 images from S3 whereas to search in collection I had to add Chris's suggestion. Thank you @chris. Only my issue was I am doing in laravel so I had to do in Laravel style but core is all your suggestion. Thanks again both of you. If someone want laravel codding let me know, If I could help.