Delete ImageProcessor.py
Browse files- ImageProcessor.py +0 -192
ImageProcessor.py
DELETED
|
@@ -1,192 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import fitz
|
| 3 |
-
import numpy as np
|
| 4 |
-
import os
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import pytesseract
|
| 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
|
| 19 |
-
mat = fitz.Matrix(zoom, zoom)
|
| 20 |
-
for (i, p) in enumerate(doc):
|
| 21 |
-
pix = p.get_pixmap(matrix=mat)
|
| 22 |
-
pix.save(folderpath + '/' + str(i) + '.png')
|
| 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)
|
| 34 |
-
pg_dict = page.get_text('dict', clip=body_rect)
|
| 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])
|
| 42 |
-
else:
|
| 43 |
-
line_data.append((l[0], l[1], l[2], l[3], get_line_text(l[-1])))
|
| 44 |
-
return line_data
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def get_footnote_bbox(filename):
|
| 48 |
-
footnotes_bbox = (None, None, None, None)
|
| 49 |
-
x1p, y1p, x2p, y2p = get_page_bbox(filename)
|
| 50 |
-
x1h, y1h, x2h, y2h = get_header_bbox(filename)
|
| 51 |
-
image = cv2.imread(filename)
|
| 52 |
-
im_h, im_w, im_d = image.shape
|
| 53 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 54 |
-
thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)[1]
|
| 55 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
|
| 56 |
-
dilate = cv2.dilate(thresh, kernel, iterations=1)
|
| 57 |
-
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 58 |
-
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
| 59 |
-
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[1])
|
| 60 |
-
for (i, c) in enumerate(cnts):
|
| 61 |
-
x, y, w, h = cv2.boundingRect(c)
|
| 62 |
-
if h < 7 and w > 50 and y > y1p and x - x1p < 30:
|
| 63 |
-
footnotes_bbox = (x, y, x2p, y2p)
|
| 64 |
-
return footnotes_bbox
|
| 65 |
-
|
| 66 |
-
def get_header_bbox(filename):
|
| 67 |
-
image = cv2.imread(filename)
|
| 68 |
-
im_h, im_w, im_d = image.shape
|
| 69 |
-
base_image = image.copy()
|
| 70 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 71 |
-
blur = cv2.GaussianBlur(gray, (9,9), 0)
|
| 72 |
-
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
| 73 |
-
|
| 74 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (200,10))
|
| 75 |
-
dilate = cv2.dilate(thresh, kernel, iterations=1)
|
| 76 |
-
|
| 77 |
-
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 78 |
-
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
| 79 |
-
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[1])
|
| 80 |
-
for (i,c) in enumerate(cnts):
|
| 81 |
-
x,y,w,h = cv2.boundingRect(c)
|
| 82 |
-
break
|
| 83 |
-
header_bbox = (x, y, x+w, y+40)
|
| 84 |
-
return header_bbox
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def get_page_bbox(filename):
|
| 88 |
-
image = cv2.imread(filename)
|
| 89 |
-
im_h, im_w, im_d = image.shape
|
| 90 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 91 |
-
blur = cv2.GaussianBlur(gray, (7, 7), 0)
|
| 92 |
-
thresh = cv2.threshold(blur, 240, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
| 93 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 10))
|
| 94 |
-
dilate = cv2.dilate(thresh, kernel, iterations=1)
|
| 95 |
-
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 96 |
-
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
| 97 |
-
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[1])
|
| 98 |
-
|
| 99 |
-
header_bbox = get_header_bbox(filename)
|
| 100 |
-
all_x1 = [cv2.boundingRect(c)[0] for c in cnts]
|
| 101 |
-
all_y1 = [cv2.boundingRect(c)[1] for c in cnts]
|
| 102 |
-
all_x2 = [cv2.boundingRect(c)[0] + cv2.boundingRect(c)[2] for c in cnts]
|
| 103 |
-
all_y2 = [cv2.boundingRect(c)[1] + cv2.boundingRect(c)[3] for c in cnts]
|
| 104 |
-
return min(all_x1), header_bbox[1], max(all_x2), max(all_y2)
|
| 105 |
-
|
| 106 |
-
def get_case_separator(filename):
|
| 107 |
-
new_case_line = (None, None, None, None)
|
| 108 |
-
x1p, y1p, x2p, y2p = get_page_bbox(filename)
|
| 109 |
-
x1h, y1h, x2h, y2h = get_header_bbox(filename)
|
| 110 |
-
|
| 111 |
-
image = cv2.imread(filename)
|
| 112 |
-
im_h, im_w, im_d = image.shape
|
| 113 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 114 |
-
blur = cv2.GaussianBlur(gray, (7, 7), 0)
|
| 115 |
-
thresh = cv2.threshold(blur, 240, 255, cv2.THRESH_BINARY_INV)[1]
|
| 116 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
|
| 117 |
-
dilate = cv2.dilate(thresh, kernel, iterations=1)
|
| 118 |
-
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 119 |
-
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
| 120 |
-
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[1])
|
| 121 |
-
for (i, c) in enumerate(cnts):
|
| 122 |
-
x, y, w, h = cv2.boundingRect(c)
|
| 123 |
-
x_center = (x1p + x2p) / 2
|
| 124 |
-
if h < 8 and w > 70 and ((x - x1p) < x_center and (x - x1p) > 0.3 * x_center) and (y > y1p and y > y1h): #
|
| 125 |
-
new_case_line = (x1p, y, x2p, y)
|
| 126 |
-
break
|
| 127 |
-
return new_case_line
|
| 128 |
-
|
| 129 |
-
def get_page_elements(filename, page):
|
| 130 |
-
page_bbox = get_page_bbox(filename)
|
| 131 |
-
header_bbox = get_header_bbox(filename)
|
| 132 |
-
fn_bbox = get_footnote_bbox(filename)
|
| 133 |
-
case_separator_bbox = get_case_separator(filename)
|
| 134 |
-
if fn_bbox[0] is not None:
|
| 135 |
-
body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], fn_bbox[1])
|
| 136 |
-
else:
|
| 137 |
-
body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], page_bbox[3])
|
| 138 |
-
if case_separator_bbox[0] is not None:
|
| 139 |
-
body_bbox = list(body_bbox)
|
| 140 |
-
if page.number == 0:
|
| 141 |
-
body_bbox[1] = case_separator_bbox[1]
|
| 142 |
-
else:
|
| 143 |
-
body_bbox[3] = case_separator_bbox[1]
|
| 144 |
-
body_bbox = tuple(body_bbox)
|
| 145 |
-
line_data = get_line_data(filename, body_bbox, page)
|
| 146 |
-
|
| 147 |
-
image = cv2.imread(filename)
|
| 148 |
-
cv2.rectangle(image, (page_bbox[0], page_bbox[1]), (page_bbox[2], page_bbox[3]), (0, 0, 0), 4)
|
| 149 |
-
cv2.rectangle(image, (header_bbox[0], header_bbox[1]), (header_bbox[2], header_bbox[3]), (0, 255, 0), 2)
|
| 150 |
-
cv2.rectangle(image, (body_bbox[0], body_bbox[1]), (body_bbox[2], body_bbox[3]), (255, 0, 0), 2)
|
| 151 |
-
if fn_bbox[0] is not None:
|
| 152 |
-
cv2.rectangle(image, (fn_bbox[0], fn_bbox[1]), (fn_bbox[2], fn_bbox[3]), (0, 0, 255), 2)
|
| 153 |
-
if case_separator_bbox[0] is not None:
|
| 154 |
-
cv2.rectangle(image, (case_separator_bbox[0], case_separator_bbox[1]), (case_separator_bbox[2], case_separator_bbox[3]), (255, 0, 255), 2)
|
| 155 |
-
|
| 156 |
-
return page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, line_data, image
|
| 157 |
-
|
| 158 |
-
def get_line_text(line):
|
| 159 |
-
words = []
|
| 160 |
-
words = "".join(s['text'] for s in line['spans'] if s['text'].strip() != "")
|
| 161 |
-
return words
|
| 162 |
-
|
| 163 |
-
def process_file(folderpath):
|
| 164 |
-
pdf2png(folderpath)
|
| 165 |
-
doc = fitz.open(folderpath + '/opinion.pdf')
|
| 166 |
-
files = [f for f in os.listdir(folderpath) if '.png' in f.lower() and "processed" not in f.lower()]
|
| 167 |
-
data = {'Pg Ind':[],
|
| 168 |
-
'Header X1':[], 'Header Y1': [], 'Header X2': [], 'Header Y2':[],
|
| 169 |
-
'Body X1':[], 'Body Y1': [], 'Body X2': [], 'Body Y2':[],
|
| 170 |
-
'Footer X1':[], 'Footer Y1': [], 'Footer X2': [], 'Footer Y2':[],
|
| 171 |
-
'Page X1':[], 'Page Y1': [], 'Page X2': [], 'Page Y2':[],
|
| 172 |
-
'Case Separator Y': [],
|
| 173 |
-
'Lines': [],
|
| 174 |
-
}
|
| 175 |
-
data_df = pd.DataFrame(data)
|
| 176 |
-
for (i,f) in enumerate(files):
|
| 177 |
-
ind = int(f.split('.png')[0])
|
| 178 |
-
page = doc[ind]
|
| 179 |
-
page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, line_data, image = get_page_elements(folderpath +'/' + f, page)
|
| 180 |
-
row = {'Pg Ind':[ind],
|
| 181 |
-
'Header X1':[header_bbox[0]], 'Header Y1': [header_bbox[1]], 'Header X2': [header_bbox[2]], 'Header Y2':[header_bbox[3]],
|
| 182 |
-
'Body X1':[body_bbox[0]], 'Body Y1': [body_bbox[1]], 'Body X2': [body_bbox[2]], 'Body Y2':[body_bbox[3]],
|
| 183 |
-
'Footer X1':[fn_bbox[0]], 'Footer Y1': [fn_bbox[1]], 'Footer X2': [fn_bbox[2]], 'Footer Y2':[fn_bbox[3]],
|
| 184 |
-
'Page X1':[page_bbox[0]], 'Page Y1': [page_bbox[1]], 'Page X2': [page_bbox[2]], 'Page Y2':[page_bbox[3]],
|
| 185 |
-
'Case Separator Y': [case_separator_bbox[1]],
|
| 186 |
-
'Lines': [line_data]
|
| 187 |
-
}
|
| 188 |
-
row_df = pd.DataFrame(row)
|
| 189 |
-
data_df = pd.concat([data_df, row_df], ignore_index=True)
|
| 190 |
-
cv2.imwrite(folderpath + '/' + str(ind) + '-processed.png', image)
|
| 191 |
-
data_df['Pg Ind'] = data_df['Pg Ind'].astype('int')
|
| 192 |
-
data_df.to_csv(folderpath +'/data.csv', index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|