使用视线为光标移动运行python代码时出错

时间:2018-07-02 07:42:35

标签: python zeromq eye-tracking

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`

在运行此代码时,我收到以下错误消息:

runfile('C:/ Users / Richa Agrawal / Downloads / Compressed / Computer_Vision_A_Z_Template_Folder / Code_for_Windows / Windows for Windows / what.py',wdir ='C:/ Users / Richa Agrawal / Downloads / Compressed / Computer_Vision_A_Z_Template_Folder Windows代码”) 追溯(最近一次通话):

文件“”,第1行,在     runfile('C:/ Users / Richa Agrawal / Downloads / Compressed / Computer_Vision_A_Z_Template_Folder / Code_for_Windows / Windows的代码/what.py',wdir ='C:/ Users / Richa Agrawal / Downloads / Compressed / Computer_Vision_A_Z_Template_Folder / Code_for的Windows ')

Runfile中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第880行     execfile(文件名,命名空间)

execfile中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第102行     exec(compile(f.read(),文件名,'exec'),命名空间)

文件“ C:/用户/ Richa Agrawal /下载/压缩/ Computer_Vision_A_Z_Template_Folder / Code_for_Windows / Windows的代码/what.py”,第20行,在     socket.setsockopt(zmq.SUBSCRIBE,'')

zmq.backend.cython.socket.Socket.set中的文件“ zmq / backend / cython / socket.pyx”,第374行(zmq \ backend \ cython \ socket.c:4610)

TypeError:不允许使用unicode,请使用setsockopt_string

1 个答案:

答案 0 :(得分:0)

setsockopt需要一个int或一个bytes对象,但是您正在传递一个unicode对象。

错误消息告诉您该怎么做:使用setsockopt_string

socket.setsockopt_string(zmq.SUBSCRIBE, '')

或者,您可以将bytes对象传递给setsockopt

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

请注意b前缀。