文字识别ocr-奇异果体育app竞彩官网下载
更新时间:2022-04-18
接口描述
可对文档版面进行分析,输出图、表、标题、文本的位置,并输出分版块内容的ocr识别结果,支持中、英两种语言,手写、印刷体混排多种场景。
在线调试
您可以在 api explorer 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
http 方法:post
请求url: https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis
url参数:
参数 | 值 |
---|---|
access_token | 通过api key和secret key获取的access_token,参考“” |
header如下:
参数 | 值 |
---|---|
content-type | application/x-www-form-urlencoded |
body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 和 url/pdf_file 三选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4m,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 优先级:image > url > pdf_file,当image字段存在时,url、pdf_file字段失效 |
url | 和 image/pdf_file 三选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过4m,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 优先级:image > url > pdf_file,当image字段存在时,url字段失效 请注意关闭url防盗链 |
pdf_file | 和 image/url 三选一 | string | - | pdf文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4m,最短边至少15px,最长边最大4096px 优先级: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(栏、页眉、页脚、页码、脚注)的分析输出 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例token、图片地址或base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
# 试卷分析与识别
curl -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'
# encoding:utf-8
import requests
import base64
'''
试卷分析与识别
'''
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {"image":img,"language_type":"chn_eng","result_type":"big"}
access_token = '[调用鉴权接口获取的token]'
request_url = request_url "?access_token=" access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
package com.baidu.ai.aip;
import com.baidu.ai.aip.utils.base64util;
import com.baidu.ai.aip.utils.fileutil;
import com.baidu.ai.aip.utils.httputil;
import java.net.urlencoder;
/**
* 文档版面分析与识别
*/
public class docanalysis {
/**
* 重要提示代码中所需工具类
* fileutil,base64util,httputil,gsonutils请从
* https://ai.baidu.com/file/658a35abab2d404fbf903f64d47c1f72
* https://ai.baidu.com/file/c8d81f3301e24d2892968f09ae1ad6e2
* https://ai.baidu.com/file/544d677f5d4e4f17b4122fbd60db82b3
* https://ai.baidu.com/file/470b3acca3fe43788b5a963bf0b625f3
* 下载
*/
public static string docanalysis() {
// 请求url
string url = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis";
try {
// 本地文件路径
string filepath = "[本地文件路径]";
byte[] imgdata = fileutil.readfilebybytes(filepath);
string imgstr = base64util.encode(imgdata);
string imgparam = urlencoder.encode(imgstr, "utf-8");
string param = "language_type=" "chn_eng" "&result_type=" "big" "&image=" imgparam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
string accesstoken = "[调用鉴权接口获取的token]";
string result = httputil.post(url, accesstoken, param);
system.out.println(result);
return result;
} catch (exception e) {
e.printstacktrace();
}
return null;
}
public static void main(string[] args) {
docanalysis.docanalysis();
}
}
#include
#include
// libcurl库下载链接:https://curl.haxx.se/download.html
// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis";
static std::string docanalysis_result;
/**
* curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
* @param 参数定义见libcurl文档
* @return 返回值定义见libcurl文档
*/
static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
// 获取到的body存放在ptr中,先将其转换为string格式
docanalysis_result = std::string((char *) ptr, size * nmemb);
return size * nmemb;
}
/**
* 文档版面分析与识别
* @return 调用成功返回0,发生错误返回其他错误码
*/
int docanalysis(std::string &json_result, const std::string &access_token) {
std::string url = request_url "?access_token=" access_token;
curl *curl = null;
curlcode result_code;
int is_success;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, curlopt_url, url.data());
curl_easy_setopt(curl, curlopt_post, 1);
curl_httppost *post = null;
curl_httppost *last = null;
curl_formadd(&post, &last, curlform_copyname, "language_type", curlform_copycontents, "chn_eng", curlform_end);
curl_formadd(&post, &last, curlform_copyname, "result_type", curlform_copycontents, "big", curlform_end);
curl_formadd(&post, &last, curlform_copyname, "image", curlform_copycontents, "【base64_img】", curlform_end);
curl_easy_setopt(curl, curlopt_httppost, post);
curl_easy_setopt(curl, curlopt_writefunction, callback);
result_code = curl_easy_perform(curl);
if (result_code != curle_ok) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(result_code));
is_success = 1;
return is_success;
}
json_result = docanalysis_result;
curl_easy_cleanup(curl);
is_success = 0;
} else {
fprintf(stderr, "curl_easy_init() failed.");
is_success = 1;
}
return is_success;
}
/**
* 发起http post请求(rest api), 并获取rest请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$posturl = $url;
$curlpost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt($curl, curlopt_url, $posturl);
curl_setopt($curl, curlopt_header, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, curlopt_returntransfer, 1);
curl_setopt($curl, curlopt_ssl_verifypeer, false);
// post提交方式
curl_setopt($curl, curlopt_post, 1);
curl_setopt($curl, curlopt_postfields, $curlpost);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
$token = '[调用鉴权接口获取的token]';
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis?access_token=' . $token;
$img = file_get_contents('[本地文件路径]');
$img = base64_encode($img);
$bodys = array(
'language_type' => "chn_eng",
'result_type' => "big",
'image' => $img
);
$res = request_post($url, $bodys);
var_dump($res);
using system;
using system.io;
using system.net;
using system.text;
using system.web;
namespace com.baidu.ai
{
public class docanalysis
{
// 文档版面分析与识别
public static string docanalysis()
{
string token = "[调用鉴权接口获取的token]";
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis?access_token=" token;
encoding encoding = encoding.default;
httpwebrequest request = (httpwebrequest)webrequest.create(host);
request.method = "post";
request.keepalive = true;
// 图片的base64编码
string base64 = getfilebase64("[本地图片文件]");
string str = "language_type=" "chn_eng" "&result_type=" "big" "&image=" httputility.urlencode(base64);
byte[] buffer = encoding.getbytes(str);
request.contentlength = buffer.length;
request.getrequeststream().write(buffer, 0, buffer.length);
httpwebresponse response = (httpwebresponse)request.getresponse();
streamreader reader = new streamreader(response.getresponsestream(), encoding.default);
string result = reader.readtoend();
console.writeline("文档版面分析与识别:");
console.writeline(result);
return result;
}
public static string getfilebase64(string filename) {
filestream filestream = new filestream(filename, filemode.open);
byte[] arr = new byte[filestream.length];
filestream.read(arr, 0, (int)filestream.length);
string baser64 = convert.tobase64string(arr);
filestream.close();
return baser64;
}
}
}
返回说明
返回参数
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
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 | 是 | float | 整行的识别结果 |
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 | 否 | array[] | 每个单字的矩形框坐标。位置数组(坐标0点为左上角) |
left | 否 | uint32 | 表示定位位置的长方形左上顶点的水平坐标 |
top | 否 | uint32 | 表示定位位置的长方形左上顶点的垂直坐标 |
width | 否 | uint32 | 表示定位定位位置的长方形的宽度 |
height | 否 | uint32 | 表示位置的长方形的高度 |
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。 |
pdf_file_size | 否 | string | 传入pdf文件的总页数,当 pdf_file 参数有效时返回该字段 |
返回示例
{
"results_num": 6,
"log_id": "4488766695474114139",
"img_direction": 0,
"layouts_num": 0,
"results": [
{
"words_type": "print",
"words": {
"words_location": {
"top": 124,
"left": 136,
"width": 418,
"height": 65
},
"word": "五默写(4分)"
},
},
{
"words_type": "print",
"words": {
"words_location": {
"top": 246,
"left": 136,
"width": 37,
"height": 45
},
"word": "1"
},
},
{
"words_type": "handwriting",
"words": {
"words_location": {
"top": 195,
"left": 237,
"width": 469,
"height": 104
},
"word": "采菊东篱下"
},
},
{
"words_type": "print",
"words": {
"words_location": {
"top": 241,
"left": 889,
"width": 287,
"height": 52
},
"word": "悠然见南山?"
},
},
{
"words_type": "print",
"words": {
"words_location": {
"top": 415,
"left": 134,
"width": 472,
"height": 52
},
"word": "2.商女不知亡国恨"
},
},
{
"words_type": "handwriting",
"words": {
"words_location": {
"top": 377,
"left": 607,
"width": 556,
"height": 93
},
"word": "隔江犹唱后庭花。"
},
},
]
}