Search in sources :

Example 1 with Instance

use of com.amazonaws.services.rekognition.model.Instance in project aws-doc-sdk-examples by awsdocs.

the class DetectLabels method main.

public static void main(String[] args) throws Exception {
    // Change bucket and photo to your S3 Bucket and image.
    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();
        }
    } catch (AmazonRekognitionException e) {
        e.printStackTrace();
    }
}
Also used : Instance(com.amazonaws.services.rekognition.model.Instance) Parent(com.amazonaws.services.rekognition.model.Parent) Label(com.amazonaws.services.rekognition.model.Label) Image(com.amazonaws.services.rekognition.model.Image) DetectLabelsResult(com.amazonaws.services.rekognition.model.DetectLabelsResult) AmazonRekognitionException(com.amazonaws.services.rekognition.model.AmazonRekognitionException) AmazonRekognition(com.amazonaws.services.rekognition.AmazonRekognition) DetectLabelsRequest(com.amazonaws.services.rekognition.model.DetectLabelsRequest) S3Object(com.amazonaws.services.rekognition.model.S3Object)

Example 2 with Instance

use of com.amazonaws.services.rekognition.model.Instance in project aws-doc-sdk-examples by awsdocs.

the class VideoDetect method GetResultsLabels.

// Gets the results of labels detection by calling GetLabelDetection. Label
// detection is started by a call to StartLabelDetection.
private static void GetResultsLabels() throws Exception {
    int maxResults = 10;
    String paginationToken = null;
    GetLabelDetectionResult labelDetectionResult = null;
    do {
        if (labelDetectionResult != null) {
            paginationToken = labelDetectionResult.getNextToken();
        }
        GetLabelDetectionRequest labelDetectionRequest = new GetLabelDetectionRequest().withJobId(startJobId).withSortBy(LabelDetectionSortBy.TIMESTAMP).withMaxResults(maxResults).withNextToken(paginationToken);
        labelDetectionResult = rek.getLabelDetection(labelDetectionRequest);
        VideoMetadata videoMetaData = labelDetectionResult.getVideoMetadata();
        System.out.println("Format: " + videoMetaData.getFormat());
        System.out.println("Codec: " + videoMetaData.getCodec());
        System.out.println("Duration: " + videoMetaData.getDurationMillis());
        System.out.println("FrameRate: " + videoMetaData.getFrameRate());
        // Show labels, confidence and detection times
        List<LabelDetection> detectedLabels = labelDetectionResult.getLabels();
        for (LabelDetection detectedLabel : detectedLabels) {
            long seconds = detectedLabel.getTimestamp();
            Label label = detectedLabel.getLabel();
            System.out.println("Millisecond: " + Long.toString(seconds) + " ");
            System.out.println("   Label:" + label.getName());
            System.out.println("   Confidence:" + detectedLabel.getLabel().getConfidence().toString());
            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();
        }
    } while (labelDetectionResult != null && labelDetectionResult.getNextToken() != null);
}
Also used : Instance(com.amazonaws.services.rekognition.model.Instance) Parent(com.amazonaws.services.rekognition.model.Parent) GetLabelDetectionRequest(com.amazonaws.services.rekognition.model.GetLabelDetectionRequest) Label(com.amazonaws.services.rekognition.model.Label) VideoMetadata(com.amazonaws.services.rekognition.model.VideoMetadata) GetLabelDetectionResult(com.amazonaws.services.rekognition.model.GetLabelDetectionResult) LabelDetection(com.amazonaws.services.rekognition.model.LabelDetection)

Example 3 with Instance

use of com.amazonaws.services.rekognition.model.Instance in project amplify-android by aws-amplify.

the class AWSRekognitionService method detectLabels.

private List<Label> detectLabels(ByteBuffer imageData) throws PredictionsException {
    DetectLabelsRequest request = new DetectLabelsRequest().withImage(new Image().withBytes(imageData));
    // Detect labels in the given image via Amazon Rekognition
    final DetectLabelsResult result;
    try {
        result = rekognition.detectLabels(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting labels.", serviceException, "See attached service exception for more details.");
    }
    List<Label> labels = new ArrayList<>();
    for (com.amazonaws.services.rekognition.model.Label rekognitionLabel : result.getLabels()) {
        List<String> parents = new ArrayList<>();
        for (Parent parent : rekognitionLabel.getParents()) {
            parents.add(parent.getName());
        }
        List<RectF> boxes = new ArrayList<>();
        for (Instance instance : rekognitionLabel.getInstances()) {
            boxes.add(RekognitionResultTransformers.fromBoundingBox(instance.getBoundingBox()));
        }
        Label amplifyLabel = Label.builder().value(rekognitionLabel.getName()).confidence(rekognitionLabel.getConfidence()).parentLabels(parents).boxes(boxes).build();
        labels.add(amplifyLabel);
    }
    return labels;
}
Also used : Parent(com.amazonaws.services.rekognition.model.Parent) Instance(com.amazonaws.services.rekognition.model.Instance) AmazonClientException(com.amazonaws.AmazonClientException) Label(com.amplifyframework.predictions.models.Label) ModerationLabel(com.amazonaws.services.rekognition.model.ModerationLabel) ArrayList(java.util.ArrayList) Image(com.amazonaws.services.rekognition.model.Image) DetectLabelsResult(com.amazonaws.services.rekognition.model.DetectLabelsResult) RectF(android.graphics.RectF) DetectLabelsRequest(com.amazonaws.services.rekognition.model.DetectLabelsRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Aggregations

Instance (com.amazonaws.services.rekognition.model.Instance)3 Parent (com.amazonaws.services.rekognition.model.Parent)3 DetectLabelsRequest (com.amazonaws.services.rekognition.model.DetectLabelsRequest)2 DetectLabelsResult (com.amazonaws.services.rekognition.model.DetectLabelsResult)2 Image (com.amazonaws.services.rekognition.model.Image)2 Label (com.amazonaws.services.rekognition.model.Label)2 RectF (android.graphics.RectF)1 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonRekognition (com.amazonaws.services.rekognition.AmazonRekognition)1 AmazonRekognitionException (com.amazonaws.services.rekognition.model.AmazonRekognitionException)1 GetLabelDetectionRequest (com.amazonaws.services.rekognition.model.GetLabelDetectionRequest)1 GetLabelDetectionResult (com.amazonaws.services.rekognition.model.GetLabelDetectionResult)1 LabelDetection (com.amazonaws.services.rekognition.model.LabelDetection)1 ModerationLabel (com.amazonaws.services.rekognition.model.ModerationLabel)1 S3Object (com.amazonaws.services.rekognition.model.S3Object)1 VideoMetadata (com.amazonaws.services.rekognition.model.VideoMetadata)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 Label (com.amplifyframework.predictions.models.Label)1 ArrayList (java.util.ArrayList)1