2
from pymouse.windows import PyMouse
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

                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`

here on running this code i am getting error as :

runfile('C:/Users/Richa Agrawal/Downloads/Compressed/Computer_Vision_A_Z_Template_Folder/Code_for_Windows/Code for Windows/what.py', wdir='C:/Users/Richa Agrawal/Downloads/Compressed/Computer_Vision_A_Z_Template_Folder/Code_for_Windows/Code for Windows') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Richa Agrawal/Downloads/Compressed/Computer_Vision_A_Z_Template_Folder/Code_for_Windows/Code for Windows/what.py', wdir='C:/Users/Richa Agrawal/Downloads/Compressed/Computer_Vision_A_Z_Template_Folder/Code_for_Windows/Code for Windows')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Richa Agrawal/Downloads/Compressed/Computer_Vision_A_Z_Template_Folder/Code_for_Windows/Code for Windows/what.py", line 20, in socket.setsockopt(zmq.SUBSCRIBE, '')

File "zmq/backend/cython/socket.pyx", line 374, in zmq.backend.cython.socket.Socket.set (zmq\backend\cython\socket.c:4610)

TypeError: unicode not allowed, use setsockopt_string

ndmeiri
  • 4,979
  • 12
  • 37
  • 45

1 Answers1

1

setsockopt expects an int or a bytes object, but you are passing a unicode object.

The error message tells you what to do: use setsockopt_string.

socket.setsockopt_string(zmq.SUBSCRIBE, '')

Alternatively, you can pass a bytes object to setsockopt:

socket.setsockopt(zmq.SUBSCRIBE, b'')

Note the b prefix.

Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • thanks sir for replying,after this correction,the code is working fine but it is not giving any output...any suggestions – Garima Singh Jul 03 '18 at 07:41
  • As far as I can tell from the code you posted, it is not supposed to give any output, but it should move the mouse if the right commands are sent to the socket. You have some other program sending data to the socket right? – Daniel Hepper Jul 03 '18 at 07:45
  • BTW, welcome to StackOverflow. If the original problem from your question has been solved, I would appreciate if you accept my answer by clicking the checkmark next to it. Comments are probably not a good venue to solve your new problem, it's better to ask a new question for that. – Daniel Hepper Jul 03 '18 at 07:48