Image Recognition with Python and AWS

Amazon has a whole suite of tools to add artificial intelligence capabilities to your app.
We are going to look at one tool Amazon Rekognition, which an image analysis service. Rekognition can do a number of things such as detect faces, objects, celebrities.

To interact with this service we are going use Boto 3, which is an SDK for Python.

If you do not have an AWS account you can create one now following their documentation. Once you sign up you will need to create an access key. Save your key you will need it later.


  • Python3
  • Virtualenvwrapper

Our first steps will be to create a new virtual environment and pip install boto and decouple. We use decouple just to manage our environment variables.

mkvirtualenv --python=$(which python3) py-rekognition
pip install python-decouple
pip install boto3

Now we have our virtual environment setup with all the packages we need to get started. Let's create a new file to save our environment variables mainly our AWS Access Key and Secret Key. Create a new file named .env. Set two variables


We are going to look at four functions from the library detect_labels(), detect_faces(), compare_faces(), , and recognize_celebrities().

In our first example we are going to use detect_labels. Since we have our environment variables are set. The next step is to create a python file named In this file we are going to:

  • Read in our environment variables.
  • Connect to AWS
  • Open an image locally
  • Pass that image to Rekognition
  • Print out the results

Your file will look like the following.

import sys
import boto3
from decouple import config


client = boto3.client(

image_name = sys.argv[1]

    imgfile = open(image_name, 'rb')
    imgbytes =
    print('There was an error opening the image')

imgobj = {'Bytes': imgbytes}

response = client.detect_labels(Image=imgobj)


Let's look at this line.
response = client.detect_labels(Image=imgobj)

detect_labels is the function that passes the image to Rekognition and returns an analysis of the image. detect_labels takes either and S3 object or an Image object as bytes. Two other optional parameters are MaxLabels and MinConfidence
It will try to detect all the objects in the image and give it label and confidence rating on the label.

You can run your program from the command line: python john-wall.jpg . The parameter is the name of the file you want to analysis.

The response will be:

{'Name': 'People', 'Confidence': 99.21666717529297},
{'Name': 'Person', 'Confidence': 99.21666717529297},
{'Name': 'Human', 'Confidence': 99.20529174804688},
{'Name': 'Athlete', 'Confidence': 97.75991821289062},
{'Name': 'Sport', 'Confidence': 97.75991821289062}

To find more example checkout out my project on github