-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_explorer.py
301 lines (215 loc) · 9.46 KB
/
tree_explorer.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
import cv2
import copy
import random
import numpy as np
from xml.dom import minidom
from xml.etree import ElementTree as ET
from truthpy import Table, GTElement, Row, Column, RowSpan, ColSpan
class Augmentor:
def __init__(self, table):
self.t = table
self.doc_shape = (table.y2, table.x2)
self.t.move(-self.t.x1, -self.t.y1)
def get_column_range(self, col):
if col == len(self.t.gtCells[0]):
return True, col, col, None
column_bbox = GTElement(self.t.gtCells[0][col].x1, self.t.y1, self.t.gtCells[0][col].x2, self.t.y2)
filtered = list(filter(lambda s: (column_bbox & s).area() > 1e-2, self.t.gtSpans))
if len(filtered) == 0:
return True, col, col, []
min_x = min(map(lambda s: s.x1, filtered))
max_x = max(map(lambda s: s.x2, filtered))
cell1 = self.t.getCellAtPoint((min_x, 1))
cell2 = self.t.getCellAtPoint((max_x, 1))
column_bbox = GTElement(cell1.x1, self.t.y1, cell2.x2, self.t.y2)
filtered_partial = list(filter(lambda s: (column_bbox & s).area() > 0, self.t.gtSpans))
filtered_full = list(filter(lambda s: (column_bbox & s).area() == s.area(), self.t.gtSpans))
is_convex = len(filtered_partial) == len(filtered_full)
return is_convex, cell1.startCol, cell2.endCol, filtered_full
def get_row_range(self, row):
if row == len(self.t.gtCells):
return True, row, row, None
row_bbox = GTElement(self.t.x1, self.t.gtCells[row][0].y1, self.t.x2, self.t.gtCells[row][0].y2)
filtered = list(filter(lambda s: (row_bbox & s).area() > 1e-2, self.t.gtSpans))
if len(filtered) == 0:
return True, row, row, []
min_y = min(map(lambda s: s.y1, filtered))
max_y = max(map(lambda s: s.y2, filtered))
cell1 = self.t.getCellAtPoint((1, min_y))
cell2 = self.t.getCellAtPoint((1, max_y))
row_bbox = GTElement(self.t.x1, cell1.y1, self.t.x2, cell2.y2)
filtered_partial = list(filter(lambda s: (row_bbox & s).area() > 0, self.t.gtSpans))
filtered_full = list(filter(lambda s: (row_bbox & s).area() == s.area(), self.t.gtSpans))
is_convex = len(filtered_partial) == len(filtered_full)
return is_convex, cell1.startRow, cell2.endRow, filtered_full
def replicate_row(self, idx_copy, idx_paste):
is_convex, idx_c1, idx_c2, spans = self.get_row_range(idx_copy)
is_convex2, idx_p1, idx_p2, _ = self.get_row_range(idx_paste)
if not is_convex or not is_convex2:
print("Not convex")
return False
if idx_c1 == 0:
return False
if abs(idx_p1 - idx_paste) <= abs(idx_p2 - idx_paste) and idx_p1 != 0:
idx_paste = idx_p1
else:
idx_paste = idx_p2 + 1
copy_y1 = self.t.gtCells[idx_c1][0].y1
copy_y2 = self.t.gtCells[idx_c2][0].y2
paste_y1 = self.t.gtCells[idx_paste - 1][0].y2
h = copy_y2 - copy_y1
if self.t.y2 + h > self.doc_shape[0] * 1.5:
return False
rows = [Row(self.t.x1, self.t.y1, self.t.x2)] + self.t.gtRows
rows_add = list(map(lambda x: x.move_im(0, paste_y1 - copy_y1), rows[idx_c1:idx_c2 + 1]))
rows[idx_paste:] = [row.move_im(0, h) for row in rows[idx_paste:]]
rows += rows_add
rows.sort(key=lambda x: x.y1)
self.t.gtRows = rows[1:]
spans_add = list(map(lambda x: x.move_im(0, paste_y1 - copy_y1), spans))
for span in filter(lambda x: x.y1 > paste_y1, self.t.gtSpans):
span.move(0, h)
self.t.gtSpans += spans_add
self.t.y2 += h
self.t.evaluateCells()
return True
def remove_row(self, idx):
is_convex, idx_1, idx_2, spans = self.get_row_range(idx)
if not is_convex:
print("Not convex")
return False
if idx_1 == 0:
return False
y1 = self.t.gtCells[idx_1][0].y1
y2 = self.t.gtCells[idx_2][0].y2
h = y2 - y1
if h >= self.t.y2 * 0.6 or len(self.t.gtCells) - (idx_2 - idx_1 + 1) <= 3:
return False
rows = [Row(self.t.x1, self.t.y1, self.t.x2)] + self.t.gtRows
rows = [row for row in rows if row not in rows[idx_1: idx_2 + 1]]
rows[idx_1:] = [row.move_im(0, -h) for row in rows[idx_1:]]
rows.sort(key=lambda x: x.y1)
self.t.gtRows = rows[1:]
self.t.gtSpans = [span for span in self.t.gtSpans if span not in spans]
for span in filter(lambda x: x.y1 > y1, self.t.gtSpans):
span.move(0, -h)
self.t.y2 -= h
self.t.evaluateCells()
return True
def replicate_column(self, idx_copy, idx_paste):
is_convex, idx_c1, idx_c2, spans = self.get_column_range(idx_copy)
is_convex2, idx_p1, idx_p2, _ = self.get_column_range(idx_paste)
if not is_convex or not is_convex2:
print("Not convex")
return False
if idx_c1 == 0:
return False
if abs(idx_p1 - idx_paste) <= abs(idx_p2 - idx_paste) and idx_p1 != 0:
idx_paste = idx_p1
else:
idx_paste = idx_p2 + 1
copy_x1 = self.t.gtCells[0][idx_c1].x1
copy_x2 = self.t.gtCells[0][idx_c2].x2
paste_x1 = self.t.gtCells[0][idx_paste - 1].x2
w = copy_x2 - copy_x1
if self.t.x2 + w > self.doc_shape[1] * 1.5:
return False
cols = [Column(self.t.x1, self.t.y1, self.t.y2)] + self.t.gtCols
cols_add = list(map(lambda x: x.move_im(paste_x1 - copy_x1, 0), cols[idx_c1:idx_c2 + 1]))
cols[idx_paste:] = [col.move_im(w, 0) for col in cols[idx_paste:]]
cols += cols_add
cols.sort(key=lambda x: x.x1)
self.t.gtCols = cols[1:]
spans_add = list(map(lambda x: x.move_im(paste_x1 - copy_x1, 0), spans))
for span in filter(lambda x: x.x1 > paste_x1, self.t.gtSpans):
span.move(w, 0)
self.t.gtSpans += spans_add
self.t.x2 += w
self.t.evaluateCells()
return True
def remove_column(self, idx):
is_convex, idx_1, idx_2, spans = self.get_column_range(idx)
if not is_convex:
print("Not convex")
return False
if idx_1 == 0:
return False
x1 = self.t.gtCells[0][idx_1].x1
x2 = self.t.gtCells[0][idx_2].x2
w = x2 - x1
if w >= self.t.x2 * 0.7 or len(self.t.gtCells[0]) - (idx_2 - idx_1 + 1) <= 2:
return False
cols = [Column(self.t.x1, self.t.y1, self.t.y2)] + self.t.gtCols
cols = [col for col in cols if col not in cols[idx_1: idx_2 + 1]]
cols[idx_1:] = [col.move_im(-w, 0) for col in cols[idx_1:]]
cols.sort(key=lambda x: x.x1)
self.t.gtCols = cols[1:]
self.t.gtSpans = [span for span in self.t.gtSpans if span not in spans]
for span in filter(lambda x: x.x1 > x1, self.t.gtSpans):
span.move(-w, 0)
self.t.x2 -= w
self.t.evaluateCells()
return True
def visualize(self, window="image"):
image = np.ones((self.t.y2, self.t.x2, 3), dtype=np.uint8) * 255
if len(image.shape) == 2 or image.shape[2] < 3:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
self.t.visualize(image)
cv2.imshow(window, image)
cv2.waitKey(0)
# MAX_DEPTH = 5
# MAX_WIDTH = [10, 5, 2, 1, 1]
# MAX_ATTEMPTS = [10, 5, 2, 1, 1]
MAX_DEPTH = 10
MAX_WIDTH = [8, 4, 2, 2, 2, 1, 1, 1, 1, 1]
MAX_ATTEMPTS = [x * 2 for x in MAX_WIDTH]
def apply_action(obj, action):
return getattr(obj, action[0])(*action[1])
def DFS(augmentor, depth, actions):
if augmentor.t.y2 > augmentor.doc_shape[0] * 1.5 or augmentor.t.x2 > augmentor.doc_shape[1] * 1.5 or depth < 5:
return_list = []
else:
return_list = [(
augmentor.t,
{
"h" : len(augmentor.t.gtCells),
"w" : len(augmentor.t.gtCells[0]),
"col_spans" : len([x for x in augmentor.t.gtSpans if isinstance(x, ColSpan)]),
"row_spans" : len([x for x in augmentor.t.gtSpans if isinstance(x, RowSpan)]),
},
actions
)]
if depth < MAX_DEPTH:
counter = 0
for i in range(MAX_ATTEMPTS[depth]):
if counter >= MAX_WIDTH[depth]:
break
prob = random.random()
tmp = copy.deepcopy(augmentor)
if prob < 0.25:
size = len(tmp.t.gtCells[0])
i = random.choice(range(1, size))
j = random.choice(range(1, size + 1))
action = ('replicate_column', (i, j))
elif prob < 0.5:
size = len(tmp.t.gtCells[0])
i = random.choice(range(1, size))
action = ('remove_column', (i,))
elif prob < 0.75:
size = len(tmp.t.gtCells)
i = random.choice(range(1, size))
j = random.choice(range(1, size + 1))
action = ('replicate_row', (i, j))
else:
size = len(tmp.t.gtCells)
i = random.choice(range(1, size))
action = ('remove_row', (i,))
edited = apply_action(tmp, action)
if edited:
counter += 1
actions_copy = actions.copy()
actions_copy.append(action)
return_list += DFS(tmp, depth + 1, actions_copy)
return return_list
def dfs_wrapper(table):
return DFS(Augmentor(table), 0, [])