Springboot combined with Baidu IORC implement a custom template picture identification

Preface:

First of all it has recently re-company project which encountered such a problem, is the need to identify pictures, extracting key statements pictures of them, and the identification of the statement, of course people handwriting recognition, over and over again I thought, finally decided to use Baidu's OCR help I solved this one needs

Did not talk much, we just start

 

Go to the official website to find out

https://cloud.baidu.com/product/ocr

Here we see a lot of related products, almost all picture text recognition, roughly the same

 

We are here to select a custom template recognition, so with demand in line with our business logic, allowing users to hand to fill a list, they will certainly template information, such recognition precision and accuracy compared to general recognition, it will be much better /

 

Click for immediate use, add a page into the template, we need to add a custom template to give, in order to facilitate testing, provides a template here a picture of my own testing to facilitate testing

 

 

 

Here is a company often will be used in a cost reimbursement list, it is suitable template we recognize this time;

 

 Click Add template, upload this picture as a file

 

New identification field

 

 

New marquee Identification Zone

It should identify the four new fields, we are here to build four such like. Identification field is after you upload pictures to it in accordance with this as a reference, and then to identify the corresponding identification zone;

 

 Small scale chopper

After the completion of this new, save, and then find a handwritten picture, conduct a test, and here I provide a picture as a reference:

 

 

 Just write a few names, handwritten okay! Haha

 

Test, identification operation

 

 

Carried out a wave of recognition, the effect is good, especially good recognition effect, then you can access the project which, of course, here we choose Springboot mainstream framework now, what reason is there not?

 

After successfully posted, remember template ID, as well as to create a App, Baidu has described in detail here, I will not introduce too much here, we just start a new project to give Springboot, reference documentation, introduction of POM rely directly open dry

 

 

<dependency>
    <groupId>com.baidu.aip</groupId>
    <artifactId>java-sdk</artifactId>
    <version>4.8.0</version>
</dependency>

百度这里最新的版本号是:4.8.0,引入即可

 

 新建AipOcr

AipOcr是Optical Character Recognition的Java客户端,为使用Optical Character Recognition的开发人员提供了一系列的交互方法。

用户可以参考如下代码新建一个AipOcr,初始化完成后建议单例使用,避免重复获取access_token:

 

这里我们直接使用SpringBoot的Bean注入的方式使用:

 

/**
 * 百度云token
 */
@Configuration
public class Sample {

    //设置APPID/AK/SK
    @Value("${api.baidu.com.appid}")
    private String APP_ID;
    @Value("${api.baidu.com.appkey}")
    private String API_KEY;
    @Value("${api.baidu.com.appsecret}")
    private String SECRET_KEY;

    @Bean
    public AipOcr createAipOcr(){

        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        //建立连接的超时时间(单位:毫秒)
        client.setConnectionTimeoutInMillis(2000);
        //通过打开的连接传输数据的超时时间(单位:毫秒)
        client.setSocketTimeoutInMillis(60000);

        return client;
    }



}

 

这样用起来多方便,Springboot 大爱,太好用了,全局只需要初始化一个对象,全局调用,不用反复考虑Token的问题;

 

这里用一下官方的代码,说明一下直接调用AipOcr多爽,识别后,返回Json格式字符串,判断美哟错误的话,直接解析Json字符串即可

public void sample(AipOcr client) {
    // 传入可选参数调用接口
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("templateSign", "Nsdax2424asaAS791823112");
    options.put("classifierId", "31232");
    
    
    // 参数为本地路径
    String image = "test.jpg";
    JSONObject res = client.custom(image, options);
    System.out.println(res.toString(2));

    // 参数为二进制数组
    byte[] file = readFile("test.jpg");
    res = client.custom(file, options);
    System.out.println(res.toString(2));
}

 

 

Demo演示:

 

 

 

参考:

http://ai.baidu.com/docs#/OCR-Java-SDK/aadf7467

https://cloud.baidu.com/doc/OCR/s/0jwvy03go/

https://cloud.baidu.com/doc/OCR/OCR-API.html#.E6.9B.B4.E6.96.B0.E8.AE.B0.E5.BD.95

Demo分享:如果觉得文章有帮到你的地方 不妨给个Star吧!

码云:https://gitee.com/mrc1999/baiduOcr

 

Guess you like

Origin www.cnblogs.com/ChromeT/p/11372874.html