-
Notifications
You must be signed in to change notification settings - Fork 2
/
calibrate.py
executable file
·118 lines (90 loc) · 3.61 KB
/
calibrate.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
#!/usr/bin/env python
"""
Remove distortion of camera lens and project to the real world coordinates.
(C) 2016-2019 1024jp
"""
import io
import os
import unittest
import sys
import numpy as np
from modules import argsparser
from modules.datafile import Data
from modules.undistortion import Undistorter
from modules.projection import Projector
# constants
DEFAULT_IMAGE_SIZE = (3840, 2160)
def main(data, outfile, camerafile=None, size=DEFAULT_IMAGE_SIZE):
if camerafile:
undistorter = Undistorter.load(camerafile)
else:
undistorter = Undistorter.init(data.image_points, data.dest_points,
size)
undistorded_refpoints = undistorter.calibrate_points(data.image_points)
projector = Projector(undistorded_refpoints.tolist(),
data.dest_points)
# process data file
def processor_handler(x, y, z):
x, y = undistorter.calibrate_points([(x, y)])
return projector.project_point(x, y, z)
data.process_coordinates(processor_handler, outfile)
def undistort(data, outfile, camerafile=None, size=DEFAULT_IMAGE_SIZE):
if camerafile:
undistorter = Undistorter.load(camerafile)
else:
undistorter = Undistorter.init(data.image_points, data.dest_points,
size)
# process data file
def processor_handler(x, y, z):
return undistorter.calibrate_points([(x, y)])
data.process_coordinates(processor_handler, outfile)
def project(data, outfile):
projector = Projector(data.image_points, data.dest_points)
# process data file
def processor_handler(x, y, z):
return projector.project_point(x, y, z)
data.process_coordinates(processor_handler, outfile)
class TestCase(unittest.TestCase):
dirname = 'test'
def test_projection(self):
result_filename = 'result_projection.tsv'
test_dir = os.path.join(os.path.dirname(__file__), self.dirname)
filepath = os.path.join(test_dir, 'tracklog.tsv')
resultpath = os.path.join(test_dir, result_filename)
out = io.StringIO()
with open(filepath, 'r') as f:
data = Data(f)
project(data, out)
result = out.getvalue()
with open(resultpath) as f:
expected_result = f.read()
for line, expected_line in zip(result.splitlines(),
expected_result.splitlines()):
self.assertEqual(line, expected_line)
def test_undistortion(self):
result_filename = 'result_undistortion.tsv'
test_dir = os.path.join(os.path.dirname(__file__), self.dirname)
filepath = os.path.join(test_dir, 'tracklog.tsv')
resultpath = os.path.join(test_dir, result_filename)
out = io.StringIO()
with open(filepath, 'r') as f:
data = Data(f)
undistort(data, out, size=(3840, 2160))
result = out.getvalue()
with open(resultpath) as f:
expected_result = f.read()
for line, expected_line in zip(result.splitlines(),
expected_result.splitlines()):
self.assertEqual(line, expected_line)
if __name__ == "__main__":
parser = argsparser.Parser()
args = parser.parse_args()
if args.test:
suite = unittest.TestLoader().loadTestsFromTestCase(TestCase)
unittest.TextTestRunner().run(suite)
sys.exit()
data = Data(args.file, loc_path=args.location, in_cols=args.in_cols,
z_col=args.z_col)
main(data, args.out, args.camera, args.size)
# undistort(data, args.out, args.size)
# project(data, args.out)