试卷分析与识别
更新时间:2024-12-10
接口描述
可对文档版面进行分析,输出图、表、标题、文本的位置,并输出分版块内容的OCR识别结果,支持中、英两种语言,手写、印刷体混排多种场景,支持公式识别、手写竖式识别。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 和 url/pdf_file 三选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式 优先级:image > url > pdf_file,当image字段存在时,url、pdf_file字段失效 |
url | 和 image/pdf_file 三选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式优先级:image > url > pdf_file,当image字段存在时,url字段失效请注意关闭URL防盗链 |
pdf_file | 和 image/url 三选一 | string | - | PDF文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px优先级:image > url > pdf_file,当image、url字段存在时,pdf_file字段失效 |
pdf_file_num | 否 | string | - | 需要识别的PDF文件的对应页码,当 pdf_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
language_type | 否 | string | CHN_ENG/ ENG | 识别语言类型,默认为CHN_ENG 可选值包括: = CHN_ENG:中英文 = ENG:英文 |
result_type | 否 | string | big/small | 返回识别结果是按单行结果返回,还是按单字结果返回,默认为big。 = big:返回行识别结果 = small:返回行识别结果之上还会返回单字结果 |
detect_direction | 否 | string | true/false | 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。其中, 0 :正向 1:逆时针旋转90度 2:逆时针旋转180度 3:逆时针旋转270度 |
line_probability | 否 | string | true/false | 是否返回每行识别结果的置信度。默认为false |
disp_line_poly | 否 | string | true/false | 是否返回每行的四角点坐标。默认为false |
words_type | 否 | string | handwring_only/ handprint_mix | 文字类型。 默认:印刷文字识别 = handwring_only:手写文字识别 = handprint_mix: 手写印刷混排识别 |
layout_analysis | 否 | string | true/false | 是否分析文档版面:包括layout(图、表、标题、段落、目录);attribute(栏、页眉、页脚、页码、脚注)的分析输出 |
recg_formula | 否 | string | true/false | 是否检测并识别公式,默认为false,公式以 Latex 格式文本返回。 =true:检测并识别公式 =false:不检测识别公式 |
recg_long_division | 否 | string | true/false | 是否检测并识别手写竖式,默认为false。 =true:检测并识别手写竖式 =false:不检测手写竖式 |
disp_underline_analysis | 否 | string | true/false | 是否开启下划线识别功能,可选值如下: =true:开启,在返回参数 underline 内输出下划线信息 =false:关闭,默认值,不输出下划线信息 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1# 试卷分析与识别
2curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis?access_token=【调用鉴权接口获取的token】' --data 'language_type=CHN_ENG&result_type=big&image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
1# encoding:utf-8
2
3import requests
4import base64
5
6'''
7试卷分析与识别
8'''
9
10request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = {"image":img,"language_type":"CHN_ENG","result_type":"big"}
16access_token = '[调用鉴权接口获取的token]'
17request_url = request_url + "?access_token=" + access_token
18headers = {'content-type': 'application/x-www-form-urlencoded'}
19response = requests.post(request_url, data=params, headers=headers)
20if response:
21 print (response.json())
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.Base64Util;
4import com.baidu.ai.aip.utils.FileUtil;
5import com.baidu.ai.aip.utils.HttpUtil;
6
7import java.net.URLEncoder;
8
9/**
10* 文档版面分析与识别
11*/
12public class DocAnalysis {
13
14 /**
15 * 重要提示代码中所需工具类
16 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
17 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
18 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
19 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
20 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
21 * 下载
22 */
23 public static String docAnalysis() {
24 // 请求url
25 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis";
26 try {
27 // 本地文件路径
28 String filePath = "[本地文件路径]";
29 byte[] imgData = FileUtil.readFileByBytes(filePath);
30 String imgStr = Base64Util.encode(imgData);
31 String imgParam = URLEncoder.encode(imgStr, "UTF-8");
32
33 String param = "language_type=" + "CHN_ENG" + "&result_type=" + "big" + "&image=" + imgParam;
34
35 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
36 String accessToken = "[调用鉴权接口获取的token]";
37
38 String result = HttpUtil.post(url, accessToken, param);
39 System.out.println(result);
40 return result;
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 return null;
45 }
46
47 public static void main(String[] args) {
48 DocAnalysis.docAnalysis();
49 }
50}
1#include <iostream>
2#include <curl/curl.h>
3
4// libcurl库下载链接:https://curl.haxx.se/download.html
5// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
6const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis";
7static std::string docAnalysis_result;
8/**
9 * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
10 * @param 参数定义见libcurl文档
11 * @return 返回值定义见libcurl文档
12 */
13static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
14 // 获取到的body存放在ptr中,先将其转换为string格式
15 docAnalysis_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19 * 文档版面分析与识别
20 * @return 调用成功返回0,发生错误返回其他错误码
21 */
22int docAnalysis(std::string &json_result, const std::string &access_token) {
23 std::string url = request_url + "?access_token=" + access_token;
24 CURL *curl = NULL;
25 CURLcode result_code;
26 int is_success;
27 curl = curl_easy_init();
28 if (curl) {
29 curl_easy_setopt(curl, CURLOPT_URL, url.data());
30 curl_easy_setopt(curl, CURLOPT_POST, 1);
31 curl_httppost *post = NULL;
32 curl_httppost *last = NULL;
33 curl_formadd(&post, &last, CURLFORM_COPYNAME, "language_type", CURLFORM_COPYCONTENTS, "CHN_ENG", CURLFORM_END);
34 curl_formadd(&post, &last, CURLFORM_COPYNAME, "result_type", CURLFORM_COPYCONTENTS, "big", CURLFORM_END);
35 curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
36
37 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
38 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
39 result_code = curl_easy_perform(curl);
40 if (result_code != CURLE_OK) {
41 fprintf(stderr, "curl_easy_perform() failed: %s\n",
42 curl_easy_strerror(result_code));
43 is_success = 1;
44 return is_success;
45 }
46 json_result = docAnalysis_result;
47 curl_easy_cleanup(curl);
48 is_success = 0;
49 } else {
50 fprintf(stderr, "curl_easy_init() failed.");
51 is_success = 1;
52 }
53 return is_success;
54}
1<?php
2/**
3 * 发起http post请求(REST API), 并获取REST请求的结果
4 * @param string $url
5 * @param string $param
6 * @return - http response body if succeeds, else false.
7 */
8function request_post($url = '', $param = '')
9{
10 if (empty($url) || empty($param)) {
11 return false;
12 }
13
14 $postUrl = $url;
15 $curlPost = $param;
16 // 初始化curl
17 $curl = curl_init();
18 curl_setopt($curl, CURLOPT_URL, $postUrl);
19 curl_setopt($curl, CURLOPT_HEADER, 0);
20 // 要求结果为字符串且输出到屏幕上
21 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
22 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
23 // post提交方式
24 curl_setopt($curl, CURLOPT_POST, 1);
25 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
26 // 运行curl
27 $data = curl_exec($curl);
28 curl_close($curl);
29
30 return $data;
31}
32
33$token = '[调用鉴权接口获取的token]';
34$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis?access_token=' . $token;
35$img = file_get_contents('[本地文件路径]');
36$img = base64_encode($img);
37$bodys = array(
38 'language_type' => "CHN_ENG",
39 'result_type' => "big",
40 'image' => $img
41);
42$res = request_post($url, $bodys);
43
44var_dump($res);
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using System.Web;
6
7namespace com.baidu.ai
8{
9 public class DocAnalysis
10 {
11 // 文档版面分析与识别
12 public static string docAnalysis()
13 {
14 string token = "[调用鉴权接口获取的token]";
15 string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis?access_token=" + token;
16 Encoding encoding = Encoding.Default;
17 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
18 request.Method = "post";
19 request.KeepAlive = true;
20 // 图片的base64编码
21 string base64 = getFileBase64("[本地图片文件]");
22 String str = "language_type=" + "CHN_ENG" + "&result_type=" + "big" + "&image=" + HttpUtility.UrlEncode(base64);
23 byte[] buffer = encoding.GetBytes(str);
24 request.ContentLength = buffer.Length;
25 request.GetRequestStream().Write(buffer, 0, buffer.Length);
26 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
27 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
28 string result = reader.ReadToEnd();
29 Console.WriteLine("文档版面分析与识别:");
30 Console.WriteLine(result);
31 return result;
32 }
33
34 public static String getFileBase64(String fileName) {
35 FileStream filestream = new FileStream(fileName, FileMode.Open);
36 byte[] arr = new byte[filestream.Length];
37 filestream.Read(arr, 0, (int)filestream.Length);
38 string baser64 = Convert.ToBase64String(arr);
39 filestream.Close();
40 return baser64;
41 }
42 }
43}
返回说明
返回参数
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
img_direction | 否 | int32 | detect_direction=true 时返回。检测到的图像朝向,0 :正向; 1:逆时针旋转90度;2:逆时针旋转180度;3:逆时针旋转270度 |
results_num | 是 | uint32 | 识别结果数,表示results的元素个数 |
results | 是 | array[] | 识别结果数组 |
+ words_type | 是 | string | 文字属性(手写、印刷),handwriting 手写,print 印刷 |
+ words | 是 | array[] | 整行的识别结果数组。 |
++ line_probability | 否 | array[] | line_probability=true 时返回。识别结果中每一行的置信度值,包含average:行置信度平均值,min:行置信度最小值 |
+++ average | 否 | float | 行置信度 |
+++ min | 否 | float | 整行中单字的最低置信度 |
++ word | 是 | string | 整行的识别结果 |
++ poly_location | 否 | array[] | 是否返回每行的四角点坐标,disp_line_poly=true时返回 |
++ words_location | 是 | array[] | 整行的矩形框坐标。位置信息(坐标0点为左上角) |
+++ left | 是 | uint32 | 表示定位位置的长方形左上顶点的水平坐标 |
+++ top | 是 | uint32 | 表示定位位置的长方形左上顶点的垂直坐标 |
+++ width | 是 | uint32 | 表示定位定位位置的长方形的宽度 |
+++ height | 是 | uint32 | 表示位置的长方形的高度 |
+ chars | 否 | array[] | result_type=small 时返回。单字符结果数组 |
++ char | 否 | string | result_type=small 时返回。每个单字的内容 |
++ chars_location | 否 | object | 每个单字的矩形框坐标。位置信息(坐标0点为左上角) |
+++ left | 否 | uint32 | 表示定位位置的长方形左上顶点的水平坐标 |
+++ top | 否 | uint32 | 表示定位位置的长方形左上顶点的垂直坐标 |
+++ width | 否 | uint32 | 表示定位定位位置的长方形的宽度 |
+++ height | 否 | uint32 | 表示位置的长方形的高度 |
formula_result | 否 | array[] | 识别结果中的公式数组,包括公式位置和公式内容,recg_formula=true时返回 |
+ form_location | 否 | array[] | 识别结果中公式的矩形框坐标数组(坐标0点为左上角) |
+ form_words | 否 | string | 识别结果中公式的内容 |
words_result | 否 | array[] | 将普通文字和公式融合后的识别结果数组,recg_formula=true时返回 |
+ location | 否 | array[] | 识别结果中整行的矩形框坐标数组(坐标0点为左上角) |
+ words | 否 | string | 识别结果中整行的内容 |
+ chars | 否 | array[] | 单字符结果数组,公式整体作为一个单字,result_type=small 时返回 |
++ char | 否 | string | 每个单字的内容 |
++ chars_location | 否 | object | 每个单字的矩形框坐标数组(坐标0点为左上角) |
layouts_num | 否 | uint32 | 版面分析结果数,表示layout的元素个数 |
layouts | 否 | array[] | 每个「栏:section」里面的文档版面模块数组,包含表格、图、段落文本、标题、目录等5个模块;每个模块的坐标位置;段落文本和表格内文本内容对应的行序号id。 |
+ layout | 否 | string | 版面分析的标签结果。表格:table, 图:figure, 文本:text, 标题:title ,目录:contents |
+ layout_location | 否 | array[] | 文档版面信息标签的位置,四个顶点: 左上,右上,右下,左下 |
++ x | 否 | uint32 | 水平坐标(坐标0点为左上角) |
++ y | 否 | uint32 | 水平坐标(坐标0点为左上角) |
+ layout_idx | 否 | array[] | 文档版面信息中的文本在results结果中的位置:版面文本标签对应的行序号ID为n,则此标签中的文本在results结果中第n+1条展示) |
sec_rows | 否 | uint32 | 将所有的版面中的「栏:section」内容表示成 M x N 的网格,sec_rows = M |
sec_cols | 否 | uint32 | 将所有的版面中的「分栏」内容表示成 M x N 的网格,sec_cols = N |
sections | 否 | array[] | 一张图片中包含的5大版面属性,包含:栏,页眉,页脚,页码,脚注,该数组里有属性的标签、属性的位置、属性所包含文本内容的id序号。 其中,栏(section)里面包含5个模块内容,有:表格、图、段落文本、标题、目录(在返回参数layouts里输出) |
+ attribute | 否 | string | 版面分析的属性标签结果,栏:section, 页眉:header, 页脚:footer, 页码:number,脚注:footnote |
+ attri_location | 否 | array[] | 版面分析的属性所在位置,四个顶点: 左上,右上,右下,左下 |
++ x | 否 | uint32 | 水平坐标(坐标0点为左上角) |
++ y | 否 | uint32 | 水平坐标(坐标0点为左上角) |
+ sec_idx | 否 | string | sections返回参数中的5个版面属性里,包含的内容序号标识 |
++ idx | 否 | string | sections返回参数中的5个版面属性里,每个属性下包含的文本行id序号 |
++ para_idx | 否 | string | 当且仅当attribute=section时才会返回。表示,返回参数中的「栏:section」里面,所包含的表格、图、段落文本、标题、目录等5个模块返回的顺序号id(即layouts返回结果中,每个模块的返回顺序号) |
++ row_idx | 否 | string | 当且仅当attribute=section时才会返回。表示,将所有栏表示成 M xN 的网格,所属网格的行的id |
++ col_idx | 否 | string | 当且仅当attribute=section时才会返回。表示,将所有栏表示成 M xN 的网格,所属网格的列的id |
+ long_division | 否 | array[] | 手写竖式识别结果,当 recg_long_division=true 时返回 |
+ location | 否 | object | 手写竖式的矩形框坐标数组(坐标0点为左上角) |
+ words | 否 | object | 按行输出手写竖式内文字结果 |
++ word | 否 | string | 每行文字的内容 |
++ words_location | 否 | object | 每行的矩形框坐标数组(坐标0点为左上角) |
+ long_division_num | 否 | uint32 | 手写竖式识别结果数,表示 long_division 的元素个数,当 recg_long_division=true 时返回 |
underline | 否 | array[] | 识别到的下划线结果,当 disp_underline_analysis=true 时返回 |
+ points | 否 | object | 下划线坐标信息 |
++ start_x | 否 | uint32 | 下划线起点 x 坐标 |
++ start_y | 否 | uint32 | 下划线起点 y 坐标 |
++ end_x | 否 | uint32 | 下划线终点 x 坐标 |
++ end_y | 否 | uint32 | 下划线终点 y 坐标 |
+ prob | 否 | uint32 | 下划线置信度,取值范围在 [0,1] 之间 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
返回示例
JSON
1{
2 "results_num": 6,
3 "log_id": "4488766695474114139",
4 "img_direction": 0,
5 "layouts_num": 0,
6 "results": [
7 {
8 "words_type": "print",
9 "words": {
10 "words_location": {
11 "top": 124,
12 "left": 136,
13 "width": 418,
14 "height": 65
15 },
16 "word": "五默写(4分)"
17 },
18 },
19 {
20 "words_type": "print",
21 "words": {
22 "words_location": {
23 "top": 246,
24 "left": 136,
25 "width": 37,
26 "height": 45
27 },
28 "word": "1"
29 },
30 },
31 {
32 "words_type": "handwriting",
33 "words": {
34 "words_location": {
35 "top": 195,
36 "left": 237,
37 "width": 469,
38 "height": 104
39 },
40 "word": "采菊东篱下"
41 },
42 },
43 {
44 "words_type": "print",
45 "words": {
46 "words_location": {
47 "top": 241,
48 "left": 889,
49 "width": 287,
50 "height": 52
51 },
52 "word": "悠然见南山?"
53 },
54 },
55 {
56 "words_type": "print",
57 "words": {
58 "words_location": {
59 "top": 415,
60 "left": 134,
61 "width": 472,
62 "height": 52
63 },
64 "word": "2.商女不知亡国恨"
65 },
66 },
67 {
68 "words_type": "handwriting",
69 "words": {
70 "words_location": {
71 "top": 377,
72 "left": 607,
73 "width": 556,
74 "height": 93
75 },
76 "word": "隔江犹唱后庭花。"
77 },
78 },
79 ],
80 "formula_result": [
81 {
82 "form_location": {
83 "top": 0,
84 "left": 97,
85 "width": 151,
86 "height": 77
87 },
88 "form_words": " x = \\frac { 1 } { n - 1 } - 1 1 \\frac { \\frac { 5 } { 2 } } { 5 }"
89 },
90 {
91 "form_location": {
92 "top": 119,
93 "left": 118,
94 "width": 115,
95 "height": 80
96 },
97 "form_words": " = \\sqrt { \\frac { x } { 2 } ( x - 1 ) ^ { 2 } }"
98 },
99 {
100 "form_location": {
101 "top": 196,
102 "left": 78,
103 "width": 17,
104 "height": 24
105 },
106 "form_words": " x ^ { 2 }"
107 },
108 {
109 "form_location": {
110 "top": 244,
111 "left": 79,
112 "width": 103,
113 "height": 70
114 },
115 "form_words": " s = \\frac { \\sum _ { i = 0 } { m } \\cdot i v } { - 1 }"
116 }
117 ],
118 "words_result": [
119 {
120 "location": {
121 "top": 164,
122 "left": 255,
123 "width": 111,
124 "height": 16
125 },
126 "words": "其中m表示考生"
127 },
128 {
129 "location": {
130 "top": 198,
131 "left": 24,
132 "width": 341,
133 "height": 18
134 },
135 "words": "的人数 x ^ { 2 } 表示的是滴个考上的第i题等分,"
136 },
137 ],
138}