-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathtest_wired_table_rec.py
351 lines (315 loc) · 12.2 KB
/
test_wired_table_rec.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
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
import sys
from pathlib import Path
import numpy as np
import pytest
from bs4 import BeautifulSoup
from rapidocr import RapidOCR
from wired_table_rec.main import WiredTableInput, ModelType
from wired_table_rec.utils.utils import rescale_size
from wired_table_rec.utils.utils_table_recover import (
plot_html_table,
is_single_axis_contained,
gather_ocr_list_by_row,
sorted_ocr_boxes,
is_box_contained,
)
cur_dir = Path(__file__).resolve().parent
root_dir = cur_dir.parent
sys.path.append(str(root_dir))
from wired_table_rec import WiredTableRecognition
test_file_dir = cur_dir / "test_files" / "wired"
input_args = WiredTableInput(model_type=ModelType.UNET.value)
table_recog = WiredTableRecognition(input_args)
ocr_engine = RapidOCR()
def get_td_nums(html: str) -> int:
soup = BeautifulSoup(html, "html.parser")
if not soup.table:
return 0
tds = soup.table.find_all("td")
return len(tds)
def test_squeeze_bug():
img_path = test_file_dir / "squeeze_error.jpeg"
rapid_ocr_output = ocr_engine(img_path, return_word_box=True)
ocr_result = list(
zip(rapid_ocr_output.boxes, rapid_ocr_output.txts, rapid_ocr_output.scores)
)
table_results = table_recog(str(img_path), ocr_result=ocr_result)
table_html_str, table_cell_bboxes = (
table_results.pred_html,
table_results.cell_bboxes,
)
td_nums = get_td_nums(table_html_str)
assert td_nums >= 160
@pytest.mark.parametrize(
"img_path, gt_td_nums, gt2",
[
("table_recognition.jpg", 35, "d colsp"),
("table2.jpg", 23, "td><td "),
("row_span.png", 17, "></td><"),
("no_table.jpg", 1, "d colsp"),
],
)
def test_input_normal(img_path, gt_td_nums, gt2):
img_path = test_file_dir / img_path
rapid_ocr_output = ocr_engine(img_path, return_word_box=True)
ocr_result = list(
zip(rapid_ocr_output.boxes, rapid_ocr_output.txts, rapid_ocr_output.scores)
)
table_results = table_recog(str(img_path), ocr_result=ocr_result)
table_html_str, table_cell_bboxes = (
table_results.pred_html,
table_results.cell_bboxes,
)
td_nums = get_td_nums(table_html_str)
assert td_nums >= gt_td_nums
@pytest.mark.parametrize(
"img_path, gt_td_nums",
[
("wired_big_box.png", 44),
],
)
def test_enhance_box_line(img_path, gt_td_nums):
img_path = test_file_dir / img_path
rapid_ocr_output = ocr_engine(img_path, return_word_box=True)
ocr_result = list(
zip(rapid_ocr_output.boxes, rapid_ocr_output.txts, rapid_ocr_output.scores)
)
table_results = table_recog(
str(img_path), ocr_result=ocr_result, enhance_box_line=False
)
table_html_str, table_cell_bboxes = (
table_results.pred_html,
table_results.cell_bboxes,
)
td_nums = get_td_nums(table_html_str)
assert td_nums <= gt_td_nums
@pytest.mark.parametrize(
"box1, box2, threshold, expected",
[
# Box1 完全包含在 Box2 内
([[10, 20, 30, 40], [5, 15, 45, 55], 0.2, 1]),
# Box2 完全包含在 Box1 内
([[5, 15, 45, 55], [10, 20, 30, 40], 0.2, 2]),
# Box1 和 Box2 部分重叠,但不满足阈值
([[10, 20, 30, 40], [25, 35, 45, 55], 0.2, None]),
# Box1 和 Box2 完全不重叠
([[10, 20, 30, 40], [50, 60, 70, 80], 0.2, None]),
# Box1 和 Box2 有交集,但不满足阈值
([[10, 20, 30, 40], [15, 25, 35, 45], 0.2, None]),
# Box1 和 Box2 有交集,且满足阈值
([[10, 20, 30, 40], [15, 25, 35, 45], 0.5, 1]),
# Box1 和 Box2 有交集,且满足阈值
([[15, 25, 35, 45], [14, 24, 16, 44], 0.6, 2]),
# Box1 和 Box2 相同
([[10, 20, 30, 40], [10, 20, 30, 40], 0.2, 1]),
# 使用 NumPy 数组作为输入
([np.array([10, 20, 30, 40]), np.array([5, 15, 45, 55]), 0.2, 1]),
],
)
def test_is_box_contained(box1, box2, threshold, expected):
result = is_box_contained(box1, box2, threshold)
assert result == expected, f"Expected {expected}, but got {result}"
@pytest.mark.parametrize(
"box1, box2, axis, threshold, expected",
[
# Box1 完全包含 Box2 (X轴)
([10, 10, 20, 20], [12, 12, 18, 18], "x", 0.2, 2),
# Box2 完全包含 Box1 (X轴)
([12, 12, 18, 18], [10, 10, 20, 20], "x", 0.2, 1),
# Box1 完全包含 Box2 (Y轴)
([10, 10, 20, 20], [12, 12, 18, 18], "y", 0.2, 2),
# Box2 完全包含 Box1 (Y轴)
([12, 12, 18, 18], [10, 10, 20, 20], "y", 0.2, 1),
# Box1 和 Box2 不相交 (X轴)
([10, 10, 20, 20], [25, 25, 30, 30], "x", 0.2, None),
# Box1 和 Box2 不相交 (Y轴)
([10, 10, 20, 20], [25, 25, 30, 30], "y", 0.2, None),
# Box1 部分包含 Box2 (X轴)-超过阈值
([10, 10, 20, 20], [15, 15, 25, 25], "x", 0.2, None),
# Box1 部分包含 Box2 (Y轴)-超过阈值
([10, 10, 20, 20], [15, 15, 25, 25], "y", 0.2, None),
# Box1 部分包含 Box2 (X轴)-满足阈值
([10, 10, 20, 20], [13, 15, 21, 25], "x", 0.2, 2),
# Box2 部分包含 Box1 (Y轴)-满足阈值
([10, 14, 20, 20], [15, 15, 25, 50], "y", 0.2, 1),
# Box1 和 Box2 完全重合 (X轴)
([10, 10, 20, 20], [10, 10, 20, 20], "x", 0.2, 1),
# Box1 和 Box2 完全重合 (Y轴)
([10, 10, 20, 20], [10, 10, 20, 20], "y", 0.2, 1),
],
)
def test_is_single_axis_contained(box1, box2, axis, threshold, expected):
result = is_single_axis_contained(box1, box2, axis, threshold)
assert result == expected
@pytest.mark.parametrize(
"input_ocr_list, expected_output",
[
(
[[[10, 20, 30, 40], "text1"], [[15, 23, 35, 43], "text2"]],
[[[10, 20, 35, 43], "text1text2"]],
),
(
[
[[10, 24, 30, 30], "text1"],
[[15, 25, 35, 45], "text2"],
[[5, 30, 15, 50], "text3"],
],
[[[10, 24, 35, 45], "text1text2"], [[5, 30, 15, 50], "text3"]],
),
([], []),
(
[[[10, 20, 30, 40], "text1"], [], [[15, 25, 35, 45], "text2"]],
[[[10, 20, 30, 40], "text1"], [[15, 25, 35, 45], "text2"]],
),
],
)
def test_gather_ocr_list_by_row(input_ocr_list, expected_output):
result = gather_ocr_list_by_row(input_ocr_list)
assert result == expected_output, f"Expected {expected_output}, but got {result}"
@pytest.mark.parametrize(
"dt_boxes, expected_boxes, expected_indices",
[
# 基本排序情况
(
np.array([[2, 3, 4, 5], [3, 4, 5, 6], [1, 2, 2, 3]]),
np.array([[1, 2, 2, 3], [2, 3, 4, 5], [3, 4, 5, 6]]),
[2, 0, 1],
),
# 基本排序错误,修正正确
(
np.array([[59, 0, 148, 52], [134, 0, 254, 53], [12, 13, 30, 40]]),
np.array([[12, 13, 30, 40], [59, 0, 148, 52], [134, 0, 254, 53]]),
[2, 0, 1],
),
# 一个盒子的情况
(np.array([[2, 3, 4, 5]]), np.array([[2, 3, 4, 5]]), [0]),
# 无盒子的情况
(np.array([]), np.array([]), []),
],
)
def test_sorted_ocr_boxes(dt_boxes, expected_boxes, expected_indices):
sorted_boxes, indices = sorted_ocr_boxes(dt_boxes)
assert (
sorted_boxes.tolist() == expected_boxes.tolist()
), f"Expected {expected_boxes.tolist()}, but got {sorted_boxes.tolist()}"
assert (
indices == expected_indices
), f"Expected {expected_indices}, but got {indices}"
@pytest.mark.parametrize(
"old_size, scale, return_scale, expected_result",
[
# 以短边为准进行缩放
((100, 50), (300, 100), True, ((200, 100), 2.0)),
((50, 100), (100, 300), True, ((100, 200), 2.0)),
# 以长边为准进行缩放
((100, 50), (200, 150), True, ((200, 100), 2.0)),
((50, 100), (150, 200), True, ((100, 200), 2.0)),
],
)
def test_rescale_size(old_size, scale, return_scale, expected_result):
result = rescale_size(old_size, scale, return_scale)
assert np.isclose(result[1], expected_result[1], atol=1e-5)
assert (
result[0] == expected_result[0]
), f"Expected {expected_result}, but got {result}"
@pytest.mark.parametrize(
"logi_points, cell_box_map, expected_html",
[
# 测试空输入
([], {}, "<html><body><table></table></body></html>"),
# 测试单个单元格,包含rowspan和colspan
(
[[0, 0, 0, 0]],
{0: ["Cell 1"]},
"<html><body><table><tr><td rowspan=1 colspan=1>Cell 1</td></tr></table></body></html>",
),
# 测试多个独立单元格
(
[[0, 0, 0, 0], [1, 1, 1, 1]],
{0: ["Cell 1"], 1: ["Cell 2"]},
"<html><body><table><tr><td rowspan=1 colspan=1>Cell 1</td><td></td></tr><tr><td></td><td rowspan=1 colspan=1>Cell 2</td></tr></table></body></html>",
),
# 测试跨行的单元格
(
[[0, 1, 0, 0]],
{0: ["Row 1 Col 1", "Row 2 Col 1"]},
"<html><body><table><tr><td rowspan=2 colspan=1>Row 1 Col 1<br>Row 2 Col 1</td></tr><tr></tr></table></body></html>",
),
# 测试跨列的单元格
(
[[0, 0, 0, 1]],
{0: ["Col 1 Row 1", "Col 2 Row 1"]},
"<html><body><table><tr><td rowspan=1 colspan=2>Col 1 Row 1<br>Col 2 Row 1</td></tr></table></body></html>",
),
# 测试跨多行多列的单元格
(
[[0, 1, 0, 1]],
{0: ["Row 1 Col 1", "Row 2 Col 1"]},
"<html><body><table><tr><td rowspan=2 colspan=2>Row 1 Col 1<br>Row 2 Col 1</td></tr><tr></tr></table></body></html>",
),
# 测试跨行跨行跨列的单元格出现在中间
(
[[0, 0, 0, 0], [0, 1, 1, 2]],
{0: ["Cell 1"], 1: ["Row 2", "Col 2"]},
"<html><body><table><tr><td rowspan=1 colspan=1>Cell 1</td><td rowspan=2 colspan=2>Row 2<br>Col 2</td></tr><tr><td></td></tr></table></body></html>",
),
# 测试跨行跨列的单元格出现在结尾
(
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 2, 2]],
{0: ["Cell 1"], 1: ["Cell 2"], 2: ["Row 1 Col 2", "Row 2 Col 2"]},
"<html><body><table><tr><td rowspan=1 colspan=1>Cell 1</td><td></td><td rowspan=2 colspan=1>Row 1 Col 2<br>Row 2 Col 2</td></tr><tr><td></td><td rowspan=1 colspan=1>Cell 2</td></tr></table></body></html>",
),
# 测试去除无效行和无效列
(
[[0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 1], [0, 0, 1, 2]],
{2: ["Row 3 Col 1", "Row 3 Col 2"]},
"<html><body><table><tr><td rowspan=1 colspan=1>Row 3 Col 1<br>Row 3 Col 2</td></tr></table></body></html>",
),
],
)
def test_plot_html_table(logi_points, cell_box_map, expected_html):
html_output = plot_html_table(logi_points, cell_box_map)
assert (
html_output == expected_html
), f"Expected HTML does not match. Got: {html_output}"
@pytest.mark.parametrize(
"img_path, gt_td_nums, gt2",
[
("table_recognition.jpg", 20, "d colsp"),
],
)
def test_no_rec_again(img_path, gt_td_nums, gt2):
img_path = test_file_dir / img_path
rapid_ocr_output = ocr_engine(img_path, return_word_box=True)
ocr_result = list(
zip(rapid_ocr_output.boxes, rapid_ocr_output.txts, rapid_ocr_output.scores)
)
table_results = table_recog(str(img_path), ocr_result=ocr_result)
table_html_str, table_cell_bboxes = (
table_results.pred_html,
table_results.cell_bboxes,
)
td_nums = get_td_nums(table_html_str)
assert td_nums >= gt_td_nums
@pytest.mark.parametrize(
"img_path, html_output, points_len",
[
("table2.jpg", "", 20),
("row_span.png", "", 14),
],
)
def test_no_ocr(img_path, html_output, points_len):
img_path = test_file_dir / img_path
table_results = table_recog(str(img_path), need_ocr=False)
table_html_str, table_cell_bboxes, table_logic_points = (
table_results.pred_html,
table_results.cell_bboxes,
table_results.logic_points,
)
assert len(table_cell_bboxes) > points_len
assert len(table_logic_points) > points_len
assert len(table_cell_bboxes) == len(table_logic_points)
assert table_html_str == html_output