基于java使用百度云进行文章图片内容审核

1. 内容安全第三方接口
1.1. 概述

内容安全是识别服务,支持对图片、视频、文本、语音等对象进行多样化场景检测,有效降低内容违规风险。

目前很多平台都支持内容检测,如阿里云、腾讯云、百度AI、网易云等国内大型互联网公司都对外提供了API。

按照性能和收费来看,黑马头条项目使用的就是阿里云的内容安全接口,使用到了图片和文本的审核。

1.2. 流程实现
  • 注册百度云,并实名认证
  • 领取免费的资源

  • 获得得应用列表可以得带 Appid,Api key,Secret key

  • Java客户端使用

(1) 导入依赖

<dependency>
  <groupId>com.baidu.aip</groupId>
  <artifactId>java-sdk</artifactId>
  <version>4.16.16</version>
</dependency>

(2) 在 application.yml中进行配置

baidu:
  examine:
    #你的 App ID
    AppID: 42471088
    #你的 Api Key
    API_Key: MVXlsg9xwiZH6HGyMSUcr2jG
    #你的 Secret Key
    Secret_Key: BaTNOLAgAlXrAU14L92VDs7lROsyggby

(3) 编写审核配置类

@Configuration
public class AipContentCensorClientConfig {

    @Value("${baidu.examine.AppID}")
    private String App_ID;

    @Value("${baidu.examine.API_Key}")
    private String API_Key;

    @Value("${baidu.examine.Secret_Key}")
    private 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(App_ID, API_Key, Secret_Key);
    }

}

(4)封装结果类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CensorResult {

    Boolean isPass;

    /**
     * 审核结果
     */
    ContentWithCensorStateEnum contentWithCensorStateEnum;

    /**
     * 文字审核结果的Json字符串
     */
    String textCensorJson;

    /**
     * 图片审核结果的Json字符串
     */
    String imageCensorJson;


}
public enum ContentWithCensorStateEnum  {

    /**
     * 正常状态
     */
    ADD,

    /**
     * 删除状态
     */
    REMOVE,

    /**
     * Ai审核不通过
     */
    CENSOR_FAIL,

    /**
     * Ai审核疑似不通过
     */
    CENSOR_SUSPECT,

    /**
     * Ai审核错误
     */
    CENSOR_ERROR,

    /**
     * 人工审核不通过
     */
    BLOCK


}

(5)审核结果

public enum CensorStatusEnum {
    /**
     * 百度文本审核,识别结果的的key 1.合规,2.不合规,3.疑似,4.审核失败
     */
    PASS(1, "合规"),
    REJECT(2, "不合规"),
    SUSPECT(2, "疑似"),
    REVIEW_FAIL(4, "审核失败");


    int code;
    String errorMessage;

    CensorStatusEnum(int code, String errorMessage){
        this.code = code;
        this.errorMessage = errorMessage;
    }

    public int getCode() {
        return code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}

(5)封装文本、图像审核工具类

@Component
@Slf4j
public class BaiduCensor {

    /**
     * 百度文本审核,识别结果的的key 1.合规,2.不合规,3.疑似,4.审核失败
     */
    final public static String CENSOR_CONCLUSION_TYPE_KET = "conclusionType";

    @Resource
    private AipContentCensor contentCensor;

    /**
     * 普通文本内容审核
     * @param content
     * @return
     */
    public CensorResult getCommonTextCensorResult(String content){
        if(content == null || content.isEmpty()){
            return getCensorResult(null);
        }
        try {
            JSONObject jsonObject = contentCensor.textCensorUserDefined(content);
            return getCensorResult(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return getCensorResult(null);
        }

    }

    /**
     * 图片内容审核
     * @param imageUrl
     * @return
     */
    public CensorResult getImageCensorResult(String imageUrl){
        if(imageUrl == null || imageUrl.isEmpty()){
            return getCensorResult(null);
        }
        try {
            JSONObject jsonObject = contentCensor.imageCensorUserDefined(imageUrl, EImgType.URL, null);
            return getCensorResult(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            log.error("图像审核失败");
            return getCensorResult(null);
        }
    }

    /**
     * 审核结果处理
     * @param clientJsonObject
     * @return
     */
    private CensorResult getCensorResult(JSONObject clientJsonObject){
        int conclusionType;

        if(clientJsonObject == null){
            conclusionType = CensorStatusEnum.REVIEW_FAIL.getCode();
        }else{
            conclusionType = clientJsonObject.getInt(CENSOR_CONCLUSION_TYPE_KET);
        }

        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;
                }

            boolean isPass = result == ContentWithCensorStateEnum.ADD;

            return new CensorResult(isPass, result, clientJsonObject != null ? clientJsonObject.toString():null, null);

        }catch (Exception e){
            return new CensorResult(true, null, null, null);
        }
    }
}
1.3. 测试
@SpringBootTest(classes = WemediaApplication.class)
public class WemediaApplicationTests {

    @Resource
    private BaiduCensor baiduCensor;

    @Test
    void contextLoads() {
        CensorResult result = baiduCensor.getCommonTextCensorResult("你好!!");
        System.out.println(result);
    }
    
}

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值