使用opencv python识别图像中的文本数据以读取mm/dd,描述和数量 [英] Identify text data in image to read mm/dd, description and amount using opencv python

查看:223
本文介绍了使用opencv python识别图像中的文本数据以读取mm/dd,描述和数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导入 导入cv2 导入pytesseract 从pytesseract导入输出 从PIL导入图像

import re import cv2 import pytesseract from pytesseract import Output from PIL import Image

from pytesseract import image_to_string

img = cv2.imread('/home/cybermakarov/Desktop/1.Chase Bank-page-002.jpg')
d = pytesseract.image_to_data(img, output_type=Output.DICT)
keys = list(d.keys())


date_pattern = '^(0[1-9]|[12]|[1-9]|3[02])/'
Description_pattern='([0-9]+\/[0-9]+)|([0-9]+)|([0-9\,\.]+)'


n_boxes = len(d['text'])
for i in range(n_boxes):
    if int(d['conf'][i]) > 60:
        if re.match(description_pattern, d['text'][i]):
            (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
            detect_img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 300, 0), 2)

crop_img = img[y:y+h, x:x+w]

cv2.imshow('img',img)
#cv2.imshow("Cropped",  crop_img)
cv2.waitKey(0)

请帮助我使用正则表达式来标识日期,描述和金额, 我确实尽力确定这些模式,但不能.第二 问题是我想在代码标识日期时裁剪图像, 说明和金额.

Please help me to identify date, description and amount using regular expressions, i really tried hard to identify these pattern but could not. Second thing is that I want to crop image when code identifies the date, description and amount.

以下是我的图片:

推荐答案

要识别图像中的文本,必须预处理图像.为此,我们可以删除水平和垂直网格线,然后将图像放入Pytesseract OCR中.这是检测到的要删除的行,以绿色突出显示:

To identify the text in the image, you must preprocess the image. To do this, we can remove the horizontal and vertical grid lines then throw the image into Pytesseract OCR. Here's the detected lines to be removed highlighted in green:

结果

Pytesseract的输出

Output from Pytesseract

ELECTRONIC WITHDRAWALS

DATE DESCRIPTION AMOUNT
01/02 Merchant Service Merch Fee 8030996550 CCD ID: 1841010148 $34.30
01/03 Authnet Gateway Billing 104820413 CCD ID: 1870568569 22.95
01/04 01/04 Online Transfer To Mma ...4622 Transaction#: 7794410276 200.00
01/08 01/08 Online Payment 7732727073 To Cleaning Connoisseur 300.00
01/11 01/11 Online Payment 7744233248 To Ramsey Sweis 148.80
01/11 01/11 Online Transfer To Mma ...4622 Transaction#: 7816805988 200.00
01/11 01/11 Payment To Chase Card Ending IN 2342 500.00
01/11 Aqaba Holdings, Inahl-51 Ahl-51 CCD ID: 1113720048 1,441.21
01/14 01/12 Payment To Chase Card Ending IN 2342 1,000.00
01/14 01/12 Online Transfer To Mma ...4622 Transaction#: 7841026719 1,000.00
01/16 01/16 Payment To Chase Card Ending IN 2342 1,000.00
01/16 01/16 Online Payment 7852542882 To Oakhurst Golf & Country Club 495.00
01/17 01/17 Online Payment 7762351731 To Ramsey Sweis 399.05
01/18 01/18 Online Transfer To Mma ...4622 Transaction#: 7837118990 200.00
01/18 Small Business Icpayment PPD ID: 1131414876 302.24
01/22 01/21 Payment To Chase Card Ending IN 2342 1,000.00
01/23 01/23 Payment To Chase Card Ending IN 2342 1,000.00
01/25 01/25 Online Payment 77868551 17 To Lv Office Limited Partnership 644.40

代码

import cv2
import pytesseract
import numpy as np

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.jpg')
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (35,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(result, [c], -1, (255,255,255), -1)

# Find vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,15))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(result, [c], -1, (255,255,255), -1)

data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey()

这篇关于使用opencv python识别图像中的文本数据以读取mm/dd,描述和数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