Created
October 24, 2017 13:51
-
-
Save papr/5840cdee64a4d20d5139b2dfce77dcd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Receive world camera data from Pupil using ZMQ. | |
Make sure the frame publisher plugin is loaded and confugured to gray or rgb | |
""" | |
import zmq | |
from msgpack import unpackb, packb | |
import numpy as np | |
import cv2 | |
context = zmq.Context() | |
# open a req port to talk to pupil | |
addr = '127.0.0.1' # remote ip or localhost | |
req_port = "50020" # same as in the pupil remote gui | |
req = context.socket(zmq.REQ) | |
req.connect("tcp://{}:{}".format(addr, req_port)) | |
# ask for the sub port | |
req.send_string('SUB_PORT') | |
sub_port = req.recv_string() | |
# send notification: | |
def notify(notification): | |
"""Sends ``notification`` to Pupil Remote""" | |
topic = 'notify.' + notification['subject'] | |
payload = packb(notification, use_bin_type=True) | |
req.send_string(topic, flags=zmq.SNDMORE) | |
req.send(payload) | |
return req.recv_string() | |
# Start frame publisher with format BGR | |
notify({'subject': 'start_plugin', 'name': 'Frame_Publisher', 'args': {'format': 'bgr'}}) | |
# open a sub port to listen to pupil | |
sub = context.socket(zmq.SUB) | |
sub.connect("tcp://{}:{}".format(addr, sub_port)) | |
# set subscriptions to topics | |
# recv just pupil/gaze/notifications | |
sub.setsockopt_string(zmq.SUBSCRIBE, 'frame.') | |
def recv_from_sub(): | |
'''Recv a message with topic, payload. | |
Topic is a utf-8 encoded string. Returned as unicode object. | |
Payload is a msgpack serialized dict. Returned as a python dict. | |
Any addional message frames will be added as a list | |
in the payload dict with key: '__raw_data__' . | |
''' | |
topic = sub.recv_string() | |
payload = unpackb(sub.recv(), encoding='utf-8') | |
extra_frames = [] | |
while sub.get(zmq.RCVMORE): | |
extra_frames.append(sub.recv()) | |
if extra_frames: | |
payload['__raw_data__'] = extra_frames | |
return topic, payload | |
recent_world = None | |
recent_eye0 = None | |
recent_eye1 = None | |
while True: | |
topic, msg = recv_from_sub() | |
if topic == 'frame.world': | |
recent_world = np.frombuffer(msg['__raw_data__'][0], dtype=np.uint8).reshape(msg['height'], msg['width'], 3) | |
elif topic == 'frame.eye.0': | |
recent_eye0 = np.frombuffer(msg['__raw_data__'][0], dtype=np.uint8).reshape(msg['height'], msg['width'], 3) | |
elif topic == 'frame.eye.1': | |
recent_eye1 = np.frombuffer(msg['__raw_data__'][0], dtype=np.uint8).reshape(msg['height'], msg['width'], 3) | |
if recent_world is not None and recent_eye0 is not None and recent_eye1 is not None: | |
pass # here you can do calculation on the 3 most recent world, eye0 and ey1 images |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment