我是靠谱客的博主 害羞大神,这篇文章主要介绍调用百度ai接口实现图片文字识别技术DEMO,现在分享给大家,希望可以做个参考。

一、第一步,到https://console.bce.baidu.com/


二、创建应用


三、pom.xml里面加入依赖

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
    <!-- json依赖 -->     <dependency>         <groupId>org.json</groupId>         <artifactId>json</artifactId>         <version>20160810</version>     </dependency>     <!-- https://mvnrepository.com/artifact/log4j/log4j -->     <dependency>         <groupId>log4j</groupId>         <artifactId>log4j</artifactId>         <version>1.2.17</version>     </dependency>

四、下面是代码Sample3类的代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.HashMap; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.baidu.aip.ocr.AipOcr; import com.baidu.aip.run.mapper.KeyWordMapper; @Component public class Sample3{          @Autowired     private KeyWordMapper keyWordMapper;          // 设置APPID/AK/SK     public static final String APP_ID = "自己申请的APP_ID";     public static final String API_KEY = "自己申请的API_KEY";     public static final String SECRET_KEY = "自己申请的SECRET_KEY ";          // 初始化用户对象     public static AipOcr init() {         // 初始化一个AipOcr         AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);         // 可选:设置网络连接参数         client.setConnectionTimeoutInMillis(2000);         client.setSocketTimeoutInMillis(60000);                  return client;     }     public void sample() {         // 传入可选参数调用接口         HashMap<String, String> options = new HashMap<String, String>();         options.put("language_type", "CHN_ENG");         options.put("detect_direction", "true");         options.put("detect_language", "true");         options.put("probability", "true");         AipOcr client = init();                  // 参数为本地图片路径         String image = "test.jpg";         String path = "C:\Users\Lenovo\Desktop\图片\aaa.jpg";         JSONObject res = client.basicGeneral(path, options);         System.out.println(res.toString()); //        // 参数为本地图片二进制数组 //        byte[] file = readImageFile(image); //        res = client.basicGeneral(file, options); //        System.out.println(res.toString(2));         // 通用文字识别, 图片参数为远程url图片 //        JSONObject res = client.basicGeneralUrl(url, options); //        System.out.println(res.toString(2));     } }

运行main方法
 

复制代码
1
2
3
4
5
6
7
8
9
10
package com.example.demo.controller.sign; import com.example.demo.controller.Sample3; public class test {     public static void main(String[] args) {         Sample3 sample3 = new Sample3();         sample3.sample();     } }


 

接口的返回值。返回值是以json格式返回的。经过我的测试发现一共有三种可能的返回值。
          一、图片上有字并识别成功:这种情况在json返回值中会包含一个words_result键名,值就是识别到的文字,它是一行一行识别的,所以在words_result里面可能有多个值,键名是words。
          二、图片上有字但不出:这种情况是图片上是有字的,但是没有识别出来,返回的words_result里面是空的。比如艺术字。
          三、图片格式错误:这种情况是图片上根本就没字或者没有可识别的文字,返回值会包含一个error_code键名,你可以直接通过返回值是否包含其来判断格式是否错误。

最后

以上就是害羞大神最近收集整理的关于调用百度ai接口实现图片文字识别技术DEMO的全部内容,更多相关调用百度ai接口实现图片文字识别技术DEMO内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部