forked from cndplab-founder/ctdar_measurement_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_eval.py
565 lines (483 loc) · 23.2 KB
/
test_eval.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# -*- coding: utf-8 -*-
"""
Some unit test for the evaluate_result_reg and evaluate_result_str of the eval
class from eval.py
Usage:
pytest test_eval.py
or
python -mpytest test_eval.py
(you need to add pytest to your Python installation)
JL Meunier - March 2019
Naver Labs Europe
"""
import xml.dom.minidom
from eval import eval
from data_structure import ResultStructure
# --- Utilities -------------------------------------------------------
def OkErrMiss(o):
"""
compute Ok, errors, missed from the ResultStructure object
return a triple (ok, err, miss)
"""
ok = o.truePos
err = o.resTotal - ok
miss = o.gtTotal - ok
return ok, err, miss
def _regEval(gt_dom, result_dom, iou_value):
"""
encapsulate the evaluate_result_reg to return ok, err, miss
"""
o = eval.evaluate_result_reg(gt_dom, result_dom, iou_value)
# print(o)
return OkErrMiss(o)
def _strEval(gt_dom, result_dom, iou_value):
"""
encapsulate the evaluate_result_str to return ok, err, miss
"""
o = eval.evaluate_result_str(gt_dom, result_dom, iou_value)
# print(o)
return OkErrMiss(o)
def _rectangle(w, h, x0=0, y0=0, bClosed=True, sXmlCells=None):
"""
create a rectangle with bottom left corner at 0,0 or at x,y if given
the list of coordinates is closed (start==end) if bClosed is True
return the corresponding DOM
"""
assert w > 0
assert h > 0
sXml = """<?xml version="1.0" encoding="UTF-8"?>
<document filename="table1.jpg">
%s
</document>
""" % _table_xml(x0, y0, w, h, bClosed, sXmlCells=sXmlCells)
return xml.dom.minidom.parseString(sXml)
def _multi_rectangle(lxywh):
"""
return a DOM containing multiple tables
"""
sXmlTable = ""
for x,y,w,h in lxywh:
assert w > 0 and h > 0
sXmlTable += "\n%s" % _table_xml(x, y, w, h, False)
sXml = """<?xml version="1.0" encoding="UTF-8"?>
<document filename="table1.jpg">
%s
</document>
""" % sXmlTable
return xml.dom.minidom.parseString(sXml)
def _table_xml(x, y, w, h, bClosed=True, sXmlCells=None):
"""
XML Table element as a string
"""
if bClosed:
#close the list of coordinates
sPoints = "%d,%d %d,%d %d,%d %d,%d %d,%d" % (x,y , x+w,y , x+w,y+h , x,y+h , x,y)
else:
sPoints = "%d,%d %d,%d %d,%d %d,%d" % (x,y , x+w,y , x+w,y+h , x,y+h)
if sXmlCells is None:
sXmlCells = """<cell start-row='0' start-col='0' end-row='1' end-col='2'>
<Coords points="180,160 177,456 614,456 615,163"/>
</cell>"""
sXml = """
<table>
<Coords points="%s"/>
%s
</table>
""" % (sPoints, sXmlCells)
return sXml
def _cell_xml(sr, sc, er, ec, sPointXml
, bFilled=False # do not put some text by default
, iShiftRow=0, iShiftCol=0 # shift the whole table
, bRotate=False # swap Xs and Ys (90° rotation)
):
"""
Generate the serialized XML for one cell, possibly shifted, column- or row-wise
"""
if bFilled:
sContent = "<content><xx>%d_%d_%d_%d_%s</xx></content>"%(sr, sc, er, ec, sPointXml)
else:
sContent = ""
if bRotate:
lt2 = [(sPair.split(',')[0], sPair.split(',')[1]) for sPair in sPointXml.strip().split()]
sRotatedPointXml = " ".join("%s,%s"%(y, x) for x,y in lt2)
t6 = (sc+iShiftCol, sr+iShiftRow, ec+iShiftCol, er+iShiftRow, sRotatedPointXml, sContent)
else:
t6 = (sr+iShiftRow, sc+iShiftCol, er+iShiftRow, ec+iShiftCol, sPointXml, sContent)
return """<cell start-row='%d' start-col='%d' end-row='%d' end-col='%d'>
<Coords points="%s"/>
%s
</cell>""" % t6
# -----------------------------------------------------------------------
def test_reg_simple():
"""
a square compared to itself
"""
for bClosedCoords in [True, False]:
for IoU in [0.1, 0.5, 0.9, 1.0]:
assert _regEval( _rectangle(100, 100, bClosed=bClosedCoords)
, _rectangle(100, 100)
, 0.9) == (1, 0, 0)
GT = _rectangle(100, 100, bClosed=bClosedCoords)
assert _regEval( GT, _rectangle(89, 100)
, 0.89) == (1, 0, 0)
assert _regEval( GT, _rectangle(89, 100)
, 0.9) == (0, 1, 1)
assert _regEval( GT, _rectangle(90, 100)
, 0.9) == (1, 0, 0)
assert _regEval( GT, _rectangle(91, 100)
, 0.9) == (1, 0, 0)
assert _regEval( GT, _rectangle(100, 89)
, 0.9) == (0, 1, 1)
assert _regEval( GT, _rectangle(100, 90)
, 0.9) == (1, 0, 0)
assert _regEval( GT, _rectangle(100, 91)
, 0.9) == (1, 0, 0)
def test_reg_quarter():
"""
a square compared to its quarter
"""
GT = _rectangle(100, 100)
assert _regEval( GT, _rectangle(50, 50)
, 0.9) == (0, 1, 1)
assert _regEval( GT, _rectangle(50, 50)
, 0.24) == (1, 0, 0)
assert _regEval( GT, _rectangle(50, 50)
, 0.25) == (1, 0, 0)
GT = _rectangle(100, 100, x0=50, y0=50)
assert _regEval( GT, _rectangle(100, 100)
, 0.9) == (0, 1, 1)
iou = 50*50 / (7 * 50*50)
print("expected=", iou)
assert _regEval( GT, _rectangle(100, 100)
, iou + 0.01) == (0, 1, 1)
assert _regEval( GT, _rectangle(100, 100)
, iou - 0.01) == (1, 0, 0)
def test_multi_table():
"""
what if we have several tables on a page?
"""
GT = _multi_rectangle([(0 ,0 ,100, 100)
, (200, 200, 100, 300)])
assert _regEval( GT, GT
, 1.0) == (2, 0, 0)
assert _regEval( GT, GT
, 0.1) == (2, 0, 0)
# one missing table
RUN =_multi_rectangle([(0 ,0 ,100, 100)
])
assert _regEval( GT, RUN
, 1.0) == (1, 0, 1)
assert _regEval( GT, RUN
, 0.1) == (1, 0, 1)
# one extra table
RUN =_multi_rectangle([(0 ,0 ,100, 100)
, (200, 200, 100, 300)
, (1000 ,1000 ,1100, 1100)
])
assert _regEval( GT, RUN
, 1.0) == (2, 1, 0)
assert _regEval( GT, RUN
, 0.1) == (2, 1, 0)
# messy situation with one predicted table in middle overlapping the two GT tables
# but the 2nd one is "more" overlapped
RUN =_multi_rectangle([(50 , 50 , 300, 350)
])
res = _regEval( GT, RUN, 1.0)
assert res == (0, 1, 2), res
res = _regEval( GT, RUN, 0.01)
assert res == (1, 0, 1), res
iou1 = 50*50 / (100*100+300*350-50*50) # overlap with 1st GT table
iou2 = 200*100 / (100*300+300*350-200*100) # overlap with 2nd GT table
# 1st and middle on one in RUN
RUN =_multi_rectangle([(0 ,0 ,100, 100)
, (50 , 50 , 300, 350)
])
res = _regEval( GT, RUN, 1.0)
assert res == (1, 1, 1), res
res = _regEval( GT, RUN, 0.01)
assert res == (2, 0, 0), res
res = _regEval( GT, RUN, iou2-0.01)
assert res == (2, 0, 0), res
res = _regEval( GT, RUN, iou2+0.01)
assert res == (1, 1, 1), res
# 2nd and middle on one in RUN
RUN =_multi_rectangle([(200, 200, 100, 300)
, (50 , 50 , 300, 350)
])
res = _regEval( GT, RUN, 1.0)
assert res == (1, 1, 1), res
res = _regEval( GT, RUN, 0.01)
assert res == (2, 0, 0), res
res = _regEval( GT, RUN, iou2-0.01)
assert res == (1, 1, 1), res
res = _regEval( GT, RUN, iou2+0.01)
assert res == (1, 1, 1), res
# middle one should match the 1st one
res = _regEval( GT, RUN, iou1-0.01)
assert res == (2, 0, 0), res
res = _regEval( GT, RUN, iou1+0.01)
assert res == (1, 1, 1), res
# 1st and 2nd and middle one
RUN =_multi_rectangle([(0 ,0 ,100, 100)
, (200, 200, 100, 300)
, (50 , 50 , 300, 350)
])
res = _regEval( GT, RUN, 1.0)
assert res == (2, 1, 0), res
res = _regEval( GT, RUN, 0.01)
assert res == (2, 1, 0), res
res = _regEval( GT, RUN, iou2-0.01)
assert res == (2, 1, 0), res
res = _regEval( GT, RUN, iou2+0.01)
assert res == (2, 1, 0), res
# middle one should match the 1st one
res = _regEval( GT, RUN, iou1-0.01)
assert res == (2, 1, 0), res
res = _regEval( GT, RUN, iou1+0.01)
assert res == (2, 1, 0), res
# ----------------------------------------------------------------------
# table recognition
def test_table_recognition_obvious():
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70")
, _cell_xml(0,1,0,1,"60,60 70,60 70,70 60,70")
])
GT = _rectangle(100, 100, sXmlCells=sXmlCells)
res = _strEval(GT, GT, 0.85)
assert res == (1, 0, 0), res
def test_table_recognition_simple_horizontal():
# in the run, we might have no content in the cells
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70")
, _cell_xml(0,1,0,1,"60,60 70,60 70,70 60,70")
])
GT = _rectangle(100, 100, sXmlCells=sXmlCells)
for bCellContent in [True, False]:
for iShiftRow in range(0, 3):
for iShiftCol in range(0, 3):
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
, _cell_xml(0,1,0,1,"60,60 70,60 70,70 60,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (1, 0, 0), (bCellContent, res) # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.85)
assert res == (1, 0, 0), (bCellContent, res)
# 2nd cell slightly badly positionated
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
, _cell_xml(0,1,0,1,"64,60 70,60 70,70 64,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (0, 1, 1), (bCellContent, res) # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.50)
assert res == (1, 0, 0), (bCellContent, res)
# one big cell overlapping both
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 70,60 70,70 10,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.85)
assert res == (0, 0, 1), (bCellContent, res)
# one big cell overlapping both + one in 0,1 but far
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 70,60 70,70 10,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
, _cell_xml(0,1,0,1,"260,60 270,60 270,70 260,70", bCellContent, iShiftRow=iShiftRow, iShiftCol=iShiftCol)
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.85)
assert res == (0, 1, 1), (bCellContent, res)
def test_table_recognition_simple_vertical():
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70")
, _cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90")
])
GT = _rectangle(100, 100, sXmlCells=sXmlCells)
# no adjacency
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70")
, _cell_xml(1,1,1,1,"10,70 20,70 20,80 10,90")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.50)
assert res == (0, 0, 1), res
# 2nd cells side-by-side vertically
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70")
, _cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (1, 0, 0), res # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.51)
assert res == (1, 0, 0), res
# 2nd cells side-by-side vertically - changed order in XML
sXmlRunCells = " ".join([
_cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90")
, _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (1, 0, 0), res # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.51)
assert res == (1, 0, 0), res
# 1st cell halved
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,66 10,66")
, _cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (0, 1, 1), res # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.51)
assert res == (1, 0, 0), res
# 1st cell halved and placed in 1,1
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,66 10,66")
, _cell_xml(1,1,1,1,"10,70 20,70 20,80 10,90")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (0, 0, 1), res # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.51)
assert res == (0, 0, 1), res
# 2nd cell placed in 0,0 (INCONSISTENT prediction)
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,66 10,66")
, _cell_xml(0,0,0,0,"10,70 20,70 20,80 10,90")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (0, 0, 1), res # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.51)
assert res == (0, 0, 1), res
# 2nd cell extended to cover part of the top of 1st cell
sXmlRunCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,66 10,66")
, _cell_xml(1,0,1,0,"10,66 20,66 20,80 10,90")
])
RUN = _rectangle(90 , 100, sXmlCells=sXmlRunCells) # width=90
res = _strEval(GT, RUN, 0.95)
assert res == (0, 1, 1), res # 0.95 is for cells not table!
res = _strEval(GT, RUN, 0.51)
assert res == (1, 0, 0), res
def test_table_recognition_simple_horizontal_vertical():
# some row and col displacement of the precited table...
for iRgt, iCgt in [(i,j) for i in range(6) for j in range(6)]:
# # #
# #
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", iShiftRow=iRgt, iShiftCol=iCgt)
, _cell_xml(0,1,0,1,"60,60 70,60 70,70 60,70", iShiftRow=iRgt, iShiftCol=iCgt)
, _cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90", iShiftRow=iRgt, iShiftCol=iCgt)
])
GT = _rectangle(100, 100 , sXmlCells=sXmlCells)
# RUN same as GT
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (2, 0, 0), res
# some row and col displacement of the precited table...
for iR, iC in [(i,j) for i in range(3) for j in range(3)]:
# Now some cells get reduced by half
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", iShiftRow=iR, iShiftCol=iC)
, _cell_xml(0,1,0,1,"60,60 70,60 70,70 60,70", iShiftRow=iR, iShiftCol=iC)
, _cell_xml(1,0,1,0,"10,70 20,70 20,76 10,86", iShiftRow=iR, iShiftCol=iC) # here!
])
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (1, 1, 1), res
res = _strEval(GT, RUN, 0.51)
assert res == (2, 0, 0), res
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", iShiftRow=iR, iShiftCol=iC)
, _cell_xml(0,1,0,1,"60,60 70,60 70,66 60,66", iShiftRow=iR, iShiftCol=iC) #here
, _cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90", iShiftRow=iR, iShiftCol=iC)
])
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (1, 1, 1), res
res = _strEval(GT, RUN, 0.51)
assert res == (2, 0, 0), res
# some extra cell
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", iShiftRow=iRgt, iShiftCol=iCgt)
, _cell_xml(0,1,0,1,"60,60 70,60 70,70 60,70", iShiftRow=iRgt, iShiftCol=iCgt)
, _cell_xml(1,0,1,0,"10,70 20,70 20,80 10,90", iShiftRow=iRgt, iShiftCol=iCgt)
, _cell_xml(1,1,1,1,"30,70 40,70 40,80 50,90", iShiftRow=iRgt, iShiftCol=iCgt)
])
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (2, 2, 0), res
res = _strEval(GT, RUN, 0.51)
assert res == (2, 2, 0), res
# with some reduced size
sXmlCells = " ".join([ _cell_xml(0,0,0,0,"10,60 20,60 20,70 10,70", iShiftRow=iRgt, iShiftCol=iCgt)
, _cell_xml(0,1,0,1,"60,60 70,60 70,66 60,66", iShiftRow=iRgt, iShiftCol=iCgt) # here
, _cell_xml(1,0,1,0,"10,70 20,70 20,76 10,86", iShiftRow=iRgt, iShiftCol=iCgt) # here
, _cell_xml(1,1,1,1,"30,70 40,70 40,80 50,90", iShiftRow=iRgt, iShiftCol=iCgt)
])
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (0, 4, 2), res
res = _strEval(GT, RUN, 0.51)
assert res == (2, 2, 0), res
def test_table_recognition_spanning_cells_simple():
# ###
# # #
for bRotate in [False, True]:
# bRotate allows to swap Xs and Ys"
sXmlCells = " ".join([ _cell_xml(0,0,0,2,"10,60 100,60 100,70 10,70", bRotate=bRotate) # flat horizontal rectangle spanning over 3 cols
, _cell_xml(1,0,1,0,"10,80 50,80 50,90 10,90", bRotate=bRotate)
, _cell_xml(1,2,1,2,"60,80 100,80 100,90 60,90", bRotate=bRotate)
])
GT = _rectangle(100, 100 , sXmlCells=sXmlCells)
# RUN same as GT
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (3, 0, 0), res
# ###
# ##
# showing that empty cells are ignored
sXmlCells = " ".join([ _cell_xml(0,0,0,2,"10,60 100,60 100,70 10,70", bRotate=bRotate) # flat horizontal rectangle spanning over 3 cols
, _cell_xml(1,0,1,0,"10,80 50,80 50,90 10,90", bRotate=bRotate)
, _cell_xml(1,1,1,1,"60,80 100,80 100,90 60,90", bRotate=bRotate)
])
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (3, 0, 0), res
# ###
# ###
sXmlCells = " ".join([ _cell_xml(0,0,0,2,"10,60 100,60 100,70 10,70", bRotate=bRotate) # flat horizontal rectangle spanning over 3 cols
, _cell_xml(1,0,1,0,"10,80 90,80 90,90 10,90", bRotate=bRotate) # another long flat rectangle
])
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (0, 1, 3), res
res = _strEval(GT, RUN, 0.31)
assert res == (1, 0, 2), res
def test_table_recognition_spanning_cells_less_simple():
# ### #
# # ###
for bRotate in [False, True]:
# bRotate allows to swap Xs and Ys"
sXmlCells = " ".join([ _cell_xml(0,0,0,2,"10,60 100,60 100,70 10,70", bRotate=bRotate) # flat horizontal rectangle spanning over 3 cols
, _cell_xml(0,3,0,3,"210,60 250,60 250,70 210,70", bRotate=bRotate)
, _cell_xml(1,0,1,0,"10,80 50,80 50,90 10,90", bRotate=bRotate)
, _cell_xml(1,1,1,3,"60,80 260,80 260,90 60,90", bRotate=bRotate)
])
GT = _rectangle(100, 100 , sXmlCells=sXmlCells)
# RUN same as GT
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (5, 0, 0), res
# ### #
# # #
sXmlCells = " ".join([ _cell_xml(0,0,0,2,"10,60 100,60 100,70 10,70", bRotate=bRotate) # flat horizontal rectangle spanning over 3 cols
, _cell_xml(0,3,0,3,"210,60 250,60 250,70 210,70", bRotate=bRotate)
, _cell_xml(1,0,1,0,"10,80 50,80 50,90 10,90", bRotate=bRotate)
, _cell_xml(1,3,1,3,"210,80 260,80 260,90 210,90", bRotate=bRotate)
])
# RUN same as GT
RUN = _rectangle(100 , 100, sXmlCells=sXmlCells)
res = _strEval(GT, RUN, 0.95)
assert res == (2, 2, 3), res
res = _strEval(GT, RUN, 0.1)
assert res == (4, 0, 1), res
# ----------------------------------------------------------------------
if __name__ == "__main__":
# to test manually some code...
# test_reg_quarter()
# test_multi_table()
# test_table_recognition_obvious()
# test_table_recognition_simple_horizontal()
# test_table_recognition_simple_vertical()
# test_table_recognition_simple_horizontal_vertical()
# test_table_recognition_spanning_cells_simple()
test_table_recognition_spanning_cells_less_simple()
print("ok, test(s) done")