目录
1.调用费用
2.调用流程:
1)注册有道智云账号
2)
3)获取应用ID、应用密钥
3.json文件主要信息(举例信息以字典形式给出)
开通账户,赠送50元券,约等于首次开通账户,赠送200次调用次数
调用费用:小于等于10万次-->0.25/次
官方链接:价格中心-有道智云AI开放平台 (youdao.com)
有道智云链接:有道智云AI开放平台 (youdao.com)
注册用户会向其账户赠送50元体验金,可免费体验智云所有服务
首页--鼠标点击右上角用户名--“光学字符识别服务”--表格识别--创建应用
4)调用代码
代码修改初始信息:APP_KEY、APP_SECRET、img_path、save_path
import json
import sys
import uuid
import requests
import base64
import hashlib
import time
import importlib
importlib.reload(sys)
YOUDAO_URL = 'https://openapi.youdao.com/ocr_table'
APP_KEY = '7758d732ef4c676d'
APP_SECRET = ''
image_path = 'D:\\table_generation-master\\demo\\table_recog_test\\0M208145r02110zw_1.png'
save_path = "D:\\table_generation-master\\demo\\baidu_result\\jit_youdao.json"
def truncate(q):
if q is None:
return None
q_utf8 = q.decode("utf-8")
size = len(q_utf8)
return q_utf8 if size <= 20 else q_utf8[0:10] + str(size) + q_utf8[size - 10:size]
def encrypt(signStr):
hash_algorithm = hashlib.sha256()
hash_algorithm.update(signStr.encode('utf-8'))
return hash_algorithm.hexdigest()
def do_request(data):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
return requests.post(YOUDAO_URL, data=data, headers=headers)
def connect():
f = open(image_path, 'rb') # 二进制方式打开图文件
q = base64.b64encode(f.read()) # 读取文件内容,转换为base64编码
f.close()
data = {}
data['type'] = '1'
data['q'] = q
data['docType'] = 'json'
data['signType'] = 'v3'
curtime = str(int(time.time()))
data['curtime'] = curtime
salt = str(uuid.uuid1())
signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
sign = encrypt(signStr)
data['appKey'] = APP_KEY
data['salt'] = salt
data['sign'] = sign
response = do_request(data)
print(response.content)
print(response)
print(type(response.content))
response = str(response.content, 'utf-8')
print(type(response))
with open(save_path, 'w', encoding='utf-8') as fp:
fp.write(response)
if __name__ == '__main__':
connect()
其他信息详见产品文档-文字识别OCR服务 (youdao.com)
有道api:
table
boundingBox:整体外框四顶点坐标
cells:
boundingBox:单元格四顶点坐标
rowRange:合并单元格行的跨度(a,b)
colRange:合并单元格列的跨度(a,b)
dir:h-->水平;v-->竖直
lang:zh-->中文
lines(单元格内每一行信息):
boundingBox:该行文本外框四顶点坐标
words:逐字信息
text:该行文本信息