Update ImageProcessor.py
Browse files- ImageProcessor.py +9 -6
ImageProcessor.py
CHANGED
|
@@ -7,6 +7,12 @@ import pytesseract
|
|
| 7 |
import warnings
|
| 8 |
import re
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def pdf2png(folderpath):
|
| 11 |
doc = fitz.open(folderpath + '/opinion.pdf')
|
| 12 |
zoom = 1
|
|
@@ -17,12 +23,11 @@ def pdf2png(folderpath):
|
|
| 17 |
|
| 18 |
def is_leftmost(image, x, y_top, y_bot):
|
| 19 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 20 |
-
blur = cv2.GaussianBlur(gray, (9,9), 0)
|
| 21 |
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
| 22 |
-
left_portion = thresh[int(
|
| 23 |
return np.sum(left_portion) == 0
|
| 24 |
|
| 25 |
-
|
| 26 |
def get_line_data(filename, body_bbox, page):
|
| 27 |
image = cv2.imread(filename)
|
| 28 |
body_rect = fitz.Rect(body_bbox)
|
|
@@ -30,7 +35,7 @@ def get_line_data(filename, body_bbox, page):
|
|
| 30 |
all_lines = [(int(line['bbox'][0]), int(line['bbox'][1]), int(line['bbox'][2]), int(line['bbox'][3]), line)for block in pg_dict['blocks'] for line in block['lines']]
|
| 31 |
line_data = []
|
| 32 |
for (i,l) in enumerate(all_lines):
|
| 33 |
-
if not is_leftmost(image, l[0]-
|
| 34 |
line_data[-1] = list(line_data[-1])
|
| 35 |
line_data[-1][-1] += " " + get_line_text(l[-1])
|
| 36 |
line_data[-1] = tuple(line_data[-1])
|
|
@@ -140,8 +145,6 @@ def get_page_elements(filename, page):
|
|
| 140 |
cv2.rectangle(image, (fn_bbox[0], fn_bbox[1]), (fn_bbox[2], fn_bbox[3]), (0, 0, 255), 2)
|
| 141 |
if case_separator_bbox[0] is not None:
|
| 142 |
cv2.rectangle(image, (case_separator_bbox[0], case_separator_bbox[1]), (case_separator_bbox[2], case_separator_bbox[3]), (255, 0, 255), 2)
|
| 143 |
-
# for (i, il) in enumerate(indent_lines):
|
| 144 |
-
# cv2.circle(image, (il[1]-15, int(0.5*(il[2] + il[4]))), radius=1, color=(240, 32, 160), thickness=2)
|
| 145 |
|
| 146 |
return page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, line_data, image
|
| 147 |
|
|
|
|
| 7 |
import warnings
|
| 8 |
import re
|
| 9 |
|
| 10 |
+
def show_image(img):
|
| 11 |
+
cv2.imshow("", img)
|
| 12 |
+
cv2.waitKey(0)
|
| 13 |
+
cv2.destroyAllWindows()
|
| 14 |
+
return
|
| 15 |
+
|
| 16 |
def pdf2png(folderpath):
|
| 17 |
doc = fitz.open(folderpath + '/opinion.pdf')
|
| 18 |
zoom = 1
|
|
|
|
| 23 |
|
| 24 |
def is_leftmost(image, x, y_top, y_bot):
|
| 25 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 26 |
+
blur = cv2.GaussianBlur(gray, (9, 9), 0)
|
| 27 |
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
| 28 |
+
left_portion = thresh[int(0.2*y_top+0.8*y_bot), :x]
|
| 29 |
return np.sum(left_portion) == 0
|
| 30 |
|
|
|
|
| 31 |
def get_line_data(filename, body_bbox, page):
|
| 32 |
image = cv2.imread(filename)
|
| 33 |
body_rect = fitz.Rect(body_bbox)
|
|
|
|
| 35 |
all_lines = [(int(line['bbox'][0]), int(line['bbox'][1]), int(line['bbox'][2]), int(line['bbox'][3]), line)for block in pg_dict['blocks'] for line in block['lines']]
|
| 36 |
line_data = []
|
| 37 |
for (i,l) in enumerate(all_lines):
|
| 38 |
+
if not is_leftmost(image, l[0]-9, l[1], l[3]) and i > 0: # Add it
|
| 39 |
line_data[-1] = list(line_data[-1])
|
| 40 |
line_data[-1][-1] += " " + get_line_text(l[-1])
|
| 41 |
line_data[-1] = tuple(line_data[-1])
|
|
|
|
| 145 |
cv2.rectangle(image, (fn_bbox[0], fn_bbox[1]), (fn_bbox[2], fn_bbox[3]), (0, 0, 255), 2)
|
| 146 |
if case_separator_bbox[0] is not None:
|
| 147 |
cv2.rectangle(image, (case_separator_bbox[0], case_separator_bbox[1]), (case_separator_bbox[2], case_separator_bbox[3]), (255, 0, 255), 2)
|
|
|
|
|
|
|
| 148 |
|
| 149 |
return page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, line_data, image
|
| 150 |
|