Skip to content

Instantly share code, notes, and snippets.

@lenisko
Forked from Epictek/tobii.py
Created September 14, 2018 00:30
Show Gist options
  • Save lenisko/168e59ba1b7b1df5295b5d442b34b235 to your computer and use it in GitHub Desktop.
Save lenisko/168e59ba1b7b1df5295b5d442b34b235 to your computer and use it in GitHub Desktop.
import logging
import clr
clr.AddReference("Tobii.Interaction.Net")
clr.AddReference("Tobii.Interaction.Model")
clr.AddReference("EyeXFramework")
clr.AddReference("System")
from Tobii.Interaction import *
from Tobii.Interaction.Framework import *
from System import *
class GazeStream:
def __init__(self, screensize):
self.host = Host()
self.gazePointDataStream = self.host.Streams.CreateGazePointDataStream(GazePointDataMode.LightlyFiltered)
self.gazeTrackingStateObserver = self.host.States.CreateGazeTrackingObserver()
self.width = screensize[0]
self.height = screensize[1]
self.ix, self.iy = self.width/2, self.height/2
self.gazeTracked = False
self.enabled = True
def enable(self):
self.enabled = True
self.gazePointDataStream.Next += self.OnNextGazePoint
self.gazeTrackingStateObserver.Changed += self.OnGazeTrackingStateChanged
def read(self):
return GazeStream.iy, GazeStream.ix
def OnGazeTrackingStateChanged(self, source, state):
if state.IsValid:
if state.Value == GazeTracking.GazeTracked:
logging.debug("Gaze Tracked!")
self.gazeTracked = True
elif state.Value == GazeTracking.GazeNotTracked:
logging.debug("Gaze not Tracked! Disabling controls")
self.ix, self.iy = self.width/2, self.height/2
self.gazeTracked = False
def OnNextGazePoint(self, source, args):
self.ix = args.Data.X
self.iy = args.Data.Y
self.ui.ix = self.ix
self.ui.iy = self.iy
logging.debug("Gaze Track Cords {}: x={} y={}".format(args.Data.Timestamp, self.ix, self.iy))
def disable(self):
self.enabled = False
self.gazeTracked = False
self.gazePointDataStream.Next -= self.OnNextGazePoint
self.gazeTrackingStateObserver.Changed -= self.OnGazeTrackingStateChanged
def stop(self):
self.host.Dispose()
if __name__ == "__main__":
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
width = screensize[0]
height = screensize[1]
gaze = eyetracking.GazeStream(screensize)
gaze.enable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment