Lambda Function module

This script serves as an AWS Lambda function for performing object detection on images using an ONNX model stored in an S3 bucket. The function is designed to handle image input in base64 format, download the model if it’s not already available, and make predictions on bounding boxes for detected objects.

  • Load AWS information from .env file.

  • Decode the base64-encoded image input from a HTTP request and match resize the image to the correct input.

  • Download the ONNX model from an S3 bucket and save it into /tmp.

  • Run inference.

  • Calculate Intersection Over Union (IoU) to remove overlapping boxes.

  • Return the prediction results.

Logs are saved in CloudWatch and can be viewed using the script logs/see_logs_lambda.py

lambda_function.intersectionOverUnion(bb1, bb2)

Calculate the Intersection Over Union (IoU) between two bounding boxes.

\[\text{IoU} = \frac{\text{Intersection Area}}{\text{Union Area}}\]

Parameters:

bb1list

Coordinates of the first bounding box in the format [x1, y1, x2, y2].

bb2list

Coordinates of the second bounding box in the format [x1, y1, x2, y2].

Returns:

float

IoU value

lambda_function.make_prediction(event, context)

Handle the prediction process for an image input using the model inside S3 bucket.

Parameters:

eventdict

Event data from AWS Lambda containing the encoded image.

Returns:

dict

The result of the prediction, containing detected bounding boxes or an error message. Inside “result” there is a list of dicts with the following keys:

  • Class (str): Name of the class

  • Confidence (float): Percentage of confidence for the bounding box predicted

  • Point 1 (int, int): Top left corner of the bounding box

  • Point 2 (int, int): Bottom right corner of the bounding box