Amazon Rekognition
Developer Guide

Recognizing Celebrities in a Stored Video

Amazon Rekognition Video celebrity recognition in stored videos is an asynchronous operation. To recognize celebrities in a stored video, use StartCelebrityRecognition to start video analysis. Amazon Rekognition Video publishes the completion status of the video analysis to an Amazon Simple Notification Service topic. If the video analysis is succesful, call GetCelebrityRecognition. to get the analysis results. For more information about starting video analysis and getting the results, see Calling Amazon Rekognition Video Operations.

This procedure expands on the code in Analyzing a Video Stored in an Amazon S3 Bucket with Java or Python (SDK), which uses an Amazon SQS queue to get the completion status of a video analysis request. To run this procedure, you need a video file that contains one or more celebrity faces.

To detect celebrities in a video stored in an Amazon S3 bucket (SDK)

  1. Perform Analyzing a Video Stored in an Amazon S3 Bucket with Java or Python (SDK).

  2. Add the following code to the class VideoDetect that you created in step 1.

    JavaPython
    Java
    //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) // Celebrities===================================================================== private static void StartCelebrities(String bucket, String video) throws Exception{ StartCelebrityRecognitionRequest req = new StartCelebrityRecognitionRequest() .withVideo(new Video() .withS3Object(new S3Object() .withBucket(bucket) .withName(video))) .withNotificationChannel(channel); StartCelebrityRecognitionResult startCelebrityRecognitionResult = rek.startCelebrityRecognition(req); startJobId=startCelebrityRecognitionResult.getJobId(); } private static void GetResultsCelebrities() throws Exception{ int maxResults=10; String paginationToken=null; GetCelebrityRecognitionResult celebrityRecognitionResult=null; do{ if (celebrityRecognitionResult !=null){ paginationToken = celebrityRecognitionResult.getNextToken(); } celebrityRecognitionResult = rek.getCelebrityRecognition(new GetCelebrityRecognitionRequest() .withJobId(startJobId) .withNextToken(paginationToken) .withSortBy(CelebrityRecognitionSortBy.TIMESTAMP) .withMaxResults(maxResults)); System.out.println("File info for page"); VideoMetadata videoMetaData=celebrityRecognitionResult.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()); System.out.println("Job"); System.out.println("Job status: " + celebrityRecognitionResult.getJobStatus()); //Show celebrities List<CelebrityRecognition> celebs= celebrityRecognitionResult.getCelebrities(); for (CelebrityRecognition celeb: celebs) { long seconds=celeb.getTimestamp()/1000; System.out.print("Sec: " + Long.toString(seconds) + " "); CelebrityDetail details=celeb.getCelebrity(); System.out.println("Name: " + details.getName()); System.out.println("Id: " + details.getId()); System.out.println(); } } while (celebrityRecognitionResult !=null && celebrityRecognitionResult.getNextToken() != null); }

    2a. In the function main, replace the line:

    StartLabels(bucket,video);

    with:

    StartCelebrities(bucket,video);

    2b. Replace the line:

    GetResultsLabels();

    with:

    GetResultsCelebrities();

    Python
    #Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. #PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) def GetResultsCelebrities(self, jobId): maxResults = 10 paginationToken = '' finished = False while finished == False: response = self.rek.get_celebrity_recognition(JobId=jobId, MaxResults=maxResults, NextToken=paginationToken) print(response['VideoMetadata']['Codec']) print(str(response['VideoMetadata']['DurationMillis'])) print(response['VideoMetadata']['Format']) print(response['VideoMetadata']['FrameRate']) for celebrityRecognition in response['Celebrities']: print('Celebrity: ' + str(celebrityRecognition['Celebrity']['Name'])) print('Timestamp: ' + str(celebrityRecognition['Timestamp'])) print() if 'NextToken' in response: paginationToken = response['NextToken'] else: finished = True

    2a. In the function main, replace the line:

    response = self.rek.start_label_detection(Video={'S3Object': {'Bucket': self.bucket, 'Name': self.video}}, NotificationChannel={'RoleArn': self.roleArn, 'SNSTopicArn': self.topicArn})

    with:

    response = self.rek.start_celebrity_recognition(Video={'S3Object':{'Bucket':self.bucket,'Name':self.video}}, NotificationChannel={'RoleArn':self.roleArn, 'SNSTopicArn':self.topicArn})

    2b. Replace the line:

    self.GetResultsLabels(rekMessage['JobId'])

    with:

    self.GetResultsCelebrities(rekMessage['JobId'])

    Note

    If you've already run a video example other than Analyzing a Video Stored in an Amazon S3 Bucket with Java or Python (SDK), the function name to replace is different.

  3. Run the code. Information about the celebrities recognized in the video is shown.

GetCelebrityRecognition Operation Response

The following is an example JSON response. The response includes the following:

  • Recognized celebritiesCelebrities is an array of celebrities and the times that they are recognized in a video. A CelebrityRecognition object exists for each time the celebrity is recognized in the video. Each CelebrityRecognition contains information about a recognized celebrity (CelebrityDetail) and the time (Timestamp) the celebrity was recognized in the video. Timestamp is measured in milliseconds from the start of the video.

  • CelebrityDetail – Contains information about a recognized celebrity. It includes the celebrity name (Name), identifier (ID), and a list of URLs pointing to related content (Urls). It also includes the bounding box for the celebrity's body, the confidence level that Amazon Rekognition Video has in the accuracy of the recognition, and details about the celebrity's face, FaceDetail. If you need to get the related content later, you can use ID with GetCelebrityInfo.

  • VideoMetadata – Information about the video that was analyzed.

{ "Celebrities": [ { "Celebrity": { "BoundingBox": { "Height": 0.8842592835426331, "Left": 0, "Top": 0.11574073880910873, "Width": 0.24427083134651184 }, "Confidence": 0.699999988079071, "Face": { "BoundingBox": { "Height": 0.20555555820465088, "Left": 0.029374999925494194, "Top": 0.22333332896232605, "Width": 0.11562500149011612 }, "Confidence": 99.89837646484375, "Landmarks": [ { "Type": "eyeLeft", "X": 0.06857934594154358, "Y": 0.30842265486717224 }, { "Type": "eyeRight", "X": 0.10396526008844376, "Y": 0.300625205039978 }, { "Type": "nose", "X": 0.0966852456331253, "Y": 0.34081998467445374 }, { "Type": "mouthLeft", "X": 0.075217105448246, "Y": 0.3811396062374115 }, { "Type": "mouthRight", "X": 0.10744428634643555, "Y": 0.37407416105270386 } ], "Pose": { "Pitch": -0.9784082174301147, "Roll": -8.808176040649414, "Yaw": 20.28228759765625 }, "Quality": { "Brightness": 43.312068939208984, "Sharpness": 99.9305191040039 } }, "Id": "XXXXXX", "Name": "Celeb A", "Urls": [] }, "Timestamp": 367 },...... ], "JobStatus": "SUCCEEDED", "NextToken": "XfXnZKiyMOGDhzBzYUhS5puM+g1IgezqFeYpv/H/+5noP/LmM57FitUAwSQ5D6G4AB/PNwolrw==", "VideoMetadata": { "Codec": "h264", "DurationMillis": 67301, "FileExtension": "mp4", "Format": "QuickTime / MOV", "FrameHeight": 1080, "FrameRate": 29.970029830932617, "FrameWidth": 1920 } }