Amazon Rekognition Image operations can analyze images that are supplied as image bytes or images stored in an Amazon S3 bucket.
These topics provide examples of supplying image bytes to Amazon Rekognition Image API operations by
using a file loaded from a local file system. You pass image bytes to an Amazon Rekognition API
operation by using the Image input
parameter. Within Image
, you specify the Bytes
property to
pass base64-encoded image bytes.
Image bytes passed to an Amazon Rekognition API operation by using the Bytes
input parameter must be base64 encoded. The AWS SDKs that these examples use
automatically base64-encode images. You don't need to encode image bytes before calling
an Amazon Rekognition API operation. For more information, see Image specifications.
In this example JSON request for DetectLabels
, the source image bytes
are passed in the Bytes
input parameter.
{
"Image": {
"Bytes": "/9j/4AAQSk....."
},
"MaxLabels": 10,
"MinConfidence": 77
}
The following examples use various AWS SDKs and the AWS CLI to call
DetectLabels
. For information about the DetectLabels
operation response, see DetectLabels response.
For a client-side JavaScript example, see Using JavaScript.
To detect labels in a local image
If you haven't already:
Create or update a user with
AmazonRekognitionFullAccess
andAmazonS3ReadOnlyAccess
permissions. For more information, see Step 1: Set up an AWS account and create a User.Install and configure the AWS CLI and the AWS SDKs. For more information, see Step 2: Set up the AWS CLI and AWS SDKs.
Use the following examples to call the
DetectLabels
operation.The following Java example shows how to load an image from the local file system and detect labels by using the detectLabels
AWS SDK operation. Change the value of photo
to the path and file name of an image file (.jpg or .png format).//Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) package aws.example.rekognition.image; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.List; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.AmazonClientException; import com.amazonaws.services.rekognition.model.AmazonRekognitionException; import com.amazonaws.services.rekognition.model.DetectLabelsRequest; import com.amazonaws.services.rekognition.model.DetectLabelsResult; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.Label; import com.amazonaws.util.IOUtils; public class DetectLabelsLocalFile { public static void main(String[] args) throws Exception { String photo="input.jpg"; ByteBuffer imageBytes; try (InputStream inputStream = new FileInputStream(new File(photo))) { imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream)); } AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); DetectLabelsRequest request = new DetectLabelsRequest() .withImage(new Image() .withBytes(imageBytes)) .withMaxLabels(10) .withMinConfidence(77F); try { DetectLabelsResult result = rekognitionClient.detectLabels(request); List <Label> labels = result.getLabels(); System.out.println("Detected labels for " + photo); for (Label label: labels) { System.out.println(label.getName() + ": " + label.getConfidence().toString()); } } catch (AmazonRekognitionException e) { e.printStackTrace(); } } }