97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
"""Synthetic pose + frame sources for testing the data path (plan todo 5).
|
|
|
|
`FakePoseProvider` emits deterministic 33-landmark `PoseResult`s (with a filled
|
|
segmentation mask) so the landmarks -> contract packing -> shared-memory publish
|
|
path can be verified without MediaPipe. It can also emit a "no person" frame
|
|
(returns None) to exercise the low-visibility / no-body publish branch.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
|
|
from .pose import LANDMARK_COUNT, PoseProvider, PoseResult
|
|
|
|
|
|
def make_fake_pose(
|
|
frame_index: int = 0,
|
|
image_width: int = 640,
|
|
image_height: int = 480,
|
|
visibility: float = 0.9,
|
|
mask_box: tuple[float, float, float, float] = (0.3, 0.1, 0.7, 0.95),
|
|
) -> PoseResult:
|
|
"""Build one deterministic pose: landmarks on a vertical line that sways with
|
|
`frame_index`, world landmarks in a plausible metric range, and a rectangular
|
|
body mask (fractions of the image: x0, y0, x1, y1)."""
|
|
sway = 0.05 * np.sin(frame_index * 0.2)
|
|
landmarks: list[tuple[float, float, float, float]] = []
|
|
world_landmarks: list[tuple[float, float, float]] = []
|
|
vis: list[float] = []
|
|
for i in range(LANDMARK_COUNT):
|
|
t = i / (LANDMARK_COUNT - 1)
|
|
x = 0.5 + sway + 0.1 * np.cos(t * np.pi)
|
|
y = 0.1 + 0.8 * t
|
|
z = -0.2 + 0.4 * t
|
|
landmarks.append((float(x), float(y), float(z), visibility))
|
|
world_landmarks.append((float((x - 0.5) * 1.2), float((0.5 - y) * 1.6), float(1.5 + z)))
|
|
vis.append(visibility)
|
|
|
|
mask = np.zeros((image_height, image_width), dtype=np.float32)
|
|
x0, y0, x1, y1 = mask_box
|
|
mask[int(y0 * image_height):int(y1 * image_height), int(x0 * image_width):int(x1 * image_width)] = 1.0
|
|
|
|
return PoseResult(
|
|
landmarks=landmarks,
|
|
world_landmarks=world_landmarks,
|
|
visibility=vis,
|
|
image_width=image_width,
|
|
image_height=image_height,
|
|
mask=mask,
|
|
)
|
|
|
|
|
|
class FakePoseProvider(PoseProvider):
|
|
"""Deterministic provider. `detect_every` frames yield a pose; the rest yield
|
|
None (no person), so a run exercises both the body and no-body branches."""
|
|
|
|
def __init__(
|
|
self,
|
|
image_width: int = 640,
|
|
image_height: int = 480,
|
|
detect_every: int = 1,
|
|
no_person: bool = False,
|
|
) -> None:
|
|
self.image_width = image_width
|
|
self.image_height = image_height
|
|
self.detect_every = detect_every
|
|
self.no_person = no_person
|
|
self._count = 0
|
|
|
|
def process(self, frame_bgr, timestamp_ms=None):
|
|
idx = self._count
|
|
self._count += 1
|
|
if self.no_person or (self.detect_every > 1 and idx % self.detect_every != 0):
|
|
return None
|
|
return make_fake_pose(idx, self.image_width, self.image_height)
|
|
|
|
|
|
class FakeFrameSource:
|
|
"""Yields synthetic BGR frames; `n_frames=None` streams forever (for pacing
|
|
tests), otherwise signals end-of-stream after `n_frames`."""
|
|
|
|
def __init__(self, n_frames=None, width: int = 640, height: int = 480) -> None:
|
|
self.n_frames = n_frames
|
|
self.width = width
|
|
self.height = height
|
|
self._i = 0
|
|
|
|
def read(self):
|
|
if self.n_frames is not None and self._i >= self.n_frames:
|
|
return None
|
|
frame = np.full((self.height, self.width, 3), self._i % 256, dtype=np.uint8)
|
|
self._i += 1
|
|
return frame
|
|
|
|
def release(self) -> None:
|
|
pass
|