Amazon Rekognition
Developer Guide

Exercise 1: Detect Labels in an Image (API)

In this exercise you use the DetectLabels API operation to detect objects, concepts, and scenes in an image (JPEG or PNG) that you provide as input. You can provide the input image as an image byte array (base64-encoded image bytes) or specify an S3 object. In this exercise you upload a JPEG image to your Amazon S3 bucket and specify the object key name.

You can test the operation using the AWS CLI or programmatically using the AWS SDK for Java.

  1. Upload an image (containing one or more objects, such as trees, houses, and boat etc.) to your S3 bucket. The exercise assumes a .jpg image. If you use .png, update the code accordingly.

    For instructions, see Uploading Objects into Amazon S3 in the Amazon Simple Storage Service Console User Guide.

  2. Use either the Java example code or the AWS CLI to test the DetectLabels operation.

    • Using the AWS CLI


      The command specifies the adminuser profile that you set up in Step 2: Set Up the AWS Command Line Interface (AWS CLI). Then, the AWS CLI command uses the credentials associated with the adminuser profile to sign and authenticate the request. If you don't provide this profile, the default profile is assumed.

      aws rekognition detect-labels \ --image '{"S3Object":{"Bucket":"bucketname","Name":"object.jpg"}}' \ --region us-east-1 \ --profile adminuser
    • Using the AWS SDK for Java.

      import; import; import com.amazonaws.AmazonClientException; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import; import; import; import; import; import; import java.util.List; public class DetectLabelsExample { public static void main(String[] args) throws Exception { String photo = "photo.jpg"; String bucket = "S3bucket"; AWSCredentials credentials; try { credentials = new ProfileCredentialsProvider("AdminUser").getCredentials(); } catch(Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/Users/userid/.aws/credentials), and is in a valid format.", e); } AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder .standard() .withRegion(Regions.US_WEST_2) .withCredentials(new AWSStaticCredentialsProvider(credentials)) .build(); DetectLabelsRequest request = new DetectLabelsRequest() .withImage(new Image() .withS3Object(new S3Object() .withName(photo).withBucket(bucket))) .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(); } } }

      You should get up to 10 labels with at least 75F confidence.

Next Exercise

Exercise 2: Detect Faces (API)