-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pupil Invisible API: Stream gaze locations
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
aiortsp | ||
opencv-python | ||
requests | ||
zeroconf |
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
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)) |