paint-brush
Image Analysis using AWS Rekognition via Lambda Functionby@snehalnarkar9
4,687 reads
4,687 reads

Image Analysis using AWS Rekognition via Lambda Function

by Snehal NarkarJuly 7th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Amazon Rekognition is one of the AWS services to perform image and video analysis. The flow for image analysis will be: We are going to read an image from the S3 bucket via a Lambda function. In the second step we will pass that image to rekognitions. In response to this we will return labels. In this blog, we will be going to perform label detection and object detection for an image so basically we are performing image analysis in this blog. We are using four services of AWS.

Company Mentioned

Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Image Analysis using AWS Rekognition via Lambda Function
Snehal Narkar HackerNoon profile picture

In this blog, I am going to show you how we can use rekognition for image analysis using lambda function.we will be going to perform label detection and object detection for an image so basically we are performing image analysis in this blog.

What is AWS Rekognition?

Rekognition is one of the AWS services to perform image and video analysis. So here all we need to provide is the image or video to the AWS Rekognition service and it will help us to identify an object, people, text, activities, and scenes.

Benefits of using Amazon Rekognition are as follows:

  1. Integrating powerful image and video analysis into your apps.
  2. Deep learning-based image and video analysis.
  3. Scalable image analysis.
  4. Integration with other AWS services.
  5. Low cost

Common use cases for using Amazon Rekognition mentioned in the following:

  1. Searchable image and video libraries
  2. Face-based user verification
  3. Sentiment and demographic analysis
  4. Facial Search
  5. Unsafe content detection
  6. Celebrity recognition
  7. Text detection
  8. Custom labels

For Image analysis, we are using four services of AWS.

  1. IAM
  2. S3
  3. Lamda
  4. Rekognition[This service we are using within the lambda function]

So the flow for image analysis will be:

  • Firstly, we are going to read an image from the S3 bucket via a lambda function. 
  • And in the second step we will pass that image to rekognition service via calling rekognition API. In response to this, rekognition API will return labels.

Step 1: Creating an IAM role:

  • Go to the AWS Management console.
  • Search for the IAM service and enter.
  • In the IAM service on the left side click on Roles In that click on Create Role button.
  • Select the type of trusted entity as an AWS service by default.
  • In Choose a use case selects Lambda and then click on Next: Permission button.
  • In the Attach permissions policies select two policies :
a. AmazonRekognitionFullAccess
b. AWSLambdaExecute


  • Click on Next: Tags button.
  • Add tags part is optional so click on Next: Review button.
  • Give a name to your role. You can give any name to your role[for eg.lamda_rekognition ]and click on Create role button.
  • Your role is ready.

Step 2: Create an S3 bucket to store images:

  • Go to the AWS Management console.
  • Search for the S3 service and enter.
  • Click on the Create bucket button.
  • Give any unique name to you bucket[for eg rekognition].
  • Keep all default settings as it is and click on the Create bucket button.
  • Once your bucket is created click on your bucket name. In that click on the upload button and drag and drop any image that you want and click on the upload button directly. Once the image is uploaded you can see the image as follows

For the image analysis, I uploaded the following image.

Step 3: Create a Lamda Function:

  • Go to the AWS Management console.
  • Search for the Lambda service and enter.
  • After coming onto the lambda service page click on Create function button.
  • Select the Author from Scratch default option.
  • For function name give any name of your choice[for eg lamda_rekognition].
  • In the Runtime select python 3.6
  • Expand the Choose or create an execution role. 
  • In that select Use an existing role. And in the existing role select the role that we created in our first step[I have given the name for a role is lamda_rekognition]. 
  • Finally, click on create function button
  • Once you created a lambda function then click on your function name.
  • In the function, code editor type the function that I have given in the following:
  • In the following code, you can directly pass the S3 references in the response using rekognition client and you will get a response.”MaxLables=3" term is optional using this you can able to see only three labels for the image if we did not mention the name you get more label names for your images
