-
Notifications
You must be signed in to change notification settings - Fork 45
/
viewer.py
executable file
·173 lines (127 loc) · 4.89 KB
/
viewer.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
import numpy as np
import OpenGL.GL as gl
import pangolin
import cv2
from multiprocessing import Queue, Process
class Viewer(object):
def __init__(self):
self.image_queue = Queue()
self.pose_queue = Queue()
self.view_thread = Process(target=self.view)
self.view_thread.start()
def update_pose(self, pose):
if pose is None:
return
self.pose_queue.put(pose.matrix())
def update_image(self, image):
if image is None:
return
elif image.ndim == 2:
image = np.repeat(image[..., np.newaxis], 3, axis=2)
self.image_queue.put(image)
def view(self):
pangolin.CreateWindowAndBind('Viewer', 1024, 768)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
viewpoint_x = 0
viewpoint_y = -7
viewpoint_z = -18
viewpoint_f = 1000
proj = pangolin.ProjectionMatrix(
1024, 768, viewpoint_f, viewpoint_f, 512, 389, 0.1, 300)
look_view = pangolin.ModelViewLookAt(
viewpoint_x, viewpoint_y, viewpoint_z, 0, 0, 0, 0, -1, 0)
# Camera Render Object (for view / scene browsing)
scam = pangolin.OpenGlRenderState(proj, look_view)
# Add named OpenGL viewport to window and provide 3D Handler
dcam = pangolin.CreateDisplay()
dcam.SetBounds(0.0, 1.0, 175 / 1024., 1.0, -1024 / 768.)
dcam.SetHandler(pangolin.Handler3D(scam))
# image
width, height = 376, 240
dimg = pangolin.Display('image')
dimg.SetBounds(0, height / 768., 0.0, width / 1024., 1024 / 768.)
dimg.SetLock(pangolin.Lock.LockLeft, pangolin.Lock.LockTop)
texture = pangolin.GlTexture(width, height, gl.GL_RGB, False, 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
image = np.ones((height, width, 3), 'uint8')
# axis
axis = pangolin.Renderable()
axis.Add(pangolin.Axis())
trajectory = DynamicArray()
camera = None
image = None
while not pangolin.ShouldQuit():
if not self.pose_queue.empty():
while not self.pose_queue.empty():
pose = self.pose_queue.get()
trajectory.append(pose[:3, 3])
camera = pose
if not self.image_queue.empty():
while not self.image_queue.empty():
img = self.image_queue.get()
img = img[::-1, :, ::-1]
img = cv2.resize(img, (width, height))
image = img.copy()
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glClearColor(1.0, 1.0, 1.0, 1.0)
dcam.Activate(scam)
# draw axis
axis.Render()
# draw current camera
if camera is not None:
gl.glLineWidth(1)
gl.glColor3f(0.0, 0.0, 1.0)
pangolin.DrawCameras(np.array([camera]), 0.5)
# show trajectory
if len(trajectory) > 0:
gl.glPointSize(2)
gl.glColor3f(0.0, 0.0, 0.0)
pangolin.DrawPoints(trajectory.array())
# show image
if image is not None:
texture.Upload(image, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
dimg.Activate()
gl.glColor3f(1.0, 1.0, 1.0)
texture.RenderToViewport()
pangolin.FinishFrame()
class DynamicArray(object):
def __init__(self, shape=3):
if isinstance(shape, int):
shape = (shape,)
assert isinstance(shape, tuple)
self.data = np.zeros((1000, *shape))
self.shape = shape
self.ind = 0
def clear(self):
self.ind = 0
def append(self, x):
self.extend([x])
def extend(self, xs):
if len(xs) == 0:
return
assert np.array(xs[0]).shape == self.shape
if self.ind + len(xs) >= len(self.data):
self.data.resize(
(2 * len(self.data), *self.shape) , refcheck=False)
if isinstance(xs, np.ndarray):
self.data[self.ind:self.ind+len(xs)] = xs
else:
for i, x in enumerate(xs):
self.data[self.ind+i] = x
self.ind += len(xs)
def array(self):
return self.data[:self.ind]
def __len__(self):
return self.ind
def __getitem__(self, i):
assert i < self.ind
return self.data[i]
def __iter__(self):
for x in self.data[:self.ind]:
yield x
if __name__ == '__main__':
import g2o
import time
viewer = Viewer()
viewer.update_pose(g2o.Isometry3d())