您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure 内容审查器审查电子商务产品图像Tutorial: Moderate e-commerce product images with Azure Content Moderator

本教程介绍如何使用 Azure 认知服务(包括内容审查器)有效分类和审查电子商务场景中的产品图像。In this tutorial, you will learn how to use Azure cognitive services, including Content Moderator, to effectively classify and moderate product images for an e-commerce scenario. 你将使用计算机视觉和自定义视觉向图像应用各种标记(标签),然后创建团队评审,以便将内容审查器的基于机器学习的技术与人工评审团队相结合,提供一个智能审查系统。You will use Computer Vision and Custom Vision to apply various tags (labels) to images, and then you will create a team review, which combines Content Moderator's machine-learning-based technologies with human review teams to provide an intelligent moderation system.

本教程演示如何:This tutorial shows you how to:

  • 注册内容审查器并创建评审团队。Sign up for Content Moderator and create a review team.
  • 使用内容审查器的图像 API 扫描可能出现的成人和不雅内容。Use Content Moderator's image API to scan for potential adult and racy content.
  • 使用计算机视觉服务扫描名人内容(或计算机视觉可检测到的其他标记)。Use the Computer Vision service to scan for celebrity content (or other Computer-Vision-detectable tags).
  • 使用自定义视觉服务扫描国旗、玩具和笔(或其他自定义标记)。Use the Custom Vision service to scan for the presence of flags, toys, and pens (or other custom tags).
  • 提供组合的扫描结果,供人工评审和最终决策。Present the combined scan results for human review and final decision making.

GitHub 上的电子商务目录审查示例存储库中提供了完整的示例代码。The complete sample code is available in the Samples eCommerce Catalog Moderation repository on GitHub.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don’t have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

  • 内容审查器的订阅密钥。A Content Moderator subscription key. 遵照创建认知服务帐户中的说明订阅内容审查器服务并获取密钥。Follow the instructions in Create a Cognitive Services account to subscribe to the Content Moderator service and get your key.
  • 计算机视觉订阅密钥(遵照上面相同的说明获取)。A Computer Vision subscription key (same instructions as above).
  • 任何版本的 Visual Studio 2015 或 2017Any edition of Visual Studio 2015 or 2017.
  • 为每个标签提供一组图像,供自定义视觉分类器使用(在本例中为玩具、笔和美国国旗)。A set of images for each label that the Custom Vision classifier will use (in this case toys, pens, and US flags).

创建评审团队Create a review team

请参阅在 Web 上试用内容审查器快速入门,获取有关如何注册内容审查器评审工具和创建评审团队的说明。Refer to the Try Content Moderator on the web quickstart for instructions on how to sign up for the Content Moderator Review tool and create a review team. 记下“凭据”页上的“团队 ID”值。Take note of the Team ID value on the Credentials page.

创建自定义审查标记Create custom moderation tags

接下来,在评审工具中创建自定义标记(在此过程中如需帮助,请参阅标记一文)。Next, create custom tags in the Review tool (refer to the Tags article if you need help with this process). 在本例中,我们将添加以下标记:“名人”、“美国”、“国旗”、“玩具”和“笔”。In this case, we will add the following tags: celebrity, USA, flag, toy, and pen. 请注意,并非所有标记都需要是计算机视觉中可检测到的类别(例如“名人”);可以添加自己的自定义标记,不过,在添加后,必须训练自定义视觉分类器才能检测到这些标记。Note that not all of the tags need to be detectable categories in Computer Vision (like celebrity); you can add your own custom tags as long as you train the Custom Vision classifier to detect them later on.

配置自定义标记

创建 Visual Studio 项目Create Visual Studio project

  1. 在 Visual Studio 中打开“新建项目”对话框。In Visual Studio, open the New Project dialog. 依次展开“已安装”、“Visual C#”,然后选择“控制台应用(.NET Framework)”。Expand Installed, then Visual C#, then select Console app (.NET Framework).
  2. 将应用程序命名为 EcommerceModeration,然后单击“确定”。Name the application EcommerceModeration, then click OK.
  3. 如果要将此项目添加到现有的解决方案,请将此项目选作单一启动项目。If you are adding this project to an existing solution, select this project as the single startup project.

本教程将突出显示项目的核心代码,但不会介绍所需的每个代码行。This tutorial will highlight the code that is central to the project, but it will not cover every line of code needed. 将示例项目(电子商务目录审查示例)中 Program.cs 的整个内容复制到新项目的 Program.cs 文件。Copy the full contents of Program.cs from the sample project (Samples eCommerce Catalog Moderation) into the Program.cs file of your new project. 然后,逐步完成以下各个部分,以了解项目的工作原理以及如何自行使用该项目。Then, step through the following sections to learn about how the project works and how to use it yourself.

