AWS Rekognition with AWS CLI, Python

Paul Zhao
Paul Zhao Projects
Published in
8 min readOct 13, 2020
AWS Rekognition

What is AWS Rekognition?

Amazon Rekognition makes it easy to add image and video analysis to your applications using proven, highly scalable, deep learning technology that requires no machine learning expertise to use. With Amazon Rekognition, you can identify objects, people, text, scenes, and activities in images and videos, as well as detect any inappropriate content. Amazon Rekognition also provides highly accurate facial analysis and facial search capabilities that you can use to detect, analyze, and compare faces for a wide variety of user verification, people counting, and public safety use cases.

With Amazon Rekognition Custom Labels, you can identify the objects and scenes in images that are specific to your business needs. For example, you can build a model to classify specific machine parts on your assembly line or to detect unhealthy plants. Amazon Rekognition Custom Labels takes care of the heavy lifting of model development for you, so no machine learning experience is required. You simply need to supply images of objects or scenes you want to identify, and the service handles the rest.

Prerequisites:

1 Install aws cli version 2 on Mac

To install and update for all users using the macOS command line

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload Upload Total Spent Left Speed
100 21.3M 100 21.3M 0 0 10.0M 0 0:00:02 0:00:02 --:--:-- 10.0M
$ sudo installer -pkg AWSCLIV2.pkg -target /
Password:
installer: Package name is AWS Command Line Interface
installer: Upgrading at base path /
installer: The upgrade was successful.

Verify installation

$ which aws
/usr/local/bin/aws
$ aws --version
aws-cli/2.0.56 Python/3.7.4 Darwin/19.6.0 exe/x86_64

Set up aws configure

$ aws congfigure
AWS Access Key ID [****************DOWD]:
AWS Secret Access Key [****************f3ul]:
Default region name [us-east-1]:
Default output format [json]:

For other platforms, please visit here.

2 Install Python, Pip, Boto3

Homebrew installed for Mac installation (Chocolatey if using Windows)

Installing Homebrew

Open terminal and type in

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
.
.
.
==> Installation successful!==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics
No analytics data has been sent yet (or will be during this `install` run).==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://github.com/Homebrew/brew#donations==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh

Verify Homebrew installation

$ brew --version
Homebrew 2.4.16
Homebrew/homebrew-core (git revision 23bea; last commit 2020-09-04)
Homebrew/homebrew-cask (git revision 5beb1; last commit 2020-09-05)

Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

2. Python (the version must be 3.7 to make sure that all functions will be available to present intended outcome) is required to be installed

Installing Python3

Now, we can install Python 3:

$ brew install python
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> Updated Casks
sessionWarning: python@3.8 3.8.5 is already installed and up-to-date
### my python was preinstalled, you may see different installation process. And it may take a while before python is fully installed

Verify is your python3 is installed

$ python3 --version
Python 3.8.5

Notes: you may set your default python as latest version by applying following code

$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.8 /usr/local/bin/python

Installing Pip

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1841k 100 1841k 0 0 2989k 0 --:--:-- --:--:-- --:--:-- 2984k
$ python get-pip.py
/usr/local/lib/python3.8/site-packages/setuptools/distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
warnings.warn(
Collecting pip
Using cached pip-20.2.3-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.2.3
Uninstalling pip-20.2.3:
Successfully uninstalled pip-20.2.3
Successfully installed pip-20.2.3

Verify installation

$ pip --version
pip 20.2.3 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

Installing Boto3

$ python -m pip install boto3
Requirement already satisfied: boto3 in /usr/local/lib/python3.8/site-packages (1.15.14)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /usr/local/lib/python3.8/site-packages (from boto3) (0.10.0)
Requirement already satisfied: s3transfer<0.4.0,>=0.3.0 in /usr/local/lib/python3.8/site-packages (from boto3) (0.3.3)
Requirement already satisfied: botocore<1.19.0,>=1.18.14 in /usr/local/lib/python3.8/site-packages (from boto3) (1.18.14)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.8/site-packages (from botocore<1.19.0,>=1.18.14->boto3) (2.8.1)
Requirement already satisfied: urllib3<1.26,>=1.20; python_version != "3.4" in /usr/local/lib/python3.8/site-packages (from botocore<1.19.0,>=1.18.14->boto3) (1.25.10)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.19.0,>=1.18.14->boto3) (1.15.0)

