Amazon Rekognition
Developer Guide

Analysing Images Stored in an Amazon S3 Bucket

Rekognition Image can analyze images stored in an Amazon S3 bucket or images supplied as image bytes.

In this topic you use the DetectLabels API operation to detect objects, concepts, and scenes in an image (JPEG or PNG) stored in an Amazon S3 bucket.

The following examples show how you can use the operation with the AWS CLI, the AWS SDK for Java and the AWS SDK for Python (Boto).

  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 the following example code to call 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(75F); 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(); } } }
    • Using AWS SDK for Python (Boto).

      import boto3 if __name__ == "__main__": fileName='input.jpg' bucket='rekognition-examples-bucket' client=boto3.client('rekognition','us-west-2') response = client.detect_labels(Image={'S3Object':{'Bucket':bucket,'Name':fileName}}) print('Detected labels for ' + fileName) for label in response['Labels']: print (label['Name'] + ' : ' + str(label['Confidence']))