Using AWS Rekognition in CFML: Overview and Creating the Rekognition Service Object
Posted 23 July 2018
CFML is a flexible and powerful language, encapsulating a lot of complexity in some fairly straightforward functions and tags. From creating PDFs to tackling the verbosity of file manipulation in Java, CFML makes it easy to do a lot in a few lines of code.
Current CFML engines aren’t so good at machine learning tasks. Running a machine learning (ML) infrastructure requires expertise in a lot of areas, not to mention the basic task of keeping the servers which power the ML activities running. Fortunately for developers, many companies now offer on-demand machine learning services. AWS offers a whole host of machine learning services, of which Rekognition is one.
What Can Rekognition Do for You?
Rekognition is a machine vision service. When Facebook knows that you’re in a photo that your friend just posted to their timeline, that’s machine vision at work.
Note: AWS actually offers two services called Rekognition. One is (simply) Rekognition, which we use here, and which focuses on the analysis of static images. The other is Rekognition Video, which I will not cover, and which focuses on the analysis of video streams. It’s Rekognition Video that has gotten AWS in hot water with privacy advocates. One of the primary use cases for Rekognition Video is, to be blunt, facial surveillance in security camera feeds.
Rekognition takes an image stored in S3 and performs a number of different analysis tasks on that image. The available analysis tasks are:
- Detect labels: the service tries to determine the primary elements of the image, including objects, locations, activities, and weather.
- Detect faces: the service detects all the faces of individuals in the image.
- Match faces: the service takes two images and compares them to see if they are images of the same person.
- Detect sentiment: the service analyzes faces to see if the people in the picture are happy, sad, smiling, wearing glasses, what their gender is, and a whole host of other facial emotions.
- Unsafe content detection: the service can flag images that it has learned contain “unsafe” content (ie; nudity).
- Celebrity recognition: the service can detect if one of thousands of celebrities it recognizes is in the image.
- Text in images: the service can analyze an image and pull out all the individual words/letter sequences in the image.
Rekognition can be used for simple and fast cataloging of images in a product library, or even your own personal photo library. As you’ll see, it provides many labels for images that go way beyond the identification of a single person or people. Rekognition can be used to match the face of an employee standing at a security gate with an image on file. A student waiting to take a test in an online class could have the face on their webcam matched with their photo ID in a university information system, and can only proceed with the exam once that match is made. You can quickly scan photos to make sure that users aren’t uploading photos to your application that contain nudity. You can even build a system that reads the text in an image and translates it to other languages (using AWS Translate, a service that I will cover later in the series).
As a developer, I found Rekognition to be a lot of fun to play with. It’s also dead simple to use from within CFML applications, because of the power of the AWS Java SDK.
Working with Rekognition from CFML
I’ll once again use my AWSPlaybox application for all the example code.
If you haven’t already read the entry on the basic setup needed to access AWS from CFML, please do so now.
As with all AWS services, you need to first create a client for the service that you want to use. This was covered in the basic setup needed to access AWS from CFML post, but here are the steps involved:
- Create a Client Builder object for the service — AmazonRekognitionClientBuilder.
- Tell the Client Builder what kind of builder object you want to use. It’s simplest to use the standard builder.
- Pass in your credentials via the StaticCredentialsProvider object (created upon instantiation of awsPlaybox/model/awsServiceFactory.cfc).
- Tell the Client Builder which AWS region you’re working in. (I generally use ‘us-east-1’.)
- Tell the Client Builder to build (make) the connection to the service.
Here’s the relevant code from AWSPlaybox/model/awsServiceFactory.cfc:
Now we can work with Rekognition from within our CFML application.
In the rest of this series of posts on working with Rekognition from CFML, I’ll cover detecting and processing image labels, performing face sentiment analysis, comparing faces to one another to look for matches, and detecting text in an image.