Verify installation

$ pip show boto3
Name: boto3
Version: 1.15.14
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: UNKNOWN
License: Apache License 2.0
Location: /usr/local/lib/python3.8/site-packages
Requires: botocore, jmespath, s3transfer
Required-by: aws-shell

AWS CLI To detect faces in an image

Roger.jpg

Before we extract image from S3 bucket, we first need to create a S3 bucket and upload image to it

$ aws s3api create-bucket --bucket myfacetestbucket --region us-east-1
$ aws s3 cp Roger.jpg s3://myfacetestbucket/test

The following detect-faces command detects faces in the specified image stored in an Amazon S3 bucket.

$ aws rekognition detect-faces \
> --image '{"S3Object":{"Bucket":"myfacetestbucket","Name":"test"}}' \
> --attributes "ALL"
{
"FaceDetails": [
{
"BoundingBox": {
"Width": 0.124217189848423,
"Height": 0.36695611476898193,
"Left": 0.5486249327659607,
"Top": 0.4285665452480316
},
"AgeRange": {
"Low": 32,
"High": 48
},
"Smile": {
"Value": true,
"Confidence": 93.19034576416016
},
"Eyeglasses": {
"Value": false,
"Confidence": 99.75462341308594
},
"Sunglasses": {
"Value": false,
"Confidence": 99.88736724853516
},
"Gender": {
"Value": "Male",
"Confidence": 99.1171646118164
},
"Beard": {
"Value": false,
"Confidence": 62.04547882080078
},
"Mustache": {
"Value": false,
"Confidence": 96.88816833496094
},
"EyesOpen": {
"Value": true,
"Confidence": 99.10486602783203
},
"MouthOpen": {
"Value": true,
"Confidence": 95.41327667236328
},
"Emotions": [
{
"Type": "HAPPY",
"Confidence": 74.1766357421875
},
{
"Type": "DISGUSTED",
"Confidence": 13.986865997314453
},
{
"Type": "ANGRY",
"Confidence": 5.650695323944092
},
{
"Type": "CALM",
"Confidence": 2.472118377685547
},
{
"Type": "CONFUSED",
"Confidence": 1.4807705879211426
},
{
"Type": "SAD",
"Confidence": 0.8507742285728455
},
{
"Type": "SURPRISED",
"Confidence": 0.7629778981208801
},
{
"Type": "FEAR",
"Confidence": 0.6191766858100891
}
],
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.5736368894577026,
"Y": 0.5886330604553223
},
{
"Type": "eyeRight",
"X": 0.6275773048400879,
"Y": 0.5819094181060791
},
{
"Type": "mouthLeft",
"X": 0.5846119523048401,
"Y": 0.7058932781219482
},
{
"Type": "mouthRight",
"X": 0.6296831965446472,
"Y": 0.7008261680603027
},
{
"Type": "nose",
"X": 0.5938411951065063,
"Y": 0.6590603590011597
},
{
"Type": "leftEyeBrowLeft",
"X": 0.5556175708770752,
"Y": 0.560141921043396
},
{
"Type": "leftEyeBrowRight",
"X": 0.5653775334358215,
"Y": 0.5500103235244751
},
{
"Type": "leftEyeBrowUp",
"X": 0.5794927477836609,
"Y": 0.5560234189033508
},
{
"Type": "rightEyeBrowLeft",
"X": 0.6103295087814331,
"Y": 0.5515798330307007
},
{
"Type": "rightEyeBrowRight",
"X": 0.6275314688682556,
"Y": 0.5412198901176453
},
{
"Type": "rightEyeBrowUp",
"X": 0.649406373500824,
"Y": 0.5475934147834778
},
{
"Type": "leftEyeLeft",
"X": 0.5652635097503662,
"Y": 0.5878733992576599
},
{
"Type": "leftEyeRight",
"X": 0.5843948721885681,
"Y": 0.5882930159568787
},
{
"Type": "leftEyeUp",
"X": 0.5726075172424316,
"Y": 0.5832690000534058
},
{
"Type": "leftEyeDown",
"X": 0.5740702748298645,
"Y": 0.5937448143959045
},
{
"Type": "rightEyeLeft",
"X": 0.6169412136077881,
"Y": 0.584111213684082
},
{
"Type": "rightEyeRight",
"X": 0.6384167671203613,
"Y": 0.5785976052284241
},
{
"Type": "rightEyeUp",
"X": 0.6267658472061157,
"Y": 0.5763338208198547
},
{
"Type": "rightEyeDown",
"X": 0.6275367736816406,
"Y": 0.587045431137085
},
{
"Type": "noseLeft",
"X": 0.5905563235282898,
"Y": 0.666114091873169
},
{
"Type": "noseRight",
"X": 0.6105850338935852,
"Y": 0.6634709239006042
},
{
"Type": "mouthUp",
"X": 0.6012259125709534,
"Y": 0.6936687231063843
},
{
"Type": "mouthDown",
"X": 0.6045928597450256,
"Y": 0.72735995054245
},
{
"Type": "leftPupil",
"X": 0.5736368894577026,
"Y": 0.5886330604553223
},
{
"Type": "rightPupil",
"X": 0.6275773048400879,
"Y": 0.5819094181060791
},
{
"Type": "upperJawlineLeft",
"X": 0.5575063824653625,
"Y": 0.5780422687530518
},
{
"Type": "midJawlineLeft",
"X": 0.5734659433364868,
"Y": 0.7052022218704224
},
{
"Type": "chinBottom",
"X": 0.612401008605957,
"Y": 0.7830801010131836
},
{
"Type": "midJawlineRight",
"X": 0.6692211627960205,
"Y": 0.6919513940811157
},
{
"Type": "upperJawlineRight",
"X": 0.6752238869667053,
"Y": 0.5615693926811218
}
],
"Pose": {
"Roll": -4.978456974029541,
"Yaw": -13.672887802124023,
"Pitch": -1.7135460376739502
},
"Quality": {
"Brightness": 77.12853240966797,
"Sharpness": 89.85481262207031
},
"Confidence": 99.99987030029297
}
]
}

