- [接口说明](##1)
- [接口地址](##2)
- [请求参数](##3)
- [返回值](##4)
- [调用示例](##5)
<a name = "#1"></a>
## 接口说明
手写文字识别(Handwriting words Recognition)基于深度神经网络模型的端到端文字识别系统,将图片(来源如扫描仪或数码相机)中的手写字体转化为计算机可编码的文字
<a name = "#2"></a>
## 接口地址
POST http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting HTTP/1.1
Content-Type:application/x-www-form-urlencoded; charset=utf-8
<a name = "#3"></a>
## 请求参数
<span style="color:red">在 Http Request Header 中配置授权认证参数,见【接口概述-授权认证】</span>。
其中 *X-Param* 为各配置参数组成的JSON串经BASE64编码之后的字符串,原始JSON串各字段说明如下:
|参数|类型|必须|说明|示例|
|:-------------|:-------------|:-------------|:-------------|:-------------|
|language | string | 是 | 语言,可选值:en(英文) |en|
|location | string | 否 | 是否返回文本位置信息,可选值:false(否),true(是),默认为false|true |
*X-Param生成示例:*
原始JSON串:
{
"language": "en",
"location": "false"
}
BASE64编码(即X-Param):
eyJsYW5ndWFnZSI6ImVuIiwibG9jYXRpb24iOiJmYWxzZSJ9
<span style="color:red">在 Http Request Body 中配置以下参数:</span>
|参数|类型|必须|说明|示例|
|:-----|:------|:---------|:-----------|:---------|
|image| string | 是 | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式|exSI6ICJlbiIsCgkgICAgInBvc2l0aW9uIjogImZhbHNlIgoJf...|
*注:*
base64编码后大小会增加约1/3
<a name = "#4"></a>
## 返回值
返回值为json串,各字段如下:
|参数|类型|说明|
|:------|:-----|:-----|
|code|string|结果码(具体见错误码)|
|data|json|详见data说明|
|desc|string|描述|
|sid|string|会话ID|
其中sid字段主要用于追查问题,如果出现问题,可以提供sid给讯飞技术人员帮助确认问题。
data各字段说明如下:
|参数|类型|说明|
|:------|:-----|:-----|
|block|对象数组|区域块信息|
|type|string|区域块类型(text-文本,image-图片)|
|line|对象数组|行信息|
|word|对象数组|字(中文),单词(英文)|
|content|string|内容|
|confidence|float|后验概率|
|location|对象|位置信息|
|top_left|对象|左上角位置信息|
|right_bottom|对象|右下角位置信息|
|x|int|对应点的横坐标(像素)|
|y|int|对应点的纵坐标(像素)|
*示例如下:*
失败:
{
"code": "10106",
"desc": "invalid parameter|invalid X-Appid",
"data": "",
"sid": "wcr0000bb3f@ch3d5c059d83b3477200"
}
成功
> 含位置信息
{
"code":"0",
"data":{
"block":[
{
"line":[
{
"confidence":1,
"word":[
{
"content":"with"
}
],
"location":{
"right_bottom":{
"y":52,
"x":180
},
"top_left":{
"y":10,
"x":113
}
}
}
],
"type":"text"
}
]
},
"sid":"wcr00000009@ch0fc40d9e4cdf000100",
"desc":"success"
}
> 不含位置信息
{
"code":"0",
"data":{
"block":[
{
"line":[
{
"confidence":1,
"word":[
{
"content":"with"
}
]
}
],
"type":"text"
}
]
},
"sid":"wcr00000008@ch0fc40d9e4c73000100",
"desc":"success"
}
<a name = "#5"></a>
## 调用示例
> python脚本示例
**说明**:将脚本中`IMAGE_PATH`, `API_KEY`, `APPID`, 换成相应的图片路径,讯飞开放平台提供的apiKey,讯飞开放平台应用的appid即可,运行脚本可打印相应结果。
```
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib2
import time
import urllib
import json
import hashlib
import base64
def main():
f = open("IMAGE_PATH", 'rb')
file_content = f.read()
base64_image = base64.b64encode(file_content)
body = urllib.urlencode({'image': base64_image})
url = 'http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting'
api_key = 'API_KEY'
param = {"language": "en", "location": "true"}
x_appid = 'APPID'
x_param = base64.b64encode(json.dumps(param).replace(' ', ''))
x_time = int(int(round(time.time() * 1000)) / 1000)
x_checksum = hashlib.md5(api_key + str(x_time) + x_param).hexdigest()
x_header = {'X-Appid': x_appid,
'X-CurTime': x_time,
'X-Param': x_param,
'X-CheckSum': x_checksum}
req = urllib2.Request(url, body, x_header)
result = urllib2.urlopen(req)
result = result.read()
print result
return
if __name__ == '__main__':
main()
```