python使用aip库识别图片中文字

一、获取百度智能云API的AppID / API Key / Secret Key

1、创建应用
百度智能云登录地址:https://login.bce.baidu.com/
在这里插入图片描述
2、立即创建
在这里插入图片描述
3、得到AppID / API Key / Secret Key
在这里插入图片描述

二、测试图片识别文字并保存到本地

安装百度aip库

pip install baidu-aip

测试代码

from aip import AipOcr
from os import path
APP_ID = "Your AppID"
API_KEY = "Your API Key"
SECRET_KEY = "Your Secret Key"
baidu = AipOcr(APP_ID,API_KEY,SECRET_KEY)  
father_path = path.dirname(__file__) #得到父级文件夹
content = ''
with open('本地图片地址','rb') as f:
  image = f.read()    #得到图片的二进制内容
  text = baidu.basicAccurate(image)      #调用百度的接口识别图片文字内容
  result = text['words_result']			#获取返回内容的列表
  for i in result:	
    content += i['words'] + '\n' 
# 将文件内容写入txt文件
fp = open('%s\识别内容.txt'%father_path,'a+',encoding='utf-8')
print(content,file=fp)
fp.close


版权声明:本文为weixin_51033461原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。