Guided Lab: Automating Image Analysis with Amazon Rekognition
Description
Amazon Rekognition is a powerful service provided by AWS that enables developers to integrate image and video analysis into their applications easily. This service uses deep learning models to identify objects, people, text, scenes, and activities in images and videos and can also detect inappropriate content.
In this Guided Lab, we will automate image analysis and label detection using Amazon Rekognition API and AWS Lambda Function when we upload images in an S3 Bucket.
Prerequisites
This lab assumes you have a basic understanding of Amazon S3 and AWS Lambda Services.
If you find any gaps in your knowledge, consider taking the following lab:
- Creating an Amazon S3 bucket
- Creating an AWS Lambda function
- Automated File Processing with S3 Event Notifications and Lambda function
Objectives
In this lab, you will:
- Explore Amazon Rekognition Label Detection feature.
- Set up an S3 bucket with designated folders for image uploads and analysis outputs.
- Create and deploy a Lambda function to automate the image analysis process.
- Verify the output
Lab Steps
Explore Amazon Rekognition Label Detection
1. Navigate to the Rekognition service
2. Click on “Try demo.”
3.Upload an image
-
If you do not have any available image. Add a new tab in your browser and google the word “boat” and download the first image you see it in your computer. Take note the file extension of the image you downloaded
- Navigate back to the Amazon Rekognition Label Detection tab
- .Click Upload or drag and drop to upload the boat image
4. Analyze the results:
- Once the image is uploaded, Amazon Rekognition will display labels that describe the contents of the image. You will see details such as detected objects, scenes, and activities, along with confidence scores.
5. Explore and expand the Request dropdown and Response dropdown and review the JSON details
Request: This section shows the JSON request sent to the Rekognition service. It typically includes the image details such as the base64-encoded image byte.
Response: This section displays the JSON response received from Rekognition. It includes the detected labels, confidence scores, and other metadata. Each label represents an object, scene, or activity detected in the image, and the confidence score indicates the likelihood that the label is accurate.
Now, that you explored the Amazon Rekognition Dashboard, lets automate the image and video analysis using AWS Lambda and S3.
Create an S3 Bucket and Folders
1. Navigate to the S3 service
2. Create a new bucket with the following configurations:
- Enter a unique name for your bucket (e.g.,
rekognition-lab-bucket
). - Leave the default settings for the rest of the options and click Create bucket.
3. Create folders within the bucket:
- name the folders:
- image-uploaded
- rekognition-output
Create a Lambda Function
1. Navigate to the Lambda service
2. Create a new function with the following configuration
- Function name:
rekognitionLambdaFunction
Runtime:
Python 3.8
or higher- Execution role: Use an Existing role PlayCloud-Sandbox
- Click Create function.
3. Add S3 trigger:
- Navigate back to the s3 bucket you created in the previous step.
- Navigate to the properties tab and create an Event notifications with the following configuration:
- Event name:
upload-image-s3-event
- Prefix – optional:
image-uploaded/
- Suffix – optional:
.jpg
- Object creation: check the Put
- Destination
- Destination: Select Lambda Function
- Specify Lambda function: Choose from your Lambda functions
- rekognitionLambdaFunction
- Event name:
4. Configure the Lambda function:
- Navigate back to your Lambda Function.
- Replace the default code with the following Python script:
import json
import boto3
s3_client = boto3.client('s3')
rekognition_client = boto3.client('rekognition')
def lambda_handler(event, context):
# Get the bucket name and object key from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# Call Rekognition to detect labels
response = rekognition_client.detect_labels(
Image={
'S3Object': {
'Bucket': bucket,
'Name': key
}
},
MaxLabels=10
)
# Save the response to the output folder
output_key = 'rekognition-output/' + key.split('/')[-1] + '.json'
s3_client.put_object(
Bucket=bucket,
Key=output_key,
Body=json.dumps(response, indent=4)
)
return {
'statusCode': 200,
'body': json.dumps('Image processed and labels detected successfully')
}
- Take your time to review the code
- After reviewing, Click on Deploy.
Testing
1. Upload an image to the image-uploaded folder:
- Navigate to the S3 console
- Select your bucket and the image-uploaded folder:
- Upload the boat image file you downloaded earlier.
2. Verify the output:
- After the image is uploaded, navigate to the
rekognition-output
folder in your S3 bucket. - You should see a new JSON file containing the Rekognition labels for the uploaded image.
- Download and check the JSON file to confirm it matches the Rekognition response.
That’s it! Congratulations! In this lab, you have successfully set up an S3 bucket with two folders, configured an AWS Lambda function to automatically analyze images using Amazon Rekognition. You also learned how to store the analysis results in a different folder within the same S3 bucket. Now, you can use Amazon Rekognition for various real-world scenarios, such as automating content moderation for user-uploaded images, streamlining image cataloging processes for media libraries, or enhancing security by analyzing and categorizing images from surveillance systems.
Happy Learning!