首先来解释下什么是内容审查器:Azure 内容审查器 API 是一项认知服务,用于检查文本、图像和视频中是否存在可能的冒犯性内容、危险内容或其他令人不适的内容。 找到此类内容时,此服务会将相应的标签(标记)应用到该内容。然后,应用会处理标记的内容,使之符合法规的要求,或者为用户维持一个理想的环境。

根据这些特性,我们可想而知,它的应用是十分广泛的,可以应用到社交通讯平台的内容审查,媒体公司的内容审查,游戏公司的聊天室审查等等。

如图所示,内容审查器服务包含多个可以通过 REST 调用和 .NET SDK 使用的 Web 服务 API。 它还包括人工审阅工具,让审核人员来协助服务改进或优化其审查功能。

那下面我们就使用C#调用内容审查服务的API接口来分析内容是否有【18禁】或是【冒犯性】的内容。

首先我们需要在Azure平台上创建内容审查服务,获取API连接信息。

输入名称,选择位置和定价层,然就点击创建

等待创建完成。

接下来我们需要编写一段C#代码,来调用Content Moderator API接口。

打开Visual Studio,然后再Visual Studio中创建新的控制台应用(.NET Framework) 项目并将其命名为 ImageModeration。

然后使用NuGet安装以下包:

  • Microsoft.Azure.CognitiveServices.ContentModerator

  • Microsoft.Rest.ClientRuntime

  • Newtonsoft.Json

创建Content Moderator 客户端 ,注意这里只需要更新你的API所在的区域和APIkey

1. public static class Clients  2. {  3. private static readonly string AzureRegion = "YOUR API REGION";  4. private static readonly string AzureBaseURL =$"https://{AzureRegion}.api.cognitive.microsoft.com";  5. private static readonly string CMSubscriptionKey = "YOUR API KEY";  6. public static ContentModeratorClient NewClient()  7.     {  8.         ContentModeratorClient client = new ContentModeratorClient(new ApiKeyServiceClientCredentials(CMSubscriptionKey));  9.         client.Endpoint = AzureBaseURL;  10. return client;  11.     }  12. }

然后我们需要定义分析的源和输出的结果

这里我把分析的图片URL放入txt文档中

https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg

https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png

http://pic.pimg.tw/k110107632/1387547248-3785354604.jpg

代码如下:

1. //The name of the file that contains the image URLs to evaluate. 2. private static string ImageUrlFile = "ImageFiles.txt";  3. 4. ///The name of the file to contain the output from the evaluation. 5. private static string OutputFile = "ModerationOutput.json"; 接下来我们需要定义图像评估方法,这里我们定义三种(图像审查、文本分析和人脸识别) 1. // Evaluates an image using the Image Moderation APIs. 2. private static EvaluationData EvaluateImage(  3.   ContentModeratorClient client, string imageUrl)  4. {  5.     var url = new BodyModel("URL", imageUrl.Trim());  6. 7.     var imageData = new EvaluationData();  8. 9.     imageData.ImageUrl = url.Value;  10. 11. // Evaluate for adult and racy content. 12.     imageData.ImageModeration =  13.         client.ImageModeration.EvaluateUrlInput("application/json", url, true);  14.     Thread.Sleep(1000);  15. 16. // Detect and extract text. 17.     imageData.TextDetection =  18.         client.ImageModeration.OCRUrlInput("eng", "application/json", url, true);  19.     Thread.Sleep(1000);  20. 21. // Detect faces. 22.     imageData.FaceDetection =  23.         client.ImageModeration.FindFacesUrlInput("application/json", url, true);  24.     Thread.Sleep(1000);  25. 26. return imageData;  27. }

设定完成后我们就可以使用内容审查器分析图片内容了。最后会把结果输出到json文件中。

再下一篇中我们再详细分析输出的结果内容。

PS:完整的代码https://github.com/shibaoxi/AzureProject2019/tree/ContentModerator/ImageModeration/ImageModeration