检测不当的存储视频 - Amazon Rekognition

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

检测不当的存储视频

Amazon Rekognition Video 在存储视频中检测不当或冒犯性内容是一种异步操作。要开始检测不当或令人反感的内容,请致电StartContentModeration。Amazon Rekognition Video 会将视频分析的完成状态发布到 Amazon Simple Notification Service 主题。如果视频分析成功,请GetContentModeration致电获取分析结果。有关启动视频分析和获取结果的详细信息,请参阅调用 Amazon Rekognition Video 操作。有关亚马逊 Rekognition 中的审核标签列表,请参阅使用图片和视频审核。APIs

此过程在使用 Java 或 Python 分析存储在亚马逊 S3 存储桶中的视频 (SDK)(使用 Amazon Simple Queue Service 队列获取视频分析请求的完成状态)中的代码的基础上进行了扩展。

检测存储在 Amazon S3 存储段中的视频中的不当或令人反感的内容 (SDK)
  1. 执行使用 Java 或 Python 分析存储在亚马逊 S3 存储桶中的视频 (SDK)

  2. 将以下代码添加到您在步骤 1 中创建的类 VideoDetect

    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.) //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); }

    在函数 main 中,将以下行:

    StartLabelDetection(bucket, video); if (GetSQSMessageSuccess()==true) GetLabelDetectionResults();

    替换为:

    StartUnsafeContentDetection(bucket, video); if (GetSQSMessageSuccess()==true) GetUnsafeContentDetectionResults();
    Java V2

    此代码取自 AWS 文档SDK示例 GitHub 存储库。请在此处查看完整示例。

    import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.NotificationChannel; import software.amazon.awssdk.services.rekognition.model.S3Object; import software.amazon.awssdk.services.rekognition.model.Video; import software.amazon.awssdk.services.rekognition.model.StartContentModerationRequest; import software.amazon.awssdk.services.rekognition.model.StartContentModerationResponse; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import software.amazon.awssdk.services.rekognition.model.GetContentModerationResponse; import software.amazon.awssdk.services.rekognition.model.GetContentModerationRequest; import software.amazon.awssdk.services.rekognition.model.VideoMetadata; import software.amazon.awssdk.services.rekognition.model.ContentModerationDetection; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class VideoDetectInappropriate { private static String startJobId = ""; public static void main(String[] args) { final String usage = """ Usage: <bucket> <video> <topicArn> <roleArn> Where: bucket - The name of the bucket in which the video is located (for example, (for example, myBucket).\s video - The name of video (for example, people.mp4).\s topicArn - The ARN of the Amazon Simple Notification Service (Amazon SNS) topic.\s roleArn - The ARN of the AWS Identity and Access Management (IAM) role to use.\s """; if (args.length != 4) { System.out.println(usage); System.exit(1); } String bucket = args[0]; String video = args[1]; String topicArn = args[2]; String roleArn = args[3]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); NotificationChannel channel = NotificationChannel.builder() .snsTopicArn(topicArn) .roleArn(roleArn) .build(); startModerationDetection(rekClient, channel, bucket, video); getModResults(rekClient); System.out.println("This example is done!"); rekClient.close(); } public static void startModerationDetection(RekognitionClient rekClient, NotificationChannel channel, String bucket, String video) { try { S3Object s3Obj = S3Object.builder() .bucket(bucket) .name(video) .build(); Video vidOb = Video.builder() .s3Object(s3Obj) .build(); StartContentModerationRequest modDetectionRequest = StartContentModerationRequest.builder() .jobTag("Moderation") .notificationChannel(channel) .video(vidOb) .build(); StartContentModerationResponse startModDetectionResult = rekClient .startContentModeration(modDetectionRequest); startJobId = startModDetectionResult.jobId(); } catch (RekognitionException e) { System.out.println(e.getMessage()); System.exit(1); } } public static void getModResults(RekognitionClient rekClient) { try { String paginationToken = null; GetContentModerationResponse modDetectionResponse = null; boolean finished = false; String status; int yy = 0; do { if (modDetectionResponse != null) paginationToken = modDetectionResponse.nextToken(); GetContentModerationRequest modRequest = GetContentModerationRequest.builder() .jobId(startJobId) .nextToken(paginationToken) .maxResults(10) .build(); // Wait until the job succeeds. while (!finished) { modDetectionResponse = rekClient.getContentModeration(modRequest); status = modDetectionResponse.jobStatusAsString(); if (status.compareTo("SUCCEEDED") == 0) finished = true; else { System.out.println(yy + " status is: " + status); Thread.sleep(1000); } yy++; } finished = false; // Proceed when the job is done - otherwise VideoMetadata is null. VideoMetadata videoMetaData = modDetectionResponse.videoMetadata(); System.out.println("Format: " + videoMetaData.format()); System.out.println("Codec: " + videoMetaData.codec()); System.out.println("Duration: " + videoMetaData.durationMillis()); System.out.println("FrameRate: " + videoMetaData.frameRate()); System.out.println("Job"); List<ContentModerationDetection> mods = modDetectionResponse.moderationLabels(); for (ContentModerationDetection mod : mods) { long seconds = mod.timestamp() / 1000; System.out.print("Mod label: " + seconds + " "); System.out.println(mod.moderationLabel().toString()); System.out.println(); } } while (modDetectionResponse != null && modDetectionResponse.nextToken() != null); } catch (RekognitionException | InterruptedException e) { System.out.println(e.getMessage()); System.exit(1); } } }
    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.) # ============== Unsafe content =============== def StartUnsafeContent(self): response=self.rek.start_content_moderation(Video={'S3Object': {'Bucket': self.bucket, 'Name': self.video}}, NotificationChannel={'RoleArn': self.roleArn, 'SNSTopicArn': self.snsTopicArn}) self.startJobId=response['JobId'] print('Start Job Id: ' + self.startJobId) def GetUnsafeContentResults(self): maxResults = 10 paginationToken = '' finished = False while finished == False: response = self.rek.get_content_moderation(JobId=self.startJobId, MaxResults=maxResults, NextToken=paginationToken, SortBy="NAME", AggregateBy="TIMESTAMPS") print('Codec: ' + response['VideoMetadata']['Codec']) print('Duration: ' + str(response['VideoMetadata']['DurationMillis'])) print('Format: ' + response['VideoMetadata']['Format']) print('Frame rate: ' + str(response['VideoMetadata']['FrameRate'])) print() for contentModerationDetection in response['ModerationLabels']: print('Label: ' + str(contentModerationDetection['ModerationLabel']['Name'])) print('Confidence: ' + str(contentModerationDetection['ModerationLabel']['Confidence'])) print('Parent category: ' + str(contentModerationDetection['ModerationLabel']['ParentName'])) print('Timestamp: ' + str(contentModerationDetection['Timestamp'])) print() if 'NextToken' in response: paginationToken = response['NextToken'] else: finished = True

    在函数 main 中,将以下行:

    analyzer.StartLabelDetection() if analyzer.GetSQSMessageSuccess()==True: analyzer.GetLabelDetectionResults()

    替换为:

    analyzer.StartUnsafeContent() if analyzer.GetSQSMessageSuccess()==True: analyzer.GetUnsafeContentResults()
    注意

    如果您已经运行了除 使用 Java 或 Python 分析存储在亚马逊 S3 存储桶中的视频 (SDK) 之外的视频示例,则要替换的代码可能会有所不同。

  3. 运行该代码。将显示在视频中检测到的不当内容标签的列表。

GetContentModeration 操作响应

来自的响应GetContentModeration是一个由ContentModerationDetection对象组ModerationLabels成的数组。每当检测到不当内容标签时,该数组就包含一个元素。在ContentModerationDetectionObject对象中,ModerationLabel包含检测到的不当或令人反感内容的项目的信息。 Timestamp是检测到标签的时间,以视频开头以毫秒为单位。标签将按不当内容图像分析检测标签的同一方式分层组织。有关更多信息,请参阅 审核内容

以下是来自 GetContentModeration 的示例响应,按NAME排序,按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": "bucket-name", "Name": "video-name.mp4" } }, "GetRequestMetadata": { "SortBy": "TIMESTAMP", "AggregateBy": "TIMESTAMPS" } }

以下是来自 GetContentModeration 的示例响应,按NAME排序,按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": "bucket-name", "Name": "video-name.mp4" } }, "GetRequestMetadata": { "SortBy": "TIMESTAMP", "AggregateBy": "SEGMENTS" } }