ocr learning part 2: java realizes image and text recognition through the interface provided by Baidu AI

Let's talk about it briefly: the first time I contacted the platform provided by Baidu AI, I looked at it briefly, liked it very much, and provided many practical functions. What we use here is the function of character recognition. Its usage is very simple, and it can be realized by basic http call,

There are two ways to call based on character recognition

1. Call in API mode, which is similar to the way we usually request other interfaces. Send some simple verification through http and pass in the necessary conditions, and then it's ok.

2.sdk mode call, which is easier to use with the examples and documentation provided by it.

reference material: https://blog.csdn.net/weixin_40165004/article/details/82632229

This is a brief introduction to the use process. It's clear what's written in the reference blog. Here's just a simple implementation according to the AI instruction document

a. First, you need to register a baidu account to log in

b, in accordance with The API access guide is a step-by-step operation with detailed and simple documents.

Note: to use the sdk mode, you only need to download the corresponding sdk package on the platform, extract and copy the jar file to the project, and then you can develop and use it according to the specifications.

Attached is an implementation code I made here (the api only realizes access_token acquisition, base64 processing of pictures and urlencode processing for interface access, which is not implemented here). The sdk method is simpler here.

Code structure:

 getToken.java

package com.baidu.api.ocr;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionParams;

public class GetToken {

	public static void main(String[] args) {
		String aKey = "jNEhoBwPhbUOsVxnQBrcnBZB";
		String skey = "5tXebMWoEdGDnjGGkL3BEDrhmwaNqnRr";
		String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+aKey+"&client_secret="+skey;
		HttpClient httpClient = null;
		GetMethod getMethod = null;

		try{
			httpClient = new HttpClient();
			httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 60 * 1000);
			httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 60 * 1000);
			
			getMethod = new GetMethod(url);
			httpClient.executeMethod(getMethod);

			if (getMethod != null && getMethod.getStatusCode() == 200) {
				String result = getMethod.getResponseBodyAsString();
				System.out.println(result);
			}
		}catch(Exception e){
			
		}
	}
}

/**
 * Normal return result obtained:
 * 
 * {"refresh_token":"25.646dde7abf76b9bdcd8af7550f0ebe0c.315360000.1897026620.282335-18471643",
 *  "expires_in":2592000,
 *  "session_key":"9mzdDAYsciokc8tFn6yQqnTFoIIafKMPIQMk8zxQXLaX6l2ywhh+mR5q18MnNQuPVvNJotit0AWwajikYLvUuetANro2lw==",
 *  "access_token":"24.7328eb90ba8209337fa851e8e8182c6b.2592000.1584258620.282335-18471643",
 *  "scope":"public vis-ocr_ocr vis-classify_dishes brain_ocr_scope vis-classify_car brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number vis-classify_animal vis-classify_plant brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_solution_iocr brain_ingredient brain_qrcode brain_ocr_handwriting brain_ocr_passport brain_ocr_vat_invoice brain_advanced_general_classify brain_custom_dish brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate brain_poi_recognize vis-ocr_\u53f0\u6e7e\u901a\u884c\u8bc1 vis-ocr_\u6e2f\u6fb3\u901a\u884c\u8bc1 vis-ocr_\u673a\u52a8\u8f66\u68c0\u9a8c\u5408\u683c\u8bc1\u8bc6\u522b vis-ocr_\u8f66\u8f86vin\u7801\u8bc6\u522b vis-ocr_\u5b9a\u989d\u53d1\u7968\u8bc6\u522b brain_vehicle_detect vis-ocr_\u4fdd\u5355\u8bc6\u522b vis-ocr_\u884c\u7a0b\u5355\u8bc6\u522b brain_redwine brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_certificate brain_currency brain_vehicle_damage brain_ocr_air_ticket brain_ocr_insurance_doc wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406",
 *  "session_secret":"8b1f811b41eced328bc48c0dabaf279d"}
*/

sdk complete implementation code, Sample.java:

package com.baidu.sdk.ocr;

import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.ocr.AipOcr;

public class Sample {
    //Set APPID/AK/SK
    public static final String APP_ID = "18471643";
    public static final String API_KEY = "jNEhoBwPhbUOsVxnQBrcnBZB";
    public static final String SECRET_KEY = "5tXebMWoEdGDnjGGkL3BEDrhmwaNqnRr";

    public static void main(String[] args) {
        // Initializing an AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

       /* // Optional: set network connection parameters
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // Optional: set the proxy server address, either http or socket, or none
        client.setHttpProxy("proxy_host", proxy_port);  // Set up http proxy
        client.setSocketProxy("proxy_host", proxy_port);  // Set socket proxy

        // Optional: set log4j log output format. If it is not set, the default configuration will be used
        // You can also set this environment variable directly through the jvm startup parameter
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");*/

        // Calling interface
        String path = "C:/Users/edz/Desktop/1.png";
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        System.out.println(res.toString(2));
        
    }
}

 

 

Published 23 original articles, won praise 3, visited 10000+
Private letter follow

Keywords: SDK Apache Java log4j

Added by nimzie on Fri, 14 Feb 2020 12:27:29 +0200