Spaces:
Build error
Build error
File size: 11,091 Bytes
b9d9b37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
import os
import cv2
from transformers import DetrFeatureExtractor
from transformers import DetrForObjectDetection
import torch
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Wedge, Rectangle
import streamlit as st
from PIL import Image
import math
colors = ["red", "blue", "green", "yellow", "orange", "violet"]
def table_detector(image, THRESHOLD_PROBA):
'''
Table detection using DEtect-object TRansformer pre-trained on 1 million tables
'''
feature_extractor = DetrFeatureExtractor(do_resize=True, size=800, max_size=800)
encoding = feature_extractor(image, return_tensors="pt")
# encoding.keys()
model = DetrForObjectDetection.from_pretrained("SalML/DETR-table-detection")
# SalML\DETR-table-detection
with torch.no_grad():
outputs = model(**encoding)
# keep only predictions of queries with 0.9+ confidence (excluding no-object class)
probas = outputs.logits.softmax(-1)[0, :, :-1]
keep = probas.max(-1).values > THRESHOLD_PROBA
# rescale bounding boxes
target_sizes = torch.tensor(image.size[::-1]).unsqueeze(0)
postprocessed_outputs = feature_extractor.post_process(outputs, target_sizes)
bboxes_scaled = postprocessed_outputs[0]['boxes'][keep]
return (model, image, probas[keep], bboxes_scaled)
def table_struct_recog(image, THRESHOLD_PROBA):
'''
Table structure recognition using DEtect-object TRansformer pre-trained on 1 million tables
'''
feature_extractor = DetrFeatureExtractor(do_resize=True, size=1000, max_size=1000)
encoding = feature_extractor(image, return_tensors="pt")
model = DetrForObjectDetection.from_pretrained("SalML/DETR-table-structure-recognition")
with torch.no_grad():
outputs = model(**encoding)
# keep only predictions of queries with 0.9+ confidence (excluding no-object class)
probas = outputs.logits.softmax(-1)[0, :, :-1]
keep = probas.max(-1).values > THRESHOLD_PROBA
# rescale bounding boxes
target_sizes = torch.tensor(image.size[::-1]).unsqueeze(0)
postprocessed_outputs = feature_extractor.post_process(outputs, target_sizes)
bboxes_scaled = postprocessed_outputs[0]['boxes'][keep]
return (model, image, probas[keep], bboxes_scaled)
def add_margin(pil_img, top=20, right=20, bottom=20, left=20, color=(255,255,255)):
'''
Image padding as part of TSR pre-processing to prevent missing table edges
'''
width, height = pil_img.size
new_width = width + right + left
new_height = height + top + bottom
result = Image.new(pil_img.mode, (new_width, new_height), color)
result.paste(pil_img, (left, top))
return result
def plot_results_detection(c1, model, pil_img, prob, boxes, show_only_cropped=False):
'''
Plots the full pillow pdf-page image and adds a rectangle patch for table detection
'''
plt.figure(figsize=(32,20))
plt.imshow(pil_img)
ax = plt.gca()
for p, (xmin, ymin, xmax, ymax) in zip(prob, boxes.tolist()):
cl = p.argmax()
xmin, ymin, xmax, ymax = xmin-3, ymin-3, xmax+3, ymax+3
ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,fill=False, color=colors[cl.item()], linewidth=3))
text = f'{model.config.id2label[cl.item()]}: {p[cl]:0.2f}'
ax.text(xmin, ymin, text, fontsize=15,bbox=dict(facecolor='yellow', alpha=0.5))
plt.axis('off')
plt.show()
c1.pyplot()
def plot_table_detection(c2, model, pil_img, prob, boxes):
'''
Plots only the cropped table(s) from the table detection
'''
plt.figure(figsize=(32,20))
ax = plt.gca()
cropped_img_list = []
for p, (xmin, ymin, xmax, ymax) in zip(prob, boxes.tolist()):
xmin, ymin, xmax, ymax = xmin-3, ymin-3, xmax+3, ymax+3
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
cropped_img_list.append(cropped_img)
for cropped_img in cropped_img_list:
plt.imshow(cropped_img)
plt.axis('off')
plt.show()
c2.pyplot()
return cropped_img_list
def plot_structure(c3, model, pil_img, prob, boxes, class_to_show=0):
'''
To plot table pillow image and the TSR bounding boxes on the table
'''
plt.figure(figsize=(32,20))
plt.imshow(pil_img)
ax = plt.gca()
rows = {}
cols = {}
header = {}
row_header = {}
idx = 0
for p, (xmin, ymin, xmax, ymax) in zip(prob, boxes.tolist()):
xmin, ymin, xmax, ymax = xmin-3, ymin-3, xmax+3, ymax+3
cl = p.argmax()
class_text = model.config.id2label[cl.item()]
text = f'{class_text}: {p[cl]:0.2f}'
# st.write(class_text)
if class_text != 'table':
ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,fill=False, color=colors[cl.item()], linewidth=3))
ax.text(xmin, ymin, text, fontsize=15, bbox=dict(facecolor='yellow', alpha=0.5))
# if class_text == 'table column header':
# header['header'] = (xmin, ymin, xmax, ymax)
if class_text == 'table row':
rows['table row '+str(idx)] = (xmin, ymin, xmax, ymax)
if class_text == 'table column':
cols['table column '+str(idx)] = (xmin, ymin, xmax, ymax)
# if class_text == 'table projected row header':
# row_header['header table row'+str(idx)] = (xmin, ymin, xmax, ymax)
idx += 1
plt.show()
c3.pyplot()
# return header, row_header, rows, cols
return rows, cols
def sort_table_features(header, row_header, rows, cols):
# Sometimes the header and first row overlap, and we need the header bbox not to have first row's bbox inside the headers bbox
y_header = header['header'][3] - 10
rows_ = {table_feature : (xmin, ymin, xmax, ymax) for table_feature, (xmin, ymin, xmax, ymax) in sorted(rows.items(), key=lambda tup: tup[1][1]) if ymin > y_header}
cols_ = {table_feature : (xmin, ymin, xmax, ymax) for table_feature, (xmin, ymin, xmax, ymax) in sorted(cols.items(), key=lambda tup: tup[1][0])}
row_header_ = {table_feature : (xmin, ymin, xmax, ymax) for table_feature, (xmin, ymin, xmax, ymax) in sorted(row_header.items(), key=lambda tup: tup[1][1])}
new_row = {}
idx = 0
for k1, v1 in rows_.items():
save_row = True
row_xmin, row_ymin, row_xmax, row_ymax = v1
for k2, v2 in row_header_.items():
header_row_xmin, header_row_ymin, header_row_xmax, header_row_ymax = v2
# table row and header table row are within 2 pixel range, skip saving the row
if math.isclose(row_ymin, header_row_ymin, abs_tol=2):
save_row = False
if save_row:
new_row['table row.'+str(idx)] = (row_xmin, row_ymin, row_xmax, row_ymax)
idx += 1
new_row_ = {table_feature : (xmin, ymin, xmax, ymax) for table_feature, (xmin, ymin, xmax, ymax) in sorted(new_row.items(), key=lambda tup: tup[1][1])}
return row_header_, new_row_, cols_
def sort_table_featuresv2(rows, cols):
# Sometimes the header and first row overlap, and we need the header bbox not to have first row's bbox inside the headers bbox
rows_ = {table_feature : (xmin, ymin, xmax, ymax) for table_feature, (xmin, ymin, xmax, ymax) in sorted(rows.items(), key=lambda tup: tup[1][1])}
cols_ = {table_feature : (xmin, ymin, xmax, ymax) for table_feature, (xmin, ymin, xmax, ymax) in sorted(cols.items(), key=lambda tup: tup[1][0])}
return rows_, cols_
def individual_table_features(pil_img, header, row_header, rows, cols):
for k, v in header.items():
xmin, ymin, xmax, ymax = v
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
header[k] = xmin, ymin, xmax, ymax, cropped_img
for k, v in row_header.items():
xmin, ymin, xmax, ymax = v
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
row_header[k] = xmin, ymin, xmax, ymax, cropped_img
for k, v in rows.items():
xmin, ymin, xmax, ymax = v
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
rows[k] = xmin, ymin, xmax, ymax, cropped_img
for k, v in cols.items():
xmin, ymin, xmax, ymax = v
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
cols[k] = xmin, ymin, xmax, ymax, cropped_img
return header, row_header, rows, cols
def individual_table_featuresv2(pil_img, rows, cols):
for k, v in rows.items():
xmin, ymin, xmax, ymax = v
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
rows[k] = xmin, ymin, xmax, ymax, cropped_img
for k, v in cols.items():
xmin, ymin, xmax, ymax = v
cropped_img = pil_img.crop((xmin, ymin, xmax, ymax))
cols[k] = xmin, ymin, xmax, ymax, cropped_img
return rows, cols
def plot_table_features(c2, header, row_header, rows, cols):
for k, v in header.items():
_, _, _, _, pil_img = v
for k, v in row_header.items():
_, _, _, _, pil_img = v
for k, v in rows.items():
_, _, _, _, pil_img = v
for k, v in cols.items():
_, _, _, _, pil_img = v
def master_row_set(header, row_header, rows, cols):
master_row = {**header, **row_header, **rows}
master_row_ = {table_feature : (xmin, ymin, xmax, ymax, img) for table_feature, (xmin, ymin, xmax, ymax, img) in sorted(master_row.items(), key=lambda tup: tup[1][1])}
return master_row_
def object_to_cells(master_row, cols):
'''
Iterates to every row, be it header/simple row/header table row, cuts rows into cells and saves images in dictionary where length of dictionary = total rows
'''
cells_img = {}
header_idx = 0
row_idx = 0
for k_row, v_row in master_row.items():
if k_row[:16] == 'header table row':
_, _, _, _, row_header_img = v_row
cells_img[k_row+'.'+str(row_idx)] = row_header_img
row_idx += 1
elif k_row == 'header':
_, ymin, _, ymax, header_img = v_row
xa, ya, xb, yb = 0, 0, 0, ymax-ymin
for k_col, v_col in cols.items():
xmin_col, _, xmax_col, _, col_img = v_col
xa = xmin_col-19
xb = xmax_col-20
header_img_cropped = header_img.crop((xa, ya, xb, yb))
cells_img[k_row+'.'+str(header_idx)] = header_img_cropped
header_idx += 1
elif k_row[:9] == 'table row':
xmin, ymin, xmax, ymax, row_img = v_row
xa, ya, xb, yb = 0, 0, 0, ymax-ymin
row_img_list = []
for k_col, v_col in cols.items():
xmin_col, _, xmax_col, _, col_img = v_col
xa = xmin_col-19
xb = xmax_col-20
row_img_cropped = row_img.crop((xa, ya, xb, yb))
row_img_list.append(row_img_cropped)
cells_img[k_row+'.'+str(row_idx)] = row_img_list
row_idx += 1
return cells_img
def object_to_cellsv2(master_row, cols):
'''
Iterates to every row, be it header/simple row/header table row, cuts rows into cells and saves images in dictionary where length of dictionary = total rows
'''
cells_img = {}
header_idx = 0
row_idx = 0
for k_row, v_row in master_row.items():
xmin, ymin, xmax, ymax, row_img = v_row
xa, ya, xb, yb = 0, 0, 0, ymax-ymin
row_img_list = []
for k_col, v_col in cols.items():
xmin_col, _, xmax_col, _, col_img = v_col
xa = xmin_col-19
xb = xmax_col-20
row_img_cropped = row_img.crop((xa, ya, xb, yb))
row_img_list.append(row_img_cropped)
cells_img[k_row+'.'+str(row_idx)] = row_img_list
row_idx += 1
return cells_img |