Note: In place of buket_name and image_name plz mentioned your S3 bucket name and uploaded image name.
import json
import boto3
def lambda_handler(event, context):
client = boto3.client("rekognition")
#passing s3 bucket object file reference    
response = client.detect_labels(Image = {"S3Object": {"Bucket":       "bucket_name", "Name": "image_name"}}, MaxLabels=3,  MinConfidence=70)
print(response)
return "Thanks"
  • If you want to pass the byte data in the function then also you can pass then prefer the following code.
  • import json
    import boto3
    def lambda_handler(event, context):
    client = boto3.client("rekognition")
        s3 = boto3.client("s3")
    # reading file from s3 bucket and passing it as bytes
    fileObj = s3.get_object(Bucket = "bucket_name", Key="image_name")
    file_content = fileObj["Body"].read()
    # passing bytes data
    response = client.detect_labels(Image = {"Bytes": file_content}, MaxLabels=3, MinConfidence=70)
    print(response)
    return "Thanks"
  • You can add any one of the codes that I have given above you will get the same response. Just I have shown you two different kinds of code for the lambda function.
  • After adding the code click on the save button.
  • To test the function for image analysis. Click on the Test button given on the upper right side. Once you click on the test button it will pop up one window.
  • In that select Create new test event and in Event Template give any name that you want.
  • And finally, click on the Create button.
  • Once you create the test event it will show you the test event name. So now click on the Test button.
  • Once you click on the test the response will look like below:
  • START RequestId: 3c1284c2-8611-4888-9030-7942d144a180 Version: $LATEST
    {'Labels': [{'Name': 'Person', 'Confidence': 99.60957336425781, 'Instances': [{'BoundingBox': {'Width': 0.15077881515026093, 'Height': 0.8669684529304504, 'Left': 0.6409724354743958, 'Top': 0.07304109632968903}, 'Confidence': 99.60957336425781}], 'Parents': []}, {'Name': 'Human', 'Confidence': 99.60957336425781, 'Instances': [], 'Parents': []}, {'Name': 'Transportation', 'Confidence': 94.79045104980469, 'Instances': [], 'Parents': []}, {'Name': 'Automobile', 'Confidence': 94.79045104980469, 'Instances': [], 'Parents': [{'Name': 'Transportation'}, {'Name': 'Vehicle'}]}, {'Name': 'Vehicle', 'Confidence': 94.79045104980469, 'Instances': [], 'Parents': [{'Name': 'Transportation'}]}, {'Name': 'Car', 'Confidence': 94.79045104980469, 'Instances': [{'BoundingBox': {'Width': 0.9211472868919373, 'Height': 0.803646445274353, 'Left': 0.03170054033398628, 'Top': 0.15337026119232178}, 'Confidence': 82.67008209228516}], 'Parents': [{'Name': 'Transportation'}, {'Name': 'Vehicle'}]}, {'Name': 'Apparel', 'Confidence': 94.33142852783203, 'Instances': [], 'Parents': []}, {'Name': 'Clothing', 'Confidence': 94.33142852783203, 'Instances': [], 'Parents': []}, {'Name': 'Pants', 'Confidence': 94.33142852783203, 'Instances': [], 'Parents': [{'Name': 'Clothing'}]}, {'Name': 'Jeans', 'Confidence': 81.03520202636719, 'Instances': [], 'Parents': [{'Name': 'Pants'}, {'Name': 'Clothing'}]}, {'Name': 'Denim', 'Confidence': 81.03520202636719, 'Instances': [], 'Parents': [{'Name': 'Pants'}, {'Name': 'Clothing'}]}], 'LabelModelVersion': '2.0', 'ResponseMetadata': {'RequestId': 'aea9947a-f69d-47fc-a559-ea36dc06c822', 'HTTPStatusCode': 200, 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1', 'date': 'Sat, 23 May 2020 05:28:12 GMT', 'x-amzn-requestid': 'aea9947a-f69d-47fc-a559-ea36dc06c822', 'content-length': '1409', 'connection': 'keep-alive'}, 'RetryAttempts': 0}}
    END RequestId: 3c1284c2-8611-4888-9030-7942d144a180
    REPORT RequestId: 3c1284c2-8611-4888-9030-7942d144a180 Duration: 2403.35 ms Billed Duration: 2500 ms Memory Size: 128 MB Max Memory Used: 71 MB Init Duration: 168.60 ms

What exactly you will get in response:

This rekognition API takes individual images as input and returns an ordered list of labels and a corresponding numeric confidence index. As you can see I have uploaded a person image with a car. So in response, you can see you will get all kinds of labels like a person, human, vehicle, car, and it also returns bounding boxes coordinates for items that are detected in images (for e.g height and width of persons face).

The response you will get is based on the image that you upload in the S3 bucket.

Try adding different images in the S3 bucket and check the image analysis.

I hope you like the blog. 

Happy Coding 😃