×

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to invoke Amazon Rekognition API to get the results out of the invocation.

We shall develop a python code which reads an example image using the AWS python boto3 module, which provides us with our required results, i.e., the labels in the picture.

In simple words, we can define Amazon recognition as an API that helps us to identify and differentiate various objects or people in a given image.

The Amazon Rekognition service web page (https://aws.amazon.com/rekognition/.) provides various features such as content moderation (detects inappropriate, potentially unsafe, unwanted content across images and videos), face compare and search (compares two faces in two pictures and determines the similarities in the faces),  face detection and analysis (detects faces that appear in an image or video and can also recognize features such as specs, facial hair, open eyes, etc.), labels (detects all the objects in pictures and scenes in videos), custom labels (detects custom labels such as brand logos using machine learning algorithms), text detection (extracts distorted and skewed text from images and videos), streaming video events detection (detects objects, people, beings etc. from live streaming videos) etc.

Let us now see how to label a given image using Amazon Rekognition using python. First of all, create an S3 bucket in the Amazon Rekognition interface. Then, please select an image that you want to label and add it to the s3 bucket. Now, implement the following code

import boto3 # first import boto3 on your IDE
def detect_labels( photo , bucket ) :
    client = boto3.client( ' rekognition ' )
    response = client.detect_labels( Image = { ' S3Object ' : { ' Bucket ' : bucket , ' Name ' : photo } } , MaxLabel = 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[ ' BoundingBox ' ][ ' Confidence ' ] ) )
            print( )
            print( " Parents " )
            for parent in label[ ' Parents ' ] :
                print( "    " + parent[ ' Name ' ] )
            print( " - - - - - - - - - - - - - - - - - - " )
    return len( response[ ' Labels ' ] )
def main() :
    photo = ' img.jpg ' # provide the image address
    bucket = ' amazonrekognition-cqpocs '
    label_count = detect_labels( photo , bucket )
    print( " Labels detected : " + str( label_count ) )
if __name__ == " __main__ " :
    main()

 After writing the code in your IDE, go to the console and type 'aws configure' to log in to your AWS account. Then, provide the details asked by your console. Then, run your code using local invocation. You will see that your console successfully returns an output providing each and every label in your image along with its confidence, instances, and parent information. Thus, you have successfully completed your Amazon rekognition using python for the identification of labels.

Example input:

Amazon rekognition using python

Output:

Label: Watercraft
Confidence: 99.46809387207031
Instances:
Parents: Vehicle
               Transportation
--------------------------
Label: Vehicle
Confidence: 99.46809387207031
Instances:
Parents: Transportation
--------------------------
Label: Transportation
Confidence: 99.46809387207031
Instances:
Parents:
--------------------------
Label: Boat
Confidence: 98.43656158447266
Instances: Bounding box
Top: 0.38169410824775696
Left: 0.311278020095825
Width: 0.1282336711883545
Height: 0.1624620407819748
Confidence: 98.43656158447266
Parents:
Vehicle
Transportation
--------------------------
Label: Water
Confidence: 98.2005615234375
Instances:
Parents:
--------------------------
Label: City
Confidence: 82.61547088623047
Instances:
Parents:
Urban
--------------------------
Label: Urban
Confidence: 82.61547088623047
Instances:
Parents:
----------------------
Label: Office Building
Confidence: 81.8774642944336
Instances:
Parents:
-------------------------
Label: Back
Confidence: 81.69802856445312
Instances:
Parents:
--------------------------
Label: High Rise
Confidence: 77.49475860595703
Instances:
Parents:

Similarly, we can perform other recognitions also using the Amazon recognition interface using simple python codes.


Related Topics

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Anaconda python 3 installation for windows 10

If you are a problem solver and like to solve programming questions, the anaconda distribution for python could be one of the best options to use and solve problems in...

3 minutes read.

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 minutes read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.