bachpc commited on
Commit
65b03fb
1 Parent(s): c749787

Minor changes

Browse files
Files changed (1) hide show
  1. app.py +11 -22
app.py CHANGED
@@ -84,7 +84,7 @@ def crop_image(pil_img, detection_result, padding=30):
84
  width = image.shape[1]
85
  height = image.shape[0]
86
  # print(width, height)
87
- for i, result in enumerate(detection_result):
88
  class_id = int(result[5])
89
  score = float(result[4])
90
  min_x = result[0]
@@ -113,7 +113,7 @@ def crop_image(pil_img, detection_result, padding=30):
113
 
114
  crop_images.append(crop_image)
115
 
116
- cv2.rectangle(image, (x1, y1), (x2, y2), color=(0, 0, 255))
117
  cv2.putText(image, f'{score:.2f}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(255, 0, 0))
118
 
119
  return crop_images, cv_to_PIL(image)
@@ -148,7 +148,7 @@ def convert_stucture(page_tokens, pil_img, structure_result):
148
  bboxes = []
149
  scores = []
150
  labels = []
151
- for i, result in enumerate(structure_result):
152
  class_id = int(result[5])
153
  score = float(result[4])
154
  min_x = result[0]
@@ -206,12 +206,12 @@ def visualize_ocr(pil_img, ocr_result):
206
  plt.gcf().set_size_inches(20, 20)
207
  ax = plt.gca()
208
 
209
- for i, result in enumerate(ocr_result):
210
  bbox = result['bbox']
211
  text = result['text']
212
- rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=2, edgecolor='red', facecolor='none', linestyle="-")
213
  ax.add_patch(rect)
214
- ax.text(bbox[0], bbox[3], text, horizontalalignment='left', verticalalignment='bottom', color='blue', fontsize=10)
215
 
216
  plt.xticks([], [])
217
  plt.yticks([], [])
@@ -224,17 +224,6 @@ def visualize_ocr(pil_img, ocr_result):
224
 
225
  return PIL.Image.open(img_buf)
226
 
227
- # image = PIL_to_cv(pil_img)
228
- # for i, res in enumerate(ocr_result):
229
- # bbox = res['bbox']
230
- # x1 = int(bbox[0])
231
- # y1 = int(bbox[1])
232
- # x2 = int(bbox[2])
233
- # y2 = int(bbox[3])
234
- # cv2.rectangle(image, (x1, y1), (x2, y2), color=(255, 0, 0))
235
- # cv2.putText(image, res['text'], (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.25, color=(0, 0, 255))
236
- # return cv_to_PIL(image)
237
-
238
 
239
  def get_bbox_decorations(data_type, label):
240
  if label == 0:
@@ -266,7 +255,7 @@ def visualize_structure(pil_img, structure_result):
266
  plt.gcf().set_size_inches(20, 20)
267
  ax = plt.gca()
268
 
269
- for i, result in enumerate(structure_result):
270
  class_id = int(result[5])
271
  score = float(result[4])
272
  min_x = result[0]
@@ -301,7 +290,7 @@ def visualize_structure(pil_img, structure_result):
301
  rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1],
302
  linewidth=linewidth,
303
  edgecolor=color, facecolor='none',
304
- linestyle="--")
305
  ax.add_patch(rect)
306
 
307
  plt.xticks([], [])
@@ -311,7 +300,7 @@ def visualize_structure(pil_img, structure_result):
311
  for class_name in structure_class_names[:-1]:
312
  color, alpha, linewidth, hatch = get_bbox_decorations('recognition', structure_class_map[class_name])
313
  legend_elements.append(
314
- Patch(facecolor=color, edgecolor=color, label=class_name, hatch=hatch, alpha=alpha)
315
  )
316
 
317
  plt.legend(handles=legend_elements, bbox_to_anchor=(0.5, -0.02), loc='upper center', borderaxespad=0,
@@ -326,7 +315,7 @@ def visualize_structure(pil_img, structure_result):
326
 
327
 
328
  def visualize_cells(pil_img, cells):
329
- plt.imshow(pil_img, interpolation="lanczos")
330
  plt.gcf().set_size_inches(20, 20)
331
  ax = plt.gca()
332
 
@@ -618,7 +607,7 @@ def main():
618
  str_cols[2].subheader('Structure result')
619
  str_cols[3].subheader('Cells result')
620
 
621
- for i, img in enumerate(crop_images):
622
  str_cols = st.columns(4)
623
 
624
  vis_img = visualize_image(img)
 
84
  width = image.shape[1]
85
  height = image.shape[0]
86
  # print(width, height)
87
+ for idx, result in enumerate(detection_result):
88
  class_id = int(result[5])
89
  score = float(result[4])
90
  min_x = result[0]
 
113
 
114
  crop_images.append(crop_image)
115
 
116
+ cv2.rectangle(image, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
117
  cv2.putText(image, f'{score:.2f}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(255, 0, 0))
118
 
119
  return crop_images, cv_to_PIL(image)
 
148
  bboxes = []
149
  scores = []
150
  labels = []
151
+ for idx, result in enumerate(structure_result):
152
  class_id = int(result[5])
153
  score = float(result[4])
154
  min_x = result[0]
 
206
  plt.gcf().set_size_inches(20, 20)
207
  ax = plt.gca()
208
 
209
+ for idx, result in enumerate(ocr_result):
210
  bbox = result['bbox']
211
  text = result['text']
212
+ rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=2, edgecolor='red', facecolor='none', linestyle='-')
213
  ax.add_patch(rect)
214
+ ax.text(bbox[2], bbox[3], text, horizontalalignment='left', verticalalignment='bottom', color='blue', fontsize=7)
215
 
216
  plt.xticks([], [])
217
  plt.yticks([], [])
 
224
 
225
  return PIL.Image.open(img_buf)
226
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  def get_bbox_decorations(data_type, label):
229
  if label == 0:
 
255
  plt.gcf().set_size_inches(20, 20)
256
  ax = plt.gca()
257
 
258
+ for idx, result in enumerate(structure_result):
259
  class_id = int(result[5])
260
  score = float(result[4])
261
  min_x = result[0]
 
290
  rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1],
291
  linewidth=linewidth,
292
  edgecolor=color, facecolor='none',
293
+ linestyle='--')
294
  ax.add_patch(rect)
295
 
296
  plt.xticks([], [])
 
300
  for class_name in structure_class_names[:-1]:
301
  color, alpha, linewidth, hatch = get_bbox_decorations('recognition', structure_class_map[class_name])
302
  legend_elements.append(
303
+ Patch(facecolor='none', edgecolor=color, linewidth=linewidth, linestyle='--', label=class_name, hatch=hatch)
304
  )
305
 
306
  plt.legend(handles=legend_elements, bbox_to_anchor=(0.5, -0.02), loc='upper center', borderaxespad=0,
 
315
 
316
 
317
  def visualize_cells(pil_img, cells):
318
+ plt.imshow(pil_img, interpolation='lanczos')
319
  plt.gcf().set_size_inches(20, 20)
320
  ax = plt.gca()
321
 
 
607
  str_cols[2].subheader('Structure result')
608
  str_cols[3].subheader('Cells result')
609
 
610
+ for idx, img in enumerate(crop_images):
611
  str_cols = st.columns(4)
612
 
613
  vis_img = visualize_image(img)