Skip to content

Instantly share code, notes, and snippets.

@willpatera
Created December 22, 2016 06:13
Show Gist options
  • Save willpatera/34fb0a7e82ea73e178569bbfc4a08158 to your computer and use it in GitHub Desktop.
Save willpatera/34fb0a7e82ea73e178569bbfc4a08158 to your computer and use it in GitHub Desktop.
Scan Path Without Flow
'''
(*)~----------------------------------------------------------------------------------
Pupil - eye tracking platform
Copyright (C) 2012-2016 Pupil Labs
Distributed under the terms of the GNU Lesser General Public License (LGPL v3.0).
License details are in the file license.txt, distributed as part of this software.
----------------------------------------------------------------------------------~(*)
'''
from plugin import Plugin
from pyglui import ui
class Scan_Path_No_Flow(Plugin):
"""docstring
using this plugin will extend the recent_gaze_positions by x extra dots from previous frames.
lock recent gaze points onto pixels.
"""
def __init__(self, g_pool,timeframe=.5):
super(Scan_Path_No_Flow, self).__init__(g_pool)
#let the plugin work after most other plugins.
self.order = .6
self.menu = None
#user settings
self.timeframe = timeframe
self.prev_frame_idx = -1
self.past_gaze_positions = []
def update(self,frame,events):
updated_past_gaze = []
succeeding_frame = frame.index-self.prev_frame_idx == 1
same_frame = frame.index == self.prev_frame_idx
if same_frame:
# paused
# re-use last result
events['gaze_positions'][:] = self.past_gaze_positions[:]
else:
# trim gaze that is too old
if events['gaze_positions']:
now = events['gaze_positions'][0]['timestamp']
cutoff = now-self.timeframe
updated_past_gaze = [g for g in self.past_gaze_positions if g['timestamp']>cutoff]
#inject the scan path gaze points into recent_gaze_positions
events['gaze_positions'][:] = updated_past_gaze + events['gaze_positions']
events['gaze_positions'].sort(key=lambda x: x['timestamp']) #this may be redundant...
self.past_gaze_positions = events['gaze_positions']
def init_gui(self):
# initialize the menu
self.menu = ui.Scrolling_Menu('Scan Path No Flow')
self.g_pool.gui.append(self.menu)
self.menu.append(ui.Button('Close',self.unset_alive))
# max is set to 100000 seconds which is about 27 hours
self.menu.append(ui.Slider('timeframe',self,min=0,step=0.5,max=100000,label="duration in sec"))
def deinit_gui(self):
if self.menu:
self.g_pool.gui.remove(self.menu)
self.menu = None
def unset_alive(self):
self.alive = False
def get_init_dict(self):
return {'timeframe':self.timeframe}
def cleanup(self):
""" called when the plugin gets terminated.
This happens either voluntarily or forced.
if you have a GUI or glfw window destroy it here.
"""
self.deinit_gui()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment