Ruan Bekker's Blog

From a Curious mind to Posts on Github

Using Python for Image Analysis With Amazons Rekognition Service

Amazon’s Rekognition Service, which falls under their Artificial Intelligence tier, makes it easy to add image analysis to your applications.

Today we will use Rekognition to analyze an image, to determine the percentage of detection that the service analyzes. We will be using the Python SDK to do this.

Getting a Random Image:

So, I got this drunk guy on the couch, which I thought we could use to analyze.

Image Used: - http://imgur.com/a/CHnSu

Our Python Code:

Our code will use boto3 to use rekognition from Amazon Web Services, detects the image, and prints out the values.

Note that I am not specifying any credentials, as my credentials is configured in my local credential provider, where boto will pick it up from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import boto3

BUCKET = "rekognition-bucket"
KEY = "images/image-02.jpg"

def detect_labels(bucket, key, max_labels=10, min_confidence=90, region="eu-west-1", profile_name="aws"):
    rekognition = boto3.client("rekognition")
    response = rekognition.detect_labels(
        Image={
        "S3Object": {
        "Bucket": BUCKET,
        "Name": KEY,
    }
        },
        MaxLabels=max_labels,
        MinConfidence=min_confidence,
    )
    return response['Labels']


for label in detect_labels(BUCKET, KEY):
    print("{Name} - {Confidence}%".format(**label))

Running the App:

Running our Python App, will result in the following:

1
2
3
4
5
6
7
8
9
10
11
$ python rekog.py
People - 98.9893875122%
Person - 98.9893951416%
Human - 98.9505844116%
Alcohol - 98.573425293%
Beer - 98.573425293%
Beer Bottle - 98.573425293%
Beverage - 98.573425293%
Bottle - 98.573425293%
Drink - 98.573425293%
Couch - 98.4713821411%

Resources: