草庐IT

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

挥墨的书童 2023-03-28 原文
首先来解释下什么是内容审查器: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

有关利用Azure内容审查器审查违规内容(上)的更多相关文章

  1. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  2. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  3. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  4. ruby-on-rails - Ruby on Rails 可以部署在 Azure 网站上吗? - 2

    我可以在Azure网站上部署RubyonRails吗? 最佳答案 还没有。目前仅支持.NET和PHP。 关于ruby-on-rails-RubyonRails可以部署在Azure网站上吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/12964010/

  5. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  6. ruby - 如何在ruby中提取方括号内的内容 - 2

    我正在尝试提取方括号内的内容。到目前为止,我一直在使用它,它有效,但我想知道我是否可以直接在正则表达式中使用某些东西,而不是使用这个删除功能。a="Thisissuchagreatday[coolawesome]"a[/\[.*?\]/].delete('[]')#=>"coolawesome" 最佳答案 差不多。a="Thisissuchagreatday[coolawesome]"a[/\[(.*?)\]/,1]#=>"coolawesome"a[/(?"coolawesome"第一个依赖于提取组而不是完全匹配;第二个利用前瞻和

  7. ruby-on-rails - 如何找出拦截 'method_missing' 的内容 - 2

    使用Ruby1.8.6/Rails2.3.2我注意到在我的任何ActiveRecord模型类上调用的任何方法都返回nil而不是NoMethodError。除了烦人之外,这还破坏了动态查找器(find_by_name、find_by_id等),因为即使存在记录,它们也总是返回nil。不从ActiveRecord::Base派生的标准类不受影响。有没有办法追踪在ActiveRecord::Base之前拦截method_missing的是什么?更新:切换到1.8.7后,我发现(感谢@MichaelKohl)will_paginate插件首先处理method_missing。但是will_pa

  8. ruby-on-rails - 如何在 ActionController::TestCase 请求中设置内容类型 - 2

    我试图像这样在我的测试用例中执行获取:request.env['CONTENT_TYPE']='application/json'get:index,:application_name=>"Heka"虽然,它失败了:ActionView::MissingTemplate:Missingtemplatealarm_events/indexwith{:handlers=>[:builder,:haml,:erb,:rjs,:rhtml,:rxml],:locale=>[:en,:en],:formats=>[:html]尽管在我的Controller中我有:respond_to:html,

  9. ruby - 为 capybara 设置 app_host 的内容 - 2

    我的测试尝试访问网页并验证页面上是否存在某些元素。例如,它访问http://foo.com/homepage.html并检查Logo图像,然后访问http://bar.com/store/blah.html并检查页面上是否出现了某些文本。我的目标是访问经过Kerberos身份验证的网页。我发现Kerberos代码如下:主文件uri=URI.parse(Capybara.app_host)kerberos=Kerberos.new(uri.host)@kerberos_token=kerberos.encoded_tokenkerberos.rb文件classKerberosdefini

  10. ruby - gsub 删除第一个逗号前的所有内容 - 2

    我有这个字符串:auteur="comtedeFlandreetHainaut,Baudouin,Jacques,Thierry"我想删除第一个逗号之前的所有内容,即在这种情况下保留“Baudouin,Jacques,Thierry”试过这个:nom=auteur.gsub(/.*,/,'')但这会删除最后一个逗号之前的每个逗号,只保留“Thierry”。 最佳答案 auteur.partition(",").last#=>"Baudouin,Jacques,Thierry" 关于rub

随机推荐