Add telemetry event detection
Browse files- race_core.py +74 -0
race_core.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import csv
|
| 4 |
+
import io
|
| 5 |
+
import json
|
| 6 |
+
import math
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _num(row, *names):
|
| 11 |
+
for name in names:
|
| 12 |
+
value = row.get(name)
|
| 13 |
+
if value not in (None, ""):
|
| 14 |
+
try: return float(value)
|
| 15 |
+
except ValueError: pass
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _text(row, *names):
|
| 20 |
+
for name in names:
|
| 21 |
+
value = row.get(name)
|
| 22 |
+
if value not in (None, ""): return str(value).strip()
|
| 23 |
+
return ""
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def parse_telemetry(csv_text: str):
|
| 27 |
+
reader = csv.DictReader(io.StringIO(csv_text))
|
| 28 |
+
rows = [{str(k).strip().lower(): v for k, v in row.items()} for row in reader]
|
| 29 |
+
events: list[dict[str, Any]] = [{"time_s": 0.0, "label": "Start", "source": "timeline", "confidence": 1.0}]
|
| 30 |
+
previous = None; best_lap = math.inf; pit_start = None; close_start = None
|
| 31 |
+
for row in rows:
|
| 32 |
+
t = _num(row, "timestamp", "time_s", "time", "seconds")
|
| 33 |
+
if t is None: continue
|
| 34 |
+
pos = _num(row, "position", "pos", "race_position")
|
| 35 |
+
speed = _num(row, "speed", "speed_kph", "velocity")
|
| 36 |
+
gap = _num(row, "gap_s", "gap", "interval")
|
| 37 |
+
lap = _num(row, "lap_time_s", "lap_time", "last_lap_s")
|
| 38 |
+
pit = _text(row, "pit_lane", "pit", "pit_stop").lower() in {"1", "true", "yes", "y", "in"}
|
| 39 |
+
control = _text(row, "race_control", "flag", "control")
|
| 40 |
+
status = _text(row, "status", "event", "incident").lower()
|
| 41 |
+
if previous is not None and pos is not None and previous[1] is not None and pos != previous[1]:
|
| 42 |
+
direction = "gained" if pos < previous[1] else "lost"
|
| 43 |
+
events.append({"time_s": t, "label": f"Position change ({direction})", "source": "telemetry", "confidence": 0.98, "position": pos})
|
| 44 |
+
if pos < previous[1]: events.append({"time_s": t, "label": "Overtake candidate", "source": "telemetry", "confidence": 0.82})
|
| 45 |
+
if pit and pit_start is None: pit_start = t
|
| 46 |
+
if not pit and pit_start is not None:
|
| 47 |
+
events.append({"time_s": pit_start, "label": "Pit Stop", "source": "telemetry", "confidence": 0.95, "duration_s": round(t - pit_start, 2)})
|
| 48 |
+
pit_start = None
|
| 49 |
+
if gap is not None and gap <= 0.7 and close_start is None: close_start = t
|
| 50 |
+
if (gap is None or gap > 0.9) and close_start is not None:
|
| 51 |
+
events.append({"time_s": close_start, "label": "Close battle candidate", "source": "telemetry", "confidence": 0.78, "duration_s": round(t - close_start, 2)})
|
| 52 |
+
close_start = None
|
| 53 |
+
if lap is not None and lap > 0 and lap < best_lap:
|
| 54 |
+
best_lap = lap; events.append({"time_s": t, "label": "Fastest lap candidate", "source": "telemetry", "confidence": 0.9, "lap_time_s": lap})
|
| 55 |
+
if control or any(word in status for word in ("safety", "yellow", "red", "incident", "crash", "sc")):
|
| 56 |
+
label = control or _text(row, "status", "event", "incident") or "Race-control event"
|
| 57 |
+
events.append({"time_s": t, "label": label, "source": "telemetry", "confidence": 0.9})
|
| 58 |
+
previous = (t, pos, speed)
|
| 59 |
+
if pit_start is not None: events.append({"time_s": pit_start, "label": "Pit Stop (open interval)", "source": "telemetry", "confidence": 0.7})
|
| 60 |
+
if close_start is not None: events.append({"time_s": close_start, "label": "Close battle (open interval)", "source": "telemetry", "confidence": 0.65})
|
| 61 |
+
return sorted(events, key=lambda x: (x["time_s"], x["label"]))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def build_manifest(video_name: str, csv_text: str | None):
|
| 65 |
+
events = parse_telemetry(csv_text) if csv_text and csv_text.strip() else [{"time_s": 0.0, "label": "Start", "source": "timeline", "confidence": 1.0}]
|
| 66 |
+
return {"project": "SimamRaceHighlights", "version": "0.1.0", "video": video_name or None, "events": events, "xr_action": "Jump to Moment", "contract": {"seek_time_field": "time_s", "video": "seek(time_s)", "race_clock": "seek(time_s)", "telemetry": "seek(time_s)", "camera": "optional event camera state", "vehicles": "optional event vehicle transforms"}, "learned_video_understanding_used": False, "claim_boundary": "telemetry-derived event candidates; not validated race broadcast understanding"}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def markdown_timeline(manifest):
|
| 70 |
+
lines = ["# Race Highlights", "", "| Time | Event | Source | Confidence |", "|---:|---|---|---:|"]
|
| 71 |
+
for event in manifest["events"]:
|
| 72 |
+
t = float(event["time_s"]); stamp = f"{int(t // 3600):02d}:{int(t % 3600 // 60):02d}:{int(t % 60):02d}"
|
| 73 |
+
lines.append(f"| {stamp} | {event['label']} | {event['source']} | {event['confidence']:.2f} |")
|
| 74 |
+
return "\n".join(lines)
|