Pythonを利用した電子計算書データの一括認識方法
はじめに
下記のような請求書があります。Pythonで電子計算書データを一括で識別し、Excelにデータを保存します。

Baiduのスマートクラウドインターフェース
開くhttps://cloud.baidu.com/登録していない場合は、まず登録してください。管理コンソールをクリックして、左側の製品サービス→人工知能→文字認識をクリックして、アプリケーションを作成します。OCRは、学習事務などの用途を選択し、最後に簡単な応用説明を行います。クリックしてすぐに作成できます。アプリリストが表示されます。アプリID、API Key、Secret Keyなどの情報が含まれています。これらは後で使います。


二、Baidu aipを呼び出して識別する
まず百度のインターフェースをインストールしてください。コマンドラインは以下の通り入力します。


AipOcrはOCRのPython SDKクライアントであり、OCRを利用した開発者に一連のインタラクション方法を提供する。参考下記のコードでAipOcrを新規作成します。


三、大量識別電子請求書
識別すべき電子計算書の画像をすべて取得します。


client.baic Generalを呼び出し、汎用文字認識、-5.90は590と認識されていますが、画像内での支払い時間年月日は分秒間隔が小さいので、認識は全部一緒になりました。支払い時間の年月日は分秒に分けて識別し、client.baicAccuurate(image)を呼び出し、汎用文字認識(高精度版)が必要です。
完全な実装は以下の通りです。


識別結果は悪くないです。Pythonを利用して電子計算書データを大量に識別し、Excelにデータを保存しました。
ここで、Pythonを利用して電子計算書のデータを大量に識別することについての記事を紹介します。Python関連の電子計算書の内容は以前の文章を検索してください。または下記の関連記事を引き続きご覧ください。これからもよろしくお願いします。
下記のような請求書があります。Pythonで電子計算書データを一括で識別し、Excelにデータを保存します。

Baiduのスマートクラウドインターフェース
開くhttps://cloud.baidu.com/登録していない場合は、まず登録してください。管理コンソールをクリックして、左側の製品サービス→人工知能→文字認識をクリックして、アプリケーションを作成します。OCRは、学習事務などの用途を選択し、最後に簡単な応用説明を行います。クリックしてすぐに作成できます。アプリリストが表示されます。アプリID、API Key、Secret Keyなどの情報が含まれています。これらは後で使います。


二、Baidu aipを呼び出して識別する
まず百度のインターフェースをインストールしてください。コマンドラインは以下の通り入力します。
pip install baidu-aip -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
PythonのSDK文書を表示します。

AipOcrはOCRのPython SDKクライアントであり、OCRを利用した開発者に一連のインタラクション方法を提供する。参考下記のコードで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)
ユーザーはサービス要求に対して、ある図のすべてのテキストを識別する。
""" """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" , """
client.basicGeneral(image)
""" ( ) """
client.basicAccurate(image)
次のような画像のテキストを識別しました。例は以下の通りです。
from aip import AipOcr
# """ 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')
# ,
result = client.basicGeneral(image)
print(result)
#
info = '
'.join([i['words'] for i in result['words_result']])
print(info)
結果は以下の通りです
三、大量識別電子請求書
識別すべき電子計算書の画像をすべて取得します。
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'> str
img_file = str(img_file)
print(img_file)
識別精度を高めるために、請求書から抽出するデータ領域を分割し、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))
""" , """
result = client.basicGeneral(image)
print(result)
sleep(2)
#
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))

client.baic Generalを呼び出し、汎用文字認識、-5.90は590と認識されていますが、画像内での支払い時間年月日は分秒間隔が小さいので、認識は全部一緒になりました。支払い時間の年月日は分秒に分けて識別し、client.baicAccuurate(image)を呼び出し、汎用文字認識(高精度版)が必要です。
完全な実装は以下の通りです。
"""
@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([' ', ' ', ' ', ' ', ' ', ' '])
# """ 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))
""" , """
result = client.basicAccurate(image)
print(result)
sleep(1)
#
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=' .xlsx')
結果は以下の通りです

識別結果は悪くないです。Pythonを利用して電子計算書データを大量に識別し、Excelにデータを保存しました。
ここで、Pythonを利用して電子計算書のデータを大量に識別することについての記事を紹介します。Python関連の電子計算書の内容は以前の文章を検索してください。または下記の関連記事を引き続きご覧ください。これからもよろしくお願いします。