-
Notifications
You must be signed in to change notification settings - Fork 56
/
recv_world_video_frames.py
67 lines (54 loc) · 1.97 KB
/
recv_world_video_frames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
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
while True:
topic, msg = recv_from_sub()
if topic == 'frame.world':
img = np.frombuffer(msg['__raw_data__'][0], dtype=np.uint8).reshape(msg['height'], msg['width'], 3)
cv2.imshow('test', img)
cv2.waitKey(1)