文章目录
- 一.打开[百度内容审核平台])
- 二.创建AppID、API Key及Secret Key
- 三.构建百度内容审核客户端
- 1.pom中添加依赖
- 2.在yaml文件中配置你的AppID、API Key及Secret Key
- 3.构建百度内容审核客户端
- 四.使用百度云内容审核API
- 1.封装结果类
- 2.封装内容审核状态
- 3.service层,调用API
- 五.测试
最近要对用户输入的内容进行文本检查。不能输入一些敏感的内容、当时想着这个东西很简单、自己创建一个敏感词汇、使用过滤器对请求的内容进行过滤就好了。运行一段时间过后才发现、维护这个敏感词汇很是花力气。一些“名人”、“网络敏感词”、甚至敏感数字隔一段时间就冒出来了。等你维护进去的时候、你数据库早就有这些信息了。后面决定还是使用第三方的库算了。至少不用太多的维护就能预防绝大多数的敏感词。就算是有一些滞后了、也可以自己手动导入到第三库里面去。
而且百度云最近有活动,免费送新人一年的内容审核接口,上万的调用次数,所以选择百度云了。
一.打开百度内容审核平台
二.创建AppID、API Key及Secret Key
接入指南(获取百度内容审核需要用到的AppID、API Key及Secret Key) 创建后就可以得到AppID、API Key及Secret Key
三.构建百度内容审核客户端
内容审核平台快速入门
1.pom中添加依赖
<!--百度内容审核SDK-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.15.7</version>
</dependency>
2.在yaml文件中配置你的AppID、API Key及Secret Key
#百度内容审核
baidu:
examine:
#你的 App ID
AppID: xxx
#你的 Api Key
API_Key: xxx
#你的 Secret Key
Secret_Key: xxx
3.构建百度内容审核客户端
import com.baidu.aip.contentcensor.AipContentCensor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AipContentCensorClientConfig {
/**
* 百度云审核的AppID
*/
@Value("${baidu.examine.AppID}")
String AppID;
/**
* 百度云审核的Api Key
*/
@Value("${baidu.examine.API_Key}")
String API_Key;
/**
* 百度云审核的Secret Key
*/
@Value("${baidu.examine.Secret_Key}")
String Secret_Key;
@Bean(name = "commonTextCensorClient")
AipContentCensor commonTextCensorClient() {
/**
* 可以选择在客户端中添加参数,参考 https://ai.baidu.com/ai-doc/ANTIPORN/ik3h6xdze
* 如:
* // 可选:设置网络连接参数
* 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 new AipContentCensor(AppID, API_Key, Secret_Key);
}
}
四.使用百度云内容审核API
1.封装结果类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CensorResult {
/**
* 内容是否审核通过
*/
Boolean isPass;
/**
* 审核结果
*/
ContentWithCensorStateEnum contentWithCensorStateEnum;
/**
* 文字审核结果的Json字符串
*/
String textCensorJson;
/**
* 图片审核结果的Json字符串
*/
String imageCensorJson;
}
2.封装内容审核状态
/**
* 内容审核状态
*/
public enum ContentWithCensorStateEnum {
/**
* 正常状态
*/
ADD,
/**
* 删除状态
*/
REMOVE,
/**
* Ai审核不通过
*/
CENSOR_FAIL,
/**
* Ai审核疑似不通过
*/
CENSOR_SUSPECT,
/**
* Ai审核错误
*/
CENSOR_ERROR,
/**
* 人工审核不通过
*/
BLOCK
}
3.service层,调用API
里面仅有两个demo功能,常规文本审核和图片审核,如需更多功能参见 接口说明
package com.bus.jisou.wemedia.service.impl;
import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.bus.jisou.common.baiduAiScan.util.CensorResult;
import com.bus.jisou.common.baiduAiScan.util.ContentWithCensorStateEnum;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class BaiduContentCensorService {
/**
* 百度文本审核,识别审核结果的JSON KEY
*/
final public static String CENSOR_CONCLUSION_TYPE_KEY = "conclusionType";
@Resource(name = "commonTextCensorClient")
AipContentCensor commonTextCensorClient;
/**
* 获取常规文本审核结果
*
* @param content 内容
* @return 百度内容审核JSON
*/
public CensorResult getCommonTextCensorResult(String content) {
//如果内容为空,则直接返回
if (content == null || content.isEmpty()) {
return getCensorResult(null);
}
try {
JSONObject response = commonTextCensorClient.textCensorUserDefined(content);
return getCensorResult(response);
} catch (Exception exception) {
System.out.println(exception);
return getCensorResult(null);
}
}
/**
* 获取照片审核结果
*
* @param imageUrl 本地图片路径或图片url
* @return 百度图片审核JSON
*/
public CensorResult getImageCensorResult(String imageUrl) {
//如果内容为空,则直接返回
if (imageUrl == null || imageUrl.isEmpty()) {
return getCensorResult(null);
}
try {
JSONObject response = commonTextCensorClient.imageCensorUserDefined(imageUrl, EImgType.URL, null);
return getCensorResult(response);
} catch (Exception exception) {
System.out.println(exception);
return getCensorResult(null);
}
}
/**
* 获取照片审核结果
*
* @param imgData 图片二进制数据
* @return 百度图片审核JSON
*/
public CensorResult getImageCensorResult(byte[] imgData) {
//如果内容为空,则直接返回
if (imgData == null || imgData.length == 0) {
return getCensorResult(null);
}
try {
JSONObject response = commonTextCensorClient.imageCensorUserDefined(imgData, null);
return getCensorResult(response);
} catch (Exception exception) {
System.out.println(exception);
return getCensorResult(null);
}
}
/**
* 获取审核结果
*
* @param clientJsonObject 百度审核的JSON字段
* @return 审核结果
*/
private CensorResult getCensorResult(JSONObject clientJsonObject) {
//获取代表审核结果的字段
//审核结果类型,可取值1.合规,2.不合规,3.疑似,4.审核失败
int conclusionType;
//如果是null就直接判定为失败
if (clientJsonObject == null) {
conclusionType = 4;
} else {
conclusionType = clientJsonObject.getInt(CENSOR_CONCLUSION_TYPE_KEY);
}
try {
ContentWithCensorStateEnum result;
switch (conclusionType) {
case 1:
//合规情况
result = ContentWithCensorStateEnum.ADD;
break;
case 2:
//不合规情况
result = ContentWithCensorStateEnum.CENSOR_FAIL;
break;
case 3:
//疑似不合规
result = ContentWithCensorStateEnum.CENSOR_SUSPECT;
break;
default:
//审核失败和其他情况,都归结到censor_error上去
result = ContentWithCensorStateEnum.CENSOR_ERROR;
break;
}
//过审要求:只能是合规情况
//解释:因为百度云控制台是可以调节不合规和疑似不合规的参数值的,因此这里只写合规情况就可以了
boolean isPass = result == ContentWithCensorStateEnum.ADD;
return new CensorResult(isPass, result, clientJsonObject != null ? clientJsonObject.toString() : null, null);
} catch (Exception exception) {
System.out.println(exception);
//如果出错,就直接返回true
return new CensorResult(true, null, null, null);
}
}
}
五.测试
package com.bus.jisou.wemedia;
import com.bus.file.service.FileStorageService;
import com.bus.jisou.common.baiduAiScan.util.CensorResult;
import com.bus.jisou.wemedia.service.BaiduContentCensorService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.UnsupportedEncodingException;
@SpringBootTest(classes = WemediaApplication.class)
@RunWith(SpringRunner.class)
public class BaiduyunTest {
@Autowired
private com.bus.jisou.wemedia.service.BaiduContentCensorService BaiduContentCensorService;
@Autowired
private FileStorageService fileStorageService;
@Test
public void testContent() {
String content = "我们要热爱祖国热爱党";
CensorResult commonTextCensorResult = BaiduContentCensorService.getCommonTextCensorResult(content);
System.out.println(commonTextCensorResult);
}
@Test
public void testImage() throws UnsupportedEncodingException {
byte[] imgData = fileStorageService.downLoadFile("http://192.168.1.52:9000/leadnews/2023/03/13/0622ef2fc40b4fdaa0b747c6f7035ebb.jpeg");
CensorResult imageCensorResult = BaiduContentCensorService.getImageCensorResult(imgData);
System.out.println(imageCensorResult);
}
}
备注:
- 如果你们的图片是存放在本机上,则直接调用BaiduContentCensorService接口的第二个方法
2.图像审核请求说明
我是将图片存放在虚拟机上的minio(分布式文件系统),这里调用其地址,转化为byte数组,再调用第三个接口;你们也可以按照官方的示例,转换成byte数组,调用BaiduContentCensorService接口的第三个方法
default
CTRLIST
Rate-2-125707432-blog-126154930.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2
default
CTRLIST
Rate-2-125707432-blog-126154930.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=5