APP_ID = '********'#百度申请的app_id
API_KEY = '***********'#百度申请的app_key
SECRET_KEY = '**********'#百度申请的secret_key
path = r'D:/python'#图片存放的文件夹,需确保文件夹只有一张图片,否则有可能识别的是其他图片
text = ""
for filename in os.listdir(path):
if filename.endswith('png') or filename.endswith('jpg'):
text = filename
break
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#认证身份
fp=open(text,"rb").read()
res=client.basicGeneral(fp)#将图片转换为文字
with open("result.txt","w") as f:#将结果保存在当前文件夹下
for tex in res["words_result"]:
f.write(tex["words"])
f.write('\r\n')
下面是识别的图片和识别的结果