springboot项目整合百度AI内容审核(文本,图片) 您所在的位置:网站首页 百度图片处理api springboot项目整合百度AI内容审核(文本,图片)

springboot项目整合百度AI内容审核(文本,图片)

2024-07-12 19:02| 来源: 网络整理| 查看: 265

毕设项目做得差不多了,但功能上基本都是本地完成的,除了有一个支付功能以及图片上传的优化,其他貌似没有用到云的东西,显得过于单调,由于是社区型项目,用户发送文本以及上传图片的频率是十分高的,于是就打算利用百度的AI审核加入文本和图片违规检测功能。

首先导入maven依赖

com.baidu.aip java-sdk 4.12.0

这里版本我用的最新的4.12.0,官方文档里的是4.8.0。无论哪个版本,总会有些类和方法找不到,心累

初始化应用参数

public class BaiDuAiConfig { public static final String APP_ID ="11111"; public static final String API_KEY = "11111"; public static final String SECRET_KEY = "111111"; /*初始化客户端*/ public static final AipContentCensor client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY); // public static AipContentCensor getClient() { // // 初始化一个AipImageCensor // // // // 可选:设置网络连接参数 client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); // // // 可选:设置代理服务器地址, http和socket二选一,或者均不设置 client.setHttpProxy("proxy_host", proxy_port); // 设置http代理 client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理 // // // 可选:设置log4j日志输出格式,若不设置,则使用默认配置 // // 也可以直接通过jvm启动参数设置此环境变量 System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties"); // return client; // } }

官方文档里的可选的参数我都没配置。

里面的appid等填入自己对应的即可。

图片审核和文本审核方法

public class BaiDuAiCheck { /** *@Author: ZongMao on 2020/3/28 13:14 *图像审核功能 *@return */ public static JSONObject checkImg(MultipartFile file) throws IOException { // 参数为本地图片路径 // JSONObject response = BaiDuAiConfig.client.imageCensorUserDefined(imgPath, EImgType.FILE, null); // System.out.println(response.toString()); // 参数为url // String url = "http://testurl"; // response = client.imageCensorUserDefined(url, EImgType.URL, null); // System.out.println(response.toString()); // 参数为本地图片文件二进制数组 byte[] files = FileCopyUtils.copyToByteArray(file.getInputStream()); JSONObject response = BaiDuAiConfig.client.imageCensorUserDefined(files, null); System.out.println(response); return response; } /** *@Author: ZongMao on 2020/3/28 13:14 *文本审核功能 *@return */ public static JSONObject checkText(String text){ // 参数为输入文本 JSONObject response = BaiDuAiConfig.client.textCensorUserDefined(text); return response; } }

文本审核只需要传入文本参数即可。图片官方文档给了三种方法,第一种是本地url,服务端好像得不到?第二种是网络URL,但如果每次都上传好了再来审核,浪费资源,且速度太慢。另一种是二进制数组,但是官方的readImageFile函数找不到,我一直以为是我的操作出错,但应该是版本问题。然后百度了一下可以用FileCopyUtils.copyToByteArray(file.getInputStream())方法将流文件转换为二进制数组。

调用文本审核方法

@PostMapping("/addDiscuss") public JSONObject addDiscuss(DiscussInfo discussInfo, Chat chat, Integer blogId){ JSONObject jsonObject = new JSONObject(); org.json.JSONObject result = BaiDuAiCheck.checkText(discussInfo.getContent()); // System.out.println(result); if (!CheckSession.checkSession()||result.get("conclusion")==null){ jsonObject.put("code",2); }else if (result.get("conclusion").equals("合规")){ /*获取当前登录的用户id*/ Integer userId = GetUserId.getUserId(); String time = Now.getNowTime("yyyy-MM-dd HH:mm:ss"); discussInfo.setUserId(userId); discussInfo.setTime(time); discussRepositories.save(discussInfo); String account = userRepositories.findUserInfoById(userId).getAccount(); String msg = account+"评论了您的文章,"+"点此查看"; Integer receiver = blogInfoRepositories.findById(blogId).getUserId(); chat.setSender(13); chat.setReceiver(receiver); chat.setNews(msg); chat.setTime(time); chatRepositories.save(chat); WebSocket_Chat.sendToOne(13,receiver); jsonObject.put("code",1); }else{ jsonObject.put("code",3); } return jsonObject; }

最终效果:

调用图片审核方法

@PostMapping({"/upLoadImg/{type}"}) public JSONObject upLoadImg(@RequestParam("file") MultipartFile file, @PathVariable("type") String type) throws Exception{ JSONObject jsonObject = new JSONObject(); org.json.JSONObject result = BaiDuAiCheck.checkImg(file); // System.out.println(result); if (!CheckSession.checkSession()||result.get("conclusion")==null){ jsonObject.put("code",1); } else if (result.get("conclusion").equals("合规")){ JSONObject srcJson = new JSONObject(); srcJson.put("src",src); jsonObject.put("code",0); jsonObject.put("msg","上传成功"); jsonObject.put("data",srcJson); }else { jsonObject.put("code",3); jsonObject.put("msg","疑似或存在违规图片!请勿传播非法内容!"); } return jsonObject; }

效果:(用的官方演示图)

 如果项目启动时日志包冲突报红:https://blog.csdn.net/zongmaomx/article/details/105160372



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有