Face Rekognition, powerful yet simple way!

Someone applies for a position, you do the background check, which comes as all nice and dandy but just what if the IDs were changed or background check itself was not good enough? What if I wanted to see if someone is part of an offenders list before I open the door or allow someone to join my company or if I want to analyse images and index my data based on that?

So I decided to try AWS Rekognition (I am sure you noticed the weird spelling in the title) to see if I can develop a basic Serverless application which can be scaled if needed to utilise other cloud based services with minimum possible cost .

Amazon Rekognition is a service that makes it easy to add image analysis to your applications. With Rekognition, you can detect objects, scenes, faces; recognise celebrities; and identify inappropriate content in images. You can also search and compare faces. Rekognition’s API enables you to quickly add sophisticated deep learning-based visual search and image classification to your applications.

So for the fun, I selected 3 different pictures of myself, they were taken 1 year apart…uploaded them to a S3 Bucket called xx-anshu.

And wrote some simple #python code which tries to detect object in first picture and tries to compare picture 2 and 3.

#! /usr/bin/python
import boto3

BUCKET = "xx-anshu"
GLASSES_FACE = "an1.jpg"
IMAGE_SOURCE = "an2.jpg"
SCANNED_FACE = "an3.jpg"

def findmea_match(bucket, image_source, bucket_target, scanned_face, threshold=80, region='us-east-1'):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.compare_faces(
        SourceImage={"S3Object": {"Bucket": bucket, "Name": image_source, }},
        TargetImage={"S3Object": {"Bucket": bucket_target, "Name": scanned_face, }},
        SimilarityThreshold=threshold,
        )
    return response['SourceImageFace'], response['FaceMatches']


def check_glasses(bucket_target, scanned_face, threshold=48, region='us-east-1'):
    rekognition = boto3.client("rekognition", region)
    label_data = rekognition.detect_labels(
        Image={"S3Object": {"Bucket": bucket_target, "Name": scanned_face, }},
        MinConfidence=threshold,
        )
    return label_data['Labels']

matches = check_glasses(BUCKET, GLASSES_FACE)

for key in matches:
    if 'Glasses' in key['Name']:
        print "Glasses Detected in Target Image: ", GLASSES_FACE
        print "Remove and try again..........................."

print "Comparing Source and Scanned Images now............", IMAGE_SOURCE, SCANNED_FACE

source_face, matches = findmea_match(BUCKET, IMAGE_SOURCE, BUCKET, SCANNED_FACE)

print "Confidence in source Detected as a Face: ({Confidence}%)".format(**source_face)

# Print if matched above threshold
for match in matches:
    print "Confidence in scanned Face detected as a face ({Confidence}%)".format(**match['Face'])
    print "The Similarity between source face and scanned face is: {}%".format(match['Similarity'])

What do I get??

$ ./facecomp.py 
Glasses Detected in Target Image:  an1.jpg
Remove and try again...........................

Comparing Source and Scanned Images now............ an2.jpg an3.jpg
Confidence in source Detected as a Face: (99.9611740112%)
Confidence in scanned Face detected as a face (99.9963607788%)
The Similarity between source face and scanned face is: 97.0%

What above did:

  1. Identified I was wearing glasses. You can filter down to objects you desire or don’t like (shopping centre/concert hall security)!
  2. Compared image 2 and 3 and confirmed its a 97% match for the face with 99% confidence that uploaded image was indeed a face (and not a helmet for example).
  3. Ran on a complete Serverless infrastructure which can support Real Time or Batch analysis of Images, Objects and Face comparisons. You can even get the emotions from the picture!

Costed me nothing to test it as its under Free Tier but pricing model is pretty competitive. $1 for the first 1 million images processed per month! Get the calculator out?

Tie it with SNS and you can send a notification to your SecOps or store the result in a database for Image Stream analysis/mining job later on.

What this can be used for (imagination is your friend as always):

  • Supercharge your IP camera which can now detect objects, compare known faces and take actions using AWS Service offerings.
  • Increase the ability of our law enforcement officers to act quickly and decisively.

Amazon Rekognition is based on the same proven, highly scalable, deep learning technology developed by Amazon’s computer vision scientists to analyse billions of images daily for Prime Photos. Amazon Rekognition uses deep neural network models to detect and label thousands of objects and scenes in images, and AWS is continually adding new labels and facial recognition features to the service.

Whats not to love about Cloud or Automating things? 🙂


Continue reading...

Everything you missed at DevDay
Natalie Byrgiotis
October 30, 2018
Chameleon SMS
Ibrahim Tareq
October 29, 2018
MessageMedia x Auth0
Ibrahim Tareq
October 25, 2018