-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntests.py
executable file
·1062 lines (961 loc) · 37.6 KB
/
runtests.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
This software is copyrighted material (C) Marion Anderson 2020
The library is provided under the Apache 2.0 License,
as found at https://www.apache.org/licenses/LICENSE-2.0
OVERVIEW:
This script provides unit testing for CalibratePSEye.py
USAGE:
runtests.py [-h] [--loglvl{CRITICAL,ERROR,WARNING,INFO,DEBUG}]
TODO:
check file permissions and existence at get-go
"""
from argparse import ArgumentParser
from logging import CRITICAL as log_CRITICAL
from logging import DEBUG as log_DEBUG
from logging import ERROR as log_ERROR
from logging import INFO as log_INFO
from logging import WARNING as log_WARNING
from logging import basicConfig, critical, debug, error, info, warning
from os import listdir, mkdir
from os import remove as os_remove
from os.path import dirname
from os.path import exists as os_exists
from os.path import isdir, realpath
from shutil import rmtree
from time import localtime, strftime
from cv2 import (COLOR_GRAY2RGB, COLOR_RGB2GRAY, IMWRITE_JPEG_QUALITY,
TERM_CRITERIA_EPS, TERM_CRITERIA_MAX_ITER, cvtColor,
destroyAllWindows)
from cv2 import imread as cv_imread
from cv2 import imshow as cv_imshow
from cv2 import imwrite as cv_imwrite
from cv2 import undistort, waitKey
from numpy import (around, array_equal, argwhere, concatenate, asarray, float32, log10, mgrid,
newaxis, reshape, uint8, zeros)
from CalibratePSEye import *
parser = ArgumentParser(description='Run CalibratePSEye unittests')
parser.add_argument('--loglvl',
choices=['CRITICAL','ERROR','WARNING','INFO','DEBUG'],
default='CRITICAL', help='set unittest log level')
if __name__ == '__main__':
args = parser.parse_args()
level=log_CRITICAL
if args.loglvl == 'ERROR':
level=log_ERROR
elif args.loglvl == 'WARNING':
level=log_WARNING
elif args.loglvl == 'INFO':
level=log_INFO
elif args.loglvl == 'DEBUG':
level=log_DEBUG
basicConfig(level=level, format='\t%(levelname)s:%(funcName)s:%(message)s')
fdir = realpath(dirname(__file__))
testdir = realpath(fdir + '/tests')
calibsdir = testdir + '/calibs_00000000-000000'
if not os_exists(fdir + '/data'):
mkdir(fdir + '/data')
warning('data/ directory did not exists; created it')
# params for chessboard on marand's HP monitor
# last updated: 2020-05-22
# points of corners on chessboard
boardsize = (3, 4)
sq_len = 63 # mm
o = zeros((boardsize[0]*boardsize[1],3), float32)
o[:,:2] = mgrid[0:boardsize[0], 0:boardsize[1]].T.reshape(-1,2) * sq_len
# chessboard detection rules
p = {
'boardsize': boardsize,
'winsize': (11, 11),
'zerozone': (-1, -1),
'criteria': (
TERM_CRITERIA_EPS+TERM_CRITERIA_MAX_ITER, 30, 0.001
)
}
cpdict = {} # for testing calibration loading later
def test_report( func=None ):
"""
Use as decorator for a unit test to run it & prepare a report. Call
without any arguments to generate an overall test report.
:param func: Unit test, passed in like to a decorator, defaults to None
:type func: function, optional
When func is None, reports how many tests passed and how many tests
failed.
"""
# set static vars
if not hasattr(test_report, 'p'):
test_report.p = 0
if not hasattr(test_report, 'f'):
test_report.f = 0
# color-code messages for ease of reading
PASS_C = '\033[92m'
FAIL_C = '\033[91m'
REPORT_C = '\033[93m'
END_C = '\033[0m'
# overall unit testing report
if func is None:
msg = REPORT_C + '\nREPORT: %d/%d tests passed\n' % (
test_report.p, test_report.p+test_report.f
) + END_C
# running unit test
else:
try:
func()
except Exception as e:
# 46 - longest testname and pass label
# 4 - tabsize
max_msg_len = 46 + 4
msg = FAIL_C + 'FAIL:\t' + func.__name__ + ':' + END_C
spacer = max_msg_len - len(msg)
msg = msg + ''.ljust(spacer)
msg = msg + type(e).__name__
if len(e.args) != 0:
msg = msg + ': ' + e.args[0]
msg = msg
test_report.f += 1
else:
msg = PASS_C + 'PASS:\t' + func.__name__ + END_C
test_report.p += 1
finally:
# reset parameters regardless
global p, o, boardsize
boardsize = (3, 4)
sq_len = 63 # mm
o = zeros((boardsize[0]*boardsize[1],3), float32)
o[:,:2] = mgrid[0:boardsize[0], 0:boardsize[1]].T.reshape(-1,2) * sq_len
p = {
'boardsize': boardsize,
'winsize': (11, 11),
'zerozone': (-1, -1),
'criteria': (
TERM_CRITERIA_EPS+TERM_CRITERIA_MAX_ITER,30,0.001
)
}
print(msg)
def test_clear(calib):
"""
Asserts a bunch of things that should have been cleared on
CalibratePSEye instantiation. This code is re-used so much it gets its
own function.
"""
for k in ('calibpath', 'img_arr', 'objp',
'cameraMatrix', 'distCoeffs'):
if getattr(calib, k) is not None:
raise RuntimeError('\'calib.%s\' was not reset' % k)
debug('\'calib.%s\' was reset' % k)
for k in ('objpoints', 'corners_arr'):
if len(getattr(calib, k)) != 0:
raise RuntimeError('\'calib.%s\' was not reset' % k)
debug('\'calib.%s\' was reset' % k)
# ============================================================================
# Intialization Methods
# these also test `clear`
# ============================================================================
@test_report
def test_init_empty():
"""
Test empty constructor.
"""
calib = CalibratePSEye()
test_clear(calib)
@test_report
def test_init_chessboard_obj():
"""
Test chessboard initialization with Python objects
"""
calib = CalibratePSEye()
calib.init_chessboard(p, o)
try:
if calib.calibpath is None:
raise RuntimeError('_calib_path wasn\'t created')
for k in p.keys():
if not getattr(calib, k) == p[k]:
raise RuntimeError('\'%s\' param was loaded incorrectly' % k)
debug('\'%s\' param matched' % k)
if not array_equal(calib.objp, o):
raise RuntimeError('Failed to load objp correctly')
finally:
calib.removepath()
@test_report
def test_init_chessboard_str():
"""
Test initializing based off of filepaths.
"""
pstr = calibsdir + '/processing_params.csv'
ostr = calibsdir + '/objp.csv'
if not os_exists(pstr) or not os_exists(ostr):
raise RuntimeError('Bad test files \'%s\' and \'%s\'' % (pstr, ostr))
# Read String Files
with open(ostr, 'r') as f:
text = f.read()
data = text.split(', ')
shape = [int(v) for v in data[0].replace('\"', '').split('x')]
objp = reshape([int(v) for v in data[1:]], shape).astype('float32')
with open(pstr, 'r') as f:
lines = f.read().splitlines()
param_text = [line.split(', ') for line in lines]
params = dict()
for p in param_text:
name = p[0].replace('\"', '')
if name not in ('boardsize', 'zerozone', 'winsize', 'criteria'):
continue
if name == 'boardsize':
params[name] = (int(p[1]), int(p[2]))
elif name == 'zerozone':
params[name]= (int(p[1]), int(p[2]))
elif name == 'winsize':
params[name] = (int(p[1]), int(p[2]))
elif name == 'criteria':
# first value should be single-digit number
# use that to determine decimal place shift
powshift = -int(log10(int(p[1])))
params[name] = (
int(int(p[1]) * 10**powshift), # mode (int)
int(int(p[2]) * 10**powshift), # something about pixels (int)
int(p[3]) * 10**powshift # EPS termination (float)
)
# Test
calib = CalibratePSEye()
calib.init_chessboard(pstr, ostr)
try:
if calib.calibpath is None:
raise RuntimeError('_calib_path wasn\'t created')
for k in params.keys():
if not getattr(calib, k) == params[k]:
raise RuntimeError('\'%s\' param was loaded incorrectly' % k)
debug('\'%s\' param matched' % k)
if not array_equal(calib.objp, objp):
raise RuntimeError('Failed to load objp correctly')
finally:
calib.removepath()
# ============================================================================
# Private Methods
# ============================================================================
@test_report
def test_create_calib_path():
"""
Test creation of calibration storage path.
EXCEPTIONS
RuntimeError if failed to remove calibration directory
"""
calib = CalibratePSEye()
calib.init_chessboard(p, o)
if not isdir(calib.calibpath): # ensure creation
raise RuntimeError('Failed to create _calib_path')
time_str = strftime('%Y%m%d-%H%M%S', localtime()) # test timestamp
if not time_str[:-2] == calib.calibpath[-15:-2]:
raise RuntimeError('_calib_path timestamp doesn\'t match to current minute')
calib.removepath()
@test_report
def test_get_timestamp():
"""
Test retrieval of calib path timestamp
EXCEPTIONS
RuntimeError if failed to remove calibration directory
"""
calib = CalibratePSEye()
calib.init_chessboard(p, o)
time_str = strftime('%Y%m%d-%H%M%S', localtime()) # test timestamp
if not calib.calibpath[-15:] == calib.get_timestamp():
raise RuntimeError('Error getting timestamp')
if not time_str[:-2] == calib.get_timestamp()[:-2]:
raise RuntimeError(
'Retrieved timestamp doesn\'t match to minute - check `test_create_calib_path`'
)
calib.removepath()
@test_report
def test_verify_calib_params():
"""
Test verify_calib_params asserts
For int 2-tuples (boardsize, winsize, zerozone):
Tests too many and then too few elements to ensure equality check
Exact fit case was tested in the init testers above
Tests catching of floats
objp
`assert self.objp.shape[0] == self.boardsize[0] * self.boardsize[1]`
`assert self.objp.shape[1] == 3`
Same testing rules as above
Also checks for non-float32 types
"""
global p, o
# Test 2-tuples
for k in ('boardsize', 'winsize', 'zerozone'):
p[k] = (-1,-1,-1)
try:
calib = CalibratePSEye()
calib.init_chessboard(p, o)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch len(%s) too large' % k)
finally:
del calib
p[k] = (-1,)
try:
calib = CalibratePSEye()
calib.init_chessboard(p, o)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch len(%s) too large' % k)
finally:
del calib
p[k] = (-1.1,-1)
try:
calib = CalibratePSEye()
calib.init_chessboard(p, o)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch %s[0] non-int' % k)
finally:
del calib
p[k] = (-1,-1.1)
try:
calib = CalibratePSEye()
calib.init_chessboard(p, o)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch %s[1] non-int' % k)
finally:
del calib
p = {
'boardsize': boardsize,
'winsize': (11, 11),
'zerozone': (-1, -1),
'criteria': (
TERM_CRITERIA_EPS+TERM_CRITERIA_MAX_ITER,30,0.001
)
}
p['criteria'] = (-1.1, -1, 5, 18)
try:
calib = CalibratePSEye()
calib.init_chessboard(p, o)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch len(criteria) too large')
p['criteria'] = (-1,)
try:
calib = CalibratePSEye()
calib.init_chessboard(p, o)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch len(criteria) too small')
# check float32 case (should pass)
p['criteria'] = (3, 30, 0.001)
calib = CalibratePSEye()
calib.init_chessboard(p, o)
calib.removepath()
# check non float32 case (should fail)
for t in ('uint8', 'uint16', 'uint32', 'uint64', 'int16', 'int32',
'int64', 'float64', 'object'):
objp = o.astype(t)
try:
CalibratePSEye(p, objp)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch objp non-float32')
objp = o.copy()[...,newaxis]
try:
CalibratePSEye(p,objp)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch objp too many dimensions')
objp = zeros((boardsize[0]*boardsize[1]+1, 3))
try:
CalibratePSEye(p,objp)
except TypeError:
pass
else:
raise RuntimeError('Failed to catach objp[0] bad size')
objp = o[:, :2].copy()
try:
CalibratePSEye(p, objp)
except TypeError:
pass
else:
raise RuntimeError('Failed to catach objp[1] too small')
objp = zeros((o.shape[0], o.shape[1]+1))
try:
CalibratePSEye(p, objp)
except TypeError:
pass
else:
raise RuntimeError('Failed to catach objp[1] too large')
@test_report
def test_save_calib_params():
"""
Test that calibration params are being saved correctly, by opening
file and comparing results.
Loading params is tested in `test_init_str`.
"""
calib = CalibratePSEye()
calib.init_chessboard(p, o)
try:
pstr = calib.calibpath + '/processing_params.csv'
ostr = calib.calibpath + '/objp.csv'
calib._load_processing_params(pstr)
calib._load_objp(ostr)
for k in p.keys():
if getattr(calib, k) != p[k]:
raise RuntimeError('\'%s\' does not match' % k)
debug('\'%s\' matched' % k)
if not array_equal(o, calib.objp):
raise RuntimeError('objp does not match')
finally:
calib.removepath()
# ============================================================================
# Calibration Image Recording/Loading Methods
# ============================================================================
@test_report
def test_load_calib_imgs_asserts():
"""
Test load_calibs_imgs asserts.
Assert testing
Tests elements that are supposed to pass b/c as long as
`init_chessboard` hasn't been called, a Runtime (not Assertion)
error will be raised.
"""
calib = CalibratePSEye() # do NOT init_chessboard yet
for t in (int, float, complex, list, tuple, range, dict, set,
frozenset, bool, bytes, bytearray, memoryview):
try:
calib.load_calib_imgs(t)
except TypeError:
debug('\'%s\' calibrations' % t.__name__)
else:
raise RuntimeError('Failed to catch %s calibrations' % t.__name__)
try:
calib.load_calib_imgs('asdf', 1.5)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch bad clean param')
# check for elements that do pass
# this works b/c it fails another test w/RuntimeError (not Assertion)
# in the next line
for elem in (True, False, 0, 1):
try:
calib.load_calib_imgs('asdf', elem)
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch bad img_path')
@test_report
def test_load_calib_imgs_paths():
"""
Test load_calib_imgs path creation/checking.
All `calib_imgs_paths` tests are basically the same
"""
global p, o
if not os_exists(testdir+'/raw'):
raise RuntimeError('test \'raw\' directory could not be found')
# Setup
calib = CalibratePSEye()
calib.init_chessboard(p, o)
try:
calib.load_calib_imgs(testdir+'/raw')
# Make sure everything was created properly
for p in ('/raw', '/corners'):
if not isdir(calib.calibpath + p):
raise RuntimeError('path \'%s\' wasn\'t created')
# Make sure raw images were copied correctly
for fn in listdir(testdir + '/raw'):
f1 = calib.calibpath + '/raw/' + fn
f2 = testdir + '/raw/' + fn
g1 = cv_imread(f1)
i2 = cv_imread(f2)
g2 = cvtColor(cvtColor(i2, COLOR_RGB2GRAY), COLOR_GRAY2RGB)
if not array_equal(g1, g2):
raise RuntimeError('frame \'%s\' did not match' % fn)
debug('\'%s\' matched' % fn)
finally:
calib.removepath()
@test_report
def test_record_calib_imgs_asserts():
"""
Test record_calibs_imgs asserts. Basically just for initialization
"""
calib = CalibratePSEye()
# calib.calibpath is None
try:
calib.record_calib_imgs()
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch absence of _calib_path')
# calib.img_arr is not None
calib.img_arr = (1,2,3,4)
try:
calib.record_calib_imgs()
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch not-None img_arr')
calib.clear()
for t in (str, float, complex, list, tuple, range, dict, set,
frozenset, bool, bytes, bytearray, memoryview):
try:
calib.record_calib_imgs(nframes=t, countdown=15)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch %s nframes' % t.__name__)
calib.clear()
for t in (str, float, complex, list, tuple, range, dict, set,
frozenset, bool, bytes, bytearray, memoryview):
try:
calib.record_calib_imgs(nframes=15, countdown=t)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch %s countdown' % t.__name__)
@test_report
def test_record_calib_imgs_member_data():
"""
Test member data assignment inside of `record_calib_imgs`
`record_calib_imgs` is called with a negative countdown to force
immediate chessboard logging. The test calibration images provided
should all have valid chessboards for the provided params.
"""
# Setup
h, w, _ = cv_imread(testdir+'/raw/f00001.jpg').shape
nf = len([
f for f in listdir(testdir+'/raw') if f[-4:].lower() == '.jpg'
])
# Tests
calib = CalibratePSEye()
calib.init_chessboard(p, o)
try:
calib.record_calib_imgs(
cam=testdir+'/raw/f%05d.jpg', nframes=nf, countdown=-1
)
if calib.w != w:
raise RuntimeError('\'w\' wasn\'t set properly')
if calib.h != h:
raise RuntimeError('\'h\' wasn\'t set properly')
if calib.img_arr.shape != (h,w,1,nf):
raise RuntimeError('\'img_arr.shape\' wasn\'t set properly')
if calib.img_arr.dtype != uint8:
raise RuntimeError('\'img_arr.dtype\' wasn\'t set properly')
finally:
calib.removepath()
@test_report
def test_record_calib_imgs_paths():
"""
Test record_calib_imgs path creation/checking.
`record_calib_imgs` is called with a negative countdown to force
immediate chessboard logging. The test calibration images provided
should all have valid chessboards for the provided params.
"""
global p, o
nf = len([
f for f in listdir(testdir+'/raw') if f[-4:].lower() == '.jpg'
])
calib = CalibratePSEye()
calib.init_chessboard(p, o)
try:
calib.record_calib_imgs(
cam=testdir+'/raw/f%05d.jpg', nframes=nf, countdown=-1
)
# Make sure everything was created properly
for p in ('/raw', '/corners'):
if not isdir(calib.calibpath + p):
raise RuntimeError('\'%s\' wasn\'t created')
# Make sure raw images were handled correctly
# never actually checks for frame equality - keep running into
# jpgs quality loss issues that honestly weren't worth the time
# I spent trying to fix them
fns1 = sorted(listdir(testdir + '/raw'))
fns2 = sorted(listdir(calib.calibpath + '/raw'))
if len(fns1) != len(fns2):
raise RuntimeError('Failed to save all valid calibration images')
finally:
calib.removepath()
# ============================================================================
# Calibration Methods
# ============================================================================
@test_report
def test_load_calibrations_asserts():
"""
Test load calibrations assertions. This is separate from loading with
str or dict input to avoid duplicate code and unclear asymmetry
"""
calib = CalibratePSEye()
for t in (int, float, complex, list, tuple, range, dict, set,
frozenset, bool, bytes, bytearray, memoryview):
try:
calib.load_calibrations(t)
except TypeError:
debug('Caught \'%s\' calibrations' % t.__name__)
else:
raise RuntimeError('Failed to catch %s calibrations' % t.__name__)
@test_report
def test_load_calibrations_csv():
"""
Test load_calibrations parsing CSV.
WARNING
Call `test_load_calibrations_str` before
`test_load_calibrations_dict` and before
`test_compute_calibrations`so that the dict can be loaded
rather than manually inputted.
CSV FORMAT
- delimiter: ', '
- no floats, round and convert to int using known precision
- strings in double quotes
e.g. "varname", var[0], var[1], ...\\n
"""
testcalibpath = calibsdir + '/camera_params.csv'
if not os_exists(testcalibpath):
raise RuntimeError('Can\'t find \'%s\'' % testcalibpath)
global cpdict # global for use in other functions
cpdict = {}
# Load params from test
with open(testcalibpath, 'r') as f:
lines = f.read().splitlines()
entries = [line.split(', ') for line in lines]
for c in entries:
name = c[0].replace('\"', '')
if name not in ('cameraMatrix', 'distCoeffs', 'w', 'h'):
warning(
'variable name: \'%s\' not valid calib name' % name
)
continue
if name in ('w', 'h'):
cpdict[name] = int(c[1])
continue
shape = [int(v) for v in c[1].replace('\"', '').split('x')]
data = asarray([int(v) for v in c[2:]]) / 10**4
if name == 'cameraMatrix':
cpdict[name] = reshape(data, shape).astype('float64')
elif name == 'distCoeffs':
cpdict[name] = reshape(data, shape).astype('float64')
else:
raise RuntimeError('Unreachable state!?')
# Test calib loading
calib = CalibratePSEye()
calib.load_calibrations(calibsdir + '/camera_params.csv')
for k in cpdict.keys():
if not array_equal(getattr(calib, k), cpdict[k]):
raise RuntimeError('\'%s\' did not match' % k)
debug('\'%s\' matched' % k)
@test_report
def test_load_calibrations_dict():
"""
Test the loading of a dict of calibration matrices
CSV FORMAT
- delimiter: ', '
- no floats, round and convert to int using known precision
- strings in double quotes
e.g. "varname", var[0], var[1], ...\\n
"""
# raise NotImplementedError
global cpdict
calib = CalibratePSEye()
calib.load_calibrations(cpdict)
for k in cpdict.keys():
if not array_equal(getattr(calib, k), cpdict[k]):
raise RuntimeError('\'%s\' did not match' % k)
debug('\'%s\' matched' % k)
@test_report
def test_save_calibrations():
"""
Test save_calibrations.
Loading calibs was already tested above, so can rely on it.
CSV FORMAT
- delimiter: ', '
- no floats, round and convert to int using known precision
- strings in double quotes
e.g. "varname", var[0], var[1], ...\\n
"""
fn_cp = calibsdir + '/camera_params.csv'
if not os_exists(fn_cp):
raise RuntimeError('tests/camera_params.csv could not be found')
# set not to tests/ (`load_calibrations` will do that)
# don't want to overwrite test data
calib1 = CalibratePSEye()
calib1.init_chessboard(p, o)
try:
# Load known calibs and then save
cp = calib1.calibpath
debug('%s' % calib1.calibpath)
calib1.load_calibrations(fn_cp)
calib1.calibpath = cp
calib1.save_calibrations()
if not os_exists(calib1.calibpath):
raise RuntimeError('Failed to create calib path \'%s\''
% calib1.calib_path)
# Compare saving
with open(fn_cp, 'r') as f:
f1 = f.read()
with open(cp+'/camera_params.csv', 'r') as f:
f2 = f.read()
if f1 != f2:
raise RuntimeError('Camera parameter csvs did not match')
# Compare loading
calib2 = CalibratePSEye()
calib2.load_calibrations(calib1.calibpath+'/camera_params.csv')
paramlist = ('cameraMatrix', 'distCoeffs')
for k in paramlist:
k1 = getattr(calib1, k)
k2 = getattr(calib2, k)
if not array_equal(k1, k2):
raise RuntimeError(
'param \'%s\' does not match between calib1 and calib2' % k
)
debug('\'%s\' matched' % k)
finally:
calib1.removepath()
@test_report
def test_compute_calibrations():
"""
Test computation to the 4th decimal place on known calibration data.
"""
# Test Asserts
calib = CalibratePSEye()
calib.corners_arr = [1, 2]
calib.objpoints = [1, 2]
calib.w = 320
calib.h = 240
calib.corners_arr = []
try:
calib.compute_calibrations()
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch len(corners_arr)==0')
calib.corners_arr = [1, 2]
calib.objpoints = []
try:
calib.compute_calibrations()
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch len(objpoints)==0')
calib.objpoints = [1, 2]
calib.w = None
try:
calib.compute_calibrations()
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch _w is None')
calib.h = None
calib.w = 320
try:
calib.compute_calibrations()
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch h is None')
# Test Math
global cpdict
imgpath = testdir + '/raw'
if not os_exists(imgpath):
raise RuntimeError('Could not find imgpath')
calib = CalibratePSEye()
calib.init_chessboard(p, o)
calib.load_calib_imgs(imgpath)
try:
calib.compute_calibrations()
calib.save_calibrations()
for k in cpdict.keys():
k1 = cpdict[k] # already rounded b/c loaded from file
k2 = around(getattr(calib, k), decimals=4)
if not array_equal(k1, k2):
raise RuntimeError('\'%s\' did not match' % k)
debug('\'%s\' matched' % k)
# print(getattr(calib, k))
finally:
calib.removepath()
# ============================================================================
# Correction Methods
# ============================================================================
@test_report
def test_internal_correct():
"""
Test internal correction method.
Discovered this song while debugging this test case on 2020-06-19
https://open.spotify.com/track/5XgYWQKEqSqA5vXJmwZa6n?si=HvcZD32-T2KRspTPcb4uGQ
"""
calib = CalibratePSEye()
calib.load_calibrations(calibsdir + '/camera_params.csv')
# test datatype check
try:
for t in ('uint16', 'uint32', 'uint64', 'int16', 'int32', 'int64',
'float32', 'float64', 'object'):
frames = zeros((240, 320, 3), dtype=t)
calib.correct(frames)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch not-uint8 dtype')
# test size check
try:
frames = zeros((240,320), dtype='uint8')
calib.correct(frames)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch frames too few dimensions')
try:
frames = zeros((240,320,1,1,1), dtype='uint8')
calib.correct(frames)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch frames too many dimensions')
# setup test
frames = []
for f in listdir(testdir+'/raw'):
if f[-4:].lower() == '.jpg':
frames.append(cv_imread(testdir + '/raw/' + f))
fshape = [len(frames)] + list(frames[0].shape)
u1 = zeros(fshape, dtype='uint8')
for i in range(len(frames)):
f = frames[i].copy()
u1[i,...] = undistort(f, calib.cameraMatrix, calib.distCoeffs, None)
# test single frame
u2 = calib.correct(frames[0])
if not array_equal(u1[0,...], u2):
raise RuntimeError('Single-frame undistort/remap is incorrect')
# test several frames
u2 = calib.correct(asarray(frames, dtype='uint8'))
if not array_equal(u1, u2):
raise RuntimeError('Multi-frame ndarray undistort/remap is incorrect')
u2 = calib.correct(frames)
if not array_equal(u1, u2):
raise RuntimeError('Multi-frame list undistort/remap is incorrect')
@test_report
def test_internal_correct_and_save():
"""
Test internal correction saving method.
"""
calib = CalibratePSEye()
fn_c = calibsdir + '/camera_params.csv'
# Asserts
for t in (int, float, complex, list, tuple, range, dict, set,
frozenset, bool, bytes, bytearray, memoryview):
try:
calib.correct_and_save(t)
except TypeError:
pass
else:
raise RuntimeError('Failed to catch %s imgpath' % t.__name__)
calib.load_calibrations(fn_c)
cp = calib.calibpath
calib.calibpath = None
try:
calib.correct_and_save('file-that-does-not-exist')
except RuntimeError:
pass
else:
raise RuntimeError('Failed to catch _calib_path is None')
# Saving
calib.calibpath = cp
imgpath = testdir + '/raw'
storeddir = testdir + '/00000000-000000_undistorted'
storedcp = testdir + '/00000000-000000_camera_params.csv'
if os_exists(storeddir):
rmtree(storeddir)
if os_exists(storedcp):
os_remove(storedcp)
ud1 = calib.correct_and_save(imgpath)
try:
# Proper saving
if not os_exists(storeddir) or not os_exists(storedcp):
raise RuntimeError('Error creating corrected directories')
imgcount1 = len([f for f in listdir(imgpath) if f[-4:].lower() == '.jpg'])
imgcount2 = len([f for f in listdir(storeddir) if f[-4:].lower() == '.jpg'])
if imgcount1 != imgcount2:
raise RuntimeError('Not all images were saved')
# Correct calibration
# Check pre-save equality
imglist = [f for f in listdir(imgpath) if f[-4:].lower() == '.jpg']
rawimg = [cv_imread(imgpath + '/' + f) for f in imglist]
ud2 = calib.correct(rawimg) # will know if `correct` works
if not array_equal(ud1, ud2):
raise RuntimeError('Failed pre-save equality check')
# Check post-save equality
for i in range(len(imglist)):
fnud = storeddir + ('/_f%s' % str(i+1).zfill(5)) + '.jpg'
cv_imwrite(fnud, ud2[i,...], (IMWRITE_JPEG_QUALITY, 100))
ud1list = [cv_imread(storeddir + '/' + f) for f in imglist]
ud2list = [cv_imread(storeddir + '/_' + f) for f in imglist]
ud1reload = asarray(ud1list, dtype='uint8')
ud2reload = asarray(ud2list, dtype='uint8')
if not array_equal(ud1reload, ud2reload):
raise RuntimeError('Failed reload equality check')
finally:
os_remove(storedcp)
rmtree(storeddir)
try:
if os_exists(storedcp):
raise RuntimeError('failed to deleted cameraParams csv')
if os_exists(storeddir):
raise RuntimeError('failed to remove undistored img dir')
except AssertionError: