搜索本产品文档关键词
Java语言
所有文档
menu

文字识别

Java语言

表格文字识别同步接口

自动识别表格线及表格内容,结构化输出表头、表尾及每个单元格的文字内容。

Java
1    public void sample(AipOcr client) {
2        // 传入可选参数调用接口
3        HashMap<String, String> options = new HashMap<String, String>();
4        
5        
6        // 参数为本地图片路径
7        String image = "test.jpg";
8        JSONObject res = client.form(image, options);
9        System.out.println(res.toString(2));
10
11        // 参数为本地图片二进制数组
12        byte[] file = readImageFile(image);
13        res = client.form(file, options);
14        System.out.println(res.toString(2));
15
16    }

表格文字识别同步接口 请求参数详情

参数名称
是否必选
类型
说明
image mixed 本地图片路径或者图片二进制数据

表格文字识别同步接口 返回数据参数详情

字段
是否必选
类型
说明
log_id long 唯一的log id,用于问题定位
forms_result_num number
forms_result array(object) 识别结果

表格文字识别同步接口 返回示例

JSON
1   {
2        "log_id": 3445697108,
3        "forms_result_num": 1,
4        "forms_result": [
5            {
6                "body": [
7                    {
8                        "column": 0,
9                        "probability": 0.99855202436447,
10                        "row": 0,
11                        "vertexes_location": [
12                            {
13                                "x": -2,
14                                "y": 260
15                            },
16                            {
17                                "x": 21,
18                                "y": 244
19                            },
20                            {
21                                "x": 35,
22                                "y": 266
23                            },
24                            {
25                                "x": 12,
26                                "y": 282
27                            }
28                        ],
29                        "words": "目"
30                    },
31                    {
32                        "column": 3,
33                        "probability": 0.99960500001907,
34                        "row": 5,
35                        "vertexes_location": [
36                            {
37                                "x": 603,
38                                "y": 52
39                            },
40                            {
41                                "x": 634,
42                                "y": 32
43                            },
44                            {
45                                "x": 646,
46                                "y": 50
47                            },
48                            {
49                                "x": 615,
50                                "y": 71
51                            }
52                        ],
53                        "words": "66"
54                    },
55                    {
56                        "column": 3,
57                        "probability": 0.99756097793579,
58                        "row": 6,
59                        "vertexes_location": [
60                            {
61                                "x": 634,
62                                "y": 73
63                            },
64                            {
65                                "x": 648,
66                                "y": 63
67                            },
68                            {
69                                "x": 657,
70                                "y": 77
71                            },
72                            {
73                                "x": 643,
74                                "y": 86
75                            }
76                        ],
77                        "words": "4"
78                    },
79                    {
80                        "column": 3,
81                        "probability": 0.96489900350571,
82                        "row": 10,
83                        "vertexes_location": [
84                            {
85                                "x": 699,
86                                "y": 178
87                            },
88                            {
89                                "x": 717,
90                                "y": 167
91                            },
92                            {
93                                "x": 727,
94                                "y": 183
95                            },
96                            {
97                                "x": 710,
98                                "y": 194
99                            }
100                        ],
101                        "words": "3,"
102                    },
103                    {
104                        "column": 3,
105                        "probability": 0.99809801578522,
106                        "row": 14,
107                        "vertexes_location": [
108                            {
109                                "x": 751,
110                                "y": 296
111                            },
112                            {
113                                "x": 786,
114                                "y": 273
115                            },
116                            {
117                                "x": 797,
118                                "y": 289
119                            },
120                            {
121                                "x": 761,
122                                "y": 312
123                            }
124                        ],
125                        "words": "206"
126                    }
127                ],
128                "footer": [
129                    {
130                        "column": 0,
131                        "probability": 0.99853301048279,
132                        "row": 0,
133                        "vertexes_location": [
134                            {
135                                "x": 605,
136                                "y": 698
137                            },
138                            {
139                                "x": 632,
140                                "y": 680
141                            },
142                            {
143                                "x": 643,
144                                "y": 696
145                            },
146                            {
147                                "x": 616,
148                                "y": 714
149                            }
150                        ],
151                        "words": "22"
152                    }
153                ],
154                "header": [
155                    {
156                        "column": 0,
157                        "probability": 0.94802802801132,
158                        "row": 0,
159                        "vertexes_location": [
160                            {
161                                "x": 183,
162                                "y": 96
163                            },
164                            {
165                                "x": 286,
166                                "y": 29
167                            },
168                            {
169                                "x": 301,
170                                "y": 52
171                            },
172                            {
173                                "x": 199,
174                                "y": 120
175                            }
176                        ],
177                        "words": "29月"
178                    }
179                ],
180                "vertexes_location": [
181                    {
182                        "x": -154,
183                        "y": 286
184                    },
185                    {
186                        "x": 512,
187                        "y": -153
188                    },
189                    {
190                        "x": 953,
191                        "y": 513
192                    },
193                    {
194                        "x": 286,
195                        "y": 953
196                    }
197                ]
198            }
199        ]
200    }

表格文字识别

自动识别表格线及表格内容,结构化输出表头、表尾及每个单元格的文字内容。表格文字识别接口为异步接口,分为两个API:提交请求接口、获取结果接口。

