Type Annotations for boto3 Rekognition
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
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0. Consequently, `mypy-boto3-rekognition` versions 1.42.3 and newer require Python 3.9 or higher.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0. Specifically, argument TypeDefs were shortened (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`) and conflicting `Extra` postfixes were moved (e.g., `CreateDistributionExtraRequestTypeDef` became `CreateDistributionRequestExtraTypeDef`).
- gotcha This library provides *only* type hints (stubs). It does not include the functional `boto3` library itself. You must install `boto3` separately (`pip install boto3`) for your code to run.
- gotcha The `mypy-boto3` packages are designed to be version-matched with the `boto3` library. Using `mypy-boto3-rekognition` 1.42.3 implies you should be using `boto3` version 1.42.3. Mismatched versions can lead to incorrect or missing type hints.
- gotcha Users of PyCharm might experience slow performance with `mypy-boto3` due to issues with Literal overloads (PyCharm issue PY-40997). In such cases, it's recommended to either use `boto3-stubs-lite` (if explicit type annotations are acceptable) or disable PyCharm's internal type checker and rely on external tools like `mypy` or `pyright`.
Install
-
pip install mypy-boto3-rekognition -
pip install 'boto3-stubs[rekognition]'
Imports
- RekognitionClient
from mypy_boto3_rekognition.client import RekognitionClient
- DetectLabelsResponseTypeDef
from mypy_boto3_rekognition.type_defs import DetectLabelsResponseTypeDef
- AttributeType
from mypy_boto3_rekognition.literals import AttributeType
- ProjectVersionRunningWaiter
from mypy_boto3_rekognition.waiter import ProjectVersionRunningWaiter
- DetectFacesPaginator
from mypy_boto3_rekognition.paginator import DetectFacesPaginator
Quickstart
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}")