AWS offers a powerful facial recognition service called Amazon Rekognition. In this tutorial, we'll use Python and the AWS SDK to create a simple app that uses the Rekognition service to detect faces in an image.
To get started, you'll need an AWS account and access to the AWS Console. Once you're logged in, navigate to the Security Credentials in the right nav bar and create a user with the AmazonRekognitionFullAccess permission. Then, create the access key and copy the secret key as well.
Next, you'll need to install the AWS SDK for Python (Boto3) using the following command:
pip install boto3
Once you've installed Boto3, you can import the necessary libraries in your Python script:
import boto3
from PIL import Image, ImageDraw, ImageFont
To use the Rekognition service, you'll need to create a Boto3 client and specify the region:
rekognition = boto3.client('rekognition', aws_access_key_id='<YOUR_ACCESS_KEY_ID>', aws_secret_access_key='<YOUR_SECRET_ACCESS_KEY>', region_name='us-east-1')
For this example, we're going to use Arial font. You can use whatever you want. First, you must download the TTF file for that font.
font = ImageFont.truetype('arial.ttf', 30)
Now, we will read the image and call the detect_faces method.
with open(image_name, 'rb') as image_data:
response_content = image_data.read()
rekognition_response = rekognition.detect_faces(Image={'Bytes': response_content}, Attributes=['ALL'])
This is the summarized response from the above request.
{
"FaceDetails":[
{
"BoundingBox":{
"Width":0.1575244814157486,
"Height":0.26773321628570557,
"Left":0.7044062614440918,
"Top":0.35157522559165955
},
"AgeRange":{
"Low":22,
"High":30
},
"Smile":{
"Value":true,
"Confidence":96.33894348144531
},
"Eyeglasses":{
"Value":false,
"Confidence":97.71277618408203
},
"Sunglasses":{
"Value":false,
"Confidence":99.9967269897461
},
"Gender":{
"Value":"Male",
"Confidence":99.95927429199219
},
"Beard":{
"Value":true,
"Confidence":99.96766662597656
},
"Mustache":{
"Value":true,
"Confidence":88.09671783447266
},
"EyesOpen":{
"Value":true,
"Confidence":98.2445297241211
},
"MouthOpen":{
"Value":true,
"Confidence":95.4747314453125
},
"Emotions":[
{
"Type":"HAPPY",
"Confidence":99.83971405029297
},
{
"Type":"SURPRISED",
"Confidence":6.266087532043457
},
{
"Type":"FEAR",
"Confidence":5.88023567199707
},
{
"Type":"SAD",
"Confidence":2.1496529579162598
},
{
"Type":"CONFUSED",
"Confidence":0.06452298164367676
},
{
"Type":"CALM",
"Confidence":0.018650680780410767
},
{
"Type":"ANGRY",
"Confidence":0.012820698320865631
},
{
"Type":"DISGUSTED",
"Confidence":0.011646679602563381
}
],
"Pose":{
"Roll":-27.842042922973633,
"Yaw":-6.863317012786865,
"Pitch":6.0284576416015625
},
"Quality":{
"Brightness":93.38902282714844,
"Sharpness":92.22801208496094
},
"Confidence":99.99991607666016
}
],
"ResponseMetadata":{
"RequestId":"dd18647f-736c-44a9-aaa3-6c9782c657c7",
"HTTPStatusCode":200,
"HTTPHeaders":{
"x-amzn-requestid":"dd18647f-736c-44a9-aaa3-6c9782c657c7",
"content-type":"application/x-amz-json-1.1",
"content-length":"23339",
"date":"Mon, 20 Mar 2023 20:50:38 GMT"
},
"RetryAttempts":0
}
}

Join the conversation! Your thoughts help the community grow.