前言
百度AI是指百度公司的人工智能技术全称。它采用深度学习技术,包括自然语言处理、语音识别、计算机视觉、知识图谱等,可应用于各个领域如互联网、医疗、金融、教育、汽车、物流等。百度AI的发展将帮助人类更好地理解世界和提高生活品质,接下来就通过一个小案例演示实现百度AI在文字和图像敏感审核应用。
项目准备
1.注册并登录百度智能云账号
官方地址:https://cloud.baidu.com/

2.完成个人或企业认证

3.进入控制台总览

4创建应用,申请BaiduAI官方的授权KEY


5.应用列表

项目搭建与配置
1.创建Winform客户端



2.Nuget安装BaiduAI包
NuGet\Install-Package Baidu.AI -Version 4.15.13
3.添加应用Key配置
文件:BaseConfig.json配置申请的应用信息- {
- "BaiduAI": {
- "AppId": "xxxx",//配置自己申请的
- "ApiKey": "xxxx",//配置自己申请的
- "SecretKey": "xxxx"//配置自己申请的
- }
- }
复制代码 项目代码实现
贴出实现功能的核心代码,整个项目的源码文末尾提供获取方式。
- BaiduAI帮助类
- namespace BaiduAIAuditClient.Helper
- {
- /// <summary>
- /// 百度AI识别帮助类
- /// </summary>
- public class BaiduAI
- {
- public static string APP_ID = RamData.Instance.BaseConfig.BaiduAI.AppId;
- public static string API_KEY = RamData.Instance.BaseConfig.BaiduAI.ApiKey;
- public static string SECRET_KEY = RamData.Instance.BaseConfig.BaiduAI.SecretKey ;
- /// <summary>
- /// 检测文本
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- public static BaiDuResult CheckedText(string text)
- {
- //判断是否审核为空文本
- if (string.IsNullOrEmpty(text)) { return new BaiDuResult(); }
- //初始化
- var client = new Baidu.Aip.ContentCensor.TextCensor(API_KEY, SECRET_KEY);
- client.Timeout = 60000; // 超时,毫秒
- var result = client.TextCensorUserDefined(text);
- var baiduResult = new BaiDuResult();
- if (result != null)
- {
- if (result["conclusionType"] == null || result["conclusionType"].ToString() == "1")
- {
- baiduResult.IsSuccess = true;
- }
- else
- {
- baiduResult.IsSuccess = false;
- if (result["data"][0]["msg"] != null)
- {
- baiduResult.Messge = result["data"][0]["msg"].ToString() + ";";
- }
- if (result["data"][0]["hits"][0]["words"] != null)
- {
- baiduResult.Messge += "\r\n敏感词:\r\n" + result["data"][0]["hits"][0]["words"].ToString() + "。";
- }
- }
- }
- return baiduResult;
- }
- /// <summary>
- /// 检测图片
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- public static BaiDuResult CheckedImage(byte[] image)
- {
- //判断是够为空
- if (image == null || image.Length == 0) { return new BaiDuResult(); }
- //初始化
- var client = new Baidu.Aip.ContentCensor.ImageCensor(API_KEY, SECRET_KEY);
- client.Timeout = 60000; // 超时,毫秒
- var result = client.UserDefined(image);
- var baiduResult = new BaiDuResult();
- if (result["conclusionType"].ToString() == "1")
- {
- baiduResult.IsSuccess = true;
- }
- else
- {
- baiduResult.IsSuccess = false;
- if (result["data"][0]["msg"] != null)
- {
- baiduResult.Messge = result["data"][0]["msg"].ToString() + ";";
- }
- }
- return baiduResult;
- }
- }
- /// <summary>
- /// 自定义返回实体
- /// </summary>
- public class BaiDuResult
- {
- /// <summary>
- /// 是否通过
- /// </summary>
- public bool IsSuccess { get; set; } = true;
- /// <summary>
- /// 消息
- /// </summary>
- public string Messge { get; set; }
- }
- }
复制代码 - 文本审核调用
- #region 文本审核
- /// <summary>
- /// 审核文本
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btAuditTxt_Click(object sender, EventArgs e)
- {
- try
- {
- if (string.IsNullOrEmpty(rtbAuditContent.Text))
- {
- MessageBox.Show("审核文本不能为空!","提示",MessageBoxButtons.OK);
- return;
- }
- BaiDuResult baiDuResult = BaiduAI.CheckedText(rtbAuditContent.Text);
- if (baiDuResult.IsSuccess)
- {
- rtbTxtAuditResult.ForeColor = Color.Green;
- rtbTxtAuditResult.Text = "文字审核通过!";
- }
- else
- {
- rtbTxtAuditResult.ForeColor = Color.Red;
- rtbTxtAuditResult.Text = baiDuResult.Messge;
- }
- }
- catch (Exception ex)
- {
- _logger.Error(ex.Message);
- }
- }
- #endregion
复制代码 - 图片审核调用
- #region 图片审核
- /// <summary>
- /// 浏览选择图片
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btBrowse_Click(object sender, EventArgs e)
- {
- try
- {
- //创建对象
- OpenFileDialog ofg = new OpenFileDialog();
- //设置默认打开路径,桌面
- ofg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- //设置打开标题、后缀
- ofg.Title = "请选择审核图片";
- ofg.Filter = "Png 图片|*.png|Jpg 图片|*.jpg|Jpeg 图片|*.jpeg|Bmp 图片|*.bmp|Gif 图片|*.gif";
- if (ofg.ShowDialog() == DialogResult.OK)
- {
- //得到打开的文件路径(包括文件名)
- txtImagePath.Text = ofg.FileName.ToString();
- Image img = Image.FromFile(ofg.FileName.ToString());
- this.picAuditImage.Image = img;
- }
- }
- catch (Exception ex)
- {
- _logger.Error(ex.Message);
- }
-
- }
- /// <summary>
- /// 审核图片
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btAuditImage_Click(object sender, EventArgs e)
- {
- try
- {
- var ImageBytes = ImageHelper.ImageToBytes(txtImagePath.Text);
- if (ImageBytes==null)
- {
- lbImageAuditResult.ForeColor = Color.Red;
- lbImageAuditResult.Text = "确认选择的文件是否正确";
- }
- BaiDuResult baiDuResult = BaiduAI.CheckedImage(ImageBytes);
- if (baiDuResult.IsSuccess)
- {
- lbImageAuditResult.ForeColor =Color.Green;
- lbImageAuditResult.Text = "图片审核通过!";
- }
- else
- {
- lbImageAuditResult.ForeColor = Color.Red;
- lbImageAuditResult.Text = baiDuResult.Messge;
- }
- }
- catch (Exception ex)
- {
- _logger.Error(ex.Message);
- }
- }
- #endregion
复制代码 演示调用
- 文字审核

- 图像审核

源码获取
关注公众号,后台回复关键字:BaiduAI文字和图像敏感审核
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |