Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

fix(video-loop): a one-shot refusal where no pair of frames returns names the closest return, not the moved of returned pairs there are none of

Soohong Kim1b9f57c2f1ba
diff --git a/sprite_gen/video/loop.py b/sprite_gen/video/loop.py
index 1b0f01a..cd4180a 100644
--- a/sprite_gen/video/loop.py
+++ b/sprite_gen/video/loop.py
@@ -97,6 +97,7 @@ ONE_SHOT_MIN_MOVED = 0.4
ONE_SHOT_MIN_COHERENCE = 3.0 # departure must exceed three ordinary playback steps
ONE_SHOT_MIN_ACTIVE = 4 # isolated keying/jitter spikes are not a performed action
ONE_SHOT_PAD = 2 # one-shot: rest frames kept on each side of the excursion so the seam is rest -> rest
+ONE_SHOT_RETURN_MAX = 0.25 # one-shot: the return pair comes back to within a quarter of its departure
CYCLE_MODES = ("auto", "periodic", "one-shot", "fixed", "pinned", *nonloop.KINDS)
# --cycle pinned: the clip was asked to end on its first frame (first-last mode). A re-rendered
# first frame is never byte-identical to its source; on the analysis thumbnail (mean absolute
@@ -512,14 +513,18 @@ def detect_one_shot(D: np.ndarray, *, min_len: int, max_len: int, frame_mass: np
active component to have observed rest on BOTH sides. A clipped component
at frame zero or n-1 is never repaired by padding. The existing contrast /
moved-mass floors still reject stand jitter; return distance must additionally
- be at most a quarter of the departure. No seam threshold is relaxed.
+ be at most a quarter of the departure (ONE_SHOT_RETURN_MAX). No seam threshold is relaxed.
No cut is shorter than `min_len` (nor ONE_SHOT_MIN_LEN) or longer than `max_len`; `run_loop`
passes ONE_SHOT_MIN_LEN unless the caller set --min-len (`CutWindow`).
+
+ Where no pair returns, the refusal names the closest return (`closest_return`: the pair, its return over its
+ departure, its moved), not the moved of the returned pairs, of which there are none.
"""
n = len(D)
adjacent = np.diag(D, 1)
candidates = []
max_moved = 0.0
+ closest: dict[str, Any] | None = None
shortest = max(ONE_SHOT_MIN_LEN, min_len)
for start in range(n - ONE_SHOT_MIN_LEN + 1):
for end in range(start + shortest - 1, min(n, start + max_len)):
@@ -528,13 +533,18 @@ def detect_one_shot(D: np.ndarray, *, min_len: int, max_len: int, frame_mass: np
peak_at = start + int(np.argmax(e[start:end + 1]))
peak = float(e[peak_at])
departure = peak - seam / 2
- if departure <= 0 or seam > departure * 0.25:
+ if departure <= 0:
+ continue
+ mass = float((frame_mass[start] + frame_mass[end]) / 2) if frame_mass is not None else 0
+ moved = departure / mass if mass > 0 else None
+ if closest is None or seam / departure < closest["ratio"]:
+ closest = {"pair": [start, end], "ratio": seam / departure, "seam": seam, "departure": departure,
+ "moved": moved}
+ if seam > departure * ONE_SHOT_RETURN_MAX:
continue
med = float(np.median(e))
mad = float(np.median(np.abs(e - med))) or 1e-6
contrast = (peak - med) / mad
- mass = float((frame_mass[start] + frame_mass[end]) / 2) if frame_mass is not None else 0
- moved = departure / mass if mass > 0 else None
max_moved = max(max_moved, moved or 0)
if contrast >= ONE_SHOT_MIN_CONTRAST:
rule = "contrast"
@@ -572,11 +582,18 @@ def detect_one_shot(D: np.ndarray, *, min_len: int, max_len: int, frame_mass: np
"excursion_rule": rule, "departure": departure,
})
if not candidates:
+ if closest is not None and closest["ratio"] > ONE_SHOT_RETURN_MAX:
+ # No pair returns: the condition failed is the return, said on the pair that came closest.
+ a, b = closest["pair"]
+ said = (f"the clip does not come back to a pose it left: the closest return, frames {a}-{b}, is "
+ f"{closest['ratio']:.2f} of its departure away, over {ONE_SHOT_RETURN_MAX}"
+ + (f" (moved {closest['moved']:.2f}, need {ONE_SHOT_MIN_MOVED})" if closest["moved"] is not None else ""))
+ else:
+ said = (f"the clip never leaves its rest pose and returns with observed endpoints (moved {max_moved:.2f}, "
+ f"need {ONE_SHOT_MIN_MOVED})")
raise CycleSelectionError(
- f"video-loop: no complete one-shot return — the clip never leaves its rest pose "
- f"and returns with observed endpoints (moved {max_moved:.2f}, "
- f"need {ONE_SHOT_MIN_MOVED})",
- {"kind": "one-shot", "candidates": []},
+ f"video-loop: no complete one-shot return — {said}",
+ {"kind": "one-shot", "candidates": [], **({"closest_return": closest} if closest is not None else {})},
)
# Keep the full strongest action, then minimise excess rest. Do not choose a
# tiny low-seam twitch just because its endpoints happen to match exactly.
diff --git a/tests/video/test_one_shot_refusal.py b/tests/video/test_one_shot_refusal.py
new file mode 100644
index 0000000..f5fc7ed
--- /dev/null
+++ b/tests/video/test_one_shot_refusal.py
@@ -0,0 +1,168 @@
+# SPDX-License-Identifier: Apache-2.0
+"""A one-shot refusal says the condition the clip failed (docs/video-pipeline.md, "One-shot actions"). Where no pair of
+frames comes back to a pose it left within a quarter of its departure, the line names the closest return: the pair, how
+far it is from its departure and its moved, not the moved of the returned pairs, of which there are none. A clip that
+never departs still says it never leaves its rest pose. A body that crosses the canvas is refused so, as are a pose that
+does not come back and a stand that jitters; the same leap that comes back in place is cut. Figures drawn with
+rectangles; no real footage."""
+
+from __future__ import annotations
+
+import json
+import math
+import os
+import re
+from pathlib import Path
+
+import numpy as np
+import pytest
+from PIL import Image
+
+from sprite_gen.video import loop as loop_mod
+from tests.video.held_fill_inputs import save
+
+W, H = 360, 160
+FLOOR = 150
+REST, ACTION = 30, 24 # rest long enough that a cut from rest to rest fits the jump's window (`CutWindow`)
+X0, X1 = 40, 280 # where the body stands before the leap, and after it where it crosses the canvas
+CLOSEST = re.compile(r"no complete one-shot return — the clip does not come back to a pose it left: the closest return, "
+ r"frames (\d+)-(\d+), is ([\d.]+) of its departure away, over 0\.25 \(moved ([\d.]+), need 0\.4\)$")
+
+
+def figure(x: int, *, crouch: float = 0.0, lift: int = 0, arms: str = "down", legs: str = "together") -> Image.Image:
+ """A body of rectangles standing with its feet at `x`: legs bent by `crouch` (0..1), `together`, `tucked` up under
+ it or `apart` in a wide stance, raised `lift` px off the floor, arms hanging (`down`), overhead (`up`) or held out in
+ front (`out`)."""
+ a = np.zeros((H, W, 4), dtype=np.uint8)
+ leg = round(40 * (1 - 0.35 * crouch) * (0.5 if legs == "tucked" else 1))
+ feet = FLOOR - lift - (20 if legs == "tucked" else 0)
+ hip = feet - leg
+ spread = 7 if legs == "apart" else 0
+ a[hip:feet, x - 7 - spread:x - 1 - spread] = (60, 60, 200, 255) # legs
+ a[hip:feet, x + 1 + spread:x + 7 + spread] = (60, 60, 200, 255)
+ a[hip - 40:hip, x - 8:x + 8] = (200, 80, 30, 255) # torso
+ a[hip - 54:hip - 42, x - 6:x + 6] = (230, 190, 160, 255) # head
+ if arms == "down":
+ a[hip - 38:hip - 4, x + 8:x + 14] = (60, 60, 60, 255)
+ elif arms == "up":
+ a[hip - 72:hip - 38, x + 8:x + 14] = (60, 60, 60, 255)
+ else:
+ a[hip - 38:hip - 32, x + 8:x + 42] = (60, 60, 60, 255)
+ return Image.fromarray(a, "RGBA")
+
+
+def leap(*, x1: int = X1, comes_back: bool = True) -> list[Image.Image]:
+ """Standing still at X0; a crouch deepening into the take-off, legs together and arms down; up with the arms overhead
+ and the legs tucked, down with the arms out and the legs apart, the body carried to `x1`; a landing crouch easing up
+ in the wide stance, arms out; standing still at `x1` as at X0, or, `comes_back` False, staying in the landing's
+ stance. No two frames of the take-off, the air and the landing are one picture: the clip comes back to its rest
+ pose, or to no pose it left."""
+ frames = [figure(X0)] * REST
+ for k in range(ACTION):
+ t = (k + 1) / (ACTION + 1)
+ x = round(X0 + (x1 - X0) * min(1.0, max(0.0, (t - 0.2) / 0.6)))
+ if t < 0.2:
+ frames.append(figure(x, crouch=t / 0.2))
+ elif t < 0.8: # in the air, off the floor: at 0.8 the feet are down, the landing's deepest crouch
+ up = t < 0.5
+ frames.append(figure(x, lift=round(30 * math.sin(math.pi * (t - 0.2) / 0.6)), arms="up" if up else "out",
+ legs="tucked" if up else "apart"))
+ else:
+ frames.append(figure(x, crouch=(1.0 - t) / 0.2, arms="out", legs="apart"))
+ return frames + [figure(x1) if comes_back else figure(x1, arms="out", legs="apart")] * REST
+
+
+def jitter(count: int = 2 * REST + ACTION, seed: int = 0) -> list[Image.Image]:
+ """The body standing in place, moved up to 3 px along and up or down every frame: no action."""
+ rng = np.random.default_rng(seed)
+ out = []
+ for _ in range(count):
+ im = np.asarray(figure(X0 + int(rng.integers(-3, 4))))
+ out.append(Image.fromarray(np.roll(im, int(rng.integers(-3, 4)), axis=0), "RGBA"))
+ return out
+
+
+def _no_rife(monkeypatch, tmp_path: Path) -> None:
+ monkeypatch.delenv("SPRITE_GEN_RIFE", raising=False)
+ path = [d for d in os.environ.get("PATH", "").split(os.pathsep) if d and not (Path(d) / "rife-ncnn-vulkan").exists()]
+ monkeypatch.setenv("PATH", os.pathsep.join(path))
+ monkeypatch.setenv("SPRITE_GEN_DATA_DIR", str(tmp_path / "data"))
+
+
+def _cut(tmp_path: Path, frames: list[Image.Image], name: str) -> dict:
+ """`video-loop --cycle one-shot --anchor none --state jump`, as a single play with no end frame is cut."""
+ keyed = save(frames, tmp_path / f"{name}-keyed")
+ return loop_mod.run_loop(keyed, tmp_path / f"{name}-out", fps=24.0, min_len=None, max_len=None, n_out=None,
+ seam_max=loop_mod.SEAM_RATIO_MAX, name=name, report_path=tmp_path / f"{name}.json",
+ state="jump", cycle_mode="one-shot", anchor="none")
+
+
+def _closest(tmp_path: Path, frames: list[Image.Image], name: str) -> tuple[re.Match, dict]:
+ with pytest.raises(SystemExit) as refused:
+ _cut(tmp_path, frames, name)
+ said = CLOSEST.search(str(refused.value))
+ assert said, str(refused.value)
+ report = json.loads((tmp_path / f"{name}.json").read_text())
+ assert report["status"] == "failed" and report["cycle"]["candidates"] == []
+ return said, report
+
+
+def test_a_body_that_crosses_the_canvas_is_refused_on_its_closest_return(tmp_path, monkeypatch):
+ """No pair of frames comes back to a pose it left (the body lands where it did not leave): the line says the pair that
+ came closest, how far from its departure, and how far it moved, which was not none."""
+ _no_rife(monkeypatch, tmp_path)
+ frames = leap()
+ said, report = _closest(tmp_path, frames, "leap")
+ start, end, ratio, moved = int(said[1]), int(said[2]), float(said[3]), float(said[4])
+ assert ratio > loop_mod.ONE_SHOT_RETURN_MAX and moved > 0
+ closest = report["cycle"]["closest_return"]
+ assert closest["pair"] == [start, end]
+ assert f"{closest['ratio']:.2f}" == said[3] and f"{closest['moved']:.2f}" == said[4]
+ # The least return over departure of every pair the search read, in the window it searched.
+ files = sorted((tmp_path / "leap-keyed").glob("*.png"))
+ D = loop_mod.distance_matrix(files)
+ lo, hi = report["window"]
+ shortest, longest = loop_mod.ONE_SHOT_MIN_LEN, hi
+ ratios = {}
+ for s in range(len(D) - loop_mod.ONE_SHOT_MIN_LEN + 1):
+ for e in range(s + shortest - 1, min(len(D), s + longest)):
+ departure = float(((D[s] + D[e]) / 2)[s:e + 1].max()) - float(D[s, e]) / 2
+ if departure > 0:
+ ratios[s, e] = float(D[s, e]) / departure
+ best = min(ratios, key=ratios.get)
+ assert list(best) == closest["pair"] and ratios[best] == pytest.approx(closest["ratio"])
+ assert hi >= ACTION + 2 * loop_mod.ONE_SHOT_PAD # the leap and a rest frame or two each side fit the window searched
+
+
+def test_the_same_leap_in_place_is_cut_from_rest_to_rest(tmp_path, monkeypatch):
+ """The fixture's return is one the search reads: in place, the leap that comes back is cut, the take-off's rest to
+ the landing's."""
+ _no_rife(monkeypatch, tmp_path)
+ cycle = _cut(tmp_path, leap(x1=X0), "hop")["cycle"]
+ assert cycle["kind"] == "one-shot"
+ start, end = cycle["return_pair"]
+ assert start < REST and end >= REST + ACTION
+
+
+def test_a_pose_that_does_not_come_back_is_refused_on_its_closest_return(tmp_path, monkeypatch):
+ """In place, but the take-off's crouch and the landing's are two pictures and the clip ends in the landing's stance:
+ nothing in it comes back to a pose it left."""
+ _no_rife(monkeypatch, tmp_path)
+ said, _ = _closest(tmp_path, leap(x1=X0, comes_back=False), "stays")
+ assert float(said[3]) > loop_mod.ONE_SHOT_RETURN_MAX
+
+
+def test_a_stand_that_jitters_in_place_is_refused(tmp_path, monkeypatch):
+ """A body moved a few px along and up or down every frame performs no action, so no cut."""
+ _no_rife(monkeypatch, tmp_path)
+ with pytest.raises(SystemExit, match="no complete one-shot return"):
+ _cut(tmp_path, jitter(), "jitter")
+
+
+def test_a_clip_that_never_departs_still_says_it_never_leaves_its_rest_pose():
+ """No pair departs, so there is no closest return to name: the line is the one it was."""
+ D = np.zeros((20, 20), dtype=np.float32)
+ with pytest.raises(loop_mod.CycleSelectionError,
+ match=r"never leaves its rest pose and returns with observed endpoints \(moved 0\.00, need 0\.4\)") as refused:
+ loop_mod.detect_one_shot(D, min_len=4, max_len=20, frame_mass=np.ones(20))
+ assert "closest_return" not in refused.value.diagnostics