104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
# /// script
|
|
# requires-python = ">=3.9"
|
|
# dependencies = ["opencv-python>=4.9", "numpy>=1.26"]
|
|
# ///
|
|
# ─── How to run ───
|
|
# uv run tools/e2e_linux_validation/generate_dance_video.py --out /tmp/kinect_e2e/dance.avi
|
|
# ───────────────────
|
|
"""Deterministic recorded "dance-like" video generator for plan todo 10.
|
|
|
|
Downloads a real human-photo fixture from Google's public mediapipe-assets
|
|
bucket (the same source the project's own MediaPipe smoke evidence already
|
|
cites: storage.googleapis.com/mediapipe-assets/pose.jpg - a real photographed
|
|
person, Apache-2.0 licensed test asset) once, then synthesizes a fully
|
|
deterministic sequence of frames by panning/zooming/rotating that photo's
|
|
content across a canvas. A synthetic cartoon blob (tried in an earlier wave,
|
|
see .omo/evidence/task-wave3-verify.log) does not trigger MediaPipe's person
|
|
detector; a real photographed human does, while the affine motion still gives
|
|
a moving, dance-like subject without a live webcam.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import urllib.request
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
POSE_PHOTO_URL = "https://storage.googleapis.com/mediapipe-assets/pose.jpg"
|
|
|
|
|
|
def ensure_source_photo(cache_path: str) -> np.ndarray:
|
|
if not os.path.exists(cache_path):
|
|
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
|
|
urllib.request.urlretrieve(POSE_PHOTO_URL, cache_path)
|
|
image = cv2.imread(cache_path)
|
|
if image is None:
|
|
raise RuntimeError(f"could not decode source photo: {cache_path}")
|
|
return image
|
|
|
|
|
|
def letterbox(image: np.ndarray, size: int) -> np.ndarray:
|
|
h, w = image.shape[:2]
|
|
scale = size / max(h, w)
|
|
resized = cv2.resize(image, (int(w * scale), int(h * scale)))
|
|
canvas = np.zeros((size, size, 3), dtype=np.uint8)
|
|
rh, rw = resized.shape[:2]
|
|
top = (size - rh) // 2
|
|
left = (size - rw) // 2
|
|
canvas[top:top + rh, left:left + rw] = resized
|
|
return canvas
|
|
|
|
|
|
def render_frame(canvas: np.ndarray, frame_index: int, size: int) -> np.ndarray:
|
|
"""Pans + gently scales the canvas so the photographed subject visibly
|
|
translates/moves across frames, deterministically from `frame_index`."""
|
|
t = frame_index * 0.05
|
|
dx = 0.16 * size * np.sin(t)
|
|
dy = 0.07 * size * np.sin(t * 0.6 + 1.0)
|
|
scale = 1.0 + 0.06 * np.sin(t * 0.3)
|
|
center = (size / 2.0, size / 2.0)
|
|
matrix = cv2.getRotationMatrix2D(center, angle=0.0, scale=scale)
|
|
matrix[0, 2] += dx
|
|
matrix[1, 2] += dy
|
|
return cv2.warpAffine(
|
|
canvas, matrix, (size, size),
|
|
borderMode=cv2.BORDER_REPLICATE,
|
|
)
|
|
|
|
|
|
def generate(out_path: str, cache_path: str, size: int, frame_count: int, fps: int) -> None:
|
|
source = ensure_source_photo(cache_path)
|
|
canvas = letterbox(source, size)
|
|
os.makedirs(os.path.dirname(os.path.abspath(out_path)) or ".", exist_ok=True)
|
|
writer = cv2.VideoWriter(
|
|
out_path, cv2.VideoWriter_fourcc(*"MJPG"), fps, (size, size),
|
|
)
|
|
if not writer.isOpened():
|
|
raise RuntimeError(f"could not open VideoWriter for {out_path}")
|
|
try:
|
|
for frame_index in range(frame_count):
|
|
writer.write(render_frame(canvas, frame_index, size))
|
|
finally:
|
|
writer.release()
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--out", default="/tmp/kinect_e2e/dance.avi")
|
|
parser.add_argument("--cache", default="/tmp/kinect_e2e/pose.jpg")
|
|
parser.add_argument("--size", type=int, default=480)
|
|
parser.add_argument("--frames", type=int, default=240)
|
|
parser.add_argument("--fps", type=int, default=30)
|
|
args = parser.parse_args()
|
|
|
|
generate(args.out, args.cache, args.size, args.frames, args.fps)
|
|
print(f"wrote {args.out} ({args.frames} frames @ {args.size}x{args.size})")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|