Update ImageProcessor.py
Browse files- ImageProcessor.py +31 -4
ImageProcessor.py
CHANGED
|
@@ -14,6 +14,29 @@ def pdf2png(folderpath):
|
|
| 14 |
pix = p.get_pixmap(matrix=mat)
|
| 15 |
pix.save(folderpath + '/' + str(i) + '.png')
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def get_footnote_bbox(filename):
|
| 18 |
footnotes_bbox = (None, None, None, None)
|
| 19 |
x1p, y1p, x2p, y2p = get_page_bbox(filename)
|
|
@@ -51,7 +74,6 @@ def get_header_bbox(filename):
|
|
| 51 |
x,y,w,h = cv2.boundingRect(c)
|
| 52 |
break
|
| 53 |
header_bbox = (x, y, x+w, y+40)
|
| 54 |
-
# header_bbox = (145, 45, 465, 155) # For digitized variants
|
| 55 |
return header_bbox
|
| 56 |
|
| 57 |
|
|
@@ -106,6 +128,7 @@ def get_page_elements(filename):
|
|
| 106 |
body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], fn_bbox[1])
|
| 107 |
else:
|
| 108 |
body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], page_bbox[3])
|
|
|
|
| 109 |
|
| 110 |
image = cv2.imread(filename)
|
| 111 |
cv2.rectangle(image, (page_bbox[0], page_bbox[1]), (page_bbox[2], page_bbox[3]), (0, 0, 0), 4)
|
|
@@ -116,8 +139,10 @@ def get_page_elements(filename):
|
|
| 116 |
if case_separator_bbox[0] is not None:
|
| 117 |
cv2.rectangle(image, (case_separator_bbox[0], case_separator_bbox[1]),
|
| 118 |
(case_separator_bbox[2], case_separator_bbox[3]), (255, 0, 255), 2)
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
return page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, image
|
| 121 |
|
| 122 |
def process_file(folderpath):
|
| 123 |
pdf2png(folderpath)
|
|
@@ -128,17 +153,19 @@ def process_file(folderpath):
|
|
| 128 |
'Footer X1':[], 'Footer Y1': [], 'Footer X2': [], 'Footer Y2':[],
|
| 129 |
'Page X1':[], 'Page Y1': [], 'Page X2': [], 'Page Y2':[],
|
| 130 |
'Case Separator Y': [],
|
|
|
|
| 131 |
}
|
| 132 |
data_df = pd.DataFrame(data)
|
| 133 |
for (i,f) in enumerate(files):
|
| 134 |
-
page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, image = get_page_elements(folderpath +'/' + f)
|
| 135 |
ind = int(f.split('.png')[0])
|
| 136 |
row = {'Pg Ind':[ind],
|
| 137 |
'Header X1':[header_bbox[0]], 'Header Y1': [header_bbox[1]], 'Header X2': [header_bbox[2]], 'Header Y2':[header_bbox[3]],
|
| 138 |
'Body X1':[body_bbox[0]], 'Body Y1': [body_bbox[1]], 'Body X2': [body_bbox[2]], 'Body Y2':[body_bbox[3]],
|
| 139 |
'Footer X1':[fn_bbox[0]], 'Footer Y1': [fn_bbox[1]], 'Footer X2': [fn_bbox[2]], 'Footer Y2':[fn_bbox[3]],
|
| 140 |
'Page X1':[page_bbox[0]], 'Page Y1': [page_bbox[1]], 'Page X2': [page_bbox[2]], 'Page Y2':[page_bbox[3]],
|
| 141 |
-
'Case Separator Y': [case_separator_bbox[1]]
|
|
|
|
| 142 |
}
|
| 143 |
row_df = pd.DataFrame(row)
|
| 144 |
data_df = pd.concat([data_df, row_df], ignore_index=True)
|
|
|
|
| 14 |
pix = p.get_pixmap(matrix=mat)
|
| 15 |
pix.save(folderpath + '/' + str(i) + '.png')
|
| 16 |
|
| 17 |
+
def is_leftmost(image, x, y_top, y_bot):
|
| 18 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 19 |
+
blur = cv2.GaussianBlur(gray, (7,7), 0)
|
| 20 |
+
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
| 21 |
+
left_portion = thresh[y_top:y_bot, :x]
|
| 22 |
+
return np.sum(left_portion) == 0
|
| 23 |
+
|
| 24 |
+
def get_indents(filename, body_bbox):
|
| 25 |
+
doc = fitz.open(filename)
|
| 26 |
+
df = pd.read_csv(data_csv_filename)
|
| 27 |
+
indented_lines = []
|
| 28 |
+
for (pg_ind, page) in enumerate(doc):
|
| 29 |
+
image = cv2.imread(filename.replace('opinion.pdf', str(pg_ind) + '.png'))
|
| 30 |
+
body_rect = fitz.Rect(body_bbox)
|
| 31 |
+
pg_dict = page.get_text('dict', clip=body_rect)
|
| 32 |
+
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']]
|
| 33 |
+
body_text = page.get_text("text", clip=body_rect).strip()
|
| 34 |
+
baseline = min([l[0] for l in all_lines])
|
| 35 |
+
indented_inds = [i for (i,l) in enumerate(all_lines) if (l[0]-baseline > 9 and l[0]-baseline < 30 and is_leftmost(image, l[0]-10, l[1], l[3]))]
|
| 36 |
+
for i in indented_inds:
|
| 37 |
+
indented_lines.append((i, l[0], l[1], l[2], l[3]))
|
| 38 |
+
return indented_lines
|
| 39 |
+
|
| 40 |
def get_footnote_bbox(filename):
|
| 41 |
footnotes_bbox = (None, None, None, None)
|
| 42 |
x1p, y1p, x2p, y2p = get_page_bbox(filename)
|
|
|
|
| 74 |
x,y,w,h = cv2.boundingRect(c)
|
| 75 |
break
|
| 76 |
header_bbox = (x, y, x+w, y+40)
|
|
|
|
| 77 |
return header_bbox
|
| 78 |
|
| 79 |
|
|
|
|
| 128 |
body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], fn_bbox[1])
|
| 129 |
else:
|
| 130 |
body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], page_bbox[3])
|
| 131 |
+
indent_lines = get_indents(filename, body_bbox)
|
| 132 |
|
| 133 |
image = cv2.imread(filename)
|
| 134 |
cv2.rectangle(image, (page_bbox[0], page_bbox[1]), (page_bbox[2], page_bbox[3]), (0, 0, 0), 4)
|
|
|
|
| 139 |
if case_separator_bbox[0] is not None:
|
| 140 |
cv2.rectangle(image, (case_separator_bbox[0], case_separator_bbox[1]),
|
| 141 |
(case_separator_bbox[2], case_separator_bbox[3]), (255, 0, 255), 2)
|
| 142 |
+
for (i, il) in enumerate(il):
|
| 143 |
+
cv2.circle(image, (il[1]-10, int(0.5*(il[2] + il[4]))), radius=1, color=(240, 32, 160), thickness=2)
|
| 144 |
|
| 145 |
+
return page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, indent_lines, image
|
| 146 |
|
| 147 |
def process_file(folderpath):
|
| 148 |
pdf2png(folderpath)
|
|
|
|
| 153 |
'Footer X1':[], 'Footer Y1': [], 'Footer X2': [], 'Footer Y2':[],
|
| 154 |
'Page X1':[], 'Page Y1': [], 'Page X2': [], 'Page Y2':[],
|
| 155 |
'Case Separator Y': [],
|
| 156 |
+
'Indent Lines': [],
|
| 157 |
}
|
| 158 |
data_df = pd.DataFrame(data)
|
| 159 |
for (i,f) in enumerate(files):
|
| 160 |
+
page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, indent_lines, image = get_page_elements(folderpath +'/' + f)
|
| 161 |
ind = int(f.split('.png')[0])
|
| 162 |
row = {'Pg Ind':[ind],
|
| 163 |
'Header X1':[header_bbox[0]], 'Header Y1': [header_bbox[1]], 'Header X2': [header_bbox[2]], 'Header Y2':[header_bbox[3]],
|
| 164 |
'Body X1':[body_bbox[0]], 'Body Y1': [body_bbox[1]], 'Body X2': [body_bbox[2]], 'Body Y2':[body_bbox[3]],
|
| 165 |
'Footer X1':[fn_bbox[0]], 'Footer Y1': [fn_bbox[1]], 'Footer X2': [fn_bbox[2]], 'Footer Y2':[fn_bbox[3]],
|
| 166 |
'Page X1':[page_bbox[0]], 'Page Y1': [page_bbox[1]], 'Page X2': [page_bbox[2]], 'Page Y2':[page_bbox[3]],
|
| 167 |
+
'Case Separator Y': [case_separator_bbox[1]],
|
| 168 |
+
'Indent Lines': [indent_lines]
|
| 169 |
}
|
| 170 |
row_df = pd.DataFrame(row)
|
| 171 |
data_df = pd.concat([data_df, row_df], ignore_index=True)
|