Files
kinect/tools/e2e_linux_validation/run_e2e.py
T
kinect-bot 0af22f1783
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s
feat(calibration,contract,tracker): T-pose calibration + seqlock fix + articulated-depth wiring (wave 8a; todos 11,20,21 done)
2026-11-25 05:47:19 -05:00

186 lines
7.1 KiB
Python

# /// script
# requires-python = ">=3.9"
# ///
# ─── How to run ───
# python3 tools/e2e_linux_validation/run_e2e.py
# ───────────────────
"""Plan todo 10 end-to-end harness: recorded video -> live MediaPipe tracker ->
shared-memory IPC contract -> core/synth-backed OpenNI2 "SynthKinect" driver ->
an independent OpenNI2 consumer reading a sequence of moving depth frames.
Orchestrates (no pytest, no mocks - every step is the real binary/process):
1. generate_dance_video.py: a deterministic recorded video of a real
photographed human panning/zooming across frames (never a live webcam).
2. `cmake --build fallback/linux-openni2/build`: the SynthKinectDriver
(reading the live shared-memory contract via ShmFrameSource) and the
openni2_depth_consumer binary.
3. tracker/.venv's `python -m tracker.tracker` against the recorded video,
publishing real MediaPipe pose+mask frames into the frozen IPC contract.
4. openni2_depth_consumer, which dlopens the driver through OpenNI2 and
reports per-frame body-pixel centroid + in-band depth stats.
5. depth_stats_consumer, a second independent OpenNI2 consumer reporting
per-frame in-band depth min/max/mean (plan todo 21 evidence).
Exits non-zero if the depth consumer does not observe >=10 in-band, moving
depth frames, or if depth_stats_consumer never observes a per-frame spread of
at least MIN_ARTICULATED_SPREAD_MM (depth is pegged flat, not articulated).
"""
from __future__ import annotations
import os
import re
import shutil
import subprocess
import sys
import time
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SHM_NAME = "virtual_kinect_frames"
SHM_PATH = f"/dev/shm/{SHM_NAME}"
VIDEO_PATH = "/tmp/kinect_e2e/dance.avi"
DRIVER_BUILD_DIR = os.path.join(ROOT, "fallback", "linux-openni2", "build")
STATS_BUILD_DIR = os.path.join(ROOT, "tools", "e2e_linux_validation", "build")
TRACKER_VENV_PYTHON = os.path.join(ROOT, "tracker", ".venv", "bin", "python")
GL_LIBS_DIR = os.path.expanduser("~/.cache/kinect/gllibs")
OPENNI2_LIB_DIR = os.path.expanduser("~/.local/openni2-root/usr/lib/x86_64-linux-gnu")
MIN_ARTICULATED_SPREAD_MM = 100
DEPTHSTATS_LINE = re.compile(
r"depthstats\[\d+\] count=(\d+) min_mm=(\d+) max_mm=(\d+) mean_mm=([\d.]+) spread_mm=(\d+)"
)
def run(cmd: list[str], **kwargs) -> subprocess.CompletedProcess:
print(f"$ {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True, **kwargs)
print(result.stdout)
if result.stderr:
print(result.stderr, file=sys.stderr)
return result
def generate_video() -> None:
result = run([
TRACKER_VENV_PYTHON,
os.path.join(ROOT, "tools", "e2e_linux_validation", "generate_dance_video.py"),
"--out", VIDEO_PATH,
"--frames", "300",
])
if result.returncode != 0:
raise RuntimeError("generate_dance_video.py failed")
def build_driver() -> None:
configure = run([
"cmake", "-S", os.path.join(ROOT, "fallback", "linux-openni2"), "-B", DRIVER_BUILD_DIR,
])
if configure.returncode != 0:
raise RuntimeError("cmake configure failed")
build = run(["cmake", "--build", DRIVER_BUILD_DIR])
if build.returncode != 0:
raise RuntimeError("cmake build failed")
def build_stats_consumer() -> None:
configure = run([
"cmake", "-S", os.path.join(ROOT, "tools", "e2e_linux_validation"), "-B", STATS_BUILD_DIR,
])
if configure.returncode != 0:
raise RuntimeError("depth_stats_consumer cmake configure failed")
build = run(["cmake", "--build", STATS_BUILD_DIR])
if build.returncode != 0:
raise RuntimeError("depth_stats_consumer cmake build failed")
def assert_depth_is_articulated(stats_stdout: str) -> None:
"""Fails loudly unless a real per-frame depth min/max/mean spread was
observed (plan todo 21: an articulated body surface, not the flat
800mm-floor placeholder it replaces)."""
matches = DEPTHSTATS_LINE.findall(stats_stdout)
if not matches:
raise RuntimeError("depth_stats_consumer produced no depthstats lines")
best_spread = max(int(spread) for _count, _min, _max, _mean, spread in matches)
if best_spread < MIN_ARTICULATED_SPREAD_MM:
raise RuntimeError(
f"depth is not articulated: max observed spread {best_spread}mm "
f"< required {MIN_ARTICULATED_SPREAD_MM}mm (min==max would mean a flat body)"
)
print(
f"ARTICULATED DEPTH CONFIRMED: max per-frame spread {best_spread}mm "
f"across {len(matches)} frames"
)
def wait_for_shm(timeout_s: float) -> bool:
deadline = time.monotonic() + timeout_s
while time.monotonic() < deadline:
if os.path.exists(SHM_PATH):
return True
time.sleep(0.1)
return False
def main() -> int:
if os.path.exists(SHM_PATH):
os.remove(SHM_PATH)
generate_video()
build_driver()
build_stats_consumer()
tracker_env = dict(os.environ)
tracker_env["LD_LIBRARY_PATH"] = GL_LIBS_DIR + os.pathsep + tracker_env.get("LD_LIBRARY_PATH", "")
tracker_cmd = [
TRACKER_VENV_PYTHON, "-m", "tracker.tracker",
"--source", VIDEO_PATH,
"--model", "full",
"--target-fps", "15",
"--depth-size", "320x240",
"--color-size", "320x240",
"--shm-name", SHM_NAME,
"--max-seconds", "14",
]
print(f"$ {' '.join(tracker_cmd)} (background)")
tracker_proc = subprocess.Popen(
tracker_cmd, cwd=ROOT, env=tracker_env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
)
try:
if not wait_for_shm(timeout_s=15.0):
raise RuntimeError(f"{SHM_PATH} never appeared - tracker failed to start publishing")
time.sleep(2.0) # bounded grace so several real frames are already published
consumer_path = os.path.join(DRIVER_BUILD_DIR, "openni2_depth_consumer")
consumer_env = dict(os.environ)
consumer_env["LD_LIBRARY_PATH"] = OPENNI2_LIB_DIR + os.pathsep + consumer_env.get("LD_LIBRARY_PATH", "")
consumer = run([consumer_path, "12"], env=consumer_env, timeout=30)
stats_path = os.path.join(STATS_BUILD_DIR, "depth_stats_consumer")
stats_result = run([stats_path, "12"], env=consumer_env, timeout=30)
if stats_result.returncode != 0:
raise RuntimeError(f"depth_stats_consumer exit {stats_result.returncode}")
assert_depth_is_articulated(stats_result.stdout)
finally:
if tracker_proc.poll() is None:
tracker_proc.terminate()
try:
tracker_stdout, _ = tracker_proc.communicate(timeout=20)
except subprocess.TimeoutExpired:
tracker_proc.kill()
tracker_stdout, _ = tracker_proc.communicate()
print("--- tracker stdout ---")
print(tracker_stdout)
if os.path.exists(SHM_PATH):
os.remove(SHM_PATH)
if consumer.returncode != 0:
print(f"VERDICT: FAIL (consumer exit {consumer.returncode})")
return 1
print("VERDICT: PASS")
return 0
if __name__ == "__main__":
raise SystemExit(main())