Baidu content review interface test

Baidu content review interface test

1. Become a developer

Three steps to complete the basic registration and authentication of the account:

STEP1 : Click the console on the right side of Baidu AI Open Platform Navigation , and select the AI ​​service item to be used. If you are not logged in, you will be redirected to the login interface, please log in with your Baidu account. If you do not have a Baidu account yet, you can click here to register for a Baidu account .
STEP2 : For the first time use, after logging in, you will enter the developer certification page, please fill in the relevant information to complete the developer certification. Note: (If you are already a Baidu Cloud user or Baidu Developer Center user, this step can be skipped).
STEP3 : Through the navigation on the left side of the console, select Product Service-Artificial Intelligence, enter the control panel of specific AI service items (such as text recognition, face recognition), and perform related business operations.

2. Create an application

The account login is successful, and you need to create an application to officially call the AI ​​capability. An application is the basic operation unit for you to call API services. You can perform interface call operations and related configurations based on the API Key and Secret Key obtained after the application is successfully created.

image-20230621145436554

click to enter

image-20230621145551985

Get the required information:

AppId:

351165xx

API Key:

vUPhwzKPAyYxgOxxx

Secret Key:

X6qYI3Vf43pjaELCIpXTHKvGxxxx

3. Audit code implementation:

Add dependencies:

<!-- 百度的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>

Image review code:

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

operation result:

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

Text review code:

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

operation result:

{
    
    
	"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
}

Guess you like

Origin blog.csdn.net/hekai7217/article/details/131329455