Upload session logger
Browse files- session_logger.py +64 -0
session_logger.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
session_logger.py
|
| 3 |
+
Phase 1: logs the live gaze stream to a per session file.
|
| 4 |
+
|
| 5 |
+
Writes one line of JSON per frame to:
|
| 6 |
+
sessions/live/gaze_log.jsonl
|
| 7 |
+
|
| 8 |
+
Fields per line:
|
| 9 |
+
t wall clock seconds (time.time()). Same epoch the browser uses
|
| 10 |
+
via Date.now()/1000, so gaze and DOM logs align by time.
|
| 11 |
+
sx, sy gaze in calibration screen space (SCREEN_W x SCREEN_H)
|
| 12 |
+
fx, fy gaze as a fraction of the screen (0..1). Phase 4 multiplies
|
| 13 |
+
these by the browser viewport size to land on a real element.
|
| 14 |
+
|
| 15 |
+
The file is truncated on each run, so one tracker run equals one clean session.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
import json
|
| 20 |
+
import time
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class GazeLogger:
|
| 24 |
+
def __init__(self, screen_w, screen_h, session_dir=os.path.join("sessions", "live")):
|
| 25 |
+
self.screen_w = float(screen_w)
|
| 26 |
+
self.screen_h = float(screen_h)
|
| 27 |
+
|
| 28 |
+
os.makedirs(session_dir, exist_ok=True)
|
| 29 |
+
self.session_dir = session_dir
|
| 30 |
+
self.gaze_path = os.path.join(session_dir, "gaze_log.jsonl")
|
| 31 |
+
|
| 32 |
+
# "w" truncates: each run starts a fresh gaze log
|
| 33 |
+
self._f = open(self.gaze_path, "w", buffering=1)
|
| 34 |
+
|
| 35 |
+
meta = {
|
| 36 |
+
"type": "meta",
|
| 37 |
+
"screen_w": screen_w,
|
| 38 |
+
"screen_h": screen_h,
|
| 39 |
+
"t": round(time.time(), 4),
|
| 40 |
+
}
|
| 41 |
+
self._f.write(json.dumps(meta) + "\n")
|
| 42 |
+
print(f"[GazeLogger] writing gaze stream to {self.gaze_path}")
|
| 43 |
+
|
| 44 |
+
def log(self, sx, sy, t=None):
|
| 45 |
+
if t is None:
|
| 46 |
+
t = time.time()
|
| 47 |
+
fx = sx / self.screen_w if self.screen_w else 0.0
|
| 48 |
+
fy = sy / self.screen_h if self.screen_h else 0.0
|
| 49 |
+
rec = {
|
| 50 |
+
"type": "gaze",
|
| 51 |
+
"t": round(t, 4),
|
| 52 |
+
"sx": round(float(sx), 2),
|
| 53 |
+
"sy": round(float(sy), 2),
|
| 54 |
+
"fx": round(float(fx), 5),
|
| 55 |
+
"fy": round(float(fy), 5),
|
| 56 |
+
}
|
| 57 |
+
self._f.write(json.dumps(rec) + "\n")
|
| 58 |
+
|
| 59 |
+
def close(self):
|
| 60 |
+
try:
|
| 61 |
+
self._f.close()
|
| 62 |
+
print(f"[GazeLogger] gaze log closed: {self.gaze_path}")
|
| 63 |
+
except Exception:
|
| 64 |
+
pass
|