Files
kinect/tools/record-replay/vkrr/harness.py
T
kinect-bot 4a66699d82
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s
feat(mapping,tracker,tools): NUI mapping+smoothing, MediaPipe tracker, record/replay harness, mock NUI host (wave 3)
2026-11-25 04:21:18 -05:00

134 lines
4.8 KiB
Python

"""Record and replay Kinect frames deterministically on a fake clock (plan todo 16).
The Recorder captures frames published to the shared-memory frame contract
(newest-wins: only strictly-advancing publish sequences are kept, mirroring the
triple buffer dropping intermediate frames a consumer never read). The Replayer
feeds recorded frames back - into a consumer callback or a ``ContractFrameBuffer``
- pacing the replay with a ``FakeClock`` advanced to each frame's recorded
timestamp, so a replayed session reproduces the exact recorded sequence with no
wall-clock sleeps.
"""
from __future__ import annotations
from typing import Callable, List, Optional, Sequence
from .buffer import ContractFrameBuffer, copy_frame
from .clock import FakeClock
from .fixture import RecordedFrame, read_fixture, write_fixture
from tracker.contract import (
JOINT_COUNT,
ColorBufferDescriptor,
DepthBufferDescriptor,
FrameData,
JointData,
JointTrackingState,
SkeletonFrame,
Vector4,
)
FrameSink = Callable[[int, FrameData], None]
class Recorder:
"""Captures a newest-wins stream of published frames into a fixture."""
def __init__(self) -> None:
self._frames: List[RecordedFrame] = []
self._last_sequence = -1
def capture(self, sequence: int, frame: FrameData) -> bool:
"""Record ``frame`` if ``sequence`` advances; returns whether it was kept."""
if sequence <= self._last_sequence:
return False
self._last_sequence = sequence
self._frames.append((sequence, copy_frame(frame)))
return True
def poll(self, buffer: ContractFrameBuffer) -> bool:
"""Read the newest frame from ``buffer`` and capture it (newest-wins)."""
sequence, frame = buffer.read_latest()
return self.capture(sequence, frame)
@property
def frames(self) -> List[RecordedFrame]:
return self._frames
def write(self, path: str) -> int:
"""Flush the captured frames to a fixture file; returns the record count."""
return write_fixture(path, self._frames)
class Replayer:
"""Replays recorded frames deterministically on a fake clock."""
def __init__(self, frames: Sequence[RecordedFrame], clock: Optional[FakeClock] = None) -> None:
self._frames = list(frames)
self._clock = clock if clock is not None else FakeClock()
@classmethod
def from_fixture(cls, path: str, clock: Optional[FakeClock] = None) -> "Replayer":
_header, frames = read_fixture(path)
return cls(frames, clock)
@property
def clock(self) -> FakeClock:
return self._clock
def replay(self, sink: FrameSink) -> List[RecordedFrame]:
"""Drive ``sink(sequence, frame)`` for every frame, pacing on the fake clock.
The clock is advanced to each frame's recorded timestamp (never via a
real sleep); it starts at the first frame's timestamp so recorded
timestamps need only be monotonic, not zero-based.
"""
replayed: List[RecordedFrame] = []
for index, (sequence, frame) in enumerate(self._frames):
timestamp = int(frame.timestamp_micros)
if index == 0:
self._clock.set(max(timestamp, self._clock.now_micros()))
else:
self._clock.set(timestamp)
emitted = copy_frame(frame)
sink(sequence, emitted)
replayed.append((sequence, emitted))
return replayed
def replay_into(self, buffer: ContractFrameBuffer) -> List[RecordedFrame]:
"""Replay every recorded frame back into a ``ContractFrameBuffer``."""
return self.replay(lambda _sequence, frame: buffer.publish(frame))
def build_synthetic_frame(frame_number: int, timestamp_micros: int, seed: int = 0) -> FrameData:
"""Construct a deterministic synthetic ``FrameData`` for tests and demos."""
skeleton = SkeletonFrame()
for joint_index in range(JOINT_COUNT):
base = float(seed + frame_number + joint_index)
skeleton.joints[joint_index] = JointData(
position=Vector4(
x=base * 0.011,
y=base * 0.022,
z=1.5 + (joint_index % 5) * 0.1,
w=1.0,
),
tracking_state=(
JointTrackingState.TRACKED
if joint_index % 4
else JointTrackingState.INFERRED
),
)
skeleton.skeleton_tracking_id = 1 + (frame_number % 6)
skeleton.quality_flags = frame_number & 0x3
depth = DepthBufferDescriptor(width=640, height=480, pitch=640 * 2, byte_offset=0)
color = ColorBufferDescriptor(
width=640, height=480, pitch=640 * 4, byte_offset=640 * 480 * 2
)
return FrameData(
frame_number=frame_number,
timestamp_micros=timestamp_micros,
skeleton=skeleton,
depth=depth,
color=color,
)