Skip to content

Commit

Permalink
Pupil Invisible API: Stream gaze locations
Browse files Browse the repository at this point in the history
  • Loading branch information
papr committed Dec 6, 2021
1 parent 1458bfe commit b5accb8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions python-invisible/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiortsp
opencv-python
requests
zeroconf
58 changes: 58 additions & 0 deletions python-invisible/stream_gaze_locations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import asyncio
import argparse

import requests
from aiortsp.rtsp.reader import RTSPReader
from dpkt import Packet


class Gaze(Packet):
__hdr__ = (
("pixel_x", "f", 0.0),
("pixel_y", "f", 0.0),
("worn", "B", 255),
)


async def receive_gaze(url):
async with RTSPReader(url) as reader:
async for pkt in reader.iter_packets():
gaze = Gaze(pkt.data)
print(repr(gaze))


def get_gaze_rtsp_url(status_response):
ip = _get_phone_ip_address(status_response)
for sensor in status_response.json()["result"]:
if (
sensor["model"] == "Sensor"
and sensor["data"]["conn_type"] == "DIRECT"
and sensor["data"]["sensor"] == "gaze"
):
protocol = sensor["data"]["protocol"]
port = sensor["data"]["port"]
params = sensor["data"]["params"]
return f"{protocol}://{ip}:{port}/?{params}"


def _get_phone_ip_address(status_response):
for sensor in status_response.json()["result"]:
if sensor["model"] == "Phone":
return sensor["data"]["ip"]


if __name__ == "__main__":
host_name = "pi.local"
port = 8080
base_url = f"http://{host_name}:{port}/api"
api_endpoint_status = "/status"
print("Getting stream information...")
try:
status_response = requests.get(base_url + api_endpoint_status)
except requests.exceptions.ConnectionError as err:
print(f"Could not connect to server {base_url} ({err})")
exit(1)
gaze_rtsp_url = get_gaze_rtsp_url(status_response)
print(f"Connecting to {gaze_rtsp_url}...")

asyncio.run(receive_gaze(gaze_rtsp_url))

0 comments on commit b5accb8

Please sign in to comment.