# coding=utf-8
import os
import time
import random
import base64
import hashlib
import requests
from dataclasses import dataclass
@dataclass
class xunfeiAPIMsg():
api_id: str
api_key: str
api_url: str
image: str
curTime: str
def get_param_base64(self):
param = '{"engine_type": "recognize_document"}'
paramBase64 = base64.b64encode(param.encode("utf-8"))
return paramBase64.decode()
def get_img_base64str(self):
"""生成原始图片的base64编码数据,得到image参数"""
if not isinstance(self.image, str):
return None
if not os.path.isfile(self.image):
return None
with open(self.image, 'rb') as fp:
imgbase64 = base64.b64encode(fp.read())
return imgbase64.decode()
def gen_checkSum_md5(self):
sign = self.api_key + self.curTime + self.get_param_base64()
md5 = hashlib.md5()
md5.update(sign.encode())
return md5.hexdigest()
def get_ocr_result(self):
header = {
'X-CurTime': self.curTime,
'X-Param': self.get_param_base64(),
'X-Appid': self.api_id,
'X-CheckSum': self.gen_checkSum_md5(),
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', }
data = {'image': self.get_img_base64str()}
result = requests.post(
self.api_url, data=data, headers=header)
res = ''
try:
result = result.json()
for lines in result['data']['document']['blocks']:
res += lines['lines'][-1]['text'] + "\n"
except:
res = ''
return res
if __name__ == "__main__":
api_id = "5******6" #需要自己申请
api_key = "8************************a" #需要自己申请
api_url = "http://webapi.xfyun.cn/v1/service/v1/ocr/recognize_document"
curTime = str(int(time.time()))
image = "F:\\20180829193608871.jpg"
xunfei = xunfeiAPIMsg(api_id, api_key, api_url, image, curTime)
result = xunfei.get_ocr_result()
print(result)
Python3调用讯飞AI,进行文字识别
猜你喜欢
转载自blog.csdn.net/qq523176585/article/details/88854212
今日推荐
周排行