Author avatar

Douglas Starnes

Computer Vision with Amazon Rekognition

Douglas Starnes

  • Jul 21, 2020
  • 7 Min read
  • 56 Views
  • Jul 21, 2020
  • 7 Min read
  • 56 Views
Data
Data Analytics
Machine Learning

Introduction

Is it possible for a computer to see? If a computer analyzes an image, will it be able to interpret it in the same way a human does? In computer vision, we attempt to accomplish exactly that. And it can be done, with remarkable accuracy. But it is still a difficult problem, and the knowledge and required resources are not within the reach of everyone. But as machine learning is more frequently used, how do those without the knowledge and resource needed for computer vision keep up?

AWS Rekognition

Amazon Web Services offers a product called Rekognition (pronounced like "recognition"). The purpose of Rekognition is to analyze images and predict what objects are in the image, if there are any faces, transcribe text, and other tasks. But to use Rekognition you need to know little, if anything, about computer vision or machine learning at all! You simply point Rekognition to a stored image file, tell it what computer vision task you want, pay Amazon a little bit of money (there is a free allowance for 12 months) and get the results back. Sound simple? It is!

The recommended way to access Rekognition and other AWS products is through a client library. Many different AWS client libraries support different languages. I'll be using Python and the boto3 package for this guide. Check the documentation for the other languages that are supported. I won't go into installing boto3 here, but it's not difficult.

Getting Started

Before using Rekognition, you need to have images stored in a location that is accessible by Rekognition, generally in the cloud. For this guide, I'll upload several images to S3.

1
2
3
4
5
6
import boto3

s3 = boto3.resource('s3')

for image in ['faces.jpg', 'objects.jpg', 'text.jpg']:
    s3.Bucket('ps-guide-rekognition').upload_file(image, image)
python

The images are from Unsplash and can be found respectively at

Detecting Objects

To use Rekoginition, first create a client.

1
rek = boto3.client('rekognition', region_name='us-east-1')
python

Notice the use of client instead of resource. Create a dict representing the location of the image in S3.

1
image_dict = {'S3Object':{'Bucket': 'ps-guide-rekognition', 'Name':'objects.jpg'}}
python

Pass the image to the detect_labels method in the Image keyword argument along with the number of labels/objects to detect.

1
labels = rek.detect_labels(Image=image_dict, MaxLabels=10)
python

The resulting dict has a Labels key with the detected labels. Each label has a Name and a Confidence.

1
2
for label in labels['Labels']:
    print('{} - {}'.format(label['Name'], label['Confidence']))
python

And here are the results.

1
2
3
4
5
6
7
8
9
10
Furniture - 99.87967681884766
Table - 99.26332092285156
Wood - 99.20465087890625
Desk - 98.95134735107422
Person - 98.12129974365234
Flooring - 98.12129974365234
Hardwood - 97.50928497314453
Floor - 88.36457061767578
Electronics - 85.74508666992188
Interior Design - 83.3857192993164

And here is the image:

The bounding boxes of the objects detected are also returned in the dict. And Rekognition can also detect objects in video, not just images.

Detecting Faces

To detect a face, call the detect_faces method and pass it a dict to the Image keyword argument similar to detect_labels. The Attributes keyword argument is a list of different features to detect, such as age and gender. For this guide, I'll pass a single value, ALL, to get all of the attributes.

1
2
3
image_dict = {'S3Object': {'Bucket': 'ps-guide-rekognition', 'Name': 'faces.jpg'}}

faces = rek.detect_faces(Image=image_dict, Attributes=['ALL'])
python

This is the image:

The faces are stored in the FaceDetails key. There is only one face in this image. The keys of the face are the attributes.

1
2
3
face = faces['FaceDetails'][0]
for key in face.keys():
    print(key)
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BoundingBox
AgeRange
Smile
Eyeglasses
Sunglasses
Gender
Beard
Mustache
EyesOpen
MouthOpen
Emotions
Landmarks
Pose
Quality
Confidence

In the attributes, we can see that Rekognition predicted the AgeRange as 16-28, the Gender as Female with an almost 100% confidence, and that the subject's eyes were open but her mouth was not. And it predicted the subject's most likely emotion as CALM.

1
f['AgeRange']
python
1
{'Low': 16, 'High': 28}
1
f['Gender']
python
1
{'Value': 'Female', 'Confidence': 99.82478332519531}
1
f['EyesOpen']
python
1
{'Value': True, 'Confidence': 98.96699523925781}
1
f['MouthOpen']
python
1
{'Value': False, 'Confidence': 94.9305191040039}
1
f['Emotions']
python
1
2
3
4
5
6
7
8
[{'Type': 'CALM', 'Confidence': 93.3696060180664},
 {'Type': 'ANGRY', 'Confidence': 2.4889822006225586},
 {'Type': 'HAPPY', 'Confidence': 1.7609130144119263},
 {'Type': 'SAD', 'Confidence': 1.0307590961456299},
 {'Type': 'CONFUSED', 'Confidence': 0.5804279446601868},
 {'Type': 'SURPRISED', 'Confidence': 0.35635992884635925},
 {'Type': 'DISGUSTED', 'Confidence': 0.24075132608413696},
 {'Type': 'FEAR', 'Confidence': 0.17220167815685272}]

And like with object detection, video is also supported.

Text Detection

To detect text, call the detect_text method and just the Image keyword argument.

1
text = rek.detect_text(Image={'S3Object':{'Bucket':'ps-guide-rekognition', 'Name':'text.jpg'}})
python

Here is an image with some text:

The detected text is in the TextDetections key and each detection has a DetectedText key.

1
2
for detection in text['TextDetections']:
    print(detection['DetectedText'])
python
1
2
3
4
5
6
7
8
9
DANGER
HARD HAT
PROTECTION
REQUIRED
DANGER
HARD
HAT
PROTECTION
REQUIRED

It might look as if the text were detected twice. But notice that in the image the words "hard" and "hat" are on the same line. The first four detections are lines of text. The remaining detections are for words.

And as you might have guessed, video is also supported.

Conclusion

AWS Rekognition is a simple, easy, quick, and cost-effective way to detect objects, faces, text and more in both still images and videos. You don't need to know anything about computer or machine learning. All you need to know is how to use the API for the client libraries. This guide used Python. There are other client libraries for popular languages. This frees you from devoting resources to reinventing the wheel that Rekognition has built. Thanks for reading!

3