Search in sources :

Example 1 with Label

use of software.amazon.awssdk.services.rekognition.model.Label 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 Label

use of software.amazon.awssdk.services.rekognition.model.Label 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)

Example 3 with Label

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

the class VideoDetect method GetResultsLabels.

// Gets the job results by calling GetLabelDetection
private static void GetResultsLabels(RekognitionClient rekClient) {
    int maxResults = 10;
    String paginationToken = null;
    GetLabelDetectionResponse labelDetectionResult = null;
    try {
        do {
            if (labelDetectionResult != null)
                paginationToken = labelDetectionResult.nextToken();
            GetLabelDetectionRequest labelDetectionRequest = GetLabelDetectionRequest.builder().jobId(startJobId).sortBy(LabelDetectionSortBy.TIMESTAMP).maxResults(maxResults).nextToken(paginationToken).build();
            labelDetectionResult = rekClient.getLabelDetection(labelDetectionRequest);
            VideoMetadata videoMetaData = labelDetectionResult.videoMetadata();
            System.out.println("Format: " + videoMetaData.format());
            System.out.println("Codec: " + videoMetaData.codec());
            System.out.println("Duration: " + videoMetaData.durationMillis());
            System.out.println("FrameRate: " + videoMetaData.frameRate());
            List<LabelDetection> detectedLabels = labelDetectionResult.labels();
            for (LabelDetection detectedLabel : detectedLabels) {
                long seconds = detectedLabel.timestamp();
                Label label = detectedLabel.label();
                System.out.println("Millisecond: " + Long.toString(seconds) + " ");
                System.out.println("   Label:" + label.name());
                System.out.println("   Confidence:" + detectedLabel.label().confidence().toString());
                List<Instance> instances = label.instances();
                System.out.println("   Instances of " + label.name());
                if (instances.isEmpty()) {
                    System.out.println("        " + "None");
                } else {
                    for (Instance instance : instances) {
                        System.out.println("        Confidence: " + instance.confidence().toString());
                        System.out.println("        Bounding box: " + instance.boundingBox().toString());
                    }
                }
                System.out.println("   Parent labels for " + label.name() + ":");
                List<Parent> parents = label.parents();
                if (parents.isEmpty()) {
                    System.out.println("        None");
                } else {
                    for (Parent parent : parents) {
                        System.out.println("   " + parent.name());
                    }
                }
                System.out.println();
            }
        } while (labelDetectionResult != null && labelDetectionResult.nextToken() != null);
    } catch (RekognitionException e) {
        e.getMessage();
        System.exit(1);
    }
}
Also used : Instance(software.amazon.awssdk.services.rekognition.model.Instance) Parent(software.amazon.awssdk.services.rekognition.model.Parent) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) GetLabelDetectionRequest(software.amazon.awssdk.services.rekognition.model.GetLabelDetectionRequest) Label(software.amazon.awssdk.services.rekognition.model.Label) VideoMetadata(software.amazon.awssdk.services.rekognition.model.VideoMetadata) GetLabelDetectionResponse(software.amazon.awssdk.services.rekognition.model.GetLabelDetectionResponse) LabelDetection(software.amazon.awssdk.services.rekognition.model.LabelDetection)

Aggregations

Label (software.amazon.awssdk.services.rekognition.model.Label)3 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)3 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 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 GetLabelDetectionRequest (software.amazon.awssdk.services.rekognition.model.GetLabelDetectionRequest)1 GetLabelDetectionResponse (software.amazon.awssdk.services.rekognition.model.GetLabelDetectionResponse)1 Instance (software.amazon.awssdk.services.rekognition.model.Instance)1 LabelDetection (software.amazon.awssdk.services.rekognition.model.LabelDetection)1 Parent (software.amazon.awssdk.services.rekognition.model.Parent)1 S3Object (software.amazon.awssdk.services.rekognition.model.S3Object)1 VideoMetadata (software.amazon.awssdk.services.rekognition.model.VideoMetadata)1