濮阳杆衣贸易有限公司

主頁 > 知識庫 > 利用Python批量識別電子賬單數(shù)據(jù)的方法

利用Python批量識別電子賬單數(shù)據(jù)的方法

熱門標簽:400電話辦理費用收費 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 騰訊地圖標注有什么版本 千呼ai電話機器人免費 外呼系統(tǒng)前面有錄音播放嗎 申請辦個400電話號碼 高德地圖標注字母 柳州正規(guī)電銷機器人收費 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商

一、前言

有一定數(shù)量類似如下截圖所示的賬單,利用 Python 批量識別電子賬單數(shù)據(jù),并將數(shù)據(jù)保存到Excel。

百度智能云接口
打開https://cloud.baidu.com/,如未注冊請先注冊,然后登錄點擊管理控制臺,點擊左側(cè)產(chǎn)品服務(wù)→人工智能→文字識別,點擊創(chuàng)建應(yīng)用,輸入應(yīng)用名稱如Baidu_OCR,選擇用途如學(xué)習(xí)辦公,最后進行簡單應(yīng)用描述,即可點擊立即創(chuàng)建。會出現(xiàn)應(yīng)用列表,包括AppID、API Key、Secret Key等信息,這些稍后會用到。

二、調(diào)用Baidu aip識別

首先需要安裝百度的接口,命令行輸入如下:

pip install baidu-aip -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

查看 Python 的 SDK 文檔:

AipOcr是 OCR 的 Python SDK 客戶端,為使用 OCR 的開發(fā)人員提供了一系列的交互方法。參考如下代碼新建一個AipOcr:

from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

用戶向服務(wù)請求識別某張圖中的所有文字

""" 讀取圖片 """
def get_file_content(filePath):
 with open(filePath, 'rb') as fp:
  return fp.read()

image = get_file_content('example.jpg')

""" 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 """
client.basicGeneral(image)
""" 調(diào)用通用文字識別(高精度版) 圖片參數(shù)為本地圖片 """
client.basicAccurate(image)

識別出如下圖片中的文字,示例如下:

from aip import AipOcr

# """ 改成你的 百度云服務(wù)的 ID AK SK """
APP_ID = '18690701'
API_KEY = 'QFaTVXvZdPrR05dNlR5I49xA'
SECRET_KEY = '*******************************'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

def get_file_content(filePath):
 with open(filePath, 'rb') as fp:
  return fp.read()

image = get_file_content('example.jpg')
# 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片
result = client.basicGeneral(image)
print(result)
# 提取識別結(jié)果
info = '\n'.join([i['words'] for i in result['words_result']])
print(info)

結(jié)果如下:

三、批量識別電子賬單

獲取所有待識別的電子賬單圖像

from pathlib import Path

# 換成你放圖片的路徑
p = Path(r'D:\test\test_img')
# 得到所有文件夾下 .jpg 圖片
file = p.glob('**/*.jpg')
for img_file in file:
 print(type(img_file)) # class 'pathlib.WindowsPath'> 轉(zhuǎn)成str
 img_file = str(img_file)
 print(img_file)

為了增加識別準確率,將賬單上要提取的數(shù)據(jù)區(qū)域分割出來,再調(diào)用Baidu aip識別。

from pathlib import Path
import cv2 as cv
from aip import AipOcr
from time import sleep

APP_ID = '18690701'
API_KEY = 'QFaTVXvZdPrR05dNlR5I49xA'
SECRET_KEY = '**********************************'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

""" 讀取圖片 """
def get_file_content(filePath):
 with open(filePath, 'rb') as fp:
  return fp.read()

def identity(num):
 result_list = []
 for i in range(num):
  image = get_file_content('img{}.jpg'.format(i))
  """ 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 """
  result = client.basicGeneral(image)
  print(result)
  sleep(2)
  # 識別結(jié)果
  info = ''.join([i['words'] for i in result['words_result']])
  result_list.append(info)
 print(result_list)

src = cv.imread(r'D:\test\test_img\001.jpg')
src = cv.resize(src, None, fx=0.5, fy=0.5)
# print(src.shape)
img = src[280:850, 10:580]  # 截取圖片 高 寬
money = img[70:130, 150:450]  # 支出 收入金額
goods = img[280:330, 160:560]  # 商品
time_1 = img[380:425, 160:292] # 支付時間 年月日
time_2 = img[380:425, 160:390] # 支付時間 完整
way = img[430:475, 160:560]  # 支付方式
num_1 = img[480:520, 160:560]  # 交易單號
num_2 = img[525:570, 160:560]  # 商戶單號
img_list = [money, goods, time_1, time_2, way, num_1, num_2]
for index_, item in enumerate(img_list):
 cv.imwrite(f'img{index_}.jpg', item)

