一、簡介
- 本功能目的在于提取收據/發票上的信息,用機器代替人的方式,提高工作效率
- 實現方式是通過cv2模塊截取需要的信息,調用百度的api文字識別接口

二、代碼實現
1.導入需要的庫,包括百度的api接口跟cv2圖像截圖圖庫
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.截取圖片,獲取需要的信息,包括以下信息
- 時間Time
- 商家business
- 商品goods
- 價格money
- 單號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"] # 標題




3.定義函數將截取好的圖片另存到文件夾
def shotcut_image(args):
for index in gener:
cv2.imwrite('image/{}.png'.format(args), img)
4.調用百度api接口,實現文字識別
# 導入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.最后將信息轉為Dataframe,利用pandas的to_exccel功能,將數據放到excel里面
def convert_to_dataframe(words):
# 構建dataframe
result = words['words_result']
for word in result:
excel_data.setdefault('a', []).append(word['words'])
# 將所有words讀取后,取出語句存入excel
def convert_to_excel():
frame = DataFrame(excel_data, columns=pd_columns)
# todo 表頭需要額外處理,這里指定不設置表頭
frame.to_excel('out.xls',index=False, header=False)

到此這篇關于教你用Python實現自動提取并收集信息的功能的文章就介紹到這了,更多相關Python自動提取收集信息功能內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python自動提取文本中的時間(包含中文日期)
- 十個Python自動化常用操作,即拿即用
- 如何用 Python 子進程關閉 Excel 自動化中的彈窗
- 教你怎么用Python處理excel實現自動化辦公
- Python 制作自動化翻譯工具
- python實現百度文庫自動化爬取
- 使用Python自動化Microsoft Excel和Word的操作方法
- python使用pytest接口自動化測試的使用
- python+requests+pytest接口自動化的實現示例
- python+pywinauto+lackey實現PC端exe自動化的示例代碼
- python自動化調用百度api解決驗證碼
- python 自動化偷懶的四個實用操作
- python實現自動化群控的步驟
- python之Django自動化資產掃描的實現
- Python 實現自動化Excel報表的步驟
- python 如何用 Hypothesis 來自動化單元測試
- python自動化實現自動回復QQ消息