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