-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.py
124 lines (94 loc) · 3.34 KB
/
display.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
import pygame
from pygame.locals import DOUBLEBUF
class Display(object):
def __init__(self, W, H):
pygame.init()
self.screen = pygame.display.set_mode((W, H), DOUBLEBUF)
self.surface = pygame.Surface(self.screen.get_size()).convert()
def paint(self, img):
for event in pygame.event.get():
pass
pygame.surfarray.blit_array(self.surface, img.swapaxes(0,1)[:, :, [0, 1, 2]])
self.screen.blit(self.surface, (0, 0))
pygame.display.flip()
import pygame
from pygame.locals import DOUBLEBUF
class Display2D(object):
def __init__(self, W, H):
pygame.init()
self.screen = pygame.display.set_mode((W, H), DOUBLEBUF)
self.surface = pygame.Surface(self.screen.get_size()).convert()
def paint(self, img):
# junk
for event in pygame.event.get():
pass
# draw
#pygame.surfarray.blit_array(self.surface, img.swapaxes(0,1)[:, :, [2,1,0]])
# RGB, not BGR (might have to switch in twitchslam)
pygame.surfarray.blit_array(self.surface, img.swapaxes(0,1)[:, :, [0,1,2]])
self.screen.blit(self.surface, (0,0))
# blit
pygame.display.flip()
from multiprocessing import Process, Queue
import pangolin
import OpenGL.GL as gl
import numpy as np
class Display3D(object):
def __init__(self):
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 1:
self.viewer_refresh(q)
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 paint(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.pt)
colors.append(p.color)
self.q.put((np.array(poses), np.array(pts), np.array(colors)/256.0))