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: Integrating powerful image and video analysis into your apps. Deep learning-based image and video analysis. Scalable image analysis. Integration with other AWS services. Low cost Common use cases for using Amazon Rekognition mentioned in the following: Searchable image and video libraries Face-based user verification Sentiment and demographic analysis Facial Search Unsafe content detection Celebrity recognition Text detection Custom labels For Image analysis, we are using four services of AWS. IAM S3 Lamda 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 service and enter. IAM In the IAM service on the left side click on In that click on button. Roles Create Role Select the type of trusted entity as an by default. AWS service In Choose a use case selects and then click on button. Lambda Next: Permission In the Attach permissions policies select two policies : a. AmazonRekognitionFullAccess b. AWSLambdaExecute Click on button. Next: Tags Add tags part is optional so click on button. Next: Review Give a to your role. You can give any name to your role[for eg.lamda_rekognition ]and click on Create . name role button Your role is ready. Step 2: Create an S3 bucket to store images: Go to the AWS Management console. Search for the service and enter. S3 Click on the button. Create bucket Give any unique to you bucket[for eg rekognition]. name Keep all default settings as it is and click on the button. Create bucket Once your bucket is created . In that click on the 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 click on your bucket name upload button For the image analysis, I uploaded the following image. Step 3: Create a Lamda Function: Go to the AWS Management console. Search for the service and enter. Lambda After coming onto the lambda service page click on button. Create function Select the default option. Author from Scratch For give any name of your choice[for eg lamda_rekognition]. function name In the select Runtime python 3.6 Expand the Choose or create an execution role. In that select . 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]. Use an existing role Finally, click on button create function 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. 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 ”MaxLables=3" Note: In place of and plz mentioned your S3 bucket name and uploaded image name. buket_name image_name import json import boto3 def lambda_handler(event, context): = boto3.client( ) response = client.detect_labels(Image = { : { : , : }}, =3, =70) (response) return client "rekognition" #passing s3 bucket object file reference "S3Object" "Bucket" "bucket_name" "Name" "image_name" MaxLabels MinConfidence print "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): = boto3.client( ) s3 = boto3.client( ) fileObj = s3.get_object(Bucket = , = ) file_content = fileObj[ ].read() response = client.detect_labels(Image = { : file_content}, =3, =70) (response) return client "rekognition" "s3" # reading file from s3 bucket and passing it as bytes "bucket_name" Key "image_name" "Body" # passing bytes data "Bytes" MaxLabels MinConfidence print "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 button. save To test the function for image analysis. Click on the button given on the upper right side. Once you click on the test button it will pop up one window. Test In that select and in Event Template give any name that you want. Create new test event And finally, click on the button. Create Once you create the test event it will show you the test event name. So now click on the button. Test Once you click on the test the response will look like below: START RequestId: c1284c2- - - - d144a18 Version: $LATEST { : [{ : , : , : [{ : { : . , : . , : . , : . 9632968903}, : }], : []}, { : , : , : [], : []}, { : , : , : [], : []}, { : , : , : [], : [{ : }, { : }]}, { : , : , : [], : [{ : }]}, { : , : , : [{ : { : . , : . , : . 98628, : . }, : }], : [{ : }, { : }]}, { : , : , : [], : []}, { : , : , : [], : []}, { : , : , : [], : [{ : }]}, { : , : , : [], : [{ : }, { : }]}, { : , : , : [], : [{ : }, { : }]}], : , : { : , : , : { : , : , : , : , : }, : }} END RequestId: c1284c2- - - - d144a18 REPORT RequestId: c1284c2- - - - d144a18 Duration: ms Billed Duration: ms Memory Size: MB Max Memory Used: MB Init Duration: ms 3 8611 4888 9030 7942 0 'Labels' 'Name' 'Person' 'Confidence' 99.60957336425781 'Instances' 'BoundingBox' 'Width' 0 15077881515026093 'Height' 0 8669684529304504 'Left' 0 6409724354743958 'Top' 0 0730410 '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 031700540333 '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 3 8611 4888 9030 7942 0 3 8611 4888 9030 7942 0 2403.35 2500 128 71 168.60 What exactly you will get in response: This rekognition 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). API 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 😃