背景
内容审核(Content Moderation),是基于图像、文本、音视频的检测技术,可自动检测涉黄、涉政涉暴、涉政敏感人物、图文违规等内容,对用户上传的图片、文字、音视频进行内容审核,以满足上传要求,帮助客户降低业务违规风险。
一般涉及到TO C的很多业务都会涉及到。
内容审核-图像
图像内容审核,利用深度神经网络模型对图片内容进行检测,准确识别图像中的涉政敏感人物、暴恐元素、涉黄内容等,帮助业务规避违规风险。
内容审核-文本
文本内容审核,采用人工智能文本检测技术有效识别涉黄、涉政、广告、辱骂、违禁品和灌水文本内容,提供定制化的文本敏感内容审核方案。
需求
最近因为项目中需要接入该功能,因为公司是使用华为云,所以特地找了华为云的内容审核服务。本来想着有sdk应该是三下五乘二的事情,结果看了文档
没错人家不和你玩.net,没办法只能是退而求其次,使用它的http api。
主要代码实现
获取token
/// <summary>/// /// </summary>/// <param name="username"></param>/// <param name="password"></param>/// <param name="domainName"></param>/// <param name="projectName"></param>/// <returns></returns>private static String getToken(String username, String password, String domainName, String projectName){var client = new RestClient("https://iam.myhuaweicloud.com/v3/auth/tokens");client.Timeout = -1;var request = new RestRequest(Method.POST);request.AddHeader("Content-Type", "application/json");var body = "{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\"" + username + "\",\"password\":\"" + password + "\",\"domain\":{\"name\":\"" + domainName + "\"}}}},\"scope\":{\"project\":{\"name\":\"" + projectName + "\"}}}}";request.AddParameter("application/json", body, ParameterType.RequestBody);ServicePointManager.ServerCertificateValidationCallback = ((a1, b1, c1, d1) => { return true; });ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | (SecurityProtocolType)3072;IRestResponse response = client.Execute(request);if (response.StatusCode == HttpStatusCode.Created){return response.Headers.FirstOrDefault(o => o.Name == "X-Subject-Token")?.Value.ToString()??null;}else{Console.WriteLine("err:" + response.Content);}return null;}
内容审核
private static bool requestModerationTextContentBase64(String token, String textModeration, String projectName){var client = new RestClient("https://moderation."+ projectName + ".myhuaweicloud.com/v1.0/moderation/text");client.Timeout = -1;var request = new RestRequest(Method.POST);request.AddHeader("X-Auth-Token", token);request.AddHeader("Content-Type", "application/json");ModerationTextContentBody model = new ModerationTextContentBody();List<string> list = new List<string>{"ad","politics","abuse","porn","contraband","flood"};model.categories.AddRange(list);model.items.Add(new ItemsItem() { type= "content", text= textModeration });var body = JsonConvert.SerializeObject(model);//"{\"categories\":[\"ad\",\"politics\",\"abuse\",\"porn\",\"contraband\",\"flood\"],\"items\":[{\"text\":\""+ textModeration + "\",\"type\":\"content\"}]}";request.AddParameter("application/json", body, ParameterType.RequestBody);IRestResponse response = client.Execute(request);Console.WriteLine(response.Content);if (response.StatusCode == HttpStatusCode.OK){var r= JsonConvert.DeserializeObject<ResponseModerationTextContent>(response.Content); if(r.result.suggestion== "pass"){return true;}}else{Console.WriteLine("err:" + response.Content);}return false;}
调用
String token = getToken(username, password, domainName, projectName);// Console.WriteLine(token);string textModeration = @"呜呜呜呜呜";var result= requestModerationTextContentBase64(token, textModeration, projectName);