Search in sources :

Example 1 with DetectLabelsRequest

use of software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest in project aws-doc-sdk-examples by awsdocs.

the class DetectLabels method detectImageLabels.

// snippet-start:[rekognition.java2.detect_labels.main]
public static void detectImageLabels(RekognitionClient rekClient, String sourceImage) {
    try {
        InputStream sourceStream = new URL("https://images.unsplash.com/photo-1557456170-0cf4f4d0d362?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bGFrZXxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80").openStream();
        // InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        // Create an Image object for the source image.
        Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(souImage).maxLabels(10).build();
        DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
        List<Label> labels = labelsResponse.labels();
        System.out.println("Detected labels for the given photo");
        for (Label label : labels) {
            System.out.println(label.name() + ": " + label.confidence().toString());
        }
    } catch (RekognitionException | FileNotFoundException | MalformedURLException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Label(software.amazon.awssdk.services.rekognition.model.Label) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Image(software.amazon.awssdk.services.rekognition.model.Image) URL(java.net.URL) DetectLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) DetectLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest)

Example 2 with DetectLabelsRequest

use of software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest in project aws-doc-sdk-examples by awsdocs.

the class DetectLabelsS3 method getLabelsfromImage.

// snippet-start:[rekognition.java2.detect_labels_s3.main]
public static void getLabelsfromImage(RekognitionClient rekClient, String bucket, String image) {
    try {
        S3Object s3Object = S3Object.builder().bucket(bucket).name(image).build();
        Image myImage = Image.builder().s3Object(s3Object).build();
        DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(myImage).maxLabels(10).build();
        DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
        List<Label> labels = labelsResponse.labels();
        System.out.println("Detected labels for the given photo");
        for (Label label : labels) {
            System.out.println(label.name() + ": " + label.confidence().toString());
        }
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) Label(software.amazon.awssdk.services.rekognition.model.Label) DetectLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object) Image(software.amazon.awssdk.services.rekognition.model.Image) DetectLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse)

Aggregations

DetectLabelsRequest (software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest)2 DetectLabelsResponse (software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse)2 Image (software.amazon.awssdk.services.rekognition.model.Image)2 Label (software.amazon.awssdk.services.rekognition.model.Label)2 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 S3Object (software.amazon.awssdk.services.rekognition.model.S3Object)1