Creating an Object Detection REST API Using Lambda, Rekognition & API Gateway
Introduction
In the rapidly evolving landscape of artificial intelligence, object detection stands out as a critical technology, powering applications ranging from security systems to retail analytics. Amazon Web Services (AWS) provides robust tools to implement object detection efficiently. This blog post will guide you through creating a REST API for object detection using AWS Lambda, Rekognition, and API Gateway. By the end of this tutorial, you’ll have a fully functional API that can process images and detect objects with high accuracy.
Overview of Architecture
Our project leverages several AWS services to create a seamless object detection API:
- AWS Lambda: Serverless compute service to run our code.
- Amazon Rekognition: Image analysis service to detect objects within images.
- API Gateway: Service to create, publish, maintain, monitor, and secure APIs.
Here’s how these services interact:
- API Gateway receives image data via an HTTP request.
- API Gateway triggers an AWS Lambda function, passing the image data.
- AWS Lambda processes the image and sends it to Amazon Rekognition.
- Amazon Rekognition analyzes the image and returns the detected objects.
- AWS Lambda formats the response and sends it back through API Gateway to the client.
FOR REFERENCE :
we can see that the user will send the data
in step 2 our api will take the imagethis request will be handled by the API Gateway in step 3
all the resources contained in the rectangle is provided by aws
the api will be on the internet, that is, outside AWS.
in step 3 we get the image and trigger the lambda function
in the 4th step we will process the image and make a call to the recognition api
then the response which is generated is obtained by aws lambda then it is returned to the api gateway and the api will return the response to the user.
api gateway is connected with lambda for processing in aws lambda, in the backend, it uses rekognition.
Implementation
1. Create a Lambda Function
First, navigate to the AWS Lambda console and create a new Lambda function. Name it something meaningful, like ObjectDetectionFunction
, and select Python 3.10 as the runtime. Create the function with its default settings.
2. Modify the Python Code
Replace the default code in the Lambda function with the following:
import json
import boto3
import logging
import base64
from io import BytesIO
from PIL import Image
# Initialize AWS Rekognition client
rekognition = boto3.client('rekognition')
# Set the Logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
# Get the image from the API Gateway event
logger.info("Get the Event")
try:
# Assuming the image data is base64-encoded in the request body
logger.info(f"Got the Event: {event}")
# Decode the Image Binary
decoded_data = base64.b64decode(event['body'])
# Process the Image
image = Image.open(BytesIO(decoded_data))
stream = BytesIO()
image.save(stream, format="JPEG")
image_binary = stream.getvalue()
# Perform object detection
logger.info("Detecting the Labels....")
response = rekognition.detect_labels(
Image={
'Bytes': image_binary
},
MaxLabels=15,
MinConfidence=70
)
# Extract only labels and confidence
labels_info = [
{
'Label': label_info['Name'],
'Confidence': label_info['Confidence']
}
for label_info in response['Labels']
]
return {
'statusCode': 200,
'body': json.dumps(labels_info)
}
except Exception as e:
logger.info(e)
return {
'statusCode': 500,
'body': json.dumps(f"Failed because of: {e}")
}
Explanation:
- Initialization: Import necessary libraries and Initialize the Rekognition client.
- Logging: Set up logging for debugging and monitoring.
- Event Handling: Extract and decode the image data from the event.
- Image Processing: Convert the image for Rekognition to process.
- Object Detection: Call Rekognition’s
detect_labels
function to detect objects. - Response Handling: Format and return the detected labels and their confidence scores.
3. Deploy and Test the Code
Deploy the Lambda function and test it by invoking the function with sample event data. If you encounter a blank response, create a Lambda layer to include the missing dependencies, such as the PIL
library.
Steps to create and add a layer:
- Navigate to Lambda Layers.
- Create a new layer and specify the ARN.
- Add the ARN to your function.
Deploy and invoke the function again to verify it works correctly.
4. Adjust Lambda Settings
In the Lambda function configuration, edit the basic settings to increase the memory size to 256 MB and the timeout to 1 minute and 3 seconds. Save the changes.
5. Update Lambda Permissions
In the Lambda function permissions tab, add AmazonRekognitionFullAccess
to allow the function to call Rekognition.
6. Set Up API Gateway
- Navigate to API Gateway and create a new API.
- Create a resource named
detection
. - Under
detection
, create a POST method and enable Lambda proxy integration. Select your Lambda function.
7. Test the API
Test the API method by sending a POST request with sample data to ensure the Lambda function is triggered.
8. Deploy the API
Deploy the API to a new stage named dev
. Copy the invoke URL for testing.
9. Resolve Binary Media Type Issue
If you encounter errors, add images/*
as a binary media type in API Gateway settings. Redeploy the API.
10. Test with Postman
Use Postman to send a request with an image in the body. Ensure the response contains the detected objects.
Conclusion
By following these steps, you have successfully created an object detection REST API using AWS Lambda, Rekognition, and API Gateway. This setup can be a foundation for more complex applications, demonstrating the power and flexibility of AWS serverless architecture.