Contact Us
Thought Leadership

Deploy an API Endpoint to Resolve Image Recognition Requests

By Martin Flemate, Cloud Engineer at IO connect Services
November 26, 2020

Introduction

This article describes how to create the infrastructure step by step to implement an API Endpoint that resolves image recognition requests by integrating AWS services such as: API Gateway, Lambda Function and Amazon Rekognition.

Objective

Create an API endpoint service that resolves Image recognition requests using Amazon Web Services (AWS).

Pre-Requisite

  • You will need to get an account at AWS.
  • Basic knowledge on API and AWS services.

Overview

Below is the solution diagram, which we will be referencing throughout the article:

The image that we will be using in the “Test API” section is the following:

The image must be sent encode in base64; the body structure of the POST request must be application/json such show as follows:

{
    "fileName": "Name of the image",
    "imageFormat": "base64 encoded image"
}

The image that we will be using in the “Test API” section is the following:

The Lambda code is below:

var aws = require('aws-sdk');
var rekognition = new aws.Rekognition();

exports.handler = (event, context, callback) => {
    // Buffer created to receive the encode base64 image
    var buffer = new Buffer.from(event.imageFormat, 'base64');
    var params = {
        Image :{
            Bytes: buffer
        },
        MaxLabels: 20,
        MinConfidence: 75
    }; 
   //send params to Amazon rekognition
    rekognition.detectLabels(params, function(error, data){
       //if there is an error, return the description about it
        if(error){
            var ErrorOcurred = {
              "fileName": event.fileName,
              "Error":
              {
                "Error": error.stack,
                "Description": error
              }
            };
            callback(null, ErrorOcurred);
        }
        //if everything is ok, send the labels of the image
        else{
            var res = {
                "fileName": event.fileName,
                "Tags": data
            };
            callback(null, res);
        }
    });
};

Steps to create the Infrastructure

1. Create IAM Role to the Lambda function

Go to AWS console and navigate to IAM service, from the left panel select the “Roles” option and create a new ROLE for the Lambda service, which will interact with Amazon Rekognition.

Make sure the following policies are included in the ROLE:

  • AWSLambdaFullAccess
  • AmazonRekognitionFullAccess

2. Create Lambda Function

Go to AWS console and navigate to Lambda service and select “Create function”. On the next screen the “Author from scratch” must be selected, then enter a Lambda name and choose “Node.js 10.x” version as runtime.

Next, dropdown the “Choose or create an execution role” options, select “Use an existing role” and choose the role created in the first step.

a

Once the Lambda is created replace the code on index.js file with the one in the Overview section and save the changes.

3. Create API Gateway

a. Create REST API

Enter the AWS console and go to the API Gateway service, tap on Create API and choose “API REST” option to create one. Then, add a name to identify the new API and finish with the creation.

b. Create a new resource and method

First, we will create a resource called “/rekognition” within the rekognition-API created in the previous step, then we will create a POST method for the resource that it be configurated to use a Lambda function.

• Go to the API recently created and ensure the “Resources” option is selected in the left panel, then click on “Actions” and select Create Resource. Enter a name and enable API Gateway CORS.

• Select the /rekognition resource recently created, click on “Actions” and choose “Create Method”, a new dropdown should appear under the resource, select POST click the checkmark.

• Select Lambda Function for the integration type, choose the Region where the Lambda was created in the step 2 and enter the name of the Lambda function, after that, save changes and give Amazon API Gateway permission to invoke the function.

c. Deploy API

Tap on “Actions” option and select “Deploy API”, then select [New Stage] in the Deployment stage and enter prod for the “Stage name”.

Select “Deploy” button, after that, the invoke URL should be displayed. These URL will we use to test the API in the next section.

Test API

The URL to test the API is the following:

POSTMAN is the application that we will use to test the API. In the application copy the invoke URL from the rekogniton-API and paste into the POST method. Then, enter the json params and send the POST.

NOTE: Remember that the image must be encoded in base64

Now we can validate the API with the following “Response”displayed:

{
    "fileName": "image",
    "Tags": {
        "Labels": [
            {
                "Name": "Human",
                "Confidence": 99.88124084472656,
                "Instances": [],
                "Parents": []
            },
            {
                "Name": "Person",
                "Confidence": 99.88124084472656,
                "Instances": [],
                "Parents": []
            },
            {
                "Name": "Apparel",
                "Confidence": 99.66313171386719,
                "Instances": [],
                "Parents": []
            },
            {
                "Name": "Footwear",
                "Confidence": 99.66313171386719,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Clothing"
                    }
                ]
            },
            {
                "Name": "Shoe",
                "Confidence": 99.66313171386719,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Clothing"
                    },
                    {
                        "Name": "Footwear"
                    }
                ]
            },
            {
                "Name": "Clothing",
                "Confidence": 99.66313171386719,
                "Instances": [],
                "Parents": []
            },
            {
                "Name": "Airport",
                "Confidence": 99.64028930664062,
                "Instances": [],
                "Parents": []
            },
            {
                "Name": "Pants",
                "Confidence": 91.3695068359375,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Clothing"
                    }
                ]
            },
            {
                "Name": "Airport Terminal",
                "Confidence": 84.31376647949219,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Terminal"
                    },
                    {
                        "Name": "Airport"
                    }
                ]
            },
            {
                "Name": "Terminal",
                "Confidence": 84.31376647949219,
                "Instances": [],
                "Parents": []
            },
            {
                "Name": "Denim",
                "Confidence": 79.09687042236328,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Clothing"
                    },
                    {
                        "Name": "Pants"
                    }
                ]
            },
            {
                "Name": "Jeans",
                "Confidence": 79.09687042236328,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Clothing"
                    },
                    {
                        "Name": "Pants"
                    }
                ]
            }
        ],
        "LabelModelVersion": "2.0"
    }
}

Conclusion

In this article we show the ease to create an API endpoint using AWS services, as well as the great advantages that we have in this type of infrastructure which are integrated with serverless services such as Lambdas. In addition, we remove the dependence on servers that are on all the time and instead we are able to pay only for what we use. This gives us the advantage to grow vertically, integrating the services that are necessary to meet the business objectives.

 

Related Insights

We are results-driven with a focus on providing customer service excellence

IO Connect Services is here to help you by offering high-quality cloud technology solutions.
Connect with us
®2024 IO Connect Services
Privacy PolicyCookie Policy
magnifiercrossmenuchevron-down
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram