/
visualize.py
98 lines (81 loc) · 3.05 KB
/
visualize.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
import sdl2.ext
from multiprocessing import Process, Queue
import pangolin
import OpenGL.GL as gl
import numpy as np
class Visualize(object):
def __init__(self, W, H):
sdl2.ext.init()
self.W = W
self.H = H
self.window = sdl2.ext.Window("SLAM", size=(W, H))
self.window.show()
def draw(self, img):
events = sdl2.ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
exit(0)
surf = sdl2.ext.pixels3d(self.window.get_surface())
surf[:, :, 0:3] = img.swapaxes(0, 1)
self.window.refresh()
class Mapping(object):
def __init__(self):
self.state = None
self.state = None
self.q = Queue()
self.vp = Process(target=self.viewer_thread, args=(self.q,))
self.vp.daemon = True
self.vp.start()
def viewer_thread(self, q):
self.viewer_init(1024, 768)
while True:
self.viewer_refresh(q)
# from py_pangolin:
def viewer_init(self, w, h):
pangolin.CreateWindowAndBind('Map Viewer', w, h)
gl.glEnable(gl.GL_DEPTH_TEST)
self.scam = pangolin.OpenGlRenderState(
pangolin.ProjectionMatrix(w, h, 420, 420, w // 2, h // 2, 0.2, 10000),
pangolin.ModelViewLookAt(0, -10, -8,
0, 0, 0,
0, -1, 0))
self.handler = pangolin.Handler3D(self.scam)
# Create Interactive View in window
self.dcam = pangolin.CreateDisplay()
self.dcam.SetBounds(0.0, 1.0, 0.0, 1.0, w / h)
self.dcam.SetHandler(self.handler)
# hack to avoid small Pangolin, no idea why it's *2
self.dcam.Resize(pangolin.Viewport(0, 0, w * 2, h * 2))
self.dcam.Activate()
def viewer_refresh(self, q):
while not q.empty():
self.state = q.get()
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
self.dcam.Activate(self.scam)
if self.state is not None:
if self.state[0].shape[0] >= 2:
# draw poses
gl.glColor3f(0.0, 1.0, 0.0)
pangolin.DrawCameras(self.state[0][:-1])
if self.state[0].shape[0] >= 1:
# draw current pose as yellow
gl.glColor3f(1.0, 1.0, 0.0)
pangolin.DrawCameras(self.state[0][-1:])
if self.state[1].shape[0] != 0:
# draw keypoints
gl.glPointSize(5)
gl.glColor3f(1.0, 0.0, 0.0)
pangolin.DrawPoints(self.state[1], self.state[2])
pangolin.FinishFrame()
def draw(self, mapp):
if self.q is None:
return
poses, pts, colors = [], [], []
for f in mapp.frames:
# invert pose for display only
poses.append(np.linalg.inv(f.pose))
for p in mapp.points:
pts.append(p.loc)
colors.append(p.color)
self.q.put((np.array(poses), np.array(pts), np.array(colors) / 256.0))