This project uses Amazon Rekognition to analyze images stored in an Amazon S3 bucket and automatically generate descriptive labels with confidence scores.
The Python script connects to AWS using boto3, sends an image from S3 to Amazon Rekognition, retrieves detected labels, and displays the image with bounding boxes around detected objects.
This project demonstrates how cloud-based machine learning services can be integrated into Python applications for image analysis.
This project simulates how businesses can use cloud-based image recognition to automatically classify images, detect objects, organize media files, or support content moderation workflows.
Examples include:
- Labeling uploaded product images
- Organizing image libraries
- Detecting objects in security images
- Supporting inventory or asset tracking
- Building AI-powered media applications
- Creating automated image classification systems
- Amazon Web Services
- Amazon S3
- Amazon Rekognition
- AWS CLI
- Python
- boto3
- matplotlib
- Pillow
- VS Code
Amazon S3 is used to store the images that will be analyzed by Amazon Rekognition.
Amazon Rekognition is used to analyze images and generate labels with confidence scores.
The AWS CLI is used to configure credentials and interact with AWS services from the command line.
- Creating and configuring an Amazon S3 bucket
- Uploading images to Amazon S3
- Using Amazon Rekognition for image analysis
- Writing Python scripts with
boto3 - Reading files from S3 using Python
- Visualizing image labels with
matplotlib - Drawing bounding boxes around detected objects
- Configuring AWS CLI credentials
- Applying IAM least-privilege concepts
- Troubleshooting AWS permission and credential issues
- Documenting a cloud project for GitHub
Local Python Script
|
| boto3 SDK
|
Amazon S3 Bucket
|
| Image Object
|
Amazon Rekognition
|
| DetectLabels API
|
Label Results + Confidence Scores
|
| matplotlib + Pillow
|
Image Display with Bounding Boxes
- Create an Amazon S3 bucket.
- Upload images to the S3 bucket.
- Configure AWS CLI credentials.
- Install required Python libraries.
- Write a Python script using
boto3. - Use Amazon Rekognition to detect labels.
- Display detected labels and confidence scores.
- Draw bounding boxes around detected objects.
- Validate and troubleshoot the results.
Image-Label-Generator/
│
├── Images/
│ ├── ImageLableGen_AWSRekognition.png
│ ├── S3bucket.png
│ ├── NameBucket.png
│ ├── uploaded-image.png
│ └── generated-labels.png
│
├── image_label_generator.py
├── requirements.txt
└── README.md
Before running this project, you need:
- An AWS account
- AWS CLI installed
- Python 3 installed
- VS Code or another code editor
- An S3 bucket with uploaded images
- IAM permissions for Amazon S3 and Amazon Rekognition
Install the required Python libraries:
pip install boto3 matplotlib pillowOr create a requirements.txt file:
boto3
matplotlib
pillow
Then install from the file:
pip install -r requirements.txtBefore running the Python script, configure AWS CLI credentials:
aws configureYou will be prompted to enter:
AWS Access Key ID
AWS Secret Access Key
Default region name
Default output format
Example:
Default region name: us-east-1
Default output format: json
Security Note: Do not hardcode AWS access keys inside your Python script. Use AWS CLI profiles, environment variables, or IAM roles.
The AWS user or role running this script needs permission to:
- Read objects from the S3 bucket
- Call Amazon Rekognition
DetectLabels
Example IAM policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowRekognitionDetectLabels",
"Effect": "Allow",
"Action": [
"rekognition:DetectLabels"
],
"Resource": "*"
},
{
"Sid": "AllowS3ReadAccessToImages",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}Replace:
your-bucket-name
with the name of your actual S3 bucket.
- Log in to the AWS Management Console.
- Search for S3.
- Open the Amazon S3 service.
- Click Create bucket.
- Enter a globally unique bucket name.
- Select an AWS region.
- Leave the default settings for this lab.
- Click Create bucket.
An S3 bucket acts as a cloud storage container where image files can be stored and accessed by AWS services.
Choose a unique bucket name.
S3 bucket names must be globally unique across all AWS accounts and regions.
- Open the S3 bucket.
- Click Upload.
- Select the image files from your local computer.
- Click Upload.
- Confirm that the image appears in the bucket.
The uploaded image will be analyzed by Amazon Rekognition.
Create a Python file named:
image_label_generator.py
This file will contain the code used to connect to AWS, analyze the image, and display the results.
import boto3
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
from io import BytesIO
from botocore.exceptions import ClientError
AWS_REGION = "us-east-1"
BUCKET_NAME = "labelgen1"
IMAGE_NAME = "animals.jfif"
MAX_LABELS = 10
MIN_CONFIDENCE = 70
def detect_labels(photo, bucket):
"""
Detect labels in an image stored in Amazon S3 using Amazon Rekognition.
"""
rekognition_client = boto3.client("rekognition", region_name=AWS_REGION)
s3_resource = boto3.resource("s3", region_name=AWS_REGION)
try:
response = rekognition_client.detect_labels(
Image={
"S3Object": {
"Bucket": bucket,
"Name": photo
}
},
MaxLabels=MAX_LABELS,
MinConfidence=MIN_CONFIDENCE
)
print(f"\nDetected labels for: {photo}\n")
for label in response["Labels"]:
print(f"Label: {label['Name']}")
print(f"Confidence: {label['Confidence']:.2f}%")
print("-" * 30)
image_object = s3_resource.Object(bucket, photo)
image_data = image_object.get()["Body"].read()
image = Image.open(BytesIO(image_data))
display_image_with_bounding_boxes(image, response["Labels"])
return len(response["Labels"])
except ClientError as error:
print(f"AWS ClientError: {error}")
return 0
except Exception as error:
print(f"Unexpected error: {error}")
return 0
def display_image_with_bounding_boxes(image, labels):
"""
Display the image and draw bounding boxes around detected object instances.
"""
plt.imshow(image)
axis = plt.gca()
for label in labels:
for instance in label.get("Instances", []):
bounding_box = instance["BoundingBox"]
left = bounding_box["Left"] * image.width
top = bounding_box["Top"] * image.height
width = bounding_box["Width"] * image.width
height = bounding_box["Height"] * image.height
rectangle = patches.Rectangle(
(left, top),
width,
height,
linewidth=2,
edgecolor="red",
facecolor="none"
)
axis.add_patch(rectangle)
label_text = f"{label['Name']} ({label['Confidence']:.2f}%)"
plt.text(
left,
top - 5,
label_text,
color="red",
fontsize=8,
bbox={"facecolor": "white", "alpha": 0.7}
)
plt.axis("off")
plt.show()
def main():
label_count = detect_labels(IMAGE_NAME, BUCKET_NAME)
print(f"\nTotal labels detected: {label_count}")
if __name__ == "__main__":
main()Before running the script, update these values:
AWS_REGION = "us-east-1"
BUCKET_NAME = "labelgen1"
IMAGE_NAME = "animals.jfif"Replace them with:
- Your AWS region
- Your S3 bucket name
- Your uploaded image file name
Example:
AWS_REGION = "us-east-1"
BUCKET_NAME = "my-image-label-bucket"
IMAGE_NAME = "dog-photo.jpg"Open the terminal in the same directory as the Python file and run:
python image_label_generator.pyIf the script runs successfully, the output will show detected labels and confidence scores.
Example output:
Detected labels for: animals.jfif
Label: Animal
Confidence: 99.25%
------------------------------
Label: Dog
Confidence: 98.14%
------------------------------
Label: Pet
Confidence: 95.73%
------------------------------
Total labels detected: 3
A pop-up window will also display the uploaded image with bounding boxes around detected objects.
The project was validated using the following checks:
| Test | Expected Result | Status |
|---|---|---|
| Create S3 bucket | Bucket is created successfully | Passed |
| Upload image to S3 | Image appears in bucket | Passed |
| Configure AWS CLI | AWS credentials are available locally | Passed |
| Run Python script | Rekognition returns labels | Passed |
| Display image | Image opens with bounding boxes | Passed |
| Show confidence scores | Labels include confidence percentages | Passed |
Cause:
The IAM user or role does not have permission to call Amazon Rekognition or read from S3.
Fix:
Confirm the user has the following permissions:
rekognition:DetectLabels
s3:GetObject
Cause:
AWS credentials are not configured locally.
Fix:
Run:
aws configureThen confirm your credentials are active:
aws sts get-caller-identityCause:
The image name in the Python script does not match the object name in S3.
Fix:
Check the exact file name in the S3 bucket and update:
IMAGE_NAME = "your-image-name.jpg"Cause:
The AWS region in the script does not match the region where the services are being used.
Fix:
Update:
AWS_REGION = "us-east-1"to match your AWS region.
Cause:
Some labels returned by Amazon Rekognition do not include object instances or bounding boxes.
Fix:
This is expected behavior. Rekognition may detect general scene labels without returning object coordinates.
- Do not hardcode AWS access keys in the Python script.
- Use IAM least-privilege permissions.
- Keep S3 buckets private unless public access is required.
- Store credentials securely using AWS CLI profiles or IAM roles.
- Avoid uploading sensitive or personal images to public buckets.
- Delete test images when the project is complete.
- Rotate access keys regularly if using IAM user credentials.
- Use IAM roles when running this application on AWS compute services.
Amazon Rekognition and Amazon S3 may create charges depending on usage.
To reduce unnecessary costs:
- Delete test images after the project.
- Delete the S3 bucket if it is no longer needed.
- Monitor AWS Billing.
- Use AWS Budgets to set spending alerts.
- Avoid running the script repeatedly on large image sets unless needed.
To clean up the project:
- Go to the Amazon S3 console.
- Open the bucket used for this project.
- Delete the uploaded images.
- Delete the S3 bucket if it is no longer needed.
- Remove unused IAM permissions or access keys.
Optional AWS CLI cleanup:
aws s3 rm s3://your-bucket-name --recursive
aws s3 rb s3://your-bucket-nameThrough this project, I learned how to:
- Create and use an Amazon S3 bucket
- Upload images to AWS cloud storage
- Use Amazon Rekognition to detect image labels
- Connect Python applications to AWS using
boto3 - Display image analysis results with bounding boxes
- Configure AWS CLI credentials
- Apply IAM permissions for AWS service access
- Troubleshoot common AWS and Python errors
This project helped strengthen my understanding of AWS AI services, cloud storage, Python automation, and image analysis workflows.
This project can be upgraded by adding:
- Support for multiple image uploads
- CSV export for detected labels
- DynamoDB storage for label results
- A web interface using Flask or Streamlit
- Automatic Rekognition analysis when an image is uploaded to S3
- AWS Lambda for serverless image processing
- Amazon API Gateway for an image analysis API
- Amazon CloudWatch for logging and monitoring
- Face detection using Amazon Rekognition
- Unsafe content detection using Rekognition moderation labels
- Terraform deployment for AWS infrastructure
- CI/CD deployment using GitHub Actions
A stronger version of this project could use an event-driven AWS architecture:
User Uploads Image
|
v
Amazon S3 Bucket
|
v
AWS Lambda Trigger
|
v
Amazon Rekognition
|
v
DynamoDB Stores Labels
|
v
CloudWatch Logs
This would turn the project from a local Python script into a production-style serverless image analysis pipeline.
This project demonstrates how Amazon Rekognition and Amazon S3 can be used together to analyze images and generate labels automatically.
By combining AWS cloud storage, AI image recognition, Python scripting, and data visualization, this project shows a practical example of cloud-based machine learning integration.




