Skip to content

Instantly share code, notes, and snippets.

@sheharyarn
Last active June 27, 2022 20:23
Show Gist options
  • Save sheharyarn/9754664 to your computer and use it in GitHub Desktop.
Save sheharyarn/9754664 to your computer and use it in GitHub Desktop.
Control your Mouse using your Eye Movement

Mouse Control

This is my modification of the original script so you don't need to enable Marker Tracking or define surfaces. You simply need to start the Coordinates Streaming Server in Pupil and run this independent script.

Note: Not using Surfaces and Marker Tracking decreases the accuracy of pointer movement. This won't work well enough because norm_gaze data is being used instead of your surface gaze data.

You need PyMouse and Xlib installed:

$ sudo pip install pymouse
$ sudo apt-get install python-xlib
import zmq
from pymouse import PyMouse
#mouse setup
m = PyMouse()
x_dim, y_dim = m.screen_size()
#network setup
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://127.0.0.1:5000")
#filter by messages by stating string 'STRING'. '' receives all messages
socket.setsockopt(zmq.SUBSCRIBE, '')
smooth_x, smooth_y= 0.5, 0.5
while True:
msg = socket.recv()
items = msg.split("\n")
msg_type = items.pop(0)
items = dict([i.split(':') for i in items[:-1] ])
if msg_type == 'Pupil':
try:
my_gaze = items['norm_gaze']
if my_gaze != "None":
raw_x,raw_y = map(float,my_gaze[1:-1].split(','))
# smoothing out the gaze so the mouse has smoother movement
smooth_x += 0.5 * (raw_x-smooth_x)
smooth_y += 0.5 * (raw_y-smooth_y)
x = smooth_x
y = smooth_y
y = 1-y # inverting y so it shows up correctly on screen
x *= x_dim
y *= y_dim
# PyMouse or MacOS bugfix - can not go to extreme corners because of hot corners?
x = min(x_dim-10, max(10,x))
y = min(y_dim-10, max(10,y))
m.move(x,y)
except KeyError:
pass
else:
# process non gaze position events from plugins here
pass
@ujwalakoriraj
Copy link

Tried, but getting the following error. Please help.

"
File "c:\Users\drkbr\Anaconda3\envs\myenv\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/drkbr/Desktop/Python/eye_controlled_mouse.py", line 2, in
from pymouse import PyMouse

File "c:\Users\drkbr\Anaconda3\envs\myenv\lib\site-packages\pymouse_init_.py", line 92, in
from windows import PyMouse, PyMouseEvent

ModuleNotFoundError: No module named 'windows' "

@arvindvinay
Copy link

am getting the following error!

C:\Users\system\Desktop>1.py
Traceback (most recent call last):

File "C:\Users\system\Desktop\1.py", line 2, in
from pymouse import PyMouse

File "C:\Python38\lib\site-packages\pymouse_init_.py", line 92, in
from windows import PyMouse, PyMouseEvent

ModuleNotFoundError: No module named 'windows'

@Eddie2111
Copy link

how did you installed "Windows" module?

@SREERAMSTING
Copy link

same error

@YASHWINN
Copy link

YASHWINN commented Dec 19, 2021

same error i a also got import pymouse from pymouse

@darshannakum15
Copy link

hahahah stupid programmers windows error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment