Hashir Baig
Hashir Baig

Reputation: 2361

Download image from ImageID and collection using Amazon Rekognition

I am building a face recogition app using AWS Rekognition. I also want to show the image to user that matches with my input image. Does AWS also saves the image when we index them? If yes, how can I get that image?

If no I am thinking to do it this way. I can save the image into S3 with same name as ExternalImageId so that when ever a face is detected, I read the external id from Rekognition and fetch that from S3.

If there is a better approach than this, please let me know.

I am using following code to index an image in a collection:

import boto3
from PIL import Image
import io
import time

image1 = 'images/haad2.jpg'

client = boto3.client('rekognition')
image = Image.open(image1)

stream = io.BytesIO()
image.save(stream, format="JPEG")
image_binary = stream.getvalue()

response = client.index_faces(
    Image={
        'Bytes': image_binary
    },
    CollectionId='root_faces_data',
    ExternalImageId=str(time.time()),
    DetectionAttributes=[
        'ALL',
    ]
)

print(response)

And the following code to see if a face exists in a collection:

import boto3
import io
from PIL import Image

client = boto3.client('rekognition')

image1 = 'images/haad2.jpg'

client = boto3.client('rekognition')
image = Image.open(image1)

stream = io.BytesIO()
image.save(stream, format="JPEG")
image_binary = stream.getvalue()

response = client.search_faces_by_image(
    CollectionId='root_faces_data',
    Image={
        'Bytes': image_binary
    },
    MaxFaces=10,
    FaceMatchThreshold=90
)

print(response)

This is the output of search_faces_by_image:

{
  'SearchedFaceBoundingBox': {
    'Width': 0.2646464705467224,
    'Height': 0.39817628264427185,
    'Left': 0.3186868727207184,
    'Top': 0.23252280056476593
  },
  'SearchedFaceConfidence': 99.9957275390625,
  'FaceMatches': [
    {
      'Similarity': 99.98405456542969,
      'Face': {
        'FaceId': '5bc98595-7d30-4447-b430-4c0fd8f1b926',
        'BoundingBox': {
          'Width': 0.2646459937095642,
          'Height': 0.39817601442337036,
          'Left': 0.31868699193000793,
          'Top': 0.23252299427986145
        },
        'ImageId': '8e631731-4a0c-513d-be32-dbfe3ae5e813',
        'ExternalImageId': '1534576206.8314612',
        'Confidence': 99.9957046508789
      }
    }
  ],
  'FaceModelVersion': '3.0',
  'ResponseMetadata': {
    'RequestId': 'eca4bea6-a2b5-11e8-9345-a5eddf19f47f',
    'HTTPStatusCode': 200,
    'HTTPHeaders': {
      'content-type': 'application/x-amz-json-1.1',
      'date': 'Sat, 18 Aug 2018 07:12:09 GMT',
      'x-amzn-requestid': 'eca4bea6-a2b5-11e8-9345-a5eddf19f47f',
      'content-length': '553',
      'connection': 'keep-alive'
    },
    'RetryAttempts': 0
  }
}

Upvotes: 2

Views: 1004

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270294

No. When a face is added to a Face Collection, the original image is not kept.

From Adding Faces to a Collection - Amazon Rekognition:

For each face detected, Amazon Rekognition extracts facial features and stores the feature information in a database. In addition, the command stores metadata for each face that's detected in the specified face collection. Amazon Rekognition doesn't store the actual image bytes.

Your idea of storing the original image in Amazon S3 is good. However, you can improve it by using the BoundingBox information in the response to search_faces_by_image().

When Amazon Rekognition adds a face to a Face Collection, it searches for the largest face in the input image. The location of this face is provided by BoundingBox.

Before storing the original image, you should crop the image using the BoundingBox dimensions. Note that these are relative to the dimension size, so you'll need to use something like:

(int(face['BoundingBox']['Left'] * width),
 int(face['BoundingBox']['Top'] * height)),
(int((face['BoundingBox']['Left'] + face['BoundingBox']['Width']) * width),
 int((face['BoundingBox']['Top'] + face['BoundingBox']['Height']) * height))

The result is that all of your images will be focussed on the faces themselves.

Upvotes: 4

Related Questions