Amazon Rekognition Video inappropriate or offensive content detection in stored videos is an asynchronous operation. To start detecting inappropriate or offensive content, call StartContentModeration. Amazon Rekognition Video publishes the completion status of the video analysis to an Amazon Simple Notification Service topic. If the video analysis is successful, call GetContentModeration to get the analysis results. For more information about starting video analysis and getting the results, see Calling Amazon Rekognition Video operations. For a list of moderation labels in Amazon Rekognition, see Using the image and video moderation APIs.
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 Simple Queue Service queue to get the completion status of a video analysis request.
To detect inappropriate or offensive content in a video stored in an Amazon S3 bucket (SDK)
Perform Analyzing a video stored in an Amazon S3 bucket with Java or Python (SDK).
Add the following code to the class
VideoDetect
that you created in step 1.//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.) //Content moderation ================================================================== private static void StartUnsafeContentDetection(String bucket, String video) throws Exception{ NotificationChannel channel= new NotificationChannel() .withSNSTopicArn(snsTopicArn) .withRoleArn(roleArn); StartContentModerationRequest req = new StartContentModerationRequest() .withVideo(new Video() .withS3Object(new S3Object() .withBucket(bucket) .withName(video))) .withNotificationChannel(channel); StartContentModerationResult startModerationLabelDetectionResult = rek.startContentModeration(req); startJobId=startModerationLabelDetectionResult.getJobId(); } private static void GetUnsafeContentDetectionResults() throws Exception{ int maxResults=10; String paginationToken=null; GetContentModerationResult moderationLabelDetectionResult =null; do{ if (moderationLabelDetectionResult !=null){ paginationToken = moderationLabelDetectionResult.getNextToken(); } moderationLabelDetectionResult = rek.getContentModeration( new GetContentModerationRequest() .withJobId(startJobId) .withNextToken(paginationToken) .withSortBy(ContentModerationSortBy.TIMESTAMP) .withMaxResults(maxResults)); VideoMetadata videoMetaData=moderationLabelDetectionResult.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 moderated content labels, confidence and detection times List<ContentModerationDetection> moderationLabelsInFrames= moderationLabelDetectionResult.getModerationLabels(); for (ContentModerationDetection label: moderationLabelsInFrames) { long seconds=label.getTimestamp()/1000; System.out.print("Sec: " + Long.toString(seconds)); System.out.println(label.getModerationLabel().toString()); System.out.println(); } } while (moderationLabelDetectionResult !=null && moderationLabelDetectionResult.getNextToken() != null); }
In the function
main
, replace the lines:StartLabelDetection(amzn-s3-demo-bucket, video); if (GetSQSMessageSuccess()==true) GetLabelDetectionResults();
with:
StartUnsafeContentDetection(amzn-s3-demo-bucket, video); if (GetSQSMessageSuccess()==true) GetUnsafeContentDetectionResults();
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 code to replace might be different.
Run the code. A list of inappropriate content labels detected in the video is shown.
GetContentModeration operation response
The response from GetContentModeration
is an array, ModerationLabels
, of ContentModerationDetection objects.
The array contains an element for each time an inappropriate content label is detected. Within a ContentModerationDetectionObject
object,
ModerationLabel contains information for a detected item of inappropriate or offensive content. Timestamp
is
the time, in milliseconds from the start of the video, when the label was detected. The labels are organized hierarchically in the same
manner as the labels detected by inappropriate content image analysis. For more information, see Moderating content.
The following is an example response from GetContentModeration
,
sorted by NAME
and aggregated by TIMESTAMPS
.
{
"JobStatus": "SUCCEEDED",
"VideoMetadata": {
"Codec": "h264",
"DurationMillis": 54100,
"Format": "QuickTime / MOV",
"FrameRate": 30.0,
"FrameHeight": 462,
"FrameWidth": 884,
"ColorRange": "LIMITED"
},
"ModerationLabels": [
{
"Timestamp": 36000,
"ModerationLabel": {
"Confidence": 52.451576232910156,
"Name": "Alcohol",
"ParentName": "",
"TaxonomyLevel": 1
},
"ContentTypes": [
{
"Confidence": 99.9999008178711,
"Name": "Animated"
}
]
},
{
"Timestamp": 36000,
"ModerationLabel": {
"Confidence": 52.451576232910156,
"Name": "Alcoholic Beverages",
"ParentName": "Alcohol",
"TaxonomyLevel": 2
},
"ContentTypes": [
{
"Confidence": 99.9999008178711,
"Name": "Animated"
}
]
}
],
"ModerationModelVersion": "7.0",
"JobId": "a1b2c3d4...",
"Video": {
"S3Object": {
"Bucket": "amzn-s3-demo-bucket",
"Name": "video-name.mp4"
}
},
"GetRequestMetadata": {
"SortBy": "TIMESTAMP",
"AggregateBy": "TIMESTAMPS"
}
}
The following is an example response from GetContentModeration
, sorted by
NAME
and aggregated by SEGMENTS
.
{
"JobStatus": "SUCCEEDED",
"VideoMetadata": {
"Codec": "h264",
"DurationMillis": 54100,
"Format": "QuickTime / MOV",
"FrameRate": 30.0,
"FrameHeight": 462,
"FrameWidth": 884,
"ColorRange": "LIMITED"
},
"ModerationLabels": [
{
"Timestamp": 0,
"ModerationLabel": {
"Confidence": 0.0003000000142492354,
"Name": "Alcohol Use",
"ParentName": "Alcohol",
"TaxonomyLevel": 2
},
"StartTimestampMillis": 0,
"EndTimestampMillis": 29520,
"DurationMillis": 29520,
"ContentTypes": [
{
"Confidence": 99.9999008178711,
"Name": "Illustrated"
},
{
"Confidence": 99.9999008178711,
"Name": "Animated"
}
]
}
],
"ModerationModelVersion": "7.0",
"JobId": "a1b2c3d4...",
"Video": {
"S3Object": {
"Bucket": "amzn-s3-demo-bucket",
"Name": "video-name.mp4"
}
},
"GetRequestMetadata": {
"SortBy": "TIMESTAMP",
"AggregateBy": "SEGMENTS"
}
}