DEV Community

Angel Garrett
Angel Garrett

Posted on

Getting Started with the AWS Rekognition API

A Developer's Guide

Computer vision made easy with AWS Rekognition.

Amazon Rekognition is a powerful image and video analysis service provided by AWS. With just a few lines of code, you can analyze faces, detect objects, read text, and even identify unsafe content.

In this post, I’ll walk you through the basics of integrating Rekognition into your app using the AWS SDK. Whether you're building a photo moderation system or facial recognition features, this guide will help you get started quickly.

Scene detection


Prerequisites

Before diving in, make sure you have the following:

  • An AWS account
  • AWS credentials configured (~/.aws/credentials or IAM role if using EC2/Lambda)
  • AWS SDK installed (I'll use Python’s boto3, but the logic is similar in other SDKs)
  • An image file to test with

Installing boto3

pip install boto3
Enter fullscreen mode Exit fullscreen mode

Setting Up Credentials

Ensure your credentials are properly configured. For example:

# ~/.aws/credentials
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
Enter fullscreen mode Exit fullscreen mode

Or set them as environment variables:

export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
Enter fullscreen mode Exit fullscreen mode

Using Rekognition to Detect Labels

Label detection identifies objects, people, text, scenes, and activities in an image.

import boto3

client = boto3.client('rekognition')

with open('image.jpg', 'rb') as image:
    response = client.detect_labels(Image={'Bytes': image.read()}, MaxLabels=10, MinConfidence=80)

for label in response['Labels']:
    print(f"{label['Name']} - {label['Confidence']:.2f}%")
Enter fullscreen mode Exit fullscreen mode

Detecting Faces

Rekognition can detect faces and even return landmarks like eyes and nose positions.

with open('face.jpg', 'rb') as image:
    response = client.detect_faces(Image={'Bytes': image.read()}, Attributes=['ALL'])

for face in response['FaceDetails']:
    print(f"Age Range: {face['AgeRange']}")
    print(f"Smile: {face['Smile']['Value']}")
    print(f"Emotions: {[e['Type'] for e in face['Emotions']]}")
Enter fullscreen mode Exit fullscreen mode

Text Detection

You can also extract text from images, similar to OCR.

with open('text_image.jpg', 'rb') as image:
    response = client.detect_text(Image={'Bytes': image.read()})

for text in response['TextDetections']:
    print(f"Detected: {text['DetectedText']} - Type: {text['Type']}")
Enter fullscreen mode Exit fullscreen mode

Content Moderation

Useful for platforms where user-generated content needs filtering.

with open('moderation.jpg', 'rb') as image:
    response = client.detect_moderation_labels(Image={'Bytes': image.read()}, MinConfidence=80)

for label in response['ModerationLabels']:
    print(f"{label['Name']} - Confidence: {label['Confidence']:.2f}")
Enter fullscreen mode Exit fullscreen mode

Comparing Faces

Compare faces between two images, often used for verification.

source_img = open('source.jpg', 'rb').read()
target_img = open('target.jpg', 'rb').read()

response = client.compare_faces(SourceImage={'Bytes': source_img},
                                TargetImage={'Bytes': target_img},
                                SimilarityThreshold=90)

for match in response['FaceMatches']:
    print(f"Similarity: {match['Similarity']:.2f}%")
Enter fullscreen mode Exit fullscreen mode

Pricing

Rekognition offers a free tier for the first 12 months with:

5,000 images for label and face analysis

1,000 images for moderation

After that, pricing is based on per-image or per-second analysis.


Tips for Production

  • Store and reference images via S3 for better performance and scalability.
  • Always handle exceptions (botocore.exceptions.ClientError) for more resilient code.
  • Monitor API usage with AWS CloudWatch.
  • Use IAM roles with least privilege for security.

Final Thoughts

AWS Rekognition abstracts away the complexity of computer vision. Whether you’re adding face detection to a photo app or moderating uploaded content, Rekognition offers a reliable and scalable solution.

Top comments (0)