Face Recognition using AWS and Python

Jeganath PV
3 min readJul 24, 2021

--

Are you still using on premise server to do face recognition than using with simple RestAPI on the cloud services?

There are multiple ways to do face recognition today, there are a lot of libraries available in python fot this. But when it comes to scale the performance we can go with AWS Services to detect and recognize the face object.

We will start with the approach to make the face recognition setup that works with AWS Service.

AWS Face Rekognition

It provides highly accurate facial analysis and facial search capabilities that you can use to detect, analyze, and compare faces for a variety of user verification, people counting, and public safety use cases.

How to setup?

First thing, we need to give access for the user to use FaceRekognition services by using AWS IAM Console

  1. In IAM Console, Add/Modify the user available in Access Management.
  2. Select the user to give permissions to use FaceRekognition service. (Here I’m go with existing policies)

3. In the list of permissions, Select AmazonRekognitionServiceRole.

Note: Also you can select AmazonRekognitionReadonly mode, If you have different clients

4. Once you successfully added the roles to user, your IAM console will be look like above.

5. Then go to Security credentials, and generate new credentials for the user and download the credentials file.

That’s all the setup for Face recognition in AWS.

Lets Integrate with Python

To integrate AWS Services, Python requires boto3 SDK, it can easily install using

pip install boto3

boto3 is act as client which triggers every functions as Rest API, So this can be lightweight and reason behind the high performance on our machine.

Now I have to read the credential properties from the downloaded csv file. Once I done with that, I can able to create AWS client using boto3 like this. Here I have held the credential object as aws

boto3.client('rekognition', aws_access_key_id=aws.access_key_id,                                 aws_secret_access_key=aws.secret_access_key, region_name=aws.region)

Normally, Face recognition can split into two phases. Adding facial objects(Indexing the face) and extracting the facial objects(Recognition).

Adding the face

To add/index the face pattern into FaceRekognition, it requires image in bytes format. And once the indexing is done it will return unique object id for the face indexed.

We need to store the object id in any databases or by using some other techniques. Also we can use aws itself to store the value using DynamoDB services.

To index the face, we can use the method index_faces()

response = botoClient.index_faces(Image= {"Bytes":image_bytes},CollectionId="MyNewRekognition")
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
faceId = response['FaceRecords'][0]['Face']['FaceId']

While indexing and searching, we have specify unique collection name, where all the face patterns would be stored

Searching the face index

To search particular face from the collection, we have to use similar approach by passing image in bytes and collection name into method search_faces_by_image()

response = botoClient.search_faces_by_image(Image= {"Bytes":image_bytes},CollectionId="MyNewRekognition")

In the response you will get the list of object id which matches to the given image. By using the list, we can match the face pattern against the value we stored during the face index.

So That’s all about the face recognition using RestAPI from python boto3.

Thanks

If you’ve any questions, comments or suggestions please hit me up on Twitter or LinkedIn

--

--

Jeganath PV

Full Stack Developer | .NET | Angular | Azure | System design | JavaScript | Python | Shopify