Search in sources :

Example 1 with LabelDetection

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

the class JobCompletionHandler method GetResultsLabels.

void GetResultsLabels(String startJobId, Context context) throws Exception {
    LambdaLogger logger = context.getLogger();
    AmazonRekognition rek = AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
    int maxResults = 1000;
    String paginationToken = null;
    GetLabelDetectionResult labelDetectionResult = null;
    String labels = "";
    Integer labelsCount = 0;
    String label = "";
    String currentLabel = "";
    // Get label detection results and log them.
    do {
        GetLabelDetectionRequest labelDetectionRequest = new GetLabelDetectionRequest().withJobId(startJobId).withSortBy(LabelDetectionSortBy.NAME).withMaxResults(maxResults).withNextToken(paginationToken);
        labelDetectionResult = rek.getLabelDetection(labelDetectionRequest);
        paginationToken = labelDetectionResult.getNextToken();
        VideoMetadata videoMetaData = labelDetectionResult.getVideoMetadata();
        // Add labels to log
        List<LabelDetection> detectedLabels = labelDetectionResult.getLabels();
        for (LabelDetection detectedLabel : detectedLabels) {
            label = detectedLabel.getLabel().getName();
            if (label.equals(currentLabel)) {
                continue;
            }
            labels = labels + label + " / ";
            currentLabel = label;
            labelsCount++;
        }
    } while (labelDetectionResult != null && labelDetectionResult.getNextToken() != null);
    logger.log("Total number of labels : " + labelsCount);
    logger.log("labels : " + labels);
}
Also used : GetLabelDetectionRequest(com.amazonaws.services.rekognition.model.GetLabelDetectionRequest) AmazonRekognition(com.amazonaws.services.rekognition.AmazonRekognition) GetLabelDetectionResult(com.amazonaws.services.rekognition.model.GetLabelDetectionResult) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) VideoMetadata(com.amazonaws.services.rekognition.model.VideoMetadata) LabelDetection(com.amazonaws.services.rekognition.model.LabelDetection)

Example 2 with LabelDetection

use of com.amazonaws.services.rekognition.model.LabelDetection 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)

Aggregations

GetLabelDetectionRequest (com.amazonaws.services.rekognition.model.GetLabelDetectionRequest)2 GetLabelDetectionResult (com.amazonaws.services.rekognition.model.GetLabelDetectionResult)2 LabelDetection (com.amazonaws.services.rekognition.model.LabelDetection)2 VideoMetadata (com.amazonaws.services.rekognition.model.VideoMetadata)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 AmazonRekognition (com.amazonaws.services.rekognition.AmazonRekognition)1 Instance (com.amazonaws.services.rekognition.model.Instance)1 Label (com.amazonaws.services.rekognition.model.Label)1 Parent (com.amazonaws.services.rekognition.model.Parent)1