教你用Python實(shí)現(xiàn)自動(dòng)提取并收集信息的功能
一、簡(jiǎn)介
- 本功能目的在于提取收據(jù)/發(fā)票上的信息,用機(jī)器代替人的方式,提高工作效率
- 實(shí)現(xiàn)方式是通過cv2模塊截取需要的信息,調(diào)用百度的api文字識(shí)別接口

二、代碼實(shí)現(xiàn)
1.導(dǎo)入需要的庫(kù),包括百度的api接口跟cv2圖像截圖圖庫(kù)
import cv2
from aip import AipOcr
# 讀取圖片,利用imshow顯示圖片
pic = cv2.imread(r'Y:\cut\img1.png')
pic = cv2.resize(pic,None,fx = 0.5, fy = 0.5)
cv2.imshow('img',pic)
cv2.waitKey(0)
2.截取圖片,獲取需要的信息,包括以下信息
- 時(shí)間Time
- 商家business
- 商品goods
- 價(jià)格money
- 單號(hào)num
# 刪除不必要的部分
img = pic[210:500, 100:580]
# 截取各部分的文字
time = pic[400:430, 100:580]
business = pic[370:400, 100:580]
goods = pic[350:380, 100:580]
money = pic[210:300, 100:580]
num = pic[460:500, 100:580]
# 查看截取的部分是否合適
gener_name = ['time','business','goods','money','num']
excel_data = {}
pd_columns = ["a","b","c","d","e"] # 標(biāo)題




3.定義函數(shù)將截取好的圖片另存到文件夾
def shotcut_image(args):
for index in gener:
cv2.imwrite('image/{}.png'.format(args), img)
4.調(diào)用百度api接口,實(shí)現(xiàn)文字識(shí)別
# 導(dǎo)入api
AppID = '24177719'
API_Key = 'p8skmRYfHGoVGR4UU03Q5jiM'
Secret_Key = 'dyM0tzSILBZu9CFqZ7IkjWwECGaws4xo'
cilent = AipOcr(AppID,API_Key,Secret_Key)
def get_words(img_name):
with open('image/{}.png'.format(img_name), 'rb') as f:
result = cilent.basicAccurate(f.read())
return result
5.最后將信息轉(zhuǎn)為Dataframe,利用pandas的to_exccel功能,將數(shù)據(jù)放到excel里面
def convert_to_dataframe(words):
# 構(gòu)建dataframe
result = words['words_result']
for word in result:
excel_data.setdefault('a', []).append(word['words'])
# 將所有words讀取后,取出語(yǔ)句存入excel
def convert_to_excel():
frame = DataFrame(excel_data, columns=pd_columns)
# todo 表頭需要額外處理,這里指定不設(shè)置表頭
frame.to_excel('out.xls',index=False, header=False)

到此這篇關(guān)于教你用Python實(shí)現(xiàn)自動(dòng)提取并收集信息的功能的文章就介紹到這了,更多相關(guān)Python自動(dòng)提取收集信息功能內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信