Recreate-AAA / scripts /splat /views.py
CharlieDreemur's picture
RecreateAAA 0.1.0 from Recreate-AAA/Recreate-AAA@7da1a3d
966b234 verified
Raw History Blame Contribute Delete
3.14 kB
"""Another walk through the same place as views to test a reconstruction on (never to train on).
uv run python scripts/splat/views.py BUILD SESSION OUT [--fps 2]
BUILD is a splat_build output (its controller and .splat/data), SESSION a second recording of the same game
from the same start (recording.mp4, input.jsonl). Writes OUT/views.json and OUT/NNN.jpg: each frame with the
camera the inputs imply, expressed in the reconstruction's own frame, as train_cuda.py --views reads them."""
import argparse, json, math, shutil, subprocess, sys, tempfile
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from scripts import probe # noqa: E402
parser = argparse.ArgumentParser(); parser.add_argument("build", type=Path); parser.add_argument("session", type=Path)
parser.add_argument("out", type=Path); parser.add_argument("--fps", type=float, default=2.0); args = parser.parse_args()
def similarity(a, b):
ma, mb = a.mean(0), b.mean(0); A, B = a - ma, b - mb
U, S, Vt = np.linalg.svd(B.T @ A / len(a)); E = np.diag([1.0, 1.0, np.sign(np.linalg.det(U @ Vt))])
R = U @ E @ Vt; scale = float((S * np.diag(E)).sum() / (A ** 2).sum() * len(a))
return scale, R, mb - scale * R @ ma
# reconstruction frame -> game frame, as pack.py finds it
sfm = json.loads((args.build / ".splat/data/poses.json").read_text())
times = np.array([p["t"] for p in sfm["poses"]]); centres = np.array([p["centre"] for p in sfm["poses"]])
rows = np.array(probe.poses(args.build, float(times.max()) + 1)[1])
scale, R, shift = similarity(centres, np.stack([np.interp(times, rows[:, 0], rows[:, k]) for k in (1, 2, 3)], 1))
args.out.mkdir(parents=True, exist_ok=True)
seconds = float(subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0",
str(args.session / "recording.mp4")], capture_output=True, text=True).stdout)
with tempfile.TemporaryDirectory() as tmp: # the second walk's inputs with the first walk's controller
shutil.copy2(args.build / "controller.json", tmp); shutil.copy2(args.session / "input.jsonl", tmp)
walk = np.array(probe.poses(Path(tmp), seconds + 1)[1])
views = []
for n, t in enumerate(np.arange(0.5, seconds - 0.2, 1 / args.fps)):
name = f"{n:03d}.jpg"
subprocess.run(["ffmpeg", "-loglevel", "error", "-y", "-ss", f"{t:.3f}", "-i", str(args.session / "recording.mp4"), "-frames:v", "1",
"-q:v", "2", str(args.out / name)], check=True)
x, y, z, yaw, pitch = (float(np.interp(t, walk[:, 0], walk[:, k])) for k in (1, 2, 3, 4, 5))
cy, sy, cp, sp = math.cos(yaw), math.sin(yaw), math.cos(pitch), math.sin(pitch)
turn = np.array([[cy, 0, sy], [sy * sp, -cp, -cy * sp], [sy * cp, sp, -cy * cp]]).T # columns: right, down, ahead
views.append({"image": name, "t": round(float(t), 3), "centre": (R.T @ (np.array([x, y, z]) - shift) / scale).tolist(),
"rotation": (R.T @ turn).tolist()})
(args.out / "views.json").write_text(json.dumps(views))
print(f"{len(views)} views of {seconds:.1f} s written to {args.out}")