定义 API 密钥和终结点Define API keys and endpoints

如前所述,本教程将使用三个认知服务;因此,需要三个对应的密钥和 API 终结点。As mentioned above, this tutorial uses three cognitive services; therefore, it requires three corresponding keys and API endpoints. 查看 Program 类中的以下字段:See the following fields in the Program class:

public const string ContentModeratorKey = "XXXXXXXXXXXXX";
public const string ComputerVisionKey = "XXXXXXXXXXXX";
public const string CustomVisionKey = "XXXXXXXXXXX";

// All your end points based on the new account and subscriptions
public const string ImageUri = "https://westus.api.cognitive.microsoft.com/contentmoderator/moderate/v1.0/ProcessImage/Evaluate";
public const string ReviewUri = "https://westus.api.cognitive.microsoft.com/contentmoderator/review/v1.0/teams/YOUR-TEAM-ID/reviews";
public const string ComputerVisionUri = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze?details=celebrities";
public const string CustomVisionUri = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Prediction/XXXXXXXXXXXXXXXX/url";

需要使用订阅密钥的值(稍后将获取 CustomVisionKey)更新 ___Key 字段,并且可能需要更改 ___Uri 字段,使之包含正确的区域标识符。You'll need to update the ___Key fields with the values of your subscription keys (you'll get the CustomVisionKey later on), and you may need to change the ___Uri fields so that they contain the correct region identifiers. ReviewUri 字段的 YOURTEAMID 部分填写之前创建的评审团队的 ID。Fill in the YOURTEAMID part of the ReviewUri field with the ID of the review team you created earlier. 稍后再填充 CustomVisionUri 字段的最后一个部分。You will fill in the final part of the CustomVisionUri field later on.

主要方法调用Primary method calls

查看 Main 方法中循环访问图像 URL 列表的以下代码。See the following code in the Main method, which loops through a list of image URLs. 该代码使用三个不同的服务分析每个图像,在 ReviewTags 数组中记录已应用的标记,然后为审查人员创建一个评审(将图像发送到内容审查器评审工具)。It analyzes each image with the three different services, records the applied tags in the ReviewTags array, and then creates a review for human moderators (sends the images to the Content Moderator Review tool). 以下部分将介绍这些方法。You will explore these methods in the following sections. 请注意,此处可以根据需要控制要发送哪些图像供评审,只需在条件语句中使用 ReviewTags 数组来检查已应用的标记即可。Note that here, if you wish, you can control which images are sent to review, using the ReviewTags array in a conditional statement to check which tags were applied.

// for each image URL in the file...
foreach (var Url in Urls)
{
    // Initiatize a new review tags array
    ReviewTags = new KeyValuePair[MAXTAGSCOUNT];

    // Evaluate for potential adult and racy content with Content Moderator API
    EvaluateAdultRacy(Url, ref ReviewTags);

    // Evaluate for potential presence of celebrity (ies) in images with Computer Vision API
    EvaluateComputerVisionTags(Url, ComputerVisionUri, ComputerVisionKey, ref ReviewTags);

    // Evaluate for potential presence of custom categories other than Marijuana
    EvaluateCustomVisionTags(Url, CustomVisionUri, CustomVisionKey, ref ReviewTags);

    // Create review in the Content Moderator review tool
    CreateReview(Url, ReviewTags);
}

EvaluateAdultRacy 方法EvaluateAdultRacy method

查看 Program 类中的 EvaluateAdultRacy 方法。See the EvaluateAdultRacy method in the Program class. 此方法将图像 URL 和键值对数组用作参数。This method takes an image URL and an array of key-value pairs as parameters. 它调用内容审查器的图像 API(使用 REST)获取图像的成人和不雅评分。It calls the Content Moderator's Image API (using REST) to get the Adult and Racy scores of the image. 如果评分大于 0.4(范围从 0 到 1),则将 ReviewTags 数组中的相应值设置为 TrueIf the score for either is greater than 0.4 (the range is from 0 to 1), it sets the corresponding value in the ReviewTags array to True.

