这里是使用了讯飞的OCR接口,每日500次免费
import sys
import requests
import time
import hashlib
import base64
import json
# 讯飞OCR接口
URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/general"
# 应用ID (必须为webapi类型应用,并印刷文字识别服务,参考帖子如何创建一个webapi应用:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)
APPID = "xxxx"
# 接口密钥(webapi类型应用开通印刷文字识别服务后,控制台--我的应用---印刷文字识别---服务的apikey)
API_KEY = "xxxxxxx"
# 每日500次免费
def getHeader():
"""根据AK计算请求头"""
# 当前时间戳
curTime = str(int(time.time()))
# 支持语言类型和是否开启位置定位(默认否)
param = {"language": "cn|en", "location": "false"}
param = json.dumps(param)
paramBase64 = base64.b64encode(param.encode('utf-8'))
m2 = hashlib.md5()
str1 = API_KEY + curTime + str(paramBase64, 'utf-8')
m2.update(str1.encode('utf-8'))
checkSum = m2.hexdigest()
# 组装http请求头
header = {
'X-CurTime': curTime,
'X-Param': paramBase64,
'X-Appid': APPID,
'X-CheckSum': checkSum,
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
}
return header
def get_content(path):
"""上传图片进行OCR"""
# 上传文件并进行base64位编码
with open(path, 'rb') as f:
f1 = f.read()
f1_base64 = str(base64.b64encode(f1), 'utf-8')
data = {'image': f1_base64}
#print('-->',path)
# 请求接口
r = requests.post(URL, data=data, headers=getHeader())
result = str(r.content, 'utf-8')
# 错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看)
cct = json.loads(result)
code = cct['code']
if code != '0':
print("--> 查询失败,图片是:", path.split('/')[-1], "\n--> 错误代码:", code)
print("--> 退出程序!")
sys.exit()
lis = []
for block in cct['data']['block']:
for line in block['line']:
lis.append(' '.join([i['content'] for i in line['word']]))
return lis
res = get_content('weapon/0_0.jpg')
print(res)
['源能者娱乐系统...']
weapon/0_0.jpg
评论区