百度内容审核接口测试

百度内容审核接口测试

1、成为开发者

三步完成账号的基本注册与认证:

STEP1:点击百度AI开放平台导航右侧的控制台,选择需要使用的AI服务项。若为未登录状态,将跳转至登录界面,请您使用百度账号登录。如还未持有百度账户,可以点击此处注册百度账户
STEP2:首次使用,登录后将会进入开发者认证页面,请填写相关信息完成开发者认证。注:(如您之前已经是百度云用户或百度开发者中心用户,此步可略过)。
STEP3:通过控制台左侧导航,选择产品服务-人工智能,进入具体AI服务项的控制面板(如文字识别、人脸识别),进行相关业务操作。

2、创建应用

账号登录成功,您需要创建应用才可正式调用AI能力。应用是您调用API服务的基本操作单元,您可以基于应用创建成功后获取的API Key及Secret Key,进行接口调用操作,及相关配置。

image-20230621145436554

点击进入

image-20230621145551985

拿到需要的资料:

AppId:

351165xx

API Key:

vUPhwzKPAyYxgOxxx

Secret Key:

X6qYI3Vf43pjaELCIpXTHKvGxxxx

3、审核代码实现:

添加依赖:

<!-- 百度的api 依赖 -->
<dependency>
    <groupId>com.baidu.aip</groupId>
    <artifactId>java-sdk</artifactId>
    <version>4.8.0</version>
</dependency>

<!-- 网络请求的依赖 -->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.3</version>
</dependency>

图片审核代码:

public class Demo01 {
    
    

    public static final String API_KEY = "vUPhwzKPAyYxgOx7e8xxx";
    public static final String SECRET_KEY = "X6qYI3Vf43pjaELCIpXTHKvGxxx";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();


    public static void main(String []args) throws IOException{
    
    
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        // image 可以通过 getFileContentAsBase64("C:\fakepath\q.jpeg") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
        String path = "C:\\pic\\手枪.jpeg";
        String image = "image=" + getFileContentAsBase64(path, true);
        RequestBody body = RequestBody.create(image,mediaType);
        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();
        System.out.println(response.body().string());

    }

    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
    
    
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
    
    
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }


    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
    
    
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create("grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY,mediaType);
        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 new JSONObject(response.body().string()).getString("access_token");
    }
}

运行结果:

{
    
    
	"conclusion": "疑似",
	"log_id": 16873337141692598,
	"data": [{
    
    
		"msg": "疑似存在枪械不合规",
		"conclusion": "疑似",
		"probability": 0.88919306,
		"subType": 11,
		"conclusionType": 3,
		"type": 2
	}],
	"isHitMd5": false,
	"conclusionType": 3
}

文本审核代码:

public class Demo02 {
    
    

    public static final String API_KEY = "vUPhwzKPAyYxgOx7xxx";
    public static final String SECRET_KEY = "X6qYI3Vf43pjaELCIpXTHKvxxx";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static void main(String []args) throws IOException{
    
    

        String msg = "text=傻x";

        String decode = URLDecoder.decode(msg, "utf-8");
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(msg,mediaType);

        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();
        System.out.println(response.body().string());

    }


    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        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 new JSONObject(response.body().string()).getString("access_token");
    }
}

运行结果:

{
    
    
	"conclusion": "不合规",
	"log_id": 16873336469633031,
	"data": [{
    
    
		"msg": "存在低俗辱骂不合规",
		"conclusion": "不合规",
		"hits": [{
    
    
			"wordHitPositions": [{
    
    
				"positions": [
					[0, 1]
				],
				"label": "500100",
				"keyword": "傻x"
			}],
			"probability": 1.0,
			"datasetName": "百度默认文本反作弊库",
			"words": ["傻x"],
			"modelHitPositions": [
				[0, 1, 1.0]
			]
		}],
		"subType": 5,
		"conclusionType": 2,
		"type": 12
	}],
	"isHitMd5": false,
	"conclusionType": 2
}

猜你喜欢

转载自blog.csdn.net/hekai7217/article/details/131329455