Site iconAxway Blog

AWS Rekognition image analysis plugin for API Builder

This blog post describes an image analysis plugin for API Builder based on AWS (Amazon Web Services) Rekognition. The plugin is intended to help create API Builder-based APIs and integrations that include image analysis. For example, you can create an API that detects Personal Protective Equipment (PPE) worn by employees in an image. You can use this information to improve workplace safety practices.

In this blog post we’ll describe how to install and leverage the plugin in an API.

You can find and read about the plugin here.

The current methods exposed are:

The plugin requires that images be passed as Base64 encoded text.

Prerequisites

In order to use the plugins you will need an AWS access key ID and secret access key, which are used to sign programmatic requests that you make to AWS (through the plugin). You will use these to set the following two environment variables as per the AWS SDK for JavaScript online docs:

Refer to the AWS online docs for instructions on how to get an access key id and secret access key.

Install plugin

If you don’t already have an API project created, then create a new API Builder project using the API Builder Getting Started guide here. For example:

axway builder init rekplugintest

Install the Rekognition plugin by executing the following command in your API Builder project folder:

npm install api-builder-plugin-rekognition

One way to set environment variables for an API Builder project is to edit the /conf/.env environment file and add the following two environment variables:

For example:

PORT=8080
LOG_LEVEL=debug
AWS_ACCESS_KEY_ID=AK........UA
AWS_SECRET_ACCESS_KEY=nie......oKE

At this point the plugin is installed and ready to use in the flow editor in an integration or an API and you will find the plugin in the AI section of the Flow-Nodes palette.

You should also add the following to the /conf/default.js file in order to overcome the 1MB default limit on POST data:

bodyParser: {
  limit: '5mb'
},

Note that 5mb is just an example. You can set it to the value that meets your needs based on the image size

Test plugin

For convenience’s sake, we’ll modify the built-in Greeting API to test the plugin since the Greeting API takes a username parameter which we can used to pass a Base64 string into the plugin as the encoded image to be analyzed.

Start your API Builder project using npm start.

Start the console and open the Greeting API flow. You will see the Rekognition plugin in the AI section of the Flow-Nodes palette:

Now, let’s do the following:

{
  "status": 200,
  "body": {
    "Labels": [{
        "Name": "Car",
        "Confidence": 98.3051528930664,
        "Instances": [{
          "BoundingBox": {
            "Width": 0.9070876240730286,
            "Height": 0.4264858365058899,
            "Left": 0.04061555862426758,
            "Top": 0.3711528182029724
          },
          "Confidence": 98.3051528930664
        }],
        "Parents": [{
            "Name": "Vehicle"
          },
          {
            "Name": "Transportation"
          }
        ]
      },
      {
        "Name": "Automobile",
        "Confidence": 98.3051528930664,
        "Instances": [],
        "Parents": [{
            "Name": "Vehicle"
          },
          {
            "Name": "Transportation"
          }
        ]
      },
      {
        "Name": "Transportation",
        "Confidence": 98.3051528930664,
        "Instances": [],
        "Parents": []
      },
      {
        "Name": "Vehicle",
        "Confidence": 98.3051528930664,
        "Instances": [],
        "Parents": [{
          "Name": "Transportation"
        }]
      },
      {
        "Name": "Wheel",
        "Confidence": 96.4452133178711,
        "Instances": [],
        "Parents": [{
          "Name": "Machine"
        }]
      },
      {
        "Name": "Machine",
        "Confidence": 96.4452133178711,
        "Instances": [],
        "Parents": []
      },
      {
        "Name": "Tire",
        "Confidence": 95.14317321777344,
        "Instances": [],
        "Parents": []
      },
      {
        "Name": "Car Wheel",
        "Confidence": 89.39521026611328,
        "Instances": [],
        "Parents": [{
            "Name": "Tire"
          },
          {
            "Name": "Wheel"
          },
          {
            "Name": "Machine"
          }
        ]
      },
      {
        "Name": "Spoke",
        "Confidence": 84.72041320800781,
        "Instances": [],
        "Parents": [{
          "Name": "Machine"
        }]
      },
      {
        "Name": "Coupe",
        "Confidence": 80.97046661376953,
        "Instances": [],
        "Parents": [{
            "Name": "Sports Car"
          },
          {
            "Name": "Car"
          },
          {
            "Name": "Vehicle"
          },
          {
            "Name": "Transportation"
          }
        ]
      },
      {
        "Name": "Sports Car",
        "Confidence": 80.97046661376953,
        "Instances": [],
        "Parents": [{
            "Name": "Car"
          },
          {
            "Name": "Vehicle"
          },
          {
            "Name": "Transportation"
          }
        ]
      },
      {
        "Name": "Alloy Wheel",
        "Confidence": 71.59758758544922,
        "Instances": [],
        "Parents": [{
            "Name": "Spoke"
          },
          {
            "Name": "Wheel"
          },
          {
            "Name": "Machine"
          }
        ]
      },
      {
        "Name": "Offroad",
        "Confidence": 70.25897216796875,
        "Instances": [],
        "Parents": [{
            "Name": "Vehicle"
          },
          {
            "Name": "Transportation"
          }
        ]
      },
      {
        "Name": "Sedan",
        "Confidence": 62.23975372314453,
        "Instances": [],
        "Parents": [{
            "Name": "Car"
          },
          {
            "Name": "Vehicle"
          },
          {
            "Name": "Transportation"
          }
        ]
      }
    ],
    "LabelModelVersion": "2.0"
  }
}

Now that you see how easy it is to use the plugin in the Greeting API, you can now integrate Image Analysis in your own API Builder APIs and Integrations.

Summary

AWS Rekognition is a powerful Image Analysis service. The API Builder Plugin described here can help developers leverage this service easily in API Builder APIs and integrations.

Need help with API Builder? Extend your skills with Axway University.

Exit mobile version