×

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

Unicode to String in Python

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

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...

3 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

Event Key in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Uint8 Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

3 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.