- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
A Developer's Guide
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.
Prerequisites
Before diving in, make sure you have the following:
pip install boto3
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
Or set them as environment variables:
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
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}%")
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']]}")
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']}")
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}")
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}%")
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
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.
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.Computer vision made easy with AWS Rekognition.
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.
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
pip install boto3
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
Or set them as environment variables:
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
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}%")
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']]}")
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']}")
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}")
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}%")
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.
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.