AWS Code Sample
Catalog

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

rekognition-video-java-detect-labels-lambda.java

rekognition-video-java-detect-labels-lambda demonstrates how to detect labels in a video by using an AWS Lambda function.

/** * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ //Lambda function for detecting labels. For more information, see https://docs.aws.amazon.com/rekognition/latest/dg/stored-video-lambda.html. package com.amazonaws.lambda.demo; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.SNSEvent; import java.util.List; import com.amazonaws.regions.Regions; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.GetLabelDetectionRequest; import com.amazonaws.services.rekognition.model.GetLabelDetectionResult; import com.amazonaws.services.rekognition.model.LabelDetection; import com.amazonaws.services.rekognition.model.LabelDetectionSortBy; import com.amazonaws.services.rekognition.model.VideoMetadata; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JobCompletionHandler implements RequestHandler<SNSEvent, String> { @Override public String handleRequest(SNSEvent event, Context context) { String message = event.getRecords().get(0).getSNS().getMessage(); LambdaLogger logger = context.getLogger(); // Parse SNS event for analysis results. Log results try { ObjectMapper operationResultMapper = new ObjectMapper(); JsonNode jsonResultTree = operationResultMapper.readTree(message); logger.log("Rekognition Video Operation:========================="); logger.log("Job id: " + jsonResultTree.get("JobId")); logger.log("Status : " + jsonResultTree.get("Status")); logger.log("Job tag : " + jsonResultTree.get("JobTag")); logger.log("Operation : " + jsonResultTree.get("API")); if (jsonResultTree.get("API").asText().equals("StartLabelDetection")) { if (jsonResultTree.get("Status").asText().equals("SUCCEEDED")){ GetResultsLabels(jsonResultTree.get("JobId").asText(), context); } else{ String errorMessage = "Video analysis failed for job " + jsonResultTree.get("JobId") + "State " + jsonResultTree.get("Status"); throw new Exception(errorMessage); } } else logger.log("Operation not StartLabelDetection"); } catch (Exception e) { logger.log("Error: " + e.getMessage()); throw new RuntimeException (e); } return message; } 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); } }

Sample Details

Service: rekognition

Last tested: 2019-01-18

Author: reesch(AWS)

Type: snippet

On this page: