AWS Rekognition is a powerful service that uses deep learning to extract meaningful information from images and videos. Here’s an exploration of using Rekognition with the AWS Command Line Interface (CLI), including sample commands and expected outputs.
Prerequisites:
- An AWS account with Rekognition enabled.
- The AWS CLI installed and configured with your AWS credentials. You can install it following the instructions on the AWS website: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
Basic Structure of Rekognition Commands:
Most Rekognition commands follow a similar structure:
aws rekognition <command> \
--image <image-source> \
<optional-parameters>
<command>
: This specifies the specific Rekognition operation you want to perform. (e.g., detect-labels, detect-faces)<image-source>
: This defines the location of the image you want to analyze. It can be a local file path or an S3 object URL.<optional-parameters>
: Depending on the command, you might include additional parameters to customize the analysis. (e.g., MaxLabels for detect-labels)
AWS Rekognition Commands with Sample Outputs
Here are some even more basic AWS Rekognition commands using the AWS CLI, along with sample outputs to get you started:
1. Detect Labels in a Local Image:
This command analyzes a local image file for objects and scenes.
aws rekognition detect-labels \
--image file://path/to/your/image.jpg \
--MaxLabels 5 # Limit to top 5 labels
Sample Output (Success):
{
"Labels": [
{
"Name": "Cat",
"Confidence": 92.3f
},
{
"Name": "Living room",
"Confidence": 87.1f
},
{
"Name": "Couch",
"Confidence": 78.5f
},
{
"Name": "Rug",
"Confidence": 65.2f
},
{
"Name": "Table",
"Confidence": 59.8f
}
]
}
The output displays the top 5 identified labels (Cat
, Living room
, etc.) with their corresponding confidence scores.
2. Detect Faces in an S3 Object:
This command detects faces within an image stored in your S3 bucket.
aws rekognition detect-faces \
--image S3://your-bucket-name/images/group-photo.jpg
Sample Output (Success):
{
"FaceDetails": [
{
"BoundingBox": {
"Left": 0.25f,
"Top": 0.1f,
"Width": 0.2f,
"Height": 0.25f
},
"Confidence": 99.0f
},
{
"BoundingBox": {
"Left": 0.6f,
"Top": 0.3f,
"Width": 0.15f,
"Height": 0.2f
},
"Emotions": [
{
"Type": "Smile",
"Confidence": 82.7f
}
],
"Confidence": 98.5f
}
]
}
The output shows details for each detected face, including bounding box coordinates and confidence score. The second face also has an “Emotions” section with a detected “Smile” and its confidence level.
3. Image Moderation:
This command analyzes an image for inappropriate content according to pre-defined moderation labels.
aws rekognition detect-moderation-labels \
--image S3://your-bucket-name/images/image-to-check.jpg
Sample Output (Success):
{
"ModerationLabels": [
{
"Name": "Violence",
"Confidence": 10.0f # Low confidence (not likely violent)
},
{
"Name": "Suggestive",
"Confidence": 85.2f # High confidence (likely suggestive)
}
]
}
The output lists detected moderation labels with their confidence scores. In this case, “Suggestive” content is flagged with high confidence.
4. Celebrity Recognition:
This command identifies celebrities within an image.
aws rekognition recognize-celebrities \
--image S3://your-bucket-name/images/red-carpet-photo.jpg
Sample Output (Success):
{
"CelebrityFaces": [
{
"Name": "Actor1 Name",
"MatchConfidence": 98.7f,
"KnownGender": "Male",
"BoundingBox": {
"Left": 0.4f,
"Top": 0.2f,
"Width": 0.25f,
"Height": 0.3f
}
},
{
"Name": "Actress2 Name",
"MatchConfidence": 82.1f,
"KnownGender": "Female",
"BoundingBox": {
"Left": 0.1f,
"Top": 0.5f,
"Width": 0.15f,
"Height": 0.2f
}
}
]
}
The output displays details for recognized celebrities, including their names, confidence scores, genders, and bounding boxes within the image.
5. Text Detection in an Image:
This command detects and extracts text embedded within an image.
aws rekognition detect-text \
--image S3://your-bucket-name/images/document.jpg
Sample Output (Success):
{
"TextDetections": [
{
"DetectedText": "This is a sample document",
"Type": "LINE",
"Confidence": 95.3f,
"BoundingBox": {
"Left": 0.1f,
"Top": 0.3f,
"Width": 0.7f,
"Height": 0.1f
}
}
]
}
The output shows detected text along with its confidence score, type (e.g., LINE, WORD), and bounding box coordinates.
6. Comparing Faces:
This command compares two faces for similarity.
aws rekognition compare-faces \
--source-image S3://your-bucket-name/images/face1.jpg \
--target-image S3://your-bucket-name/images/face2.jpg \
--SimilarityThreshold 80.0f # Optional: Minimum similarity score for a match (0-100)
Sample Output (Success):
{
"FaceMatches": [
{
"Similarity": 92.7f,
"Face": {
"BoundingBox": {
"Left": 0.2f,
"Top": 0.1f,
"Width": 0.3f,
"Height": 0.4f
}
}
}
],
"UnmatchedFaces": [
{
"BoundingBox": {
"Left": 0.6f,
"Top": 0.3f,
"Width": 0.2f,
"Height": 0.25f
}
}
]
}
The output shows details of matching faces, including their similarity score and bounding box in the source image.
Important Notes:
- Replace placeholders like
path/to/your/image.jpg
andyour-bucket-name
with your actual values. - Ensure the local image file exists and is accessible by the user running the command.
- The S3 object must have appropriate permissions for Rekognition to access it.
AWS Rekognition Collections:
Sample Commands for Management
AWS Rekognition Collections allow you to group faces for image recognition tasks. Here are some sample commands using the AWS CLI to manage your collections:
1. Creating a Collection:
aws rekognition create-collection \
--collection-id my-collection-name
This command creates a new collection with the specified name (my-collection-name
). Replace it with your desired collection identifier.
2. Listing Collections:
aws rekognition list-collections
This command lists all the collections available in your AWS account. The output displays a JSON object with a “CollectionIds” array containing the IDs of your collections.
3. Describing a Collection:
aws rekognition describe-collection \
--collection-id my-collection-name
This command provides details about a specific collection, including its creation time and the number of faces it contains. Replace my-collection-name
with the ID of your collection.
4. Adding Faces to a Collection:
- There are two main ways to add faces to a collection using the AWS CLI:
- By Image S3 URL:
aws rekognition add-face-to-collection \ --image-id S3://your-bucket-name/images/person1.jpg \ --collection-id my-collection-name \ --external-image-id person1-id # Optional: Assign a unique ID to the image within the collection aws rekognition add-face-to-collection \ --image-id S3://your-bucket-name/images/person2.jpg \ --collection-id my-collection-name \ --external-image-id person2-id # Optional: Assign a unique ID to the image within the collection
These commands add faces from the specified S3 object URLs (person1.jpg
andperson2.jpg
) to the collection (my-collection-name
). You can optionally assign a unique identifier (person1-id
andperson2-id
) for each image within the collection using the--external-image-id
parameter.- By Detection in a New Image:
aws rekognition add-face-to-collection \ --image S3://your-bucket-name/images/group-photo.jpg \ --collection-id my-collection-name
This command analyzes the image (group-photo.jpg
) and attempts to detect all faces within it. It then adds those detected faces to the collection (my-collection-name
).
5. Searching Faces in a Collection:
aws rekognition search-faces-by-image \
--image S3://your-bucket-name/images/search-image.jpg \
--collection-id my-collection-name
This command searches for faces in a new image (search-image.jpg
) against the faces in your collection (my-collection-name
). The output displays details about matching faces, including their similarity score and bounding box within the search image.
6. Deleting a Collection:
aws rekognition delete-collection \
--collection-id my-collection-name
This command permanently deletes the specified collection (my-collection-name
) and all the faces associated with it. Use caution as this action cannot be reversed.
Remember to replace placeholders like my-collection-name
and image S3 URLs with your actual values.
By using these sample commands, you can effectively manage your Rekognition collections for various image recognition tasks. Refer to the AWS Rekognition documentation for a comprehensive list of commands and parameters: https://docs.aws.amazon.com/rekognition/