diff --git a/python-invisible/requirements.txt b/python-invisible/requirements.txt index 3febd3c..a4b432c 100644 --- a/python-invisible/requirements.txt +++ b/python-invisible/requirements.txt @@ -1,3 +1,4 @@ +aiortsp opencv-python requests zeroconf \ No newline at end of file diff --git a/python-invisible/stream_gaze_locations.py b/python-invisible/stream_gaze_locations.py new file mode 100644 index 0000000..7b80cb4 --- /dev/null +++ b/python-invisible/stream_gaze_locations.py @@ -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))