Ratnopam Chakrabarti is a software developer currently working for Ericsson Inc. He has been focused on IoT, machine-to-machine technologies, connected cars, and smart city domains for quite a while. He loves learning new technologies and putting them to work. When he’s not working, he enjoys spending time with his 3-year-old son.

Ratnopam Chakrabarti

Awesome AWS (Amazon Web Services)

In Parts 1 and Part 2 of the series, I talked mostly about Couchbase. In this part, I’m going to focus on AWS, and more specifically, the AWS Reckognition API.

Create an S3 Bucket

As a prerequisite to run the photogallery app, you need to create an s3 bucket using your Amazon Web Services account. You can use the cloudformation template I mentioned in Part 1 of the series to create it automatically; otherwise you can always create it manually using the AWS Console.

Bucket Policy

Couchbase and S3 both use the concept of “bucket” to store data. I will try to mention proper context while referring to the term “bucket” – as to whether it’s a Couchbase bucket or an S3 bucket. Try not tot get confused between the two, although the underlying objective of a “bucket” is to store data in both Couchbase and S3.

Here, I am referring to an S3 bucket.

In order to allow users to store images in an s3 bucket, we need to create a bucket policy. By default, access to an s3 bucket is private. In order to override the default policy, we create the following policy which allows all users to store objects into s3 and fetch objects from s3.

In my case, the created bucket name is la-image-tagger-chakrar27.

Image Analysis Using Rekognition

In late 2016, AWS launched Rekognition, an image processing service that can analyze an image and infer useful information about the image. For a detailed study, please refer to the AWS Rekognition documentation here.

In the context of the photogallery app, I used the detectLabels() API that AWS Rekognition offers. It returns a key-value pair of labels with confidence scores. The following snippet of code does the trick:

The above code calls AWS Rekognition’s detectLabels API and returns a set of labels based on the image analysis. In this case, I have limited the max number of labels to 5 and the minimum confidence percentage as 50 percent.

Once the labels are returned, it’s parsed using JSON and pushed into tags array. Once this is done, the save() function is called to store the document into the Couchbase bucket.

Rekognition offers some cool APIs like detectFaces() and compareFaces() which open up a whole new spectrum of use cases; however, for now I stuck to the simple detectLabels() API to start with.

Run the App

In order to run the photogallery node app, you need to have the S3 access key and secret keys available with you. You can use the “dotenv” module of node to store these in a file named .env in your source code. If you do this, you must add the .env file to .gitignore:

You don’t want to expose the AWS access keys to the world unless you are in an extremely philanthropic mood. I, however, prefer to pass these values as an environment variable. Here’s how to run the node app using env variables:

This post is part of the Couchbase Community Writing Program

Posted by Laura Czajkowski, Developer Community Manager, Couchbase

Laura Czajkowski is the Snr. Developer Community Manager at Couchbase overseeing the community, our incentive programs, Experts and Champions group, meetups, and defining our presence at developer events. She’s also responsible for our monthly developer newsletter and engaging with our community in various forms. Laura has been active in Open Source communities since 2000 and has been involved in various activities, including leading and organising conferences on software testing, documentation, and advocacy. Laura is an Open Source advocate and regular conference speaker who is passionate about getting people – everyone from primary school students to technology professionals – involved in Open Source communities both on IRC and in face-to-face discussions, she is easily found online at @czajkowski on twitter and on freenode.

Leave a reply