Обрезать Обнаруженную область python - PullRequest
0 голосов
/ 03 февраля 2020
import cv2
import pytesseract
import numpy as np
from pytesseract import Output
import re
from pytesseract import image_to_string
import matplotlib.pyplot as plt

kernel = np.ones((5,5),np.uint8)

# noise removal
#this works on date
image = cv2.imread('1.Chase Bank_test.jpg')
result = image.copy()

gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)


thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_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), 5)

# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_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), 5)


date_list= []

d = pytesseract.image_to_data(thresh, output_type=Output.DICT)
#d = pytesseract.image_to_data(thresh)


keys = list(d.keys())

#date_pattern = '([\d]+\/[\d]+)'

#Regular expression to get date
date_pattern = '^(0[1-9]|[12]|[1-9]|3[02])/'
#amount= '((\d+)(\,)(\d+)(\.))+\d+'

description = ''

n_boxes = len(d['text']) # make the boxes around till the lenght text, put n_boxes in for loop
for i in range(n_boxes):
    if int(d['conf'][i]) > 10:
        if re.match(date_pattern, d['text'][i]):
            (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
            detect_img = cv2.rectangle(result, (x, y), (x + w, y + h), (0, 300, 0), 2) #dates works good with opening 




#logic for crop the detected date and append to date_list 
#for i in range(n_boxes):
#       crop_detect_img = detect_img[y:y+h+8, x:x+w+8]
#       test_list=(image_to_string((crop_detect_img)))
#       print(test_list)



#date_list.append(crop_detect_img)
#print(test_list)

crop_detect_img = detect_img[y:y+h+10, x:x+w+10]
crop_date_gray = cv2.cvtColor(crop_detect_img,cv2.COLOR_BGR2GRAY)


crop_date_thresh = cv2.threshold(crop_date_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]




date=(image_to_string((crop_date_thresh)))

print(date)



plt.figure(figsize = (20,20))
plt.imshow(crop_date_thresh)

#plt.imshow()
plt.imshow(result)
#print(result)


contours = contours[1] if imutils.is_cv3() else contours[0]



ROI_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
    cv2.rectangle(result,(x,y),(x+w,y+h),(36,255,12),2)
    ROI_number += 1

plt.imshow('thresh', thresh)

**

Не могу понять, как обрезать до обнаруженных дат по изображению в al oop, чтобы я мог передать обрезанные изображения в тессеракт OCR и добавить к дате список. И, пожалуйста, предложите мне лучшее распознавание текста в python, которое бесплатно. Так что я мог бы добиться лучших результатов. Я использую OpenCV для моей проблемы с regix.

** Как вы можете видеть, он находит все даты на изображении и создает границы

enter image description here

1 Ответ

2 голосов
/ 04 февраля 2020

Вы можете нарисовать ограничивающий прямоугольник, но не знаете, как его обрезать? Это странно.

В любом случае, я добавил соответствующий код ниже. Если это неправильно l oop, по крайней мере, теперь вы знаете, как обрезать изображение в numpy:

n_boxes = len(d['text']) # make the boxes around till the lenght text, put n_boxes in for loop
for i in range(n_boxes):
    if int(d['conf'][i]) > 10:
        if re.match(date_pattern, d['text'][i]):
            (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])

            # crop ROI and dump to a file
            cropped = gray[y : y+h, x : x+w]
            imwrite('crop_' + str(i) + '.png', cropped)                

            detect_img = cv2.rectangle(result, (x, y), (x + w, y + h), (0, 300, 0), 2) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...