identity(len(img_list))

發(fā)現(xiàn)調(diào)用 client.basicGeneral(image),通用文字識別,-5.90識別成590,而圖像里支付時間年月日 時分秒之間間隔小,識別出來都在一起了,需要把支付時間的年月日 時分秒分別分割出來識別,調(diào)用 client.basicAccurate(image),通用文字識別(高精度版)。

完整實現(xiàn)如下:

"""
@File :test_01.py
@Author :葉庭云
@CSDN :https://yetingyun.blog.csdn.net/
"""
from aip import AipOcr
from pathlib import Path
import cv2 as cv
from time import sleep
import openpyxl


wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(['消費', '商品', '支付時間', '支付方式', '交易單號', '商品單號'])
# """ 改成你的 百度云服務(wù)的 ID AK SK """
APP_ID = '18690701'
API_KEY = 'QFaTVXvZdPrR05dNlR5I49xA'
SECRET_KEY = '*******************************'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

""" 讀取圖片 """
def get_file_content(filePath):
 with open(filePath, 'rb') as fp:
  return fp.read()


def identity(num):
 result_list = []
 for i in range(num):
  image = get_file_content('img{}.jpg'.format(i))
  """ 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 """
  result = client.basicAccurate(image)
  print(result)
  sleep(1)
  # 識別結(jié)果
  info = ''.join([i['words'] for i in result['words_result']])
  result_list.append(info)

 result_list[2] = result_list[2] + ' ' + result_list[3]
 result_list.pop(3)
 print(result_list)
 sheet.append(result_list)


# 換成你放圖片的路徑
p = Path(r'D:\test\test_img')
# 得到所有文件夾下 .jpg 圖片
file = p.glob('**/*.jpg')
for img_file in file:
 img_file = str(img_file)
 src = cv.imread(r'{}'.format(img_file))
 src = cv.resize(src, None, fx=0.5, fy=0.5)
 # print(src.shape)
 img = src[280:850, 10:580]  # 截取圖片 高、寬范圍
 money = img[70:130, 150:450]  # 支出金額
 goods = img[280:330, 160:560]  # 商品
 time_1 = img[380:425, 160:292] # 支付時間 年月日
 time_2 = img[380:425, 290:390] # 支付時間 時分秒
 way = img[430:475, 160:560]  # 支付方式
 num_1 = img[480:520, 160:560]  # 交易單號
 num_2 = img[525:570, 160:560]  # 商戶單號
 img_list = [money, goods, time_1, time_2, way, num_1, num_2]
 for index_, item in enumerate(img_list):
  cv.imwrite(f'img{index_}.jpg', item)
 identity(len(img_list))
 # cv.imshow('img', img)
 # cv.imshow('goods', time_2)
 # cv.waitKey(0)

wb.save(filename='識別賬單結(jié)果.xlsx')

結(jié)果如下:

識別結(jié)果還不錯,成功利用 Python 批量識別電子賬單數(shù)據(jù),并將數(shù)據(jù)保存到Excel。

到此這篇關(guān)于利用Python批量識別電子賬單數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python識別電子賬單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python操控mysql批量插入數(shù)據(jù)的實現(xiàn)方法
  • Python用5行代碼實現(xiàn)批量摳圖的示例代碼
  • python中使用you-get庫批量在線下載bilibili視頻的教程

標簽:海南 郴州 大慶 平頂山 烏蘭察布 哈爾濱 合肥 烏蘭察布

巨人網(wǎng)絡(luò)通訊聲明:本文標題《利用Python批量識別電子賬單數(shù)據(jù)的方法》,本文關(guān)鍵詞  利用,Python,批量,識別,電子,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用Python批量識別電子賬單數(shù)據(jù)的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用Python批量識別電子賬單數(shù)據(jù)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    封丘县| 平度市| 江孜县| 新巴尔虎左旗| 剑阁县| 广丰县| 丹江口市| 威宁| 田林县| 乐陵市| 黄平县| 全椒县| 客服| 长武县| 壶关县| 东辽县| 陆良县| 镇江市| 平顶山市| 伽师县| 宣武区| 页游| 邮箱| 清涧县| 清徐县| 铜川市| 称多县| 那坡县| 华容县| 永嘉县| 曲阳县| 塘沽区| 百色市| 鄂伦春自治旗| 霍邱县| 沾益县| 三门县| 凌云县| 金门县| 梁河县| 岐山县|