Deploy a Serverless Image Object Detection API with SST Ion, AWS Lambda, and Amazon Rekognition

In this post, I’ll walk you through how to deploy computer vision APIs using SST Ion, AWS Lambda, and Amazon Rekognition. This API will enable you to perform object detection on images without the need for a dedicated server.

While I won't dive too deeply into computer vision theory, it's worth noting that Amazon Rekognition offers a range of pre-trained and customizable computer vision models to extract insights from your images and videos. These capabilities are useful for applications such as content moderation, visual search, and more. For more specialized models, you can train your own using Amazon SageMaker to train your own models.

Prerequisites

You will need an AWS account before getting started. You will also need to configure the AWS CLI and your AWS credentials to follow along.

Disclaimer

This example deploys a bare bones public API. For production environments, consider securing your API with IAM principals and Cognito User Pools for web and mobile clients, or AWS SigV4 signed requests for server clients. You should also consider limiting the size of the images you send to Amazon Rekognition.

Make sure to review the Amazon Rekognition pricing before deploying this example to production.

Object Detection Demo

Text Detection Demo

Step 1: Set Up Your Project

Start by initializing a new directory and setting up the project using Yarn and SST Ion.

% mkdir serverless-object-detection && cd serverless-object-detection && yarn init -y

yarn init v1.22.19
warning The yes flag has been set. This will automatically answer yes to all questions, which may have security implications.
success Saved package.json
✨  Done in 0.03s.

Now, initialize SST:

% npx sst@latest init

   ███████╗███████╗████████╗
   ██╔════╝██╔════╝╚══██╔══╝
   ███████╗███████╗   ██║
   ╚════██║╚════██║   ██║
   ███████║███████║   ██║
   ╚══════╝╚══════╝   ╚═╝

>  JS project detected. This will...
   - use the JS template
   - create an sst.config.ts

✓  Template: js

✓  Using: aws

✓  Done 🎉

Step 2: Configure Your SST Project

Add the following code to your SST configuration file to set up the API Gateway. Note that this function requires the rekognition:DetectLabels permission to use Amazon Rekognition:

/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: "serverless-object-detection",
      removal: "remove",
      home: "aws",
    };
  },
  async run() {
    const api = new sst.aws.ApiGatewayV2("ComputerVisionApi");
    api.route("POST /objectDetection", {
      handler: "index.objectDetection",
      permissions: [{actions:["rekognition:DetectLabels"], resources:["*"]}]
    });
    api.route("POST /ocr", {
      handler: "index.ocr",
      permissions: [{actions:["rekognition:DetectText"], resources:["*"]}]
    });
  },
});

Step 3: Install Dependencies

yarn add -D @aws-sdk/client-rekognition

Step 4: Add the Object Detection Handler Function

Add an index.ts file to the root directory with the code below. This example uses Amazon Rekognition to perform object and text detection on images.

import {
  RekognitionClient,
  DetectTextCommand,
  DetectLabelsCommand,
} from "@aws-sdk/client-rekognition";

const rekognitionClient = new RekognitionClient();


export async function objectDetection(event) {
  const { imageBase64 } = event; // Expect base64-encoded image in the event
  const buffer = Buffer.from(imageBase64, 'base64');

  const params = {
    Image: { Bytes: buffer },
    MaxLabels: 10,
    MinConfidence: 70,
  };

  try {
    const command = new DetectLabelsCommand(params);
    const data = await rekognitionClient.send(command);
    return {
      statusCode: 200,
      body: JSON.stringify({
        data
      }),
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'Error detecting labels', error }),
    };
  }
};

export async function ocr(event) {
  const { imageBase64 } = event; // Expect base64-encoded image in the event
  const buffer = Buffer.from(imageBase64, 'base64');

  const params = {
    Image: { Bytes: buffer },
  };

  try {
    const command = new DetectTextCommand(params);
    const data = await rekognitionClient.send(command);
    return {
      statusCode: 200,
      body: JSON.stringify({
        data
      }),
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'Error detecting text', error }),
    };
  }
};

Step 5: Deploy Your API

Run the following command to deploy your API:

% npx sst deploy --stage production

SST 3.0.118  ready!

➜  App:        serverless-object-detection
   Stage:      production

~  Deploy

✓  Complete
   ComputerVisionApi: https://9gc2wjo7ld.execute-api.us-west-1.amazonaws.com

Step 6: Test Your API

You can now test your API by sending a POST request to the endpoint provided in the output.

% curl 'https://9gc2wjo7ld.execute-api.us-west-1.amazonaws.com/objectDetection' \
  --data-raw '{"imageBase64":"<your-base64-encoded-image>"}''

{
  "Labels": [
    {
      "Name": "Citrus Fruit",
      "Confidence": 99.99747467041016,
      "Instances": [],
      "Parents": [
        {
          "Name": "Food"
        },
        {
          "Name": "Fruit"
        },
        {
          "Name": "Plant"
        },
        {
          "Name": "Produce"
        }
      ],
      "Aliases": [],
      "Categories": [
        {
          "Name": "Food and Beverage"
        }
      ]
    },


    ...
  ],
  "LabelModelVersion": "3.0"
}

And that's it! You've successfully deployed a serverless Object Detection API using SST Ion, AWS Lambda, and Amazon Rekognition! 🚀🚀🚀