利用Azure内容审查器审查违规内容(上) 荐

栏目: ASP.NET · 发布时间: 5年前

内容简介:首先来解释下什么是内容审查器:Azure 内容审查器 API 是一项认知服务,用于检查文本、图像和视频中是否存在可能的冒犯性内容、危险内容或其他令人不适的内容。 找到此类内容时,此服务会将相应的标签(标记)应用到该内容。然后,应用会处理标记的内容,使之符合法规的要求,或者为用户维持一个理想的环境。根据这些特性,我们可想而知,它的应用是十分广泛的,可以应用到社交通讯平台的内容审查,媒体公司的内容审查,游戏公司的聊天室审查等等。

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

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

利用Azure内容审查器审查违规内容(上) 荐

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

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

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

利用Azure内容审查器审查违规内容(上) 荐

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

利用Azure内容审查器审查违规内容(上) 荐

等待创建完成。

利用Azure内容审查器审查违规内容(上) 荐

接下来我们需要编写一段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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ME2.0

ME2.0

丹·斯柯伯尔 / 2011-11 / 36.00元

《Me2.0个人品牌崛起E时代》,本书介绍在信息技术飞速发展的今天,如何使用网络来营建个人形象,建立关系网,谋求理想的工作,完成商务交易。成功学与今天的网络通讯相结合,smart 原则与SWOT分析,视频网站、博客、社交网站、搜索引擎如何使用才能让你以直线方式走向成功等内容。一起来看看 《ME2.0》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具