cools commited on
Commit
3aa5dc8
·
1 Parent(s): db3fcff

Update ImageProcessor.py

Browse files
Files changed (1) hide show
  1. ImageProcessor.py +22 -20
ImageProcessor.py CHANGED
@@ -18,22 +18,23 @@ 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
  indented_lines = []
27
- for (pg_ind, page) in enumerate(doc):
28
- image = cv2.imread(filename.replace('opinion.pdf', str(pg_ind) + '.png'))
29
- body_rect = fitz.Rect(body_bbox)
30
- pg_dict = page.get_text('dict', clip=body_rect)
31
- 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']]
32
- body_text = page.get_text("text", clip=body_rect).strip()
33
- baseline = min([l[0] for l in all_lines])
34
- 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]))]
35
- for i in indented_inds:
36
- indented_lines.append((i, l[0], l[1], l[2], l[3]))
 
37
  return indented_lines
38
 
39
  def get_footnote_bbox(filename):
@@ -118,7 +119,7 @@ def get_case_separator(filename):
118
  break
119
  return new_case_line
120
 
121
- def get_page_elements(filename):
122
  page_bbox = get_page_bbox(filename)
123
  header_bbox = get_header_bbox(filename)
124
  fn_bbox = get_footnote_bbox(filename)
@@ -127,7 +128,7 @@ def get_page_elements(filename):
127
  body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], fn_bbox[1])
128
  else:
129
  body_bbox = (page_bbox[0], header_bbox[3], page_bbox[2], page_bbox[3])
130
- indent_lines = get_indents(filename, body_bbox)
131
 
132
  image = cv2.imread(filename)
133
  cv2.rectangle(image, (page_bbox[0], page_bbox[1]), (page_bbox[2], page_bbox[3]), (0, 0, 0), 4)
@@ -138,13 +139,14 @@ def get_page_elements(filename):
138
  if case_separator_bbox[0] is not None:
139
  cv2.rectangle(image, (case_separator_bbox[0], case_separator_bbox[1]),
140
  (case_separator_bbox[2], case_separator_bbox[3]), (255, 0, 255), 2)
141
- for (i, il) in enumerate(il):
142
- cv2.circle(image, (il[1]-10, int(0.5*(il[2] + il[4]))), radius=1, color=(240, 32, 160), thickness=2)
143
 
144
  return page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, indent_lines, image
145
 
146
  def process_file(folderpath):
147
  pdf2png(folderpath)
 
148
  files = [f for f in os.listdir(folderpath) if '.png' in f.lower() and "processed" not in f.lower()]
149
  data = {'Pg Ind':[],
150
  'Header X1':[], 'Header Y1': [], 'Header X2': [], 'Header Y2':[],
@@ -156,8 +158,9 @@ def process_file(folderpath):
156
  }
157
  data_df = pd.DataFrame(data)
158
  for (i,f) in enumerate(files):
159
- page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, indent_lines, image = get_page_elements(folderpath +'/' + f)
160
  ind = int(f.split('.png')[0])
 
 
161
  row = {'Pg Ind':[ind],
162
  'Header X1':[header_bbox[0]], 'Header Y1': [header_bbox[1]], 'Header X2': [header_bbox[2]], 'Header Y2':[header_bbox[3]],
163
  'Body X1':[body_bbox[0]], 'Body Y1': [body_bbox[1]], 'Body X2': [body_bbox[2]], 'Body Y2':[body_bbox[3]],
@@ -170,5 +173,4 @@ def process_file(folderpath):
170
  data_df = pd.concat([data_df, row_df], ignore_index=True)
171
  cv2.imwrite(folderpath + '/' + str(ind) + '-processed.png', image)
172
  data_df['Pg Ind'] = data_df['Pg Ind'].astype('int')
173
- data_df.to_csv(folderpath +'/data.csv', index=False)
174
-
 
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[int((y_top+y_bot)/2), :x]
22
  return np.sum(left_portion) == 0
23
 
24
+ def get_indents(filename, body_bbox, page):
25
+ # doc = fitz.open(filename)
26
  indented_lines = []
27
+
28
+ # for (pg_ind, page) in enumerate(doc):
29
+ image = cv2.imread(filename)
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, all_lines[i][0], all_lines[i][1], all_lines[i][2], all_lines[i][3]))
38
  return indented_lines
39
 
40
  def get_footnote_bbox(filename):
 
119
  break
120
  return new_case_line
121
 
122
+ def get_page_elements(filename, page):
123
  page_bbox = get_page_bbox(filename)
124
  header_bbox = get_header_bbox(filename)
125
  fn_bbox = get_footnote_bbox(filename)
 
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, page)
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(indent_lines):
143
+ cv2.circle(image, (il[1]-15, 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)
149
+ doc = fitz.open(folderpath + '/opinion.pdf')
150
  files = [f for f in os.listdir(folderpath) if '.png' in f.lower() and "processed" not in f.lower()]
151
  data = {'Pg Ind':[],
152
  'Header X1':[], 'Header Y1': [], 'Header X2': [], 'Header Y2':[],
 
158
  }
159
  data_df = pd.DataFrame(data)
160
  for (i,f) in enumerate(files):
 
161
  ind = int(f.split('.png')[0])
162
+ page = doc[ind]
163
+ page_bbox, header_bbox, fn_bbox, body_bbox, case_separator_bbox, indent_lines, image = get_page_elements(folderpath +'/' + f, page)
164
  row = {'Pg Ind':[ind],
165
  'Header X1':[header_bbox[0]], 'Header Y1': [header_bbox[1]], 'Header X2': [header_bbox[2]], 'Header Y2':[header_bbox[3]],
166
  'Body X1':[body_bbox[0]], 'Body Y1': [body_bbox[1]], 'Body X2': [body_bbox[2]], 'Body Y2':[body_bbox[3]],
 
173
  data_df = pd.concat([data_df, row_df], ignore_index=True)
174
  cv2.imwrite(folderpath + '/' + str(ind) + '-processed.png', image)
175
  data_df['Pg Ind'] = data_df['Pg Ind'].astype('int')
176
+ data_df.to_csv(folderpath +'/data.csv', index=False)