/// <summary>
/// Use Content Moderator API to evaluate for potential adult and racy content
/// </summary>
/// <param name="ImageUrl"></param>
/// <param name="ReviewTags"></param>
/// <returns>API call success or not</returns>
public static bool EvaluateAdultRacy(string ImageUrl, ref KeyValuePair[] ReviewTags)
{
    float AdultScore = 0;
    float RacyScore = 0;

    var File = ImageUrl;
    string Body = $"{{\"DataRepresentation\":\"URL\",\"Value\":\"{File}\"}}";

    HttpResponseMessage response = CallAPI(ImageUri, ContentModeratorKey, CallType.POST,
                                           "Ocp-Apim-Subscription-Key", "application/json", "", Body);

    if (response.IsSuccessStatusCode)
    {
        // {“answers”:[{“answer”:“Hello”,“questions”:[“Hi”],“score”:100.0}]}
        // Parse the response body. Blocking!
        GetAdultRacyScores(response.Content.ReadAsStringAsync().Result, out AdultScore, out RacyScore);
    }

    ReviewTags[0] = new KeyValuePair();
    ReviewTags[0].Key = "a";
    ReviewTags[0].Value = "false";
    if (AdultScore > 0.4)
    {
        ReviewTags[0].Value = "true";
    }

    ReviewTags[1] = new KeyValuePair();
    ReviewTags[1].Key = "r";
    ReviewTags[1].Value = "false";
    if (RacyScore > 0.3)
    {
        ReviewTags[1].Value = "true";
    }
    return response.IsSuccessStatusCode;
}

EvaluateComputerVisionTags 方法EvaluateComputerVisionTags method

下一个方法采用图像 URL 和计算机视觉订阅信息,并分析图像中是否存在名人。The next method takes an image URL and your Computer Vision subscription information and analyzes the image for the presence of celebrities. 如果找到一个或多个名人,则将 ReviewTags 数组中的相应值设置为 TrueIf one or more celebrities are found, it sets the corresponding value in the ReviewTags array to True.

/// <summary>
/// Use Computer Vision API to evaluate for potential celebrity presence in image
/// </summary>
/// <param name="ImageUrl"></param>
/// <param name="ComputerVisionUri"></param>
/// <param name="ComputerVisionKey"></param>
/// <param name="ReviewTags"></param>
/// <returns>API call success or not</returns>
public static bool EvaluateComputerVisionTags(string ImageUrl, string ComputerVisionUri, string ComputerVisionKey, ref KeyValuePair[] ReviewTags)
{
    var File = ImageUrl;
    string Body = $"{{\"URL\":\"{File}\"}}";

    HttpResponseMessage Response = CallAPI(ComputerVisionUri, ComputerVisionKey, CallType.POST,
                                           "Ocp-Apim-Subscription-Key", "application/json", "", Body);

    if (Response.IsSuccessStatusCode)
    {
        ReviewTags[2] = new KeyValuePair();
        ReviewTags[2].Key = "cb";
        ReviewTags[2].Value = "false";

        ComputerVisionPrediction CVObject = JsonConvert.DeserializeObject<ComputerVisionPrediction>(Response.Content.ReadAsStringAsync().Result);

        if ((CVObject.categories[0].detail != null) && (CVObject.categories[0].detail.celebrities.Count() > 0))
        {                 
            ReviewTags[2].Value = "true";
        }
    }

    return Response.IsSuccessStatusCode;
}

EvaluateCustomVisionTags 方法EvaluateCustomVisionTags method

接下来查看 EvaluateCustomVisionTags 方法。该方法分类实际产品 — 在本例中为国旗、玩具和笔。Next, see the EvaluateCustomVisionTags method, which classifies the actual products—in this case flags, toys, and pens. 遵照如何生成分类器指南中的说明生成自己的自定义图像分类器,以检测图像中是否存在国旗、玩具和笔(或选作自定义标记的任何内容)。Follow the instructions in the How to build a classifier guide to build your own custom image classifier to detect the presence of flags, toys, and pens (or whatever you chose as your custom tags) in images. 可以使用 GitHub 存储库sample-images 文件夹中的图像快速训练此示例中的某些类别。You can use the images in the sample-images folder of the GitHub repo to quickly train some of the categories in this example.

包含笔、玩具和国旗训练图像的自定义视觉网页

训练分类器后,获取预测密钥和预测终结点 URL(检索这些信息时如需帮助,请参阅获取 URL 和预测密钥),并将这些值分别分配到 CustomVisionKeyCustomVisionUri 字段。Once you have trained your classifier, get the prediction key and prediction endpoint URL (see Get the URL and prediction key if you need help retrieving them), and assign these values to your CustomVisionKey and CustomVisionUri fields, respectively. 该方法使用这些值来查询分类器。The method uses these values to query the classifier. 如果分类器在图像中找到一个或多个自定义标记,此方法会将 ReviewTags 数组中的相应值设置为 TrueIf the classifier finds one or more of the custom tags in the image, this method sets the corresponding value(s) in the ReviewTags array to True.

