OCR文字识别
简介
HI,您好,欢迎使用学而思网校AI开放平台OCR文字识别接口服务。
本文档主要针对需要集成HTTP API的技术研发工程师,详细描述OCR文字识别能力相关的技术内容。您可以通过
接口能力
接口名称
接口能力简要描述
api 地址
OCR文字识别
检测上传图像中的文字,进行 AI 文字识别
http://openapiai.xueersi.com/v1/api/img/ocr/general
适用范围
任意操作系统,任意编程语言,均可以通过http或https调用本接口。
图像发送方式
图像数据支持两种形式发送:
URL
网络上的图片 URL 地址
图片数据
请求图片需经过base64编码:图片的base64编码指将一副图片编码成字符串数据,便于网络传输请求。您可以首先得到图片的二进制,进行base64编码后,然后再进行urlencode(注意你所使用的网络工具或网络库有没有默认的urlencode功能,如果没有,需要你自己显式转化)。
转码方式可参考
注意: 图片的base64编码是不包含图片头的,如(data:image/jpg;base64,)
图片大小
不能大于4MB
长边不能超过4096px
短边不能小于15px
图像类型支持
图像数据支持以下原生类型:
jpg
png
jpeg
bmp
文字识别范围
目前,OCR识别接口服务支持多种类型文字,包括:
中文打印体识别
英文打印体识别
表格识别
拼音识别
请求方式
HTTP 方法:POST
请求 URL:http://openapiai.xueersi.com/v1/api/img/ocr/general
注意: Content-Type为application/x-www-form-urlencoded,然后通过urlencode格式化请求体。
Header 如下:
参数名
赋值
Content-Type
application/x-www-form-urlencoded
urlencode介绍
在请求 API 的 post 请求参数中含有图片url地址或base64字符。url地址 或 base64 字符作为参数传递时,需要把 中文 以及 '/' 做一下编码,防止解析中出现歧义,从而符合url的规范。
将中文 以及 '/' 转换为百分号编码形式,这就是 urlencode格式化操作。
部分语言的第三方包内部做了 urlencode 编码,不进行 urlencode 格式化也可以正常进行 API 请求,但 是进行 urlencode 格式化之后,一定可以通过请求。所以我们推荐您对请求体中的参数进行 urlencode 操作。
快速接入方式
请求参数详情
参数名
类型
是否必选
赋值说明
样例
备注
app_key
string
是
应用标识
8102b22a5e81e840176d9f381ec6f837
img
string
是
图像数据
https://i.loli.net/2019/03/22/5c94684fad743.jpg
或
图像 base64数据:示例数据较长,见附件
图像base64字符串 / 图像URL
img_type
string
是
图像形式
URL
base64 / URL
recog_formula
int
否
是否识别公式
0 -识别 | 1-不识别 ,默认0
请求代码示例
HTTP 代码示例:
POST /v1/api/img/ocr/general HTTP/1.1
Host: openapiai.xueersi.com
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
app_key=8102b22a5e81e840176d9f381ec6f837&img=https%3a%2f%2fi.loli.net%2f2019%2f03%2f22%2f5c94684fad743.jpg&img_type=URL
cURL 代码示例:
curl -X POST \
http://openapiai.xueersi.com/v1/api/img/ocr/general \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'cache-control: no-cache' \
-d 'app_key=8102b22a5e81e840176d9f381ec6f837&img=https%3a%2f%2fi.loli.net%2f2019%2f03%2f22%2f5c94684fad743.jpg&img_type=URL'
PHP 代码示例:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://openapiai.xueersi.com/v1/api/img/ocr/general",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "app_key=8102b22a5e81e840176d9f381ec6f837&img=https%3a%2f%2fi.loli.net%2f2019%2f03%2f22%2f5c94684fad743.jpg&img_type=URL",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Python(Python3) 代码示例:
URL传参方式:
import http.client
conn = http.client.HTTPConnection("openapiai.xueersi.com")
payload = "app_key=8102b22a5e81e840176d9f381ec6f837&img=https%3A%2F%2Fai.xueersi.com%2FtextRecognition%2Fimages%2F22.jpg&img_type=URL"
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'cache-control': "no-cache",
}
conn.request("POST", "/v1/api/img/ocr/general/", payload, headers)
res = conn.getresponse()
data = res.read()
#python2.7
#print(data)
#python3.6
print(data.decode("utf-8"))
conn.close()
base64传参方式:
import http.client
import urllib
import base64
from urllib import quote
with open('./test.jpg', 'rb') as bin_data:
image_data = bin_data.read()
image_data_base64 = base64.b64encode(image_data)
image_data_base64 = quote(image_data_base64)
conn = http.client.HTTPConnection("openapiai.xueersi.com")
appkey_string = 'app_key=8102b22a5e81e840176d9f381ec6f837'
img_string = 'img=' + image_data_base64
img_type_string = 'img_type=base64'
payload = appkey_string + '&' + img_string + '&' + img_type_string
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'cache-control': "no-cache",
}
conn.request("POST", "/v1/api/img/ocr/general/", payload, headers)