Recognizing Celebrities in an Image using AWS CLI

Before we extract image from S3 bucket, we first need to create a S3 bucket and upload image to it

$ aws s3api create-bucket --bucket myfacetestbucket --region us-east-1
$ aws s3 cp Roger.jpg s3://myfacetestbucket/test

This AWS CLI command displays the JSON output for the recognize-celebrities CLI operation. Roger Federer is found in the image.

$ aws rekognition recognize-celebrities \
> --image "S3Object={Bucket=myfacetestbucket,Name=test}"

{
"CelebrityFaces": [
{
"Urls": [
"www.imdb.com/name/nm1716574"
],
"Name": "Roger Federer", ### Is it cool? Roger Federer revealed!
"Id": "6bN4xP",
"Face": {
"BoundingBox": {
"Width": 0.18400000035762787,
"Height": 0.36800000071525574,
"Left": 0.5144000053405762,
"Top": 0.42559999227523804
},
"Confidence": 99.99990844726562,
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.5754296183586121,
"Y": 0.5891759991645813
},
{
"Type": "eyeRight",
"X": 0.6299571990966797,
"Y": 0.5762431025505066
},
{
"Type": "nose",
"X": 0.5906926989555359,
"Y": 0.6588773727416992
},
{
"Type": "mouthLeft",
"X": 0.5828781127929688,
"Y": 0.7055491209030151
},
{
"Type": "mouthRight",
"X": 0.6347113847732544,
"Y": 0.6932297945022583
}
],
"Pose": {
"Roll": -5.585475921630859,
"Yaw": -25.769460678100586,
"Pitch": -6.5233259201049805
},
"Quality": {
"Brightness": 56.052215576171875,
"Sharpness": 97.46806335449219
}
},
"MatchConfidence": 100.0
}
],
"UnrecognizedFaces": []
}

Detect labels using Python

Before we extract image from S3 bucket, we first need to create a S3 bucket and upload image to it

