Type Annotations for boto3 Rekognition

1.42.3 · active · verified Sat Apr 11

This library provides comprehensive type annotations for the `boto3` AWS Rekognition service, generated by `mypy-boto3-builder`. It enhances developer experience by enabling static type checking, autocompletion, and improved error detection for `boto3` users working with Rekognition, ensuring type safety in AWS SDK interactions. The current version is 1.42.3 and it is actively maintained through regular updates from the `mypy-boto3-builder` project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Rekognition client with type annotations and use it to detect labels in an image stored in an S3 bucket. It shows explicit type hints for the client object and the response, leveraging the benefits of static type checking. Ensure `boto3` is installed and AWS credentials are configured for the example to run.

import boto3
from mypy_boto3_rekognition.client import RekognitionClient
from mypy_boto3_rekognition.type_defs import DetectLabelsResponseTypeDef
import os

# Configure AWS region from environment variable or default
aws_region = os.environ.get("AWS_REGION", "us-east-1")

# Create a Rekognition client with type hints
# boto3 will automatically pick up credentials from environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# or ~/.aws/credentials, or IAM roles.
rekognition_client: RekognitionClient = boto3.client("rekognition", region_name=aws_region)

# Example: Detect labels in an image from an S3 bucket
try:
    # Replace with your S3 bucket and object key for testing
    s3_bucket_name = os.environ.get("REKOGNITION_TEST_BUCKET", "your-rekognition-test-bucket")
    s3_object_key = os.environ.get("REKOGNITION_TEST_OBJECT_KEY", "your-image.jpg")

    if s3_bucket_name == "your-rekognition-test-bucket" or s3_object_key == "your-image.jpg":
        print("Please set REKOGNITION_TEST_BUCKET and REKOGNITION_TEST_OBJECT_KEY environment variables or replace placeholders.")
    else:
        response: DetectLabelsResponseTypeDef = rekognition_client.detect_labels(
            Image={
                'S3Object': {
                    'Bucket': s3_bucket_name,
                    'Name': s3_object_key,
                }
            },
            MaxLabels=10,
            MinConfidence=75
        )

        print(f"Detected labels in s3://{s3_bucket_name}/{s3_object_key}:")
        for label in response.get("Labels", []):
            print(f"  {label['Name']} (Confidence: {label['Confidence']:.2f}%)")

except rekognition_client.exceptions.InvalidImageFormatException:
    print("Error: Image format not supported by Rekognition.")
except rekognition_client.exceptions.ClientError as e:
    print(f"AWS Client Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →