1 注册百度账号
这一步只要有一个百度账号就行。
2 获取app_id、api_key、secret_key
使用百度账号登录 百度智能云。
然后依次点击选择 产品服务->人工智能->文字识别,如下所示
到了如下界面后,依次点击 应用列表->创建应用
接着就简单的填写一下你所要创建应用的简单信息:
然后就出现了app_id、api_key、secret_key
3 上代码
安装百度的aip包:
如果已安装pip,执行pip install baidu-aip
将替换掉下面代码的app_id、api_key、secret_key
from aip import AipOcr
import os
#读取图片,path用于传入读取图片的名字
dir = 'ocr\\'
def read_image(path):
dir_i = dir + '\\'
print(dir_i+path)
with open(dir_i+path, 'rb') as f:
image = f.read()
return image
api_key = '自己的api_key'
app_id='自己的app_id'
secret_key = '自己的secret_key'
client=AipOcr(app_id,api_key,secret_key)
fs=os.listdir(dir)
file=open(r'output.txt','w',encoding='utf-8')
for image in fs:
i=read_image(image)
inf=client.basicGeneral(i)
for response in inf['words_result']:
for words in response['words']:
file.write(words)
file.write('\n')
print(inf)
file.close()