基于百度AI做内容审核

先哭诉一下,百度提供的API和当前版本不对称,让在下头疼了一个多小时,在此作文为大家使用提供参考。

一、引入sdk

<!--百度Api-->
        <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>LATEST</version>
        </dependency>

二、初始化应用参数

    private static final String APP_ID = "xxxx";

    private static final String API_KEY = "xxxx";

    private static final String SECRET_KEY = "xxxx";

    /**图片内容审核客户端*/
    private static AipContentCensor contentCensorClient;
    /**
     * 初始化Client
     */
    private static void getClient() {

        //初始化图片审核客户端
        contentCensorClient = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
        contentCensorClient.setConnectionTimeoutInMillis(2000);
        contentCensorClient.setSocketTimeoutInMillis(60000);

        // 初始化一个AipOcr
        ocrClient = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        ocrClient.setConnectionTimeoutInMillis(2000);
        ocrClient.setSocketTimeoutInMillis(60000);
    }

三、审核文本内容

    /**
     * 识别文本中的敏感词汇(需要对百度API增强)
     *
     * @param text
     */
    public static boolean discernSensitiveWords(String text, HashMap<String, String> options)   {
        String resp;
        try {
            //防止出现特殊符号,制造异常
            resp = contentCensorClient.antiSpam(URLDecoder.decode(text, "UTF-8"), options).toString();
        } catch (UnsupportedEncodingException e) {
            return false;
        }
        //标识审核是否通过的结果所在未知
        int len = resp.lastIndexOf("m\":") + 3;
        String spam = resp.substring(len,len + 1);
        //System.out.println(spam);
        if(spam.equals("0")) {
            return true;
        }else {
            return false;
        }
    }

四、审核图形内容

 /**
     * 识别图中的敏感内容(需要对百度API增强)
     *
     * @param image
     */
    public static boolean discernSensitiveImage(byte[] image) throws IOException {

        // 参数为本地图片文件二进制数组
        String resp = contentCensorClient.imageCensorUserDefined(image, null).toString();
        //截取conclusionType的值
        String result = resp.substring(resp.length() - 2,resp.length()-1);
        if (result.equals("1")) {
            return true;
        }else  {
            return false;
        }

    }

五、测试

    public static void main(String[] args) throws IOException {
        //文本内容审核
        boolean result1 = discernSensitiveWords("共产党", null);
        System.out.println("文本内容是否通过:" + result1);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        //图片审核
        InputStream inputStream = FileUtils.getInputStream("/file/test.jpg");
        byte[] b = new byte[inputStream.available()];
        inputStream.read(b);
        boolean result2 = discernSensitiveImage(b);
        System.out.println("图片内容是否通过:" + result2);
    }

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值