

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

# 使用 Amazon Augmented AI 审核不当内容
<a name="a2i-rekognition"></a>

通过 Amazon Augmented AI (Amazon A2I)，您可以构建人工审核机器学习预测所需的工作流程。

Amazon Rekognition 可直接与 Amazon A2I 集成，以便您轻松实施人工审核，检测出不安全图像的使用案例。Amazon A2I 提供了用于图像监管的人工审核工作流程。这样一来，您可以从 Amazon Rekognition 轻松审核预测。您可以为使用案例定义置信度阈值，并随着时间的推移进行调整。借助 Amazon A2I，您可以使用自己的组织或 Amazon Mechanical Turk 中的审核人员池。您还可以使用经 AWS 预先筛选的供应商队伍，以确保质量和遵守安全程序。

以下步骤将指导您如何设置 Amazon A2I 和 Amazon Rekognition。首先，使用 Amazon A2I 创建具有人工审核触发条件的流定义。然后，将流定义的 Amazon 资源名称 (ARN) 传递给 Amazon Rekognition `DetectModerationLabel` 操作。在 `DetectModerationLabel` 响应中，可以看到是否需要人工审核。人工审核的结果位于流定义设置的 Amazon S3 存储桶中。

*要查看如何将亚马逊 A2I 与 Amazon Rekognition 配合使用的端到端演示，请参阅亚马逊 AI 开发者指南中的以下教程之一。 SageMaker *
+ [演示：开始使用 Amazon A2I 控制台](https://docs.aws.amazon.com/sagemaker/latest/dg/a2i-get-started-console.html)
+ [演示：开始使用 Amazon A2I API](https://docs.aws.amazon.com/sagemaker/latest/dg/a2i-get-started-api.html)

  要开始使用 API，您还可以运行示例 Jupyter 笔记本。参见[将 SageMaker 笔记本实例与亚马逊 A2I Jupyter Notebook 配合使用，在人工智能笔记本](https://docs.aws.amazon.com/sagemaker/latest/dg/a2i-task-types-general.html#a2i-task-types-notebook-demo)[实例中使用与亚马逊 Rekognition [示例] 集成的笔记本亚马逊增强人工智能（亚马逊 A2I）[示例]](https://github.com/aws-samples/amazon-a2i-sample-jupyter-notebooks/blob/master/Amazon%20Augmented%20AI%20(A2I)%20and%20Rekognition%20DetectModerationLabels.ipynb)。 SageMaker 

**DetectModerationLabels 使用亚马逊 A2I 运行**
**注意**  
在同一 AWS 区域中创建所有 Amazon A2I 资源和 Amazon Rekognition 资源。

1. 完成 AI *文档*中 [Amazon Agumented AI 入门](https://docs.aws.amazon.com/sagemaker/latest/dg/a2i-getting-started.html)中列出的先决条件。SageMaker 

   此外，请记得按照 AI *文档中的 A [mazon Agumented AI 中的权限和安全](https://docs.aws.amazon.com/sagemaker/latest/dg/a2i-permissions-security.html)页面设置您的 I SageMaker A* M 权限。

1. 按照 *SageMaker AI 文档中有关创建人工*[审阅工作流程](https://docs.aws.amazon.com/sagemaker/latest/dg/create-human-review-console.html)的说明进行操作。

   人工审核工作流程用于管理图像的处理。它包含触发人工审核的条件、图像发送到的工作团队、工作团队使用的 UI 模板，以及工作团队的结果发送到的 Amazon S3 存储桶。

   在`CreateFlowDefinition`通话中，您需要将设置`HumanLoopRequestSource`为 “AWS/Rekognition/DetectModerationLabels/Image/V3”。之后，您需要决定希望如何设置人工审核的触发条件。

   Amazon Rekognition 为 `ConditionType` 提供了两个选项：`ModerationLabelConfidenceCheck` 和 `Sampling`。

   当审核标签的置信度在一定范围内时，`ModerationLabelConfidenceCheck` 会创建人工循环。最后，`Sampling` 会发送随机比例的已处理文档供人工审核。每个 `ConditionType` 会使用一组不同的 `ConditionParameters` 来设置人工审核中的结果。

   `ModerationLabelConfidenceCheck` 具有 `ConditionParameters` `ModerationLableName`，用于设置需要人工审核的键。此外，它还具有置信度，它设定了发送给人工审核的百分比范围，设置了 LessThan GreaterThan、和 Equals。 `Sampling``RandomSamplingPercentage`它设定了将要发送给人工审查的文档的百分比。

   以下代码示例是 `CreateFlowDefinition` 的部分调用。如果图像在标签“暗示”上的评级低于 98%，并且在标签“女性泳装或内衣”上的评级高于 95%，则该图像将发送到人工审核。这意味着，如果图像不被认为是暗示性的，但确实有穿着内衣或泳装的女性，您可以使用人工审核再次确认检查图像。

   ```
       def create_flow_definition():
       '''
       Creates a Flow Definition resource
   
       Returns:
       struct: FlowDefinitionArn
       '''
       humanLoopActivationConditions = json.dumps(
           {
               "Conditions": [
                   {
                     "And": [
                       {
                           "ConditionType": "ModerationLabelConfidenceCheck",
                           "ConditionParameters": {
                               "ModerationLabelName": "Suggestive",
                               "ConfidenceLessThan": 98
                           }
                       },
                       {
                           "ConditionType": "ModerationLabelConfidenceCheck",
                           "ConditionParameters": {
                               "ModerationLabelName": "Female Swimwear Or Underwear",
                               "ConfidenceGreaterThan": 95
                           }
                       }
                     ]
                  }
               ]
           }
       )
   ```

   `CreateFlowDefinition` 将返回 `FlowDefinitionArn`，您在下一步中调用 `DetectModerationLabels` 时可使用该 ARN。

   有关更多信息，请参阅 * SageMaker AI API 参考[CreateFlowDefinition](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateFlowDefinition.html)*中的。

1. 调用 `DetectModerationLabels` 时，请设置 `HumanLoopConfig` 参数，如[检测不当图像](procedure-moderate-images.md)中所述。有关带有 `HumanLoopConfig` 设置的 `DetectModerationLabels` 调用示例，请参阅步骤 4。

   1. 在 `HumanLoopConfig` 参数中，将 `FlowDefinitionArn` 设置为在步骤 2 中创建的流定义的 ARN。

   1. 设置 `HumanLoopName`。此名称在区域内应该是唯一的，并且必须采用小写。

   1. （可选）可以使用 `DataAttributes` 参数，设置传递到 Amazon Rekognition 的图像是否不包含个人身份信息。必须设置此参数才能将信息发送到 Amazon Mechanical Turk。

1. 运行 `DetectModerationLabels`。

   以下示例说明如何使用 AWS CLI 和在 s 适用于 Python (Boto3) 的 AWS SDK e `DetectModerationLabels` `HumanLoopConfig` t 中运行。

------
#### [ 适用于 Python (Boto3) 的 AWS SDK ]

   以下请求示例使用 SDK for Python (Boto3)。有关更多信息，请参阅**《AWS SDK for Python (Boto) API 参考》中的 [detect\_moderation\_labels](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.detect_moderation_labels)。

   ```
   import boto3
   
   rekognition = boto3.client("rekognition", aws-region)
   
   response = rekognition.detect_moderation_labels( \
           Image={'S3Object': {'Bucket': bucket_name, 'Name': image_name}}, \
           HumanLoopConfig={ \
               'HumanLoopName': 'human_loop_name', \
               'FlowDefinitionArn': , "arn:aws:sagemaker:aws-region:aws_account_number:flow-definition/flow_def_name" \
               'DataAttributes': {'ContentClassifiers': ['FreeOfPersonallyIdentifiableInformation','FreeOfAdultContent']}
            })
   ```

------
#### [ AWS CLI ]

   以下请求示例使用 AWS CLI。有关更多信息，请参阅[《AWS CLI 命令参考》](https://docs.aws.amazon.com/cli/latest/reference/)**中的 [detect-moderation-labels](https://docs.aws.amazon.com/cli/latest/reference/rekognition/detect-moderation-labels.html)。

   ```
   $ aws rekognition detect-moderation-labels \
       --image "S3Object={Bucket='{{bucket_name}}',Name='{{image_name}}'}" \
       --human-loop-config HumanLoopName="{{human_loop_name}}",FlowDefinitionArn="arn:aws:sagemaker:{{aws-region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}",DataAttributes='{ContentClassifiers=["FreeOfPersonallyIdentifiableInformation", "FreeOfAdultContent"]}'
   ```

   ```
   $ aws rekognition detect-moderation-labels \
       --image "S3Object={Bucket='{{bucket_name}}',Name='{{image_name}}'}" \
       --human-loop-config \
           '{"HumanLoopName": "{{human_loop_name}}", "FlowDefinitionArn": "arn:aws:sagemaker:{{aws-region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}", "DataAttributes": {"ContentClassifiers": ["FreeOfPersonallyIdentifiableInformation", "FreeOfAdultContent"]}}'
   ```

------

   当你在`HumanLoopConfig`启用状态下运行`DetectModerationLabels`时，Amazon Rekognition 会调用 AI API 操作 SageMaker 。`StartHumanLoop`此命令从 `DetectModerationLabels` 获取响应，并根据示例中的流定义条件对其进行检查。如果符合审核条件，则返回 `HumanLoopArn`。这表示您在流程定义中设置的工作团队成员现在可以审核该图像。调用 Amazon Augmented AI 运行时系统操作 `DescribeHumanLoop` 可提供有关循环的结果的信息。有关更多信息，请参阅 *Amazon A [ DescribeHumanLoop](https://docs.aws.amazon.com/augmented-ai/2019-11-07/APIReference/API_DescribeHumanLoop.html)gumented AI API 参考文档*。

   图像经审核后，可以在流定义输出路径中指定的存储桶中看到结果。审核完成后，亚马逊 A2I 还会通过亚马逊 CloudWatch 活动通知您。要查看要查找的事件，请参阅 *SageMaker AI 文档*中的[CloudWatch 事件](https://docs.aws.amazon.com/sagemaker/latest/dg/augmented-ai-cloudwatch-events.html)。

   有关更多信息，请参阅 A [I *文档中的 Amazon Agumented SageMaker AI* 入门](https://docs.aws.amazon.com/sagemaker/latest/dg/a2i-getting-started.html)。