/// <summary>
/// Use Custom Vision API to evaluate for potential content from custom-trained categories 
/// </summary>
/// <param name="ImageUrl"></param>
/// <param name="CustomVisionUri"></param>
/// <param name="CustomVisionKey"></param>
/// <param name="ReviewTags"></param>
/// <returns>API call success or not</returns>
 public static bool EvaluateCustomVisionTags(string ImageUrl, string CustomVisionUri, string CustomVisionKey, ref KeyValuePair[] ReviewTags)
 {
     var File = ImageUrl;
     string Body = $"{{\"URL\":\"{File}\"}}";

     HttpResponseMessage response = CallAPI(CustomVisionUri, CustomVisionKey, CallType.POST,
                                            "Prediction-Key", "application/json", "", Body);

     if (response.IsSuccessStatusCode)
     {
         // Parse the response body. Blocking!
         SaveCustomVisionTags(response.Content.ReadAsStringAsync().Result, ref ReviewTags);
     }

     return response.IsSuccessStatusCode;
 }

为评审工具创建评审Create reviews for Review tool

前面的部分介绍了可以扫描传入图像中的成人和不雅内容(内容审查器)、名人(计算机视觉)和其他各种对象(自定义视觉)的方法。In the previous sections, you noted the methods that scan incoming images for adult and racy content (Content Moderator), celebrities (Computer Vision) and various other objects (Custom Vision). 接下来了解 CreateReview 方法。该方法将图像及其中应用的所有标记(作为元数据传入)上传到内容审查器审阅工具,使之可供人工审阅。Next, see the CreateReview method, which uploads the images, with all of their applied tags (passed in as Metadata), to the Content Moderator Review tool so they can be available for human review.

/// <summary>
/// Call Content Moderator's Review API to create a review with the image (URL) and the review tags as inputs
/// </summary>
/// <param name="ImageUrl"></param>
/// <param name="Metadata"></param>
/// <returns>API call success or not</returns>
public static bool CreateReview(string ImageUrl, KeyValuePair[] Metadata)
{

    ReviewCreationRequest Review = new ReviewCreationRequest();
    Review.Item[0] = new ReviewItem();
    Review.Item[0].Content = ImageUrl;
    Review.Item[0].Metadata = new KeyValuePair[MAXTAGSCOUNT];
    Metadata.CopyTo(Review.Item[0].Metadata, 0);

    //SortReviewItems(ref Review);

    string Body = JsonConvert.SerializeObject(Review.Item);

    HttpResponseMessage response = CallAPI(ReviewUri, ContentModeratorKey, CallType.POST,
                                           "Ocp-Apim-Subscription-Key", "application/json", "", Body);

    return response.IsSuccessStatusCode;
}

图像将显示在内容审查器评审工具的“评审”选项卡中。The images will show up in the Review tab of the Content Moderator Review tool.

内容审查器评审工具的屏幕截图,其中包含多个图像及其突出显示的标记

提交测试图像列表Submit a list of test images

Main 方法中可以看到,此程序将查找“C:Test”目录,该目录中的 Urls.txt 文件包含图像 URL 列表。As you can see in the Main method, this program looks for a "C:Test" directory with a Urls.txt file that contains a list of image Urls. 请创建此类文件和目录,或者将路径更改为指向你的文本文件,并在此文件中填充要测试的图像的 URL。Create such a file and directory, or change the path to point to your text file, and populate this file with the URLs of images you'd like to test.

// Check for a test directory for a text file with the list of Image URLs to scan
var topdir = @"C:\test\";
var Urlsfile = topdir + "Urls.txt";

if (!Directory.Exists(topdir))
    return;

if (!File.Exists(Urlsfile))
{
    return;
}

// Read all image URLs in the file
var Urls = File.ReadLines(Urlsfile);

运行程序Run the program

如果已遵循上述所有步骤,该程序应会处理每个图像(在所有三个服务中查询其相关标记),然后将图像连同标记信息一起上传到内容审查器评审工具。If you have followed all of the above steps, the program should process each image (querying all three services for their relevant tags) and then upload the images with tag information to the Content Moderator Review tool.

后续步骤Next steps

在本教程中,你已设置一个程序用于分析产品图像,以便按产品类型对其进行标记,并使评审团队能够在内容审查方面做出明智的决策。In this tutorial, you set up a program to analyze product images for the purpose of tagging them by product type and allowing a review team to make informed decisions about content moderation. 接下来,请了解有关图像审查的详细信息。Next, learn more about the details of image moderation.