首先来解释下什么是内容审查器:Azure 内容审查器 API 是一项认知服务,用于检查文本、图像和视频中是否存在可能的冒犯性内容、危险内容或其余使人不适的内容。 找到此类内容时,此服务会将相应的标签(标记)应用到该内容。而后,应用会处理标记的内容,使之符合法规的要求,或者为用户维持一个理想的环境。git
根据这些特性,咱们可想而知,它的应用是十分普遍的,能够应用到社交通信平台的内容审查,媒体公司的内容审查,游戏公司的聊天室审查等等。github
如图所示,内容审查器服务包含多个能够经过 REST 调用和 .NET SDK 使用的 Web 服务 API。 它还包括人工审阅工具,让审核人员来协助服务改进或优化其审查功能。json
那下面咱们就使用C#调用内容审查服务的API接口来分析内容是否有【18禁】或是【冒犯性】的内容。c#
首先咱们须要在Azure平台上建立内容审查服务,获取API链接信息。windows
输入名称,选择位置和订价层,然就点击建立api
等待建立完成。app
接下来咱们须要编写一段C#代码,来调用Content Moderator API接口。ide
打开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