百度云内容审核

发布于:2024-05-19 ⋅ 阅读:(219) ⋅ 点赞:(0)

百度云内容审核介绍


百度智能云内容审核平台:是一款针对多媒体内容进行智能审核的服务平台。支持对图像、文本、音频、视频、直播等内容进行安全审核,具有精准的审核模型、丰富的审核维度、灵活的规则配置等特点。通过可视化界面选择审核维度、个性化调整松紧度,实现自动检测涉黄、辱骂、违禁、广告等内容,降低业务违规风险。

  • 官网地址:https://cloud.baidu.com/solution/censoring
  • 文档地址:https://cloud.baidu.com/doc/ANTIPORN/s/dkk6wyt3z
  • 产品价格介绍:https://ai.baidu.com/ai-doc/ANTIPORN/Xkp5jt5oc

使用步骤:

1、前往百度智能云官网,注册账号并完成实名认证:https://cloud.baidu.com/

2、打开控制台:https://console.bce.baidu.com/ai/#/ai/antiporn/overview/index ,按照操作指引先领取免费资源,然后创建应用。

image-20240513205021771

创建新应用:

image-20240514055802618

创建成功后,会自动生成一个AppID和两个秘钥,后续需要用到这些参数来调百度服务:

image-20240514060032020

3、测试

image-20240514060801802

image-20240514063047949


Java 示例代码


添加依赖:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.14.9</version>
</dependency>

编写工具类:

import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Base64;

public class BaiduScanUtil {
    private static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
    private static String apiKey = "你自己百度云的应用对应的API Key的值";
    private static String secretKey= "你自己百度云的应用对应的Secret Key的值";

    /**
     * 审核文本
     * @param text
     */
    public  static Integer textScan(String text){
        try {
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            RequestBody body = RequestBody.create(mediaType, "text="+text);
            Request request = new Request.Builder()
                    .url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + getAccessToken())
                    .method("POST", body)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .addHeader("Accept", "application/json")
                    .build();
            Response response = HTTP_CLIENT.newCall(request).execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
            //响应结果里conclusionType的四种可能值:1.合规,2.不合规,3.疑似,4.审核失败
            return JSON.parseObject(responseBody).getInteger("conclusionType");
        } catch (IOException e) {
            e.printStackTrace();
            return 4;
        }
    }

    /**
     * 获取文件base64编码
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(byte[] bytes) throws IOException {
        return URLEncoder.encode(Base64.getEncoder().encodeToString(bytes),"utf-8") ;
    }

    /**
     * 审核图片
     */
    public static Integer imageScan(byte[] bytes){
        try {
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            // image 可以通过 getFileContentAsBase64("C:\fakepath\3.png") 方法获取
            String base64 = getFileContentAsBase64(bytes);
            //System.out.println(base64);
            RequestBody body = RequestBody.create(mediaType, "image="+base64);
            Request request = new Request.Builder()
                    .url("https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=" + getAccessToken())
                    .method("POST", body)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .addHeader("Accept", "application/json")
                    .build();
            Response response = HTTP_CLIENT.newCall(request).execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
            //响应结果里conclusionType的四种可能值:1.合规,2.不合规,3.疑似,4.审核失败
            return JSON.parseObject(responseBody).getInteger("conclusionType");
        } catch (IOException e) {
            e.printStackTrace();
            return 4;
        }
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    private static String getAccessToken() throws IOException{
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + apiKey
                + "&client_secret=" + secretKey);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return JSON.parseObject(response.body().string()).getString("access_token");
    }
}

测试:

import cn.aopmin.utils.common.BaiduScanUtil;
import org.junit.jupiter.api.Test;

import javax.imageio.stream.FileImageInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class BaiduScanUtilTest {
    @Test
    void testTextScan() {
        Integer type = BaiduScanUtil.textScan("我是一个好人");
        if(type==1){
            System.out.println("内容合规");
        } else if(type==2){
            System.out.println("内容不合规");
        } else if(type==3){
            System.out.println("疑似不确定");
        }
    }

    /**
     * 测试图片审核
     */
    @Test
    public void testImageScan(){
        //1.先下载图片的字节数组
        byte[] bytes = image2byte("d:/1111/pic3.png"); //图片字节数组

        //2.再进行图片审核
        Integer type = BaiduScanUtil.imageScan(bytes);
        if(type==1){
            System.out.println("内容合规");
        } else if(type==2){
            System.out.println("内容不合规");
        } else if(type==3){
            System.out.println("疑似不确定");
        }
    }

    //图片到byte数组
    public byte[] image2byte(String path){
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        }
        catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        }
        catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到