Image and Video Analysis with AWS Rekognition

AWS Rekognition is a cloud-based service that can analyze image and video data. This service offers powerful artificial intelligence capabilities for image and video analysis. It is particularly important for various industries such as security, media and entertainment, e-commerce, and more. 

In this article, I will provide a detailed explanation of the fundamental functions of AWS Rekognition, its usage scenarios, and how to use it with an example application. 

Figure 1 AWS Rekognition 

Setting Up AWS Credentials 

As shown in Figure 1, log in to the AWS Management Console and access the AWS Rekognition service. To access Amazon Rekognition with Python, you need to configure your AWS credentials. You can do this by creating an IAM user and obtaining an access key and secret key. 

  1. Log in to the AWS Management Console. 
  1. Go to the IAM dashboard. 
  1. Create a new IAM user with programmatic access. 
  1. Make note of the access key and secret key information for the IAM user. 

Installation of Required Libraries 

You will need to install the required libraries to get started: 

pip install boto3 pillow 

Facial Analysis 

import boto3 
from PIL import Image 
import io 
 
# AWS'den aldığınız bilgileri buradaki 'your_region', 'your_access_key' ve 'your_secret_key' bilgileri ile değiştirin. 
rekognition_client = boto3.client('rekognition', region_name='your_region', aws_access_key_id='your_access_key', aws_secret_access_key='your_secret_key') 
 
image_file_path = 'path_to_your_image.jpg' 
 
# Dosyadan resmi okuyun 
with open(image_file_path, 'rb') as image_file: 
    image_bytes = image_file.read() 
 
 
# rekognition servisine resmi gönderin 
response = rekognition_client.detect_faces( 
    Image={ 
        'Bytes': image_bytes 
    }, 
    Attributes=['ALL']  # Resim üzerinde nelerin bulunacağını spesifik olarak belirtebilirsiniz.('ALL', 'DEFAULT', 'AGE', 'GENDER', 'EMOTION') 
) 
 
# Sonuç çıktılarının gösterilmesi 
for face_detail in response.get('FaceDetails', []): 
    print(f"Age: {face_detail.get('AgeRange', {}).get('Low')} - {face_detail.get('AgeRange', {}).get('High')}") 
    print(f"Gender: {face_detail.get('Gender', {}).get('Value')}") 
    print(f"Emotions:") 
    for emotion in face_detail.get('Emotions', []): 
        print(f"{emotion.get('Type')}: {emotion.get('Confidence')}%") 
    print("----------------------") 

Let's also demonstrate this code on the example image shown in Figure 2. 

Figure 2 Örnek Resim 

Since we selected the 'ALL' option, Rekognition returns all the information related to the image as shown below: 

Age: 37 - 45 
Gender: Male 
Emotions: 
HAPPY: 99.01817321777344%       
SURPRISED: 6.309852123260498%   
FEAR: 5.911846160888672%        
SAD: 2.1603987216949463%        
CONFUSED: 0.2645522952079773%   
DISGUSTED: 0.22894439101219177% 
ANGRY: 0.1428459733724594%      
CALM: 0.08611113578081131%      
---------------------- 

AWS Rekognition Custom Labels 

Custom Labels in AWS Rekognition provide users with the ability to identify objects or scenes tailored to their specific use cases and create their own models instead of using pre-trained models.  

In this section, we will provide an example using AWS's image classification project called the "rooms project," as shown in Figure 3. First, we will create an S3 bucket area to store the dataset and allow the model to be registered. 

Figure 3 - Example Projects 

To label unlabeled images, start by selecting the image you want to label and click on the "draw bounding boxes" option. In Figure 4, you can see our labeling editor. In this editor, you can select the previously defined labels and draw frames on the image to define specific areas.  

Figure 4 - Labeling Editor 

Finally, we initiate the training of our model by clicking on "train model." The trained model is stored in the S3 bucket. The training duration can vary from 30 minutes to 24 hours. For pricing information regarding the Amazon Rekognition service, you can refer to their pricing page. link üzerinden ulaşabilirsiniz. 

AWS Rekognition Demos 

AWS Rekognition documentation and console provide various demo applications and sample codes. Some of the demos you can try include: 

  • Image Analysis with Rekognition: This demo allows you to upload an image and displays the detected labels, faces, and text within the image. 
  • Face Comparison and Recognition: You can compare faces in two images and determine if they match. This is commonly used in identity verification and security applications. 
  • Custom Labels: You can train Rekognition to recognize your custom objects, which is useful for specific object recognition tasks. 

Amazon Rekognition makes facial analysis in Python applications easier. In this guide, we covered the basics from setting up your AWS credentials to installing the necessary libraries and performing facial analysis on an image. You can customize this code to fit your specific use case. 

You can explore the Amazon Rekognition documentation for more advanced features and options.