Java
1    public void sample(AipOcr client) {
2        // 传入可选参数调用接口
3        HashMap<String, String> options = new HashMap<String, String>();
4        
5        
6        // 参数为本地图片路径
7        String image = "test.jpg";
8        JSONObject res = client.tableRecognitionAsync(image, options);
9        System.out.println(res.toString(2));
10
11        // 参数为本地图片二进制数组
12        byte[] file = readImageFile(image);
13        res = client.tableRecognitionAsync(file, options);
14        System.out.println(res.toString(2));
15
16    }

表格文字识别 请求参数详情

参数名称
是否必选
类型
说明
image mixed 本地图片路径或者图片二进制数据

表格文字识别 返回数据参数详情

字段
是否必选
类型
说明
log_id long 唯一的log id,用于问题定位
result list 返回的结果列表
+request_id string 该请求生成的request_id,后续使用该request_id获取识别结果

表格文字识别 返回示例

JSON
1    {
2        "result" : [
3            {
4                "request_id" : "1234_6789"
5            }
6        ],
7        "log_id":149689853984104
8    }

失败应答示例(详细的错误码说明见本文档底部):

JSON
1    {
2        "log_id": 149319909347709,
3        "error_code": 282000
4        "error_msg":"internal error"
5    }

表格识别结果

获取表格文字识别结果。

Java
1    public void sample(AipOcr client) {
2        // 传入可选参数调用接口
3        HashMap<String, String> options = new HashMap<String, String>();
4        options.put("result_type", "json");
5        
6        String requestId = "23454320-23255";
7        
8        // 表格识别结果
9        JSONObject res = client.tableResultGet(requestId, options);
10        System.out.println(res.toString(2));
11
12    }

表格识别结果 请求参数详情

参数名称
是否必选
类型
可选值范围
默认值
说明
request_id String 发送表格文字识别请求时返回的request id
result_type String json
excel
excel 期望获取结果的类型,取值为“excel”时返回xls文件的地址,取值为“json”时返回json格式的字符串,默认为”excel”

表格识别结果 返回数据参数详情

字段
是否必选
类型
说明
log_id long 唯一的log id,用于问题定位
result object 返回的结果
+result_data string 识别结果字符串,如果request_type是excel,则返回excel的文件下载地址,如果request_type是json,则返回json格式的字符串
+percent int 表格识别进度(百分比)
+request_id string 该图片对应请求的request_id
+ret_code int 识别状态,1:任务未开始,2:进行中,3:已完成
+ret_msg string 识别状态信息,任务未开始,进行中,已完成

表格识别结果 返回示例

成功应答示例:

JSON
1    {
2        "result" : {
3            "result_data" : "",
4            "persent":100,
5            "request_id": "149691317905102",
6            "ret_code": 3
7            "ret_msg": "已完成",
8        },
9        "log_id":149689853984104
10    }

当request_type为excel时,result_data格式样例为:

JSON
1    {
2        "file_url":"https://ai.baidu.com/file/xxxfffddd"
3    }

当request_type为json时,result_data格式样例为:

JSON
1   {
2        "form_num": 1,
3        "forms": [
4            {
5                "header": [
6                    {
7                    "row": [
8                        1
9                    ],
10                    "column": [
11                        1,
12                        2
13                    ],
14                    "word": "表头信息1",
15                }
16            ],
17            "footer": [
18                {
19                    "row": [
20                        1
21                    ],
22                    "column": [
23                        1,
24                        2
25                    ],
26                    "word": "表尾信息1",
27                }
28            ],
29            "body": [
30                {
31                    "row": [
32                        1
33                    ],
34                    "column": [
35                        1,
36                        2
37                    ],
38                    "word": "单元格文字",
39                }
40            ]
41        }
42    ]
43    }

其中各个参数的说明(json方式返回结果时):

字段
是否必选
类型
说明
form_num int 表格数量(可能一张图片中包含多个表格)
forms list 表格内容信息的列表
+header list 每个表格中,表头数据的相关信息
+footer list 表尾的相关信息
+body list 表格主体部分的数据
++row list 该单元格占据的行号
++column list 该单元格占据的列号
++word string 该单元格中的文字信息

失败应答示例(详细的错误码说明见本文档底部):

JSON
1   {
2        "log_id": 149319909347709,
3        "error_code": 282000
4        "error_msg":"internal error"
5    }

表格识别轮询接口

代码示例

调用表格识别请求,获取请求id之后轮询调用表格识别获取结果的接口。

Java
1public void tableRecognition(AipOcr client) {
2    //异步接口
3
4    //使用封装的同步轮询接口
5    JSONObject jsonres = client.tableRecognizeToJson(file, 20000);
6    System.out.println(jsonres.toString(2));
7
8    JSONObject excelres = client.tableRecognizeToExcelUrl(file, 20000);
9    System.out.println(excelres.toString(2));
10
11}

请求参数

参数
类型
描述
是否必须
imgPath/imgData byte[] 图像文件路径或二进制数据
timeoutMiliseconds long 最长等待时间,超时将返回错误, 一般任务在20s完成

返回参数与表格识别结果接口返回相同

上一篇
Python语言
下一篇
PHP文档