首先來解釋下什麼是內容審查器: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