1. 程式人生 > 其它 >【手寫文字識別】-JavaAPI示例程式碼

【手寫文字識別】-JavaAPI示例程式碼

手寫文字識別-JavaAPI示例程式碼
  • 不知不覺手寫文字識別百度已經開始邀測了。需要的小夥伴去申請了哦。申請方式加入文字識別群找PM。或者工單提交申請。都要說明自己的APPID哦。
  • 介面地址:https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting

-----------------------------------------下面開始程式碼--------------------------------------------------

  • 手寫文字識別-示例程式碼
import java.net.URLEncoder;

import com.baidu.aip.util.Base64Util;
import com.xiaoshuai.baidu.util.FileUtil;
import com.xiaoshuai.baidu.util.HttpUtil;

/**
 * @author 小帥丶
 * @類名稱  HandWriteTest
 * @remark 手寫文字介面示例程式碼
 * @date  2018-1-26
 */
public class HandWriteTest {
	public static void main(String[] args) throws Exception {
		String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting";
		String accessToken = "自己的access_token";
		byte[] imageData = FileUtil.readFileByBytes("C:/Users/Administrator/Desktop/xs.jpg");
		String img64 = Base64Util.encode(imageData);
		String param ="image="+URLEncoder.encode(img64,"UTF-8");
		System.out.println("請求的引數:"+param);
		String object = HttpUtil.post(url, accessToken, param);
		System.out.println(new String(object));
	}
}
  • 手寫文字識別-返回的JSON字串(全部內容)

因為文件還沒有。不太確定引數是什麼。預設只傳遞到了image引數。會對圖片上的橫線也做了識別。

{"log_id": 8502255431261249697, "words_result_num": 11, "words_result": [{"location": {"width": 323, "top": 20, "left": 5, "height": 18}, "words": "………………….…………………………………………"}, {"location": {"width": 1041, "top": 25, "left": 347, "height": 41}, "words": "……………………………………………………………………………………………………………………………………………………………………………………………………"}, {"location": {"width": 944, "top": 159, "left": 2, "height": 39}, "words": "………………………"}, {"location": {"width": 438, "top": 176, "left": 981, "height": 25}, "words": "…………………………………:*"}, {"location": {"width": 243, "top": 298, "left": 2, "height": 23}, "words": "…………………………………………………………."}, {"location": {"width": 436, "top": 309, "left": 266, "height": 20}, "words": "……………………………………"}, {"location": {"width": 729, "top": 314, "left": 698, "height": 23}, "words": "………………………………………………"}, {"location": {"width": 233, "top": 588, "left": 5, "height": 22}, "words": "…………"}, {"location": {"width": 692, "top": 454, "left": 366, "height": 198}, "words": "開發者小帥"}, {"location": {"width": 398, "top": 732, "left": 423, "height": 15}, "words": "………………,………………………………………………………………………………"}, {"location": {"width": 596, "top": 862, "left": 840, "height": 19}, "words": "……………………………………"}]}
  • 手寫文字識別-返回的JSON字串(圖片文字內容)
{
    "log_id": 8502255431261250000, 
    "words_result_num": 11, 
    "words_result": [
        {
            "location": {
                "width": 692, 
                "top": 454, 
                "left": 366, 
                "height": 198
            }, 
            "words": "開發者小帥"
        }
    ]
}
  • 手寫文字識別-測試圖片

是不是發現識別還是蠻不錯的。相當準確。當然前提是作者寫的字不錯了。哈哈。

過於潦草的也進行了測試。準確率在75%左右