百度OCR接口调用:
去百度智能云管理中心(找不到直接百度一下“OCR识别”,百度把自己排在第一位了)新建项目,查看生成的AppID、API Key、Secret Key。
用python调用。顺便一提,aip这个module名字不太一样。conda和pip时候没有找到(其他人好像能装上,我也是奇了怪了)。后来在其他人那里看到正确的加载方式:pip install baidu-aip
from aip import AipOcr
import glob
import os
import shutil
APP_ID = 'xxxxxx' #'你的 App ID'
API_KEY = 'xxxxxx' #'你的 Api Key'
SECRET_KEY = 'xxxxxx' #'你的 Secret Key'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
path_pic = 'pic' #图片存放路径
path_save = 'baidu' #文字储存路径
if not os.path.exists(path_save):
os.mkdir(path_save)
for path in glob.glob(os.path.join(path_pic,'*.jpg')):
image = get_file_content(path)
#res=client.basicGeneralUrl(url);
res=client.general(image)
name = path.split('/')[-1][:-4]
path_txt = path_save + '/' + name + '.txt'
path_txt1 = path_save + '/1' + name + '.txt'
file = open(path_txt,'w')
file.write(str(res['words_result']))
file.close()
file2=open(path_txt1,'w')
for item in res['words_result']:
print(item['words'])
file2.write(item['words'])
file2.close()
顺便说一下,调用回来的格式是有位置信息和文字。图示:
[{‘location’: {‘width’: 513, ‘top’: 36, ‘left’: 7, ‘height’: 60}, ‘words’: ‘努力吧,直到你的账’}, {‘location’: {‘width’: 514, ‘top’: 115, ‘left’: 4, ‘height’: 62}, ‘words’: ‘户余额看起来像电话’}, {‘location’: {‘width’: 145, ‘top’: 198, ‘left’: 175, ‘height’: 58}, ‘words’: ‘号码。’}, {‘location’: {‘width’: 96, ‘top’: 261, ‘left’: 433, ‘height’: 24}, ‘words’: ‘S6GNET’}]