百度内容审核实现

百度内容审核平台
Baidu-AIP的java-sdk的guithub

流程

打开百度内容审核平台
在这里插入图片描述
在这里插入图片描述

一、创建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;

}

/**
 * 内容审核状态
 */
public enum ContentWithCensorStateEnum {
    /**
     * 正常状态
     */
    ADD,

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

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

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

    /**
     * Ai审核错误
     */
    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 {

    /**
     * 百度文本审核,识别审核结果的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 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);
        }

    }

}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,我可以帮你解答这个问题。 首先,你需要在百度AI平台上申请图像审核的API Key和Secret Key。 然后在Python中安装百度AI的Python SDK: ``` pip install baidu-aip ``` 接着,你需要使用Tkinter编写一个GUI界面,其中包括一个选择文件的按钮和一个文本框,用于显示审核结果。 当用户点击选择文件按钮后,可以使用文件对话框选择要审核的图片文件。 最后,你需要编写Python代码,将选中的图片文件上传到百度AI的图像审核API中进行审核,并将审核结果显示在文本框中。 以下是一个示例代码: ```python from tkinter import * from tkinter import filedialog from aip import AipContentCensor # 申请的API Key和Secret Key APP_ID = 'your_app_id' API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' # 创建AipContentCensor客户端 client = AipContentCensor(APP_ID, API_KEY, SECRET_KEY) # Tkinter GUI界面 root = Tk() # 选择文件按钮回调函数 def select_file(): # 打开文件对话框 file_path = filedialog.askopenfilename() # 将文件上传到百度AI的图像审核API中进行审核 with open(file_path, 'rb') as f: image = f.read() result = client.imageCensorUserDefined(image) # 将审核结果显示在文本框中 text.delete('1.0', END) text.insert(END, result) # 创建选择文件按钮 button = Button(root, text='选择文件', command=select_file) button.pack() # 创建文本框 text = Text(root) text.pack() root.mainloop() ``` 注意:这只是一个示例代码,你需要根据自己的需求进行修改和完善。同时,由于百度AI的图像审核API是收费的,所以在实际使用时需要根据自己的情况进行选择和付费。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值