Serverless Application Using AWS Rekognition for Face Detection

Building applications using AWS services has become a passion of mine as I study to become an AWS Certified Solutions Architect and Developer. I am truly amazed at what can be built using the various services. Even more amazing is all the serverless apps that can be built.

In this article I’ll be using AWS Rekognition with Lambda, S3, and SNS to detect uploaded photos to S3 with certain celebrities and notify fans of the photos. Once again this is a serverless app using highly available, scalable, and durable AWS services.

AWS Rekognition - Detect Labels, Faces, and Celebrities

Although AWS Rekognition probably won’t show up on the AWS Solutions Architect and Developer Exams, I really want to understand how it works and how I can leverage it.

Rekognition has a number of cool features, but I was interested in 3 API calls:

  • Detect Labels
  • Detect Faces
  • Recognize-Celebrities

Detect labels using Rekognition is really cool. One can upload a photo and Rekognition will identify various items in the photos as labels that can be used for all kinds of cool purposes. One could use this to detect company brands and products in social media. It can be used as part of an image search engine when looking for photos with certain items or scenes.

Detect faces can be used as part of a facial recognition system for security purposes. The API returns bounding rectangle coordinates with faces detected in a photo. There are a number of complementary API calls that allow one to index and compare faces to make this even more interesting.

Recognize Celebrities is a fun API call that will return a list of known celebrities in a photo. It returns the names of the celebrities, the rectangular coordinates of their faces, and a percent confidence in the match.

I used all 3 API calls in various examples, but here I will just show a bit about the recognize-celebrities API call.

AWS Rekognition Recognize Celebrities of Mamamoo

I created a serverless app using AWS Rekognition, S3, Lambda, and SNS to detect photos uploaded to S3 that contain at least one member of the Korean Pop Group Mamamoo. If one of the members is detected, an SNS message is sent to fans with with a link to the photo.

AWS Rekognition Recognize Celebrities Serverless App

Trigger AWS Lambda Function on S3 Put

One of the most useful triggers of a Lambda Function is an S3 Put. In this scenario a user uploads a new photo to an S3 bucket. S3 sends an event to a Lambda Function with the name of the bucket and key. I like to use Python 3 for my Lambda Functions, but you can use Node.js, Java, or C#, too.

import urllib.parse


def lambda_handler(event, context):
    # Get the bucket and photo name
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    ...

Recognize Celebrities Rekognition API Call

Once I have the name of the S3 bucket and key, I can pass this information to AWS Rekognition to detect any celebrities. You will probably want to increase the timeout for the Lambda Function to be something other than the default of 3 seconds. AWS Rekognition needs to download the photo from S3, recognize any celebrities, and return the results. With the addition of your custom code this may take longer than 3 seconds.

I added the AWS Python SDK boto3 to make the API call to Rekognition to detect the celebrities in the photo. I am also using environment variables for the celebrity names and minimum acceptable match confidence.

import urllib.parse
import boto3
import os


# ['Hwasa','Moonbyul','Solar','Wheein']
mamamoo = os.getenv('mamamoo')

# 50
min_confidence = int(os.getenv('min_confidence'))

rek = boto3.client('rekognition')


def lambda_handler(event, context):
    # Get the bucket and photo name
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    # Look for Mamamoo!
    response = rek.recognize_celebrities(
        Image={
            'S3Object': {
                'Bucket': bucket,
                'Name': key
            }
        }
    )

    for celebrity in response['CelebrityFaces']:
        name = celebrity['Name']
        confidence = celebrity['MatchConfidence']

        if name in mamamoo and confidence >= min_confidence:
            # Notify Fans using SNS

AWS SNS Publish to Topic to Notify Celebrity Fans

You can send a notification to Mamamoo fans via SNS if they are detected in the photo. As a rule, I would put all the hardcoded SNS information in environment variables.

  • SNS TopicArn
  • Subject of SNS messages for email subscribers
  • URL of the S3 website for photos
  • The message format
sns = boto3.client('sns')

...

if name in mamamoo and confidence >= min_confidence:

    # sns_topic, sns_subject, url_prefix, and
    # sns_message_format pulled from environment
    # variables.
    message = sns_message_format.format(url_prefix, key)
    sns.publish(
        TopicArn=sns_topic,
        Subject=sns_subject,
        Message = message
    )

...

Amazing how much you can do with AWS Rekognition and AWS Services in general! As with all these examples, IAM Roles and the policies attached to them are crucial for security and to make sure the Lambda Functions can perform the necessary actions.

Contents