|
| 1 | +"""In-process orchestrator for all five analytics renders. |
| 2 | +
|
| 3 | +Builds a shared :class:`~sports.common.video_tracking.VideoTrackingSession` once |
| 4 | +(one BoTSORT pass, one team-classifier fit, homography maps, kinematics) and drives |
| 5 | +each mode renderer in-process. Each mode writes its own output video. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import copy |
| 11 | +import time |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from sports.common.video_tracking import build_video_tracking_session |
| 15 | +from direction import run_direction |
| 16 | +from distance import run_distance |
| 17 | +from speed import run_speed |
| 18 | +from speed_and_distance import run_speed_and_distance |
| 19 | + |
| 20 | +_RENDER_PLAN = ( |
| 21 | + ("direction", "DIRECTION"), |
| 22 | + ("speed", "SPEED"), |
| 23 | + ("distance", "DISTANCE"), |
| 24 | + ("speed-distance-all", "SPEED_AND_DISTANCE (all players)"), |
| 25 | + ("speed-distance-single", "SPEED_AND_DISTANCE (spotlight)"), |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def _derive_target(base_path: str, suffix: str) -> str: |
| 30 | + """``data/renders/08fd33_0.mp4`` + ``direction`` → ``…/08fd33_0-direction.mp4``.""" |
| 31 | + p = Path(base_path) |
| 32 | + return str(p.with_name(f"{p.stem}-{suffix}{p.suffix or '.mp4'}")) |
| 33 | + |
| 34 | + |
| 35 | +def _mode_args(args, *, target: str, track_id: int | None = None): |
| 36 | + """Shallow copy of ``args`` with a per-mode target path and spotlight track id.""" |
| 37 | + new = copy.copy(args) |
| 38 | + new.target_video_path = target |
| 39 | + new.track_id = track_id |
| 40 | + return new |
| 41 | + |
| 42 | + |
| 43 | +def _pick_spotlight_track_id(tracks) -> int | None: |
| 44 | + """Pick the most-active tracked player (max cumulative distance) for the spotlight.""" |
| 45 | + best_tid: int | None = None |
| 46 | + best_distance = -1.0 |
| 47 | + for tid, track in tracks.items(): |
| 48 | + distance = float(getattr(track, "distance_m", 0.0) or 0.0) |
| 49 | + if distance > best_distance: |
| 50 | + best_distance = distance |
| 51 | + best_tid = int(tid) |
| 52 | + return best_tid |
| 53 | + |
| 54 | + |
| 55 | +def run_all(args) -> list[str]: |
| 56 | + """Build shared session once and render all five analytics videos in-process.""" |
| 57 | + t_start = time.time() |
| 58 | + |
| 59 | + print("── run-all: building shared VideoTrackingSession ─────────") |
| 60 | + session = build_video_tracking_session(args, need_homography=True) |
| 61 | + print(f"Shared session ready in {time.time() - t_start:.1f}s.") |
| 62 | + |
| 63 | + base = args.target_video_path |
| 64 | + explicit_spotlight = getattr(args, "track_id", None) |
| 65 | + spotlight_id = ( |
| 66 | + explicit_spotlight |
| 67 | + if explicit_spotlight is not None |
| 68 | + else _pick_spotlight_track_id(session.tracks) |
| 69 | + ) |
| 70 | + print(f"Spotlight (SPEED_AND_DISTANCE) track id: {spotlight_id}") |
| 71 | + |
| 72 | + outputs: list[str] = [] |
| 73 | + timings: list[tuple[str, float]] = [] |
| 74 | + for suffix, label in _RENDER_PLAN: |
| 75 | + target = _derive_target(base, suffix) |
| 76 | + track_id = spotlight_id if suffix == "speed-distance-single" else None |
| 77 | + t_mode = time.time() |
| 78 | + print(f"── run-all: rendering {label} → {target} ─────────") |
| 79 | + if suffix == "direction": |
| 80 | + run_direction(_mode_args(args, target=target), session) |
| 81 | + elif suffix == "speed": |
| 82 | + run_speed(_mode_args(args, target=target), session) |
| 83 | + elif suffix == "distance": |
| 84 | + run_distance(_mode_args(args, target=target), session) |
| 85 | + else: |
| 86 | + run_speed_and_distance( |
| 87 | + _mode_args(args, target=target, track_id=track_id), session, |
| 88 | + ) |
| 89 | + outputs.append(target) |
| 90 | + timings.append((label, time.time() - t_mode)) |
| 91 | + |
| 92 | + total_time = time.time() - t_start |
| 93 | + print("── run-all: done ─────────────────────────────────────────────────────") |
| 94 | + for label, dt in timings: |
| 95 | + print(f" {label:<28} {dt:6.1f}s") |
| 96 | + print(f" {'TOTAL':<28} {total_time:6.1f}s") |
| 97 | + print(f" All {len(outputs)} renders used a single shared tracking pass.") |
| 98 | + print("Outputs:") |
| 99 | + for path in outputs: |
| 100 | + print(f" {path}") |
| 101 | + return outputs |
0 commit comments