Create an image label detector with Amazon Rekognition

AimanAiman
4 min read

I've taken a break from writing over the past year because life got pretty busy and I had to put my side projects on hold (My last article was actually a year ago). But now, things have settled down and I’m ready to jump back in and learn new things.

Recently, I earned my AWS Certified Cloud Practitioner and AWS Certified Solutions Architect Associate certifications. Studying for these was quite a journey! Now that I'm back to working on side projects, it's the perfect time to have some fun experimenting with AWS services.

As always, here's my pick for your reading jam: "Stuck on the Puzzle" by Alex Turner. Enjoy the perfect soundtrack for your reading session!


Table of Content

  • Project Overview

  • Technology Stack & Implementation

  • Business Value

  • Conclusion


Project Overview

For this project, I'm creating an image label detector with Amazon Rekognition. The idea is to upload an image, use the Amazon SDK to call Rekognition's API, and let it analyze the image and give us some labels. It's actually a short project, but the excitement lies in utilizing Amazon Rekognition's AI capability.

Technology Stack & Implementation

Technology Stack

  • Python: The programming language I use to write the script

  • boto3: AWS SDK for Python to interact with Amazon Rekognition

  • AWS Services:

    • Amazon Rekognition: For image analysis and labeling

    • Amazon S3: For storing images

Implementation

  1. Create Amazon S3 bucket

    • Amazon S3 is a place where you can keep your files safe and easily accessible with permissions. S3 bucket is like a virtual storage box in the cloud.

    • For this step, you can log in to your AWS Management Console and navigate to Amazon S3 service. Create a bucket and then upload an image to it.

  2. Install and configure AWS CLI

    • AWS CLI is used to interact with various AWS services from the command line.

    • For a more detailed steps to install AWS CLI, you may refer here.

    • To configure the AWS CLI, you need to set up a user account and generate access keys. These keys are used for authentication to access AWS services.

  3. Write some Python code

    • Below is the code I run to generate the label
    # boto3 is AWS SDK for Python library
    import boto3
    # pyplot module to create visualization in Python
    import matplotlib.pyplot as plt
    # patches module to create shapes using matplotlib library
    import matplotlib.patches as patches
    # Image module to work with images
    from PIL import Image
    # Bytes10 class to treat binary data as if it were object
    from io import BytesIO

    # Create function to analyze the image
    def detect_labels(photo, bucket):
        # Create a Rekognition client
        client = boto3.client('rekognition')

        # Detect labels
        response = client.detect_labels(
            Image={'S3Object': {'Bucket': bucket, 'Name': photo}},
            MaxLabels=10)

        # Print detected labels
        print('Detected labels for ' + photo)
        print()
        for label in response['Labels']:
            print("Label:", label['Name'])
            print("Confidence:", label['Confidence'])
            print()

        # Load image from S3
        s3 = boto3.resource('s3')
        obj = s3.Object(bucket, photo)
        img_data = obj.get()['Body'].read()
        img = Image.open(BytesIO(img_data))

    # Display the image with bouding box
        plt.imshow(img)
        ax = plt.gca()
        for label in response['Labels']:
            for instance in label.get('Instances', []):
                bbox = instance['BoundingBox']
                left = bbox['Left'] * img.width
                top = bbox['Top'] * img.height
                width = bbox['Width'] * img.width
                height = bbox['Height'] * img.height
                rect = patches.Rectangle((left, top), width, height, linewidth=1, edgecolor='r', facecolor='none')
                ax.add_patch(rect)
                label_text = label['Name'] + ' (' + str(round(label['Confidence'], 2)) + '%)'
                plt.text(left, top - 2, label_text, color='r', fontsize=8, bbox=dict(facecolor='white', alpha=0.7))
        plt.show()

        return len(response['Labels'])

    # Main function of the script
    def main():
        photo = 'your photo name'
        bucket = 'your bucket name'
        label_count = detect_labels(photo, bucket)
        print("Labels detected:", label_count)

    # Code to ensure the script is run directly
    if __name__ == "__main__":
        main()
  • Then, you can open your terminal and run the Python command to execute the script.

  • This is the result of labelling a busy street in Hong Kong using Amazon Rekognition.

  • Simple architecture:

Business Value

Since I am in the financial services industry, a great use case for Amazon Rekognition is enhancing eKYC processes. It can streamline KYC procedures by automatically verifying customer identities through facial recognition and document analysis. This accelerates onboarding, reduces fraud, and ensures compliance with regulatory requirements. Here is an excerpt from eKYC policy document:

Conclusion

Working on the image label detector with Amazon Rekognition has been a great learning experience. This project shows me how powerful AI can be in automating tasks. By using Rekognition, I can build a tool that quickly and accurately labels images.

This kind of technology has big potential in many fields, including banking where it can help streamline eKYC processes, cut down on fraud, and stay compliant with regulations.

Overall, this project was a fun and eye-opening experience. If you're curious about AI and cloud services, give a project like this a try. It's a simple project but totally worth it!

0
Subscribe to my newsletter

Read articles from Aiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aiman
Aiman

I build, therefore I am