AWS Rekognition: Face detection & analysis of an image

Amazon Rekognition is a deep-learning-powered video analysis service that tracks people, detects activities, and recognizes objects in live streams and returns a specific label of activity, person, faces and object with timestamps. With Amazon Rekognition video service, you receive a detailed analysis of an image, which includes bounding boxes and confidence scores for persons. 

In this tutorial, you will get to know about how to perform a facial analysis of an image. We can provide an input image from an S3 bucket to AWS Rekognition. In the below example, we perform face analysis on an image from the S3 bucket.  First of all, you need to upload an image to your S3 bucket.

To use the AWS Rekognition service, you required AmazonRekognitionFullAccessand AmazonS3ReadOnlyAccess permission to IAM user.

Let’s write the Python code to get an image from the S3 bucket and analyze it using AWS Rekognition. This will return the details information of each detected faces such as coordinates of the bounding box, confidence score, facial landmarks, facial attributes, quality, pose and emotions.

# Import libraries
import boto3
import json

# Specify bucket name and image key name
image_name = 'image1.jpg'
bucket_name = 'bucket01'

# Invoke rekognition client
client=boto3.client('rekognition')
response = client.detect_faces(Image={'S3Object':{'Bucket':bucket_name, 'Name':image_name}}, Attributes=['ALL'])

print(response)

Below is the return response of the AWS Rekognition :

{'FaceDetails': [{'BoundingBox': {'Width': 0.02050507813692093,
    'Height': 0.05144232138991356,
    'Left': 0.3497592508792877,
    'Top': 0.12268004566431046},
   'AgeRange': {'Low': 22, 'High': 34},
   'Smile': {'Value': False, 'Confidence': 98.50390625},
   'Eyeglasses': {'Value': False, 'Confidence': 99.05378723144531},
   'Sunglasses': {'Value': False, 'Confidence': 99.56298065185547},
   'Gender': {'Value': 'Male', 'Confidence': 99.27241516113281},
   'Beard': {'Value': True, 'Confidence': 59.3468017578125},
   'Mustache': {'Value': False, 'Confidence': 90.42159271240234},
   'EyesOpen': {'Value': True, 'Confidence': 90.51400756835938},
   'MouthOpen': {'Value': False, 'Confidence': 90.04197692871094},
   'Emotions': [{'Type': 'SAD', 'Confidence': 61.79646682739258},
    {'Type': 'CALM', 'Confidence': 33.470298767089844},
    {'Type': 'CONFUSED', 'Confidence': 3.1943230628967285},
    {'Type': 'SURPRISED', 'Confidence': 0.6049894690513611},
    {'Type': 'HAPPY', 'Confidence': 0.54181307554245},
    {'Type': 'DISGUSTED', 'Confidence': 0.1901642382144928},
    {'Type': 'FEAR', 'Confidence': 0.10350687801837921},
    {'Type': 'ANGRY', 'Confidence': 0.0984325185418129}],
   'Landmarks': [{'Type': 'eyeLeft',
     'X': 0.361727774143219,
     'Y': 0.1472964584827423},
    {'Type': 'eyeRight', 'X': 0.3680972158908844, 'Y': 0.1410059779882431},
    {'Type': 'mouthLeft', 'X': 0.3645886182785034, 'Y': 0.16544601321220398},
    {'Type': 'mouthRight', 'X': 0.3698621094226837, 'Y': 0.1599721908569336},
    {'Type': 'nose', 'X': 0.37093040347099304, 'Y': 0.15358413755893707},
    {'Type': 'leftEyeBrowLeft',
     'X': 0.35687291622161865,
     'Y': 0.1457904428243637},
    {'Type': 'leftEyeBrowRight',
     'X': 0.36093810200691223,
     'Y': 0.14151668548583984},
    {'Type': 'leftEyeBrowUp',
     'X': 0.3638649582862854,
     'Y': 0.14060719311237335},
    {'Type': 'rightEyeBrowLeft',
     'X': 0.3676479756832123,
     'Y': 0.13723088800907135},
    {'Type': 'rightEyeBrowRight',
     'X': 0.3685528039932251,
     'Y': 0.13464553654193878},
    {'Type': 'rightEyeBrowUp',
     'X': 0.36820659041404724,
     'Y': 0.13512185215950012},
    {'Type': 'leftEyeLeft', 'X': 0.3598822355270386, 'Y': 0.14842648804187775},
    {'Type': 'leftEyeRight', 'X': 0.36293825507164, 'Y': 0.1462772786617279},
    {'Type': 'leftEyeUp', 'X': 0.3617890477180481, 'Y': 0.1463918536901474},
    {'Type': 'leftEyeDown', 'X': 0.3618181645870209, 'Y': 0.1481175422668457},
    {'Type': 'rightEyeLeft',
     'X': 0.36680564284324646,
     'Y': 0.14252303540706635},
    {'Type': 'rightEyeRight',
     'X': 0.36862584948539734,
     'Y': 0.13995178043842316},
    {'Type': 'rightEyeUp', 'X': 0.36824333667755127, 'Y': 0.1401406228542328},
    {'Type': 'rightEyeDown',
     'X': 0.3681838810443878,
     'Y': 0.14188992977142334},
    {'Type': 'noseLeft', 'X': 0.3669634163379669, 'Y': 0.15682452917099},
    {'Type': 'noseRight', 'X': 0.3694436848163605, 'Y': 0.15454916656017303},
    {'Type': 'mouthUp', 'X': 0.36918479204177856, 'Y': 0.15993355214595795},
    {'Type': 'mouthDown', 'X': 0.36921653151512146, 'Y': 0.16538703441619873},
    {'Type': 'leftPupil', 'X': 0.361727774143219, 'Y': 0.1472964584827423},
    {'Type': 'rightPupil', 'X': 0.3680972158908844, 'Y': 0.1410059779882431},
    {'Type': 'upperJawlineLeft',
     'X': 0.350109726190567,
     'Y': 0.15218301117420197},
    {'Type': 'midJawlineLeft',
     'X': 0.35492420196533203,
     'Y': 0.16981038451194763},
    {'Type': 'chinBottom', 'X': 0.3683694303035736, 'Y': 0.17458897829055786},
    {'Type': 'midJawlineRight',
     'X': 0.3668743968009949,
     'Y': 0.15912339091300964},
    {'Type': 'upperJawlineRight',
     'X': 0.36458173394203186,
     'Y': 0.13908009231090546}],
   'Pose': {'Roll': -41.972652435302734,
    'Yaw': 42.499916076660156,
    'Pitch': -40.27721405029297},
   'Quality': {'Brightness': 42.23616409301758,
    'Sharpness': 32.20803451538086},
   'Confidence': 99.9810791015625}],
 'ResponseMetadata': {'RequestId': 'bae8719b-8f4a-490e-a5b4-4510d0e5845e',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',
   'date': 'Sat, 28 Nov 2020 17:59:08 GMT',
   'x-amzn-requestid': 'bae8719b-8f4a-490e-a5b4-4510d0e5845e',
   'content-length': '3350',
   'connection': 'keep-alive'},
  'RetryAttempts': 0}}

Leave a Reply

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

Machine Learning Model Tutorials