Skip to content

Instantly share code, notes, and snippets.

@yumu19
Created August 18, 2022 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumu19/4cf2f28b316bb9eea0170bcb028a5b28 to your computer and use it in GitHub Desktop.
Save yumu19/4cf2f28b316bb9eea0170bcb028a5b28 to your computer and use it in GitHub Desktop.
import zmq
ctx = zmq.Context()
# The REQ talks to Pupil remote and receives the session unique IPC SUB PORT
pupil_remote = ctx.socket(zmq.REQ)
ip = '127.0.0.1' # Pupil Serverが稼働しているマシンのIPアドレス。このスクリプトを動かすPCでPupil Serverが稼働しているのであればローカルアドレスでOK
#ip = '10.124.52.242' # If you talk to a different machine use its IP.
port = 50020 # The port defaults to 50020. Set in Pupil Capture GUI.
pupil_remote.connect(f'tcp://{ip}:{port}')
# Request 'SUB_PORT' for reading data
pupil_remote.send_string('SUB_PORT')
sub_port = pupil_remote.recv_string()
# Request 'PUB_PORT' for writing data
pupil_remote.send_string('PUB_PORT')
pub_port = pupil_remote.recv_string()
#...continued from above
# Assumes `sub_port` to be set to the current subscription port
subscriber = ctx.socket(zmq.SUB)
subscriber.connect(f'tcp://{ip}:{sub_port}')
subscriber.subscribe('pupil') # receive all gaze messages
# osc settings
from pythonosc import osc_message_builder
from pythonosc import udp_client
osc_ip = "10.124.52.227" #OSCメッセージ送信先のIPアドレス
osc_port = 8888 #OSCメッセージ送信先のポート
osc_client = udp_client.UDPClient(osc_ip, osc_port)
# we need a serializer
import msgpack
count_max = 21 #データが多いので間引く。count_max回に1回送る。
c = 0
while True:
topic, payload = subscriber.recv_multipart()
zmq_message = msgpack.loads(payload)
#print(f"{topic}: {zmq_message}")
topic_str = topic.decode('ascii')
if ('3d' in topic_str):
c = c + 1
if (c >= count_max):
id = zmq_message[b'id']
angle = zmq_message[b'ellipse'][b'angle']
print(id)
print(angle)
c = 0
if id == 0:
osc_msg = osc_message_builder.OscMessageBuilder(address="/pupil/0")
else:
osc_msg = osc_message_builder.OscMessageBuilder(address="/pupil/1")
osc_msg.add_arg(angle)
osc_msg = osc_msg.build()
osc_client.send(osc_msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment