forked from Azmalent/auto-calibration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
175 lines (128 loc) · 5.29 KB
/
generator.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
from getopt import getopt
from math import radians
from multiprocessing.connection import Client, Listener
from random import Random
from screeninfo import get_monitors
from utils import *
import cv2
import numpy as np
import os
import sys
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
CHECKERBOARD = cv2.imread( os.path.join(SCRIPT_DIR, 'board/checkerboard.png') )
CHECKERBOARD = cv2.cvtColor(CHECKERBOARD, cv2.COLOR_RGB2RGBA)
def init_window(monitor):
cv2.namedWindow('window', cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow('window', monitor.x, monitor.y)
cv2.setWindowProperty('window', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
class ImageGenerator():
MAX_ANGLE = 50
BACKGROUND_COLOR = (0, 255, 0, 255)
def __init__(self, monitor):
self.monitor = monitor
self.random = Random()
self.randomize_dz = True
w, h = monitor.width, monitor.height
f = np.sqrt(w**2 + h**2)
self.camera_matrix = np.array([
[f, 0, w / 2, 0],
[0, f, h / 2, 0],
[0, 0, 1, 0]
])
self.proj_3d = proj_matrix_3d(CHECKERBOARD.shape[1], CHECKERBOARD.shape[0])
def log(self, message):
print('[Generator] ' + message)
def next(self):
"""Generate the next image."""
mat = self.random_matrix()
return cv2.warpPerspective(CHECKERBOARD, mat, (self.monitor.width, self.monitor.height), borderMode=cv2.BORDER_CONSTANT, borderValue=ImageGenerator.BACKGROUND_COLOR)
# For lens calibration
# Random 3D rotation + random 2D offset
def random_matrix(self):
"""Generate random 3D rotation and random 2D offset"""
degrees = self.random.uniform(0, 50)
axis = self.random_unit_vector()
#rx = self.random_angle(ImageGenerator.MAX_ANGLE)
#ry = self.random_angle(ImageGenerator.MAX_ANGLE)
#rz = self.random_angle(180)
#rot = rotation_matrix(rx, ry, rz)
rot = rotation_matrix_from_axis(axis, np.radians(degrees))
if self.random.random() > 0.5:
rot = rot @ rotation_matrix(0, 0, np.radians(90))
dz = self.random.uniform(750, 1000) if self.randomize_dz else 750
pixels_per_mm = self.monitor.width / self.monitor.width_mm
trans = translation_matrix_3d(0, 0, dz * pixels_per_mm)
mat = self.camera_matrix @ trans @ rot @ self.proj_3d
(dx, dy) = self.random_offsets(mat)
offset_2d = translation_matrix_2d(dx, dy)
return offset_2d @ mat
def random_angle(self, max_degrees):
"""Generate random angle in range [-max_degrees, max_degrees]"""
degrees = self.random.uniform(-max_degrees, max_degrees)
return radians(degrees)
def random_unit_vector(self):
"""Generate random unit vector"""
phi = self.random.uniform(0, np.pi * 2)
costheta = self.random.uniform(-1, 1)
theta = np.arccos(costheta)
x = np.sin(theta) * np.cos(phi)
y = np.sin(theta) * np.sin(phi)
z = np.cos(theta)
return (x, y, z)
def random_offsets(self, mat):
"""Generate random offsets for the checkerboard"""
corners = corner_positions(CHECKERBOARD, mat)
(left, right, top, bottom) = bounds(corners)
dx_min = int(-left)
dx_max = int(self.monitor.width - right)
dy_min = int(-top)
dy_max = int(self.monitor.height - bottom)
dx = max([self.random.uniform(dx_min, dx_max) for _ in range(1)], key=abs)
dy = max([self.random.uniform(dy_min, dy_max) for _ in range(1)], key=abs)
return (dx, dy)
if __name__ == '__main__':
listener_port = None
client_port = None
monitor_id = 0
opts, args = getopt(sys.argv[1:], '', ['listener-port=', 'client-port=', 'monitor='])
for opt, arg in opts:
if opt == '--listener-port':
listener_port = int(arg)
elif opt == '--client-port':
client_port = int(arg)
elif opt == '--monitor':
monitor_id = int(arg)
assert listener_port is not None
assert client_port is not None
listener = Listener(('localhost', listener_port), authkey=b'password')
monitor = get_monitors()[monitor_id]
gen = ImageGenerator(monitor)
init_window(monitor)
conn = listener.accept()
camera_client = Client(('localhost', client_port), authkey=b'password')
gen.log('connected to camera driver')
try:
while True:
msg = conn.recv()
if msg is not None:
command, args = msg[0], msg[1:]
if command == 'next':
img = gen.next()
cv2.imshow('window', img)
cv2.waitKey(300)
camera_client.send('capture')
elif command == 'set_seed':
gen.random.seed(args[0])
gen.randomize_dz = False # no dz randomization when we're calibrating nodal offset!
elif command == 'exit':
gen.log('received exit message')
break
except Exception as e:
gen.log('error: ' + e.__class__.__name__)
print(e)
finally:
listener.close()
conn.close()
camera_client.send('exit')
camera_client.close()
cv2.destroyAllWindows()