python中baidu-aip是什么?

475次阅读
没有评论
python中baidu-aip是什么?

python中baidu-aip是什么?

1、ocr接口获取

首先登陆百度AI开放平台,进入到管理页面后,点击左侧菜单“文字识别”,再点击“创建应用”,如图所示:

2、创建一个应用:

名称为“图片文字识别”,会自动生成AppID和API Key。如图所示:

3、安装baidu-aip

pip install baidu-aip

4、调用方法

result=client.basicGeneral(img) 
result=client.basicAccurate(img)

5、实现代码

from aip import AipOcr
import glob
import os
cur_path=os.path.dirname(__file__)
jpgfile=os.path.join(cur_path, 'ocr',"幻灯片2.jpg")
jpgsfile=os.path.join(cur_path, 'ocr',"*.jpg")
resulttxt=os.path.join(cur_path,'ocr' ,"all.txt")
class ApiOcr():
def __init__(self,appid,api_key,secret_key):
self.appid=appid
self.api_key=api_key
self.secret_key=secret_key
 
def ocr(self,srcname):
client=AipOcr(self.appid,self.api_key,self.secret_key)
with open(srcname,'rb') as f:
img=f.read()
try:
result=client.basicGeneral(img) #通用识别,每天5万次免费
except:
result=client.basicAccurate(img) #高精度识别,每天500次免费
return result
#批量转化
def ocrs(self,filepath):
for x in glob.glob(filepath):
txts=[]
result=ApiOcr.ocr(self,x)
for txt in result.get('words_result'):
txts.append(txt.get('words'))
print(x)
ApiOcr.__write_file(self,x,txts)
 
def __write_file(self,picfile,txts):
with open(resulttxt,'a') as f:
f.writelines('识别图片:'+picfile+'n')
f.writelines('识别内容:'+'n')
f.writelines(txts)
f.writelines('n')
if __name__=='__main__':
AppID='21334886'
API_Key='RlMcTlCT0nv7SkRW4wvVcn1T'
Secret_Key='B8SBmqgr2RAVIFBqwaj4WioBOgGEtBBr'
api=ApiOcr(AppID,API_Key,Secret_Key)
print(api.ocr(jpgfile))
print(api.ocrs(jpgsfile))

6、输出结果

上述给大家介绍的案例,演示延伸学习baidu-aip,大家现在都掌握清楚了吧,好了,以上就是全部内容了,尝试学习吧~

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-10发表,共计1432字。
新手QQ群:570568346,欢迎进群讨论 Python51学习