流程
打开
一、创建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 {
@Value("${baidu.examine.AppID}")
String AppID;
@Value("${baidu.examine.API_Key}")
String API_Key;
@Value("${baidu.examine.Secret_Key}")
String Secret_Key;
@Bean(name = "commonTextCensorClient")
AipContentCensor commonTextCensorClient() {
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;
String textCensorJson;
String imageCensorJson;
}
public enum ContentWithCensorStateEnum {
ADD,
REMOVE,
CENSOR_FAIL,
CENSOR_SUSPECT,
CENSOR_ERROR,
BLOCK
}
3.service层,调用API
里面仅有两个demo功能,常规文本审核和图片审核,如需更多功能参见
import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.xunan.baidu.pojo.CensorResult;
import com.xunan.baidu.pojo.ContentWithCensorStateEnum;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class BaiduContentCensorService {
final public static String CENSOR_CONCLUSION_TYPE_KEY = "conclusionType";
@Resource(name = "commonTextCensorClient")
AipContentCensor commonTextCensorClient;
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);
}
}
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);
}
}
private CensorResult getCensorResult(JSONObject clientJsonObject) {
int conclusionType;
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:
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);
return new CensorResult(true, null, null, null);
}
}
}