$ aws s3api create-bucket --bucket myfacetestbucket --region us-east-1
$ aws s3 cp Roger.jpg s3://myfacetestbucket/test

vim label.py

Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)

import boto3

def detect_labels(photo, bucket):

client=boto3.client('rekognition')

response = client.detect_labels(Image={'S3Object':{'Bucket':bucket,'Name':photo}},
MaxLabels=10)

print('Detected labels for ' + photo)
print()
for label in response['Labels']:
print ("Label: " + label['Name'])
print ("Confidence: " + str(label['Confidence']))
print ("Instances:")
for instance in label['Instances']:
print (" Bounding box")
print (" Top: " + str(instance['BoundingBox']['Top']))
print (" Left: " + str(instance['BoundingBox']['Left']))
print (" Width: " + str(instance['BoundingBox']['Width']))
print (" Height: " + str(instance['BoundingBox']['Height']))
print (" Confidence: " + str(instance['Confidence']))
print()

print ("Parents:")
for parent in label['Parents']:
print (" " + parent['Name'])
print ("----------")
print ()
return len(response['Labels'])


def main():
photo='test' ### Input name of your object in S3 bucket
bucket='myfacetestbucket' ### Name of your S3 bucket
label_count=detect_labels(photo, bucket)
print("Labels detected: " + str(label_count))
if __name__ == "__main__":
main()

Outcome of it

$ python label.py 
Detected labels for test

Label: Person
Confidence: 98.4091796875
Instances:
Bounding box
Top: 0.11844990402460098
Left: 0.1067546159029007
Width: 0.7130014896392822
Height: 0.8799501061439514
Confidence: 96.84175109863281

Parents:
----------

Label: Sport
Confidence: 93.61238861083984
Instances:
Parents:
Person
----------

Label: People
Confidence: 80.27007293701172
Instances:
Parents:
Person
----------

Label: Finger
Confidence: 79.93012237548828
Instances:
Parents:
----------

Label: Face
Confidence: 72.68374633789062
Instances:
Parents:
Person
----------

Label: Tennis
Confidence: 72.09969329833984
Instances:
Parents:
Sport
Person
----------

Label: Tennis Racket
Confidence: 61.02318572998047
Instances:
Parents:
----------

Label: Sphere
Confidence: 60.62116622924805
Instances:
Parents:
----------

Label: Team Sport
Confidence: 60.16021728515625
Instances:
Parents:
Sport
Person
People
----------

Label: Ping Pong
Confidence: 57.140323638916016
Instances:
Parents:
Sport
Person
----------

Labels detected: 10

Detecting text in image using Python

Before we extract image from S3 bucket, we first need to create a S3 bucket and upload image to it

$ aws s3api create-bucket --bucket rekognitiontextbucket --region us-east-1
$ aws s3 cp Forest.jpg s3://rekognitiontextbucket/test

vim detectext.py

# Change the value of bucket to the S3 bucket that contains your image file.
# Change the value of photo to your image file name.
bucket='rekognitiontextbucket'
photo='test'

client=boto3.client('rekognition')


response=client.detect_text(Image={'S3Object':{'Bucket':bucket,'Name':photo}})

textDetections=response['TextDetections']
print ('Detected text')
for text in textDetections:
print ('Detected text:' + text['DetectedText'])
print ('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%")
print ('Id: {}'.format(text['Id']))
if 'ParentId' in text:
print ('Parent Id: {}'.format(text['ParentId']))
print ('Type:' + text['Type'])
print

Test it

$ python detectext.py
Detected text
Detected text:FOREST
Confidence: 99.90%
Id: 0
Type:LINE
Detected text:FOREST
Confidence: 99.90%
Id: 1
Parent Id: 0
Type:WORD

Voila, C’est fini!

In this project, we demonstrated how AWS rekognition may detect, analyze data from image. If you are interested in this, please visit here to find more examples.

Thoughout the project, we only touched upon both AWS Rekognition using AWS CLI and Python, but there are a whole bunch of more coding languages and possibilities out there, you can dive in if interested.

As AI and Big data continuously develop, there are a lot more technologies may come along the way.

--

--

Paul Zhao
Paul Zhao Projects

Amazon Web Service Certified Solutions Architect Professional & Devops Engineer