Site icon The Last Dev Talk about Technologies

Amazon Rekognition – Serverless image tagging

tennis ball recognition header
Image from Kevin Mueller, Unsplash. https://unsplash.com/@kevinmueller

In a previous post, we took a closer look at Amazon Rekognition on Amazon Console; if you missed it, I strongly recommend going back and read it since we will follow pretty much the same way to tag images programmatically. In this post, we see how to create a serverless app and automatically tag newly uploaded images. We use the following AWS services.

To create and deploy the app, we use the Serverless Application Model (SAM). We first create a Lambda that will extract the labels from an image, and then we make a trigger where it takes the pictures and uses Rekognition to generate the tags. Lastly, we save the results on DynamoDB, and we keep the name of the image, the label, and the confidence, the last one optional.

Recognize Images using Lambdas

In the picture above, the architecture of the app is depicted; based on that schema, we create our serverless app using the Serverless Application Model. You can find the code of the project here.

Without further ado, let’s begin.

Creating SAM template

First of all, we create a Cloudformation stack using SAM to deploy our app; that way, we will have full control of the spawned resources. First of all, let’s take a quick look at the SAM template.

See the gist on github.

I know that looks confusing, but once we explain everything, you will see it is straightforward. We go line by line to fully understand the template.

Creating the Lambda

Now that we have created the SAM template, we will need to provide the code for the Lambda function. Remember the lines 43-44, where we defined the code of the project. Create a folder src and inside, create a file named recognize_image.py this will be our lambda handler. Inside this file, paste the following code.

See the gist on github.

This function gets the parameters from the trigger (line 13-14) and calls Amazon Rekognition to detect the labels. I have forced the parameters (line 24-25) for the maximum number of labels and the confidence threshold, but you can parameterize those values any way you want. Once I have the labels, I insert them to our newly created DynamoDB table. Notice that I pass the name of the table as an environment variable.

Well, that’s pretty much it! We have created our app. Now we have to deploy it and test it.

Deploying the stack

Finally, to deploy the stack, you will need to run a few commands. I usually create a deploy.sh file with the following commands. You will only need a bucket to save the template.yaml, and you need read-write access to this bucket (Line 2, –s3-bucket argument).

See the gist on github.

See the gist on github.

The stack is now deployed, and you will see at the end the ARNs and the bucket name of our app, we use the bucket name to upload the files in the next section, so grab it for now.

Key                 ImageBucket
Description         The bucket to save the images
Value               image-bucket-**********   <--- This

Uploading files

As a next step, all we have to do is to upload images into the bucket. We will use the script from the previous tutorial to upload files to our bucket.

See the gist on github.

Once we upload an image using the above script, we can see that in DynamoDB table, we have multiple entries for the image, one for each label accompanied by the confidence of the result. Now we can execute queries on DynamoDB and filter images by their labels.

Image Labeling table on DynamoDB using the header image (limit on labels 6)

Deleting everything

To delete everything you can either go to your AWS Console, select CloudFormation service and delete the stack manually or simply run the following command.

aws cloudformation delete-stack --stack-name imageTagging --profile <your profile>

Again you are not obligated to use the profile argument if you are using your default profile. We defined the stack-name when we deployed our stack.

Conclusion

Well, that is it for today. As you can see, it is effortless to create a serverless auto-tagging app for your system. Let me know if you have any questions and/or suggestions in the comment section below or at my Twitter @siaterliskonsta. See you at the next one!