bachpc commited on
Commit
c9a4627
1 Parent(s): be7e0a8

Update visualization

Browse files
Files changed (1) hide show
  1. app.py +63 -18
app.py CHANGED
@@ -14,6 +14,7 @@ from tempfile import TemporaryDirectory
14
  import xlsxwriter
15
  import matplotlib.pyplot as plt
16
  import matplotlib.patches as patches
 
17
 
18
  from paddleocr import PaddleOCR
19
  # import pytesseract
@@ -218,8 +219,9 @@ def visualize_structure(pil_img, structure_result):
218
  height = image.shape[0]
219
  # print(width, height)
220
 
221
- fig, ax = plt.subplots(1)
222
- ax.imshow(pil_img, interpolation='lanczos')
 
223
 
224
  for i, result in enumerate(structure_result):
225
  class_id = int(result[5])
@@ -250,47 +252,90 @@ def visualize_structure(pil_img, structure_result):
250
  # Hatch
251
  rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1],
252
  linewidth=1, alpha=0.4,
253
- edgecolor=color,facecolor='none',
254
  linestyle='--',hatch=hatch)
255
  ax.add_patch(rect)
256
  # Edge
257
  rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1],
258
  linewidth=linewidth,
259
- edgecolor=color,facecolor='none',
260
  linestyle="--")
261
  ax.add_patch(rect)
262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  plt.axis('off')
264
  img_buf = io.BytesIO()
265
- plt.savefig(img_buf, bbox_inches='tight', dpi=1000)
 
266
 
267
  return PIL.Image.open(img_buf)
268
 
269
 
270
  def visualize_cells(pil_img, cells):
271
- fig, ax = plt.subplots(1)
272
- ax.imshow(pil_img, interpolation='lanczos')
 
273
 
274
- for i, cell in enumerate(cells):
275
  bbox = cell['bbox']
276
- if cell['header']:
 
 
 
277
  alpha = 0.3
 
 
 
 
 
 
 
 
278
  else:
279
- alpha = 0.125
280
- rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=1,
281
- edgecolor='none',facecolor="magenta", alpha=alpha)
 
 
 
 
 
282
  ax.add_patch(rect)
283
- rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=1,
284
- edgecolor="magenta",facecolor='none',linestyle="--",
285
- alpha=0.08, hatch='///')
286
  ax.add_patch(rect)
287
- rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=1,
288
- edgecolor="magenta",facecolor='none',linestyle="--")
289
  ax.add_patch(rect)
290
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  plt.axis('off')
292
  img_buf = io.BytesIO()
293
- plt.savefig(img_buf, bbox_inches='tight', dpi=1000)
 
294
 
295
  return PIL.Image.open(img_buf)
296
 
 
14
  import xlsxwriter
15
  import matplotlib.pyplot as plt
16
  import matplotlib.patches as patches
17
+ from matplotlib.patches import Patch
18
 
19
  from paddleocr import PaddleOCR
20
  # import pytesseract
 
219
  height = image.shape[0]
220
  # print(width, height)
221
 
222
+ plt.imshow(pil_img, interpolation='lanczos')
223
+ plt.gcf().set_size_inches(20, 20)
224
+ ax = plt.gca()
225
 
226
  for i, result in enumerate(structure_result):
227
  class_id = int(result[5])
 
252
  # Hatch
253
  rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1],
254
  linewidth=1, alpha=0.4,
255
+ edgecolor=color, facecolor='none',
256
  linestyle='--',hatch=hatch)
257
  ax.add_patch(rect)
258
  # Edge
259
  rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1],
260
  linewidth=linewidth,
261
+ edgecolor=color, facecolor='none',
262
  linestyle="--")
263
  ax.add_patch(rect)
264
 
265
+ plt.xticks([], [])
266
+ plt.yticks([], [])
267
+
268
+ legend_elements = []
269
+ for class_name in structure_class_names:
270
+ color, alpha, linewidth, hatch = get_bbox_decorations('recognition', structure_class_map[class_name])
271
+ legend_elements.append(
272
+ Patch(facecolor=color, edgecolor=color, label=class_name, hatch=hatch, alpha=alpha)
273
+ )
274
+
275
+ plt.legend(handles=legend_elements, bbox_to_anchor=(0.5, -0.02), loc='upper center', borderaxespad=0,
276
+ fontsize=10, ncol=3)
277
+ plt.gcf().set_size_inches(10, 10)
278
  plt.axis('off')
279
  img_buf = io.BytesIO()
280
+ plt.savefig(img_buf, bbox_inches='tight', dpi=150)
281
+ plt.close()
282
 
283
  return PIL.Image.open(img_buf)
284
 
285
 
286
  def visualize_cells(pil_img, cells):
287
+ plt.imshow(pil_img, interpolation="lanczos")
288
+ plt.gcf().set_size_inches(20, 20)
289
+ ax = plt.gca()
290
 
291
+ for cell in cells:
292
  bbox = cell['bbox']
293
+
294
+ if cell['column header']:
295
+ facecolor = (1, 0, 0.45)
296
+ edgecolor = (1, 0, 0.45)
297
  alpha = 0.3
298
+ linewidth = 2
299
+ hatch='//////'
300
+ elif cell['projected row header']:
301
+ facecolor = (0.95, 0.6, 0.1)
302
+ edgecolor = (0.95, 0.6, 0.1)
303
+ alpha = 0.3
304
+ linewidth = 2
305
+ hatch='//////'
306
  else:
307
+ facecolor = (0.3, 0.74, 0.8)
308
+ edgecolor = (0.3, 0.7, 0.6)
309
+ alpha = 0.3
310
+ linewidth = 2
311
+ hatch='\\\\\\\\\\\\'
312
+
313
+ rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
314
+ edgecolor='none',facecolor=facecolor, alpha=0.1)
315
  ax.add_patch(rect)
316
+ rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
317
+ edgecolor=edgecolor,facecolor='none',linestyle='-', alpha=alpha)
 
318
  ax.add_patch(rect)
319
+ rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=0,
320
+ edgecolor=edgecolor,facecolor='none',linestyle='-', hatch=hatch, alpha=0.2)
321
  ax.add_patch(rect)
322
 
323
+ plt.xticks([], [])
324
+ plt.yticks([], [])
325
+
326
+ legend_elements = [Patch(facecolor=(0.3, 0.74, 0.8), edgecolor=(0.3, 0.7, 0.6),
327
+ label='Data cell', hatch='\\\\\\\\\\\\', alpha=0.3),
328
+ Patch(facecolor=(1, 0, 0.45), edgecolor=(1, 0, 0.45),
329
+ label='Column header cell', hatch='//////', alpha=0.3),
330
+ Patch(facecolor=(0.95, 0.6, 0.1), edgecolor=(0.95, 0.6, 0.1),
331
+ label='Projected row header cell', hatch='//////', alpha=0.3)]
332
+ plt.legend(handles=legend_elements, bbox_to_anchor=(0.5, -0.02), loc='upper center', borderaxespad=0,
333
+ fontsize=10, ncol=3)
334
+ plt.gcf().set_size_inches(10, 10)
335
  plt.axis('off')
336
  img_buf = io.BytesIO()
337
+ plt.savefig(img_buf, bbox_inches='tight', dpi=150)
338
+ plt.close()
339
 
340
  return PIL.Image.open(img_buf)
341