Чтобы идентифицировать текст на изображении, необходимо предварительно обработать изображение. Чтобы сделать это, мы можем удалить горизонтальные и вертикальные линии сетки, а затем бросить изображение в OCR Pytesseract. Вот обнаруженные строки, которые нужно удалить, выделены зеленым цветом:
Результат
Вывод из 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()