Exploring the benefits of Amazon Rekognition – AWS Application Services for AI/ML – MLS-C01 Study Guide

Exploring the benefits of Amazon Rekognition

Here are some of the benefits of using Amazon Rekognition:

  • AWS manages the infrastructure it runs on. In short, just use the API for your image analysis. You need to only focus on building and managing your deep learning pipelines.

With or without knowledge of image processing, you can perform image and video analysis just by using the APIs provided in Amazon Rekognition, which can be used for any application or service on several platforms.

  • The Labels API’s response will identify real-world entities within an image through the DetectLabels API. These labels include city, town, table, home, garden, animal, pets, food, drink, electronics, flowers, and more. The entities are classified based on their confidence score, which indicates the probability that a given prediction is correct — the higher the score, the better. Similarly, you can use the DetectText API to extract the text in an image. Amazon Rekognition may detect multiple lines based on the gap between words. Periods do not represent the end of a line.
  • Amazon Rekognition can be integrated with AWS Kinesis Video Stream, AWS S3, and AWS Lambda for seamless and affordable image and video analysis. With the AWS IAM service, Amazon Rekognition API calls can easily be secured and controlled.
  • Low cost: you only pay for the images and videos that are analyzed.
  • Through AWS CloudTrail, all the API calls for Amazon Rekognition can be captured as events. It captures all calls made from the console, the CLI, or code calls for APIs, which further enables the user to create Amazon SNS notifications based on CloudTrail events.
  • You can create a VPC endpoint policy for specific API calls to establish a private connection between your VPC and Amazon Rekognition. This helps you leverage enhanced security. As per the AWS Shared Responsibility Model, AWS takes care of the security of the infrastructure and software, and you have to take care of the security of your content in the cloud.

Getting hands-on with Amazon Rekognition

In this section, you will learn how to integrate AWS Lambda with Amazon Rekognition to detect the labels in our image (uploaded at https://github.com/PacktPublishing/AWS-Certified-Machine-Learning-Specialty-MLS-C01-Certification-Guide-Second-Edition/tree/main/Chapter08/Amazon%20Rekognition%20Demo/images) and print the detected objects in the CloudWatch console. You will use the detect_labels API from Amazon Rekognition in the code.

You will begin by creating an IAM role for Lambda:

  1. Navigate to the IAM console page.
  2. Select Roles from the left-hand menu.
  3. Select Create role.
  4. Select Lambda from the Choose a use case section.
  5. Add the following managed policies:
    • AmazonS3ReadOnlyAccess
    • AmazonRekognitionFullAccess
    • CloudWatchLogsFullAccess
  6. Name the role rekognition-lambda-role:

Figure 8.1 – The Create role dialog

Next, you will create a Lambda function.

  • Navigate to the AWS Lambda console page.
  • Select Create function.
  • Create a function:
    • Select Author from scratch.
    • Select Use an existing role. Add the name of the role you created previously; that is, rekognition-lambda-role:
    • Give the function a name, such as lambda-rekognition.Choose Python 3.6 from the Runtime dropdown.

Figure 8.2 – Creating the Lambda function

  1. Enter the following code in lambda_function.py:

from __future__ import print_function

import boto3

def lambda_handler(event, context):

    print(“========lambda_handler started=======”)

    # read the bucket name (key) from the event

    name_of_the_bucket=event[‘Records’][0][‘s3’][‘bucket’]

[‘name’]

    # read the object from the event

    name_of_the_photo=event[‘Records’][0][‘s3’][‘object’][‘key’]

    detect_labels(name_of_the_photo,name_of_the_bucket)

    print(“Labels detected Successfully”)

def detect_labels(photo, bucket):

    client=boto3.client(‘rekognition’)

  response=client.detect_labels(Image={‘S3Object’:{‘Bucket’:bucket,’Name’:photo}})

    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 response

Now, you will create a trigger for the Lambda Function.

  1. Navigate to the AWS S3 console page. Create a bucket, for example, rekognition-test-baba, as shown in Figure 8.3:

Figure 8.3 – AWS S3 console page

  1. Click on Create folder and name it images. Click Save.
  2. Click the Properties tab of our bucket.
  3. Scroll to Events for that bucket.
  4. Inside the Events window, select Add notification and set the following properties:
    • Name: rekognition_event
    • Events: All object create events
    • Prefix: images
    • /Send to: Lambda Function
    • Lambda: lambda-rekognition:

Figure 8.4 – S3 bucket Events window

Next, you will upload the image from the shared GitHub repository to the S3 bucket images folder.

  1. As soon as you upload, you can check the Monitoring tab in the Lambda console to monitor the events, as shown in Figure 8.5:

Figure 8.5 – CloudWatch monitoring the event in the Lambda console

  1. Navigate to CloudWatch > CloudWatch Logs > Log groups > /aws/lambda/lambda-rekognition. Select the latest stream from all the streams listed on the AWS console and scroll down in the logs to see your output.

In this section, you learned how to implement the Amazon Rekognition AI service to detect objects in an image and get a confidence score for each. You will see more use cases for Amazon Rekognition in the upcoming sections, where you will detect text in images. In the next section, you will learn about Amazon’s text-to-speech service and implement it.