2019-01-21 09:25:52 -08:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-01-16 10:41:11 -05:00
2019-01-21 09:25:52 -08:00
// snippet-start:[rekognition.java.rekognition-image-java-detect-labels.complete]
2019-01-17 17:03:17 -08:00
2019-01-22 12:36:04 -08:00
package aws.example.rekognition.image ;
2024-01-16 10:41:11 -05:00
2019-01-17 17:03:17 -08:00
import java.util.List ;
import com.amazonaws.services.rekognition.model.BoundingBox ;
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.Instance ;
import com.amazonaws.services.rekognition.model.Label ;
import com.amazonaws.services.rekognition.model.Parent ;
import com.amazonaws.services.rekognition.model.S3Object ;
import com.amazonaws.services.rekognition.AmazonRekognition ;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder ;
import com.amazonaws.services.rekognition.model.AmazonRekognitionException ;
public class DetectLabels {
public static void main ( String [ ] args ) throws Exception {
2019-01-22 12:36:04 -08:00
// Change bucket and photo to your S3 Bucket and image.
2019-01-17 17:03:17 -08:00
String photo = " photo " ;
String bucket = " bucket " ;
AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder . defaultClient ( ) ;
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 + " \ n " ) ;
for ( Label label : labels ) {
System . out . println ( " Label: " + label . getName ( ) ) ;
System . out . println ( " Confidence: " + label . getConfidence ( ) . toString ( ) + " \ n " ) ;
List < Instance > instances = label . getInstances ( ) ;
System . out . println ( " Instances of " + label . getName ( ) ) ;
if ( instances . isEmpty ( ) ) {
System . out . println ( " " + " None " ) ;
} else {
for ( Instance instance : instances ) {
System . out . println ( " Confidence: " + instance . getConfidence ( ) . toString ( ) ) ;
System . out . println ( " Bounding box: " + instance . getBoundingBox ( ) . toString ( ) ) ;
}
}
System . out . println ( " Parent labels for " + label . getName ( ) + " : " ) ;
List < Parent > parents = label . getParents ( ) ;
if ( parents . isEmpty ( ) ) {
System . out . println ( " None " ) ;
} else {
for ( Parent parent : parents ) {
System . out . println ( " " + parent . getName ( ) ) ;
}
}
System . out . println ( " -------------------- " ) ;
System . out . println ( ) ;
2024-01-16 10:41:11 -05:00
2019-01-17 17:03:17 -08:00
}
} catch ( AmazonRekognitionException e ) {
e . printStackTrace ( ) ;
}
}
2019-01-21 09:25:52 -08:00
}
// snippet-end:[rekognition.java.rekognition-image-java-detect-labels.complete]