I'm trying to write a vispy program that will display an image and superimpose two circles on top; ultimately the information for both of these will come from real-time data steams.
To start with, I'm trying to write something that will just display the circles periodically. I modified the real time signal example from the Vispy gallery and this worked o.k. Since it seems that I want to use scenegraphs in order to combine this with an image, I changed the Canvas to sceneCanvas. Parts of the new class are here; I've tried to include only what I think may be relevant.
class Canvas(scene.SceneCanvas):
def __init__(self, lsl_inlet):
scene.SceneCanvas.__init__(self, keys='interactive', size=(800, 600))
ps = self.pixel_scale
self.unfreeze()
self.inlet = lsl_inlet
# Create vertices
n = 2
self.data = np.zeros(n, [('a_position', np.float32, 2),
('a_bg_color', np.float32, 4),
('a_fg_color', np.float32, 4),
('a_size', np.float32, 1)])
self.data['a_fg_color'] = 255, 0, 255, 1
self.data['a_bg_color'] = 0, 0, 255, 200
self.data['a_size'] = np.random.uniform(5*ps, 10*ps, n)
u_linewidth = 1.0
u_antialias = 1.0
self.translate = 5
self.program = gloo.Program(vert, frag)
self.view = translate((0, 0, -self.translate))
self.model = np.eye(4, dtype=np.float32)
self.projection = np.eye(4, dtype=np.float32)
self.apply_zoom()
self.program.bind(gloo.VertexBuffer(self.data))
self.program['u_linewidth'] = u_linewidth
self.program['u_antialias'] = u_antialias
self.program['u_model'] = self.model
self.program['u_view'] = self.view
self.program['u_size'] = 5 / self.translate
self.theta = 0
self.phi = 0
gloo.set_state('translucent', clear_color='white')
self.timer = app.Timer(0.5, connect=self.on_timer, start=True)
self.freeze()
self.show()
def read_from_lsl(self):
sample, time = self.inlet.pull_sample()
return np.array(sample[:2]), np.array(sample[2:4]), sample[4]
def on_timer(self, event):
x, y, diam = self.read_from_lsl()
self.data['a_position'] = np.array([x,y])
self.data['a_size'] = diam*np.ones(2)
self.program.bind(gloo.VertexBuffer(self.data))
self.program.draw()
self.update()
What I'm seeing is that the canvas will only update very irregularly and seems to freeze after a little bit (but the image might update if I move the frame). This was working o.k. when I was using an app.Canvas instead of the SceneCanvas. Does anyone see what I might be doing wrong? Is it the view? I noticed a lot of programs used something like central_widget.add_view() but when I tried that setting the 'u_view' for self.program didn't work.
Any ideas?