Identify text data in image to read mm/dd, description and amount using opencv python

送分小仙女□ 提交于 2020-02-24 11:15:07

问题


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.

following is my image:


回答1:


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:

Result

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

Code

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()


来源:https://stackoverflow.com/questions/59848328/identify-text-data-in-image-to-read-mm-dd-description-and-amount-using-opencv-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!