-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
375 lines (317 loc) · 12.2 KB
/
main.py
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
MIT License:
Copyright (c) 2022 Muhammad Umer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
End-to-end script for table recognition
"""
import warnings
warnings.filterwarnings("ignore")
import os
import os.path as osp
import cv2
import torch
from mmdet.apis import inference_detector
from mmdet.apis.inference import init_detector
import lxml.etree as etree
import argparse
from sys import exit
from termcolor import colored
from contextlib import contextmanager
import sys, os
from model import (
detection_dict,
structure_dict,
cell_dict,
column_mapping,
row_mapping,
count_columns,
columns_to_lines,
get_preds,
)
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
def parse_args():
parser = argparse.ArgumentParser(
description="Perform inference on your table image."
)
parser.add_argument(
"--config-file",
help="Path to detector config file. (In configs/ directories.)",
required=True,
)
parser.add_argument(
"--input",
help="Path to input image to perform inference on.",
required=True,
)
parser.add_argument(
"--weights-dir",
help="Directory to load weights from.",
default="weights/",
required=False,
)
parser.add_argument(
"--output",
help="Path to output where bounding box predictions are saved.",
default="output/",
required=False,
)
parser.add_argument(
"--device",
help="Choose inference runtime, either CPU or CUDA. (Defaults to CPU)",
default="cpu",
required=False,
)
parser.add_argument(
"--quiet",
help="Perform inference with minimal console output.",
action="store_true",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
assert osp.exists(
args.input
), "Input image/directory does not exist. Recheck passed argument."
allowed_extensions = ["jpg", "jpeg", "bmp", "png"]
file_list = []
if osp.isfile(args.input):
path, base_name = (
osp.split(str(args.input))[0],
osp.split(str(args.input))[1],
)
file_list.append(base_name)
else:
path = str(args.input)
file_list = os.listdir(str(args.input))
input_list = [
fn for fn in file_list if any(fn.endswith(ext) for ext in allowed_extensions)
]
assert (
len(input_list) != 0
), "Input file(s) must be among the allowed extensions. Allowed Extensions: [jpg, jpeg, bmp, png, webp, tiff]."
if args.device == "cuda":
assert torch.cuda.is_available(), f"No CUDA Runtime found."
table_det = os.path.join(os.path.abspath(args.weights_dir), "ptn_detection.pth")
structure_rec = os.path.join(
os.path.abspath(args.weights_dir), "ptn_recognition.pth"
)
cell_det = os.path.join(os.path.abspath(args.weights_dir), "ptn_cells.pth")
config_file = args.config_file
with suppress_stdout():
det_model = init_detector(
config_file, table_det, device=args.device, cfg_options=detection_dict
)
cell_model = init_detector(
config_file, cell_det, device=args.device, cfg_options=cell_dict
)
structure_model = init_detector(
config_file, structure_rec, device=args.device, cfg_options=structure_dict
)
print(
colored(
"Models loaded successfully.",
"cyan",
)
)
print(colored(f"Results will be saved to {osp.abspath(args.output)}", "blue"))
for input in input_list:
image = cv2.imread(osp.join(path, input), cv2.IMREAD_COLOR)
image = image[:, :, :3] # Removing possible alpha channel
save_tables = image.copy()
result_tables, tables, conf = get_preds(
image, det_model, 0.8, axis=0, confidence=True
)
# Exit the inference script if no predictions are made
if (result_tables, tables) == (0, 0):
print(
colored(
f"No predictions were made in image: {input}",
"red",
)
)
continue
else:
save_dir = osp.join(osp.abspath(args.output), input[:-4])
os.makedirs(save_dir, exist_ok=True)
# Saving bounding box coordinates in a text file
file = open(osp.join(save_dir, "table_coords.txt"), "w")
for outer_idx in range(len(tables)):
file.write(
"table "
+ str(conf[outer_idx])
+ " "
+ str(tables[outer_idx][0])
+ " "
+ str(tables[outer_idx][1])
+ " "
+ str(tables[outer_idx][2])
+ " "
+ str(tables[outer_idx][3])
+ "\n"
)
save_tables = cv2.rectangle(
save_tables,
(tables[outer_idx][0], tables[outer_idx][1]),
(tables[outer_idx][2], tables[outer_idx][3]),
(255, 0, 0),
2,
)
cropped_table = image[
tables[outer_idx][1] : tables[outer_idx][3],
tables[outer_idx][0] : tables[outer_idx][2],
]
cv2.imwrite(
osp.join(save_dir, f"table_{outer_idx}.png"),
cropped_table,
)
file.close()
cv2.imwrite(
osp.join(save_dir, "table_detections.png"),
save_tables,
)
root = etree.Element("document")
file = open(osp.join(save_dir, "structure.xml"), "w")
file.write('<?xml version="1.0" encoding="UTF-8"?>\n')
for inner_idx in range(len(tables)):
table_image = cv2.imread(osp.join(save_dir, f"table_{inner_idx}.png"))
table_image = table_image[:, :, :3] # Removing possible alpha channel
save_cells = table_image.copy()
save_columns = table_image.copy()
result_cells, cells = get_preds(
table_image, cell_model, 0.3, axis=0, craft=True, device=args.device
)
result_columns, column_boxes = get_preds(
table_image,
structure_model,
0.3,
axis=0,
merge=False,
device=args.device,
)
# Exit the inference script if no cells are detected.
if (result_cells, cells) == (0, 0) or (
result_columns,
column_boxes,
) == (0, 0):
print(
colored(
f"No cells were detected in image: table_{inner_idx}.png",
"red",
)
)
exit()
else:
tableXML = etree.Element("table")
tabelCoords = etree.Element(
"Coords",
points=str(tables[inner_idx][0])
+ ","
+ str(tables[inner_idx][1])
+ " "
+ str(tables[inner_idx][2])
+ ","
+ str(tables[inner_idx][3])
+ " "
+ str(tables[inner_idx][2])
+ ","
+ str(tables[inner_idx][3])
+ " "
+ str(tables[inner_idx][2])
+ ","
+ str(tables[inner_idx][1]),
)
tableXML.append(tabelCoords)
columns = columns_to_lines(column_boxes)
col_structure = column_mapping(columns, cells)
_, rows = count_columns(col_structure)
rows = list(set(rows))
row_structure = row_mapping(rows, cells)
if row_structure == {} or col_structure == {}:
print("Failed to fetch table structure.")
for cell in cells:
cellXML = etree.Element("cell")
try:
row_info = row_structure[tuple(cell)]
col_info = col_structure[tuple(cell)]
except KeyError:
continue
start_col, start_row, end_col, end_row = (
min(col_info),
min(row_info),
max(col_info),
max(row_info),
)
cellXML.set("start-col", str(start_col))
cellXML.set("start-row", str(start_row))
cellXML.set("end-col", str(end_col))
cellXML.set("end-row", str(end_row))
c1 = (
str(cell[0] + tables[inner_idx][0])
+ ","
+ str(cell[1] + tables[inner_idx][1])
)
c2 = (
str(cell[0] + tables[inner_idx][0])
+ ","
+ str(cell[3] + tables[inner_idx][1])
)
c3 = (
str(cell[2] + tables[inner_idx][0])
+ ","
+ str(cell[3] + tables[inner_idx][1])
)
c4 = (
str(cell[2] + tables[inner_idx][0])
+ ","
+ str(cell[1] + tables[inner_idx][1])
)
coords = etree.Element(
"Coords", points=c1 + " " + c2 + " " + c3 + " " + c4
)
cellXML.append(coords)
tableXML.append(cellXML)
root.append(tableXML)
for cell in cells:
save_cells = cv2.rectangle(
save_cells,
(cell[0], cell[1]),
(cell[2], cell[3]),
(255, 0, 0),
2,
)
for column_box in column_boxes:
save_columns = cv2.rectangle(
save_columns,
(column_box[0], column_box[1]),
(column_box[2], column_box[3]),
(255, 0, 255),
2,
)
cv2.imwrite(
osp.join(save_dir, f"table_{inner_idx}_cells.png"),
save_cells,
)
cv2.imwrite(
osp.join(save_dir, f"table_{inner_idx}_columns.png"),
save_columns,
)
file.write(etree.tostring(root, pretty_print=True, encoding="unicode"))
file.close()
if not args.quiet:
print(colored(f"Inference on {input} completed.", "blue"))