Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(loop-ease): an eased stretch that stops the legs is refused, and the loop keeps the flow repair
Soohong Kima0a35d114c21
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6daed34..9f816c9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,11 @@All notable public changes to `sprite-gen` are recorded here. Versions track the `version:` field in `SKILL.md` and `pyproject.toml`.+## Unreleased++- **Fix: `video-loop --repair auto` (and `on`) no longer plays an eased stretch that stops the legs.** A frame RIFE makes is the whole picture at one time, so the times that spread a rushing tail gave the legs, the arms and the head the same times: they slowed to a stop where the tail was given its time and hurried on after (on the synthetic walkers of the tests, a leg 1.5 to 2.7 of its ordinary step off its step as filmed). The stretch is still read, made and recorded, and is played only where every window of the rest of the body steps, in each step of it, as far as it did there as the loop played, to within a quarter of its usual step (`repair.EASE_BEAT`, the round's `beat`); otherwise it is refused for `the body off its beat` and the loop keeps the flow repair, each frame made at its own time. The calls the stretch made are still spent. [loop-repair](docs/loop-repair.md) section 2, "The body's beat".+- **Tests** — `tests/video/test_loop_ease.py` (whatever spreads a rush, the two legs and the feet of four walkers step as filmed to within a quarter of their ordinary step, the flow repair alone as the control; the stretches 2.45.0 took — 24 frames, 16, 14, 48 on twos, mirrored, through `video-loop`, at the half-loop cap — read, made and refused for the legs' beat alone, their frames made rebuilt to show the legs stop, the loop the flow repair's; with the bound lifted, a stretch is played as made).+## v2.45.0 - Walk loops repaired along their flow, eased, closed from the clip's next cycle and cut two steps from their feet; RIFE runs with its augmentation; frames between kept for a later alignment; video-stop, play modes and logo**Upgrading from 2.44.0: RIFE costs more a frame, the jump repair names its readings anew, and a default codex that cannot run asks.** Every RIFE call runs the binary's augmentation `-x -z` (`rife.TTA`), 15 to 19 times a plain call's CPU on a machine without a GPU: a caller on CPU sets `SPRITE_GEN_RIFE_TTA` (`-x`, `-z`, both, or empty for none) where it cannot afford it, and a call past 600 s is a RIFE that failed. Under `video-loop --repair auto` (and `on`) a loop may now call RIFE up to 3 times for the flow repair, up to 9 for the eased retime and, where a part on its own beat leaves the wrap open, 34 calls a frame for the overlapped wrap. `jump_repair` reports `off_max_before` / `off_max_after` in place of `score_max_before` / `score_max_after`, in the summary and in `video-set`'s item record too, and `repair.JUMP_RATIO` is gone; a walk cut two steps from its crossings records `cycle.steps` and `strip.json` `steps: 2`. A call that names no provider, where codex cannot run, exits 75 with one question unless `SPRITE_GEN_CODEX_UNAVAILABLE` or `sprite-gen defaults save --codex-unavailable` chose another provider, and `preferences.json` is written as version 2, which 2.44.0 refuses. The restoration contract is unchanged: `source-restoration-v5`, repair report `schema_version` 4, comparison `schema_version` 6.diff --git a/SKILL.md b/SKILL.mdindex e9a000f..8d085dc 100644--- a/SKILL.md+++ b/SKILL.md@@ -160,7 +160,7 @@ Scene creation consumes finished assets and remains optional. Asset metadata own## RIFE for walk and run loops-`video-loop` repairs a walk's or run's jump frames, eases a part that rushes on its way at the wrap, closes a two-step cut's wrap that a part on its own beat leaves open with the clip's frames a cycle apart, and gives its filmed ghosts way (a half-drawn frame the video model left, `ghost_given_way`), and `video-set` gives a direction set one cycle length, both with RIFE in-betweens. Install it once per machine: `$SPRITE_GEN_ROOT/.venv/bin/sprite-gen rife install` (pinned rife-ncnn-vulkan 20221029 and model rife-v4.6, SHA-256 checked, into the user data directory, ending with a check frame; Linux without a GPU first needs `libvulkan1 mesa-vulkan-drivers`). Every call runs the binary's test-time augmentation `-x -z`; on a CPU it costs 15 to 19 times a plain call, so a CPU-only machine aligning a set may set `SPRITE_GEN_RIFE_TTA=` (none) or `-z`. Without it every loop is still cut — a jump or a rush as filmed, each filmed ghost given way to the clean frame beside it — and what was left unrepaired says so: a `warning:` line on stderr and `jump_repair.rife` with `jump_repair.unmade` (the jump left as filmed) or `jump_repair.ease_skipped` (the rush) in the loop report (`applied` alone is not the sign: a ghost given way reads true), `cycle_align.<state>.applied: false` and `warnings` in `set.report.json`. When that happens, tell the user which loops went unrepaired and offer the install; after it, cut those loops again and run `video-cycle-align`. `video-loop --repair on` and `video-cycle-align` fail without RIFE instead. [loop-repair](docs/loop-repair.md).+`video-loop` repairs a walk's or run's jump frames, reads a part that rushes on its way at the wrap and eases it only where the rest of the body keeps its own beat (a frame made is the whole picture, so the legs take the part's time: such a loop keeps the flow repair), closes a two-step cut's wrap that a part on its own beat leaves open with the clip's frames a cycle apart, and gives its filmed ghosts way (a half-drawn frame the video model left, `ghost_given_way`), and `video-set` gives a direction set one cycle length, both with RIFE in-betweens. Install it once per machine: `$SPRITE_GEN_ROOT/.venv/bin/sprite-gen rife install` (pinned rife-ncnn-vulkan 20221029 and model rife-v4.6, SHA-256 checked, into the user data directory, ending with a check frame; Linux without a GPU first needs `libvulkan1 mesa-vulkan-drivers`). Every call runs the binary's test-time augmentation `-x -z`; on a CPU it costs 15 to 19 times a plain call, so a CPU-only machine aligning a set may set `SPRITE_GEN_RIFE_TTA=` (none) or `-z`. Without it every loop is still cut — a jump or a rush as filmed, each filmed ghost given way to the clean frame beside it — and what was left unrepaired says so: a `warning:` line on stderr and `jump_repair.rife` with `jump_repair.unmade` (the jump left as filmed) or `jump_repair.ease_skipped` (the rush) in the loop report (`applied` alone is not the sign: a ghost given way reads true), `cycle_align.<state>.applied: false` and `warnings` in `set.report.json`. When that happens, tell the user which loops went unrepaired and offer the install; after it, cut those loops again and run `video-cycle-align`. `video-loop --repair on` and `video-cycle-align` fail without RIFE instead. [loop-repair](docs/loop-repair.md).## Contracts and advanced toolsdiff --git a/docs/loop-repair.md b/docs/loop-repair.mdindex 6f3fa03..20d2e24 100644--- a/docs/loop-repair.md+++ b/docs/loop-repair.md@@ -222,8 +222,10 @@ that lie far off the line between the frames either side of them, by direction ahow much a step changes ([the flow reading](#the-flow-reading--a-frame-off-the-walks-way-not-a-large-step)below) — are taken out and made again by RIFE between the frames either side. Where a part keeps its wayand rushes — a tail that runs short of the cycle, rushing on at the wrap — the frames either side of-the rush are then made again at times that spread it, the body's own steps kept ([the eased-retime](#the-eased-retime--a-part-that-rushes-on-its-way) below). Before all of it, a cut two steps+the rush are then made again at times that spread it, and played only where the rest of the body+keeps its own beat on them ([the eased retime](#the-eased-retime--a-part-that-rushes-on-its-way)+below; a frame made is the whole picture at one time, so the legs take the time the part is given,+and on every walker measured the loop keeps the flow repair). Before all of it, a cut two stepslong whose wrap a part on its own beat leaves open has the frames beside its wrap made again betweenthe clip's frames a cycle apart and its own ([the overlappedwrap](#the-overlapped-wrap--a-part-on-its-own-beat-that-no-cut-of-two-steps-closes) below).@@ -527,10 +529,28 @@ on the frames made:| no frame of the stretch is read further off the flow than before and at or over its limit (`off_max`) | `off the flow` || no rush is read in the stretch in a window the part does not rush in: the rest of the body was hurried | `a new rush` || no window the part does not rush in steps further than its own largest step over the stretch as it played (`rest`: `windows`, `step_max_before`, `step_max_after`, in the usual steps of the loop the flow groups left, and `faster`) | `the rest faster` |+| every window the part does not rush in steps, in each step of the stretch, as far as it did in that step as the loop played, to within a quarter of its usual step (`EASE_BEAT`; `beat`: `bound`, `off` per step in play order, its largest over the windows, and `off_max`) | `the body off its beat` |A refused stretch keeps the loop as the flow groups left it; the loop keeps its length and everyframe outside the stretch is as it was.+**The body's beat.** A frame RIFE makes is the whole picture at one time, the legs, the arms and+the head with the part. A time that gives the part's rush more steps gives the body the same: where+the part is given its time the legs slow to a stop, and after it they hurry on. In such a frame the+part's step and the body's are in the ratio they have at that time, so bounding the body's step+below as well as above (the search bounds it only above) leaves the rush to spread by no more than+the share of its own step the body may lose: a body held to three quarters of its step leaves the+part three quarters of its rush. The stretch is+therefore read on the frames made, step by step, against the loop as the flow groups left it: every+window of the rest of the body steps as far as it did there, to within a quarter of its usual step,+the walk's beat as `tests/video/test_loop_ease.py` reads it on a walker's legs (the centres of their+colours, a quarter of their ordinary step). A window of the rest of the body can hold some of the+part as well; a stretch that moved the part alone could be refused there, and the engine makes none —+its frames are made whole. Every stretch the stand-in makes on the synthetic walkers is refused for+it (below); the rush is recorded, `surge_after` names it, and the loop plays as the flow repair left+it, its frames each made at their own time and the legs on their steps as filmed. A part spread+apart from the body would have to be made apart from it, which this stage does not do.+**The rest of the body, read again.** The search reads each window of the rest of the body on theline between the states of the frames as filmed. The frames made go the body's own way betweenthem, and where a part moves far between two frames — a leg through its swing, two legs crossing —@@ -546,17 +566,19 @@ of every reading by speed, eases none of those walkers drawn every frame. Searchframes made added to the frames as filmed does not settle either: after five tries those walkerswere still refused.-| On the synthetic walker (the stand-in in-between of `tests/video/test_loop_ease.py`) | Part's largest step | Frames made |-|---|---|---|-| 24 frames a cycle, the tail a quarter short, after the flow repair at the wrap | 5.3 → 3.4, no rush left | 8, at four a side, 8 calls |-| 16 frames, 15 % short (under the flow reading's limit) | 3.6 → 1.7, no rush left | 6, at three a side, 5 calls |-| 16 frames, a fifth short | 3.1 → 1.5, no rush left | 6, 6 calls |-| 14 frames, 15 % short | 3.1 → 1.5, no rush left | 6, 6 calls |-| 48 frames on twos, a quarter short, after the flow repair | 5.3 → 3.4 (on its drawings), no rush left | 8 drawings, 16 frames, 8 calls |-| 36 frames, 5 % to 30 % short drawn every frame, 10 % to 30 % on twos | refused: the rest of the body 1.5 % to 19 % past its own largest step on the frames made | 6 to 9 calls, none kept |--Over 36 frames a cycle every stretch of the stand-in is refused: the loop keeps the flow repair,-`surge_after` names the rush, and the calls the stretch made are spent. On a Mac's GPU a call is a+| On the synthetic walker (the stand-in in-between of `tests/video/test_loop_ease.py`) | Part's largest step, on the frames made | Frames made | The rest of the body off its beat, at most (`beat`) · the legs off their steps as filmed, ordinary steps |+|---|---|---|---|+| 24 frames a cycle, the tail a quarter short, after the flow repair at the wrap | 5.3 → 3.4, no rush left | 8, at four a side, 8 calls | 1.52 · 2.70: refused |+| 16 frames, 15 % short (under the flow reading's limit) | 3.6 → 1.7, no rush left | 6, at three a side, 5 calls | 1.35 · 2.00: refused |+| 16 frames, a fifth short | 3.1 → 1.5, no rush left | 6, 6 calls | 2.42 · 2.63: refused |+| 14 frames, 15 % short | 3.1 → 1.5, no rush left | 6, 6 calls | 1.60 · 1.50: refused |+| 48 frames on twos, a quarter short, after the flow repair | 5.3 → 3.4 (on its drawings), no rush left | 8 drawings, 16 frames, 8 calls | 1.52 · 2.70: refused |+| 36 frames, 5 % to 30 % short drawn every frame, 10 % to 30 % on twos | refused: the rest of the body 1.5 % to 19 % past its own largest step on the frames made | 6 to 9 calls | refused |+| the flow repair alone, each frame made at its own time | | | the legs 0 off |++Every stretch of the stand-in is refused: the loop keeps the flow repair, `surge_after` names the+rush, and the calls the stretch made are spent. Up to 2.45.0 the first five were taken, and their+legs stopped where the tail was given its time. On a Mac's GPU a call is asecond or so; on Modal's CPU with `-x -z` twelve to thirty seconds a280 × 520 frame (section 1), so a loop that rushes costs two to four and a half minutes more, and aloop that does not, nothing.@@ -571,7 +593,7 @@ stretch in play order), `sources` (the frames as filmed it was made from), `supe(frames from anchor to anchor), `made` (per drawing: `frames`, `time` and `own` in frames from thefirst anchor, `from`, `t`, `proposal`, `faults`, `look`), `accel` (`before`, `chosen`), `part`(`windows`, `step_max_before`, `step_max_after`), `rest` (`windows`, `step_max_before`,-`step_max_after`, `faster`), `drift`, `off_max`, `calls`, `faults`, `look` and `outcome`, and+`step_max_after`, `faster`), `beat` (`bound`, `off`, `off_max`), `drift`, `off_max`, `calls`, `faults`, `look` and `outcome`, and`replaced` or `rejected_for`. The record's `replaced` holds the frames it changed, theseam gate reads the rendered cells, and the summary names them (`eased`). Without RIFE the rush iscut as filmed with a warning line (`why`: "RIFE not installed — the rush cut as filmed"), anddiff --git a/docs/video-pipeline.md b/docs/video-pipeline.mdindex 6eb248a..afcda29 100644--- a/docs/video-pipeline.md+++ b/docs/video-pipeline.md@@ -942,7 +942,9 @@ the cycle, rushing on at the wrap — has the three or four drawings either sideagain at times that spread it, made from the frames as filmed, the body's feet, torso (the headand torso the motion anchor tracks; a part swaying on its own beat is not drift) and ground linemoving no further in a step, nor the rest of it faster, than they did there (the eased retime, up-to nine calls of its own). Before all of it, a cut two steps long whose wrap a part on its own beat+to nine calls of its own) — played only where the rest of the body keeps its own beat on the frames+made, step by step; a frame made is the whole picture at one time, so the legs take the time the+tail is given, and the loop then keeps the flow repair. Before all of it, a cut two steps long whose wrap a part on its own beatleaves open — a tail swinging round on a beat of its own, closing on no cut of two steps — has fourto twelve frames beside its wrap made again between the clip's frames a cycle apart and its own, sothe wrap plays a step the clip filmed, as far as the torso held to the stretch as the loop plays itdiff --git a/sprite_gen/video/repair.py b/sprite_gen/video/repair.pyindex b345714..402fd8f 100644--- a/sprite_gen/video/repair.py+++ b/sprite_gen/video/repair.py@@ -20,7 +20,9 @@ or four drawings each way, are made again by RIFE at times chosen so the part'sfaster, than they did in that stretch — read again on the frames made, which go the body's own way between theframes as filmed (docs/loop-repair.md section 2, "The eased retime"). The torso is the head and torso the motionanchor tracks (`Torso`): a part that sways on its own beat pulls the body's mass along, and moving it is what the-repair is for, not drift.+repair is for, not drift. A frame RIFE makes is the whole picture at one time, so the legs take the part's time with+it: the stretch is delivered only where the rest of the body keeps its own beat on the frames made, step by step+(`EASE_BEAT`), and otherwise the loop stays as the flow repair left it.A part on a beat of its own — a tail swinging round every 41 frames on a walk of 26 — closes on no cut of two steps,and turns back at the wrap, where no retime reaches it. Before the rounds above, the frames beside such a cut's wrap@@ -410,6 +412,17 @@ DRIFT_MEASURES = ("foot_centre", "torso_centre", "ground")# With the frames the ghost and flow rounds replaced, a stretch leaves at most this share of the loop made:# `video-loop-compare` reads no more back as the repair's (`source.py`, "repair-locations-unverified").MADE_SHARE = 0.5+# The body keeps its own beat. A frame RIFE makes is the whole picture at one time, the body with the part: a time that+# gives the part's rush more steps gives the legs, the arms and the head the same, so they slow to a stop where the part+# is given its time and hurry on after. In a frame made so, the part's step and the body's are in the ratio they have at+# that time, and bounding the body's step below as well as above leaves the rush to spread by no more than the share of+# its own step the body may lose. So the stretch is read on the frames made, step by step: every window the part does+# not rush in steps as far as it did in that step as the loop played before, to within EASE_BEAT of its usual step —+# the walk's beat as tests/video/test_loop_ease.py reads it on a walker's legs, a quarter of their ordinary step.+# Drawings made at their own times, as the flow repair makes them, keep the walkers' legs on their steps as filmed+# (0 off). The stand-in's stretches the earlier reading took on the walkers of 14 to 48 frames read 1.35 to 2.42 here,+# their legs 1.5 to 2.7 ordinary steps off.+EASE_BEAT = 0.25# Drift is the body sliding or lifting off its line. A part that sways on its own beat — a tail, the hair's ends — pulls# the body's mass along, and moving such a part is what the repairs here are for: that is not drift. The body's point is# the torso, the head and torso the motion anchor tracks (`auto_motion.discover` on the loop as cut), which a part swaying@@ -779,9 +792,15 @@ def _ease(frames: list[Image.Image], out: list[Image.Image], interpolate: Interprest_before = flow["step"][stretch[:-1]][:, rest].max(axis=0) if rest else np.zeros(0)rest_after = after_flow["step"][stretch[:-1]][:, rest].max(axis=0) if rest else np.zeros(0)faster = [w for w, b, a in zip(rest, rest_before, rest_after) if a > b + 1e-9]+ # the rest of the body on its own beat (EASE_BEAT): each step of the stretch against the same step as it played,+ # in every window; a stretch whose body cannot be read apart from the part keeps no beat it can show+ beat = (np.abs(after_flow["step"][stretch[:-1]][:, rest] - flow["step"][stretch[:-1]][:, rest])+ / flow["scale"][rest]).max(axis=1) if rest else np.zeros(0)+ unbeat = not rest or float(beat.max()) > EASE_BEATrejected = ((["fault"] if wrong else []) + (["drift"] if over else []) + (["no gain"] if part_after >= part_before else [])+ (["off the flow"] if off_after >= FLOW_LIMIT and off_after > off_before else [])- + (["a new rush"] if rushes else []) + (["the rest faster"] if faster else []))+ + (["a new rush"] if rushes else []) + (["the rest faster"] if faster else [])+ + (["the body off its beat"] if unbeat else []))changed = [t for d in stretch[1:-1] for t in runs[d] if new[t] is not out[t]]round_: dict[str, Any] = {"why": "ease", "surge": _said_surge(s, flow), "width": plan["k"], "anchors": [first[stretch[0]], first[stretch[-1]]],@@ -795,6 +814,8 @@ def _ease(frames: list[Image.Image], out: list[Image.Image], interpolate: Interp"step_max_before": [round(float(v), 4) for v in rest_before / flow["scale"][rest]],"step_max_after": [round(float(v), 4) for v in rest_after / flow["scale"][rest]],"faster": [[round(v, 4) for v in flow["fractions"][w]] for w in faster]},+ "beat": {"bound": EASE_BEAT, "off": [round(float(v), 4) for v in beat],+ "off_max": round(float(beat.max()), 4) if rest else None},"drift": {"measures": list(DRIFT_MEASURES), "bound": [round(float(v), 3) for v in plan["bound"]],"moved": [round(float(v), 3) for v in moved], "over": over,"body_mass": {"bound": mass_step([out[first[d]] for d in stretch]),@@ -1151,10 +1172,11 @@ def repair_jumps(frames: list[Image.Image], interpolate: Interpolate | None, *,chooses and shown on every frame it was; the stretch is taken only where no made frame has a fault, no bodymeasure moves further than the stretch did and a pixel (`drift`, the body's point the torso, `torso` — the loop's`Torso` when not given, recorded as `torso`), the part's largest step falls (`part`), no- frame is left further off the flow than the limit and before, no new rush appears in the rest of the body, and- no window of the rest of the body steps further than its own largest step over the stretch as it played (`rest`)- (`rejected_for` names what failed). Its calls are its own, outside `max_frames` (`ease_calls`). `surge_after`- is the reading of the loop as it now plays. `facing` reads nothing here: it is recorded for the jump guard of+ frame is left further off the flow than the limit and before, no new rush appears in the rest of the body, no+ window of the rest of the body steps further than its own largest step over the stretch as it played (`rest`),+ and every one steps in each step of the stretch as far as it did there, to within EASE_BEAT of its usual step+ (`beat`) (`rejected_for` names what failed; a refused stretch leaves the loop as the flow groups left it). Its+ calls are its own, outside `max_frames` (`ease_calls`). `surge_after` is the reading of the loop as it now plays. `facing` reads nothing here: it is recorded for the jump guard of`video-loop-compare` (`restoration.jump`)."""out = list(frames)n = len(out)diff --git a/tests/video/test_loop_ease.py b/tests/video/test_loop_ease.pyindex 78db627..b7d18f8 100644--- a/tests/video/test_loop_ease.py+++ b/tests/video/test_loop_ease.py@@ -12,8 +12,15 @@ rushes on at the wrap (`lag`). The interpolator is a stand-in that draws the walan optical-flow in-between does: the body at the time between them, the tail moved on between its two places.Drawn over 36 frames a cycle, the stand-in's frames made at the times the search chooses step further in thelegs, the arm and the head than the line between the frames as filmed the search reads them on, and every such-stretch is refused on the frames made. The walkers eased here are drawn over 14 to 48 frames, where the frames-made keep the rest of the body within its own steps."""+stretch is refused on the frames made. The walkers drawn over 14 to 48 frames have frames made that keep the rest+of the body within its own steps.++A frame made is the whole picture at one time, the legs with the tail: the time that spreads the tail's rush+stops the legs where the tail is given its time and hurries them on after. A stretch is delivered only where the+rest of the body keeps its own beat on the frames made, step by step (`repair.EASE_BEAT`), and on every walker+here it does not: the stretch is read, made and recorded, and refused for that, and the loop stays as the flow+repair left it, the legs on their steps as filmed. The tests that took these stretches (2.45.0) read the same+stretches and their frames made, and now say they are refused; with the beat bound lifted, one is played as made."""from __future__ import annotations@@ -107,7 +114,49 @@ def _kept_rest(frames: list[Image.Image], flow_only: list[Image.Image], out: lisassert all(a <= b + 1e-4 for a, b in zip(round_["rest"]["step_max_after"], round_["rest"]["step_max_before"]))-def test_a_tail_that_rushes_on_at_the_wrap_is_eased_over_the_frames_either_side():+def _made(loop: list[Image.Image], frames: list[Image.Image], round_: dict, made: list[Image.Image]) -> list[Image.Image]:+ """The loop a stretch would have played, taken or refused: `loop` as the flow left it, each drawing of the stretch+ shown as it was made — the interpolator's last `calls` frames `made`, in order — or as the frame as filmed its time+ falls on."""+ new, calls = list(loop), iter(made[len(made) - round_["calls"]:])+ for row in round_["made"]:+ if row["from"] is None:+ continue+ frame = next(calls) if row["proposal"] is not None else frames[row["from"][0]]+ for k in row["frames"]:+ new[k] = frame+ return new+++LEGS = {"near leg": (80, 70, 130), "far leg": (90, 80, 150), "feet": (40, 30, 30)} # the walker's colours, dark parts whole+BEAT = 0.25 # of a leg's ordinary step: a step of the legs this far off the same step as filmed is off the walk's beat+++def _legs(frames: list[Image.Image]) -> dict[str, np.ndarray]:+ """Per step of the loop as it plays (cyclic), how far each leg and the feet move: the centre of their colour, px."""+ centres = []+ for f in frames:+ p = np.asarray(f)+ at = {}+ for part, colour in LEGS.items():+ ys, xs = np.nonzero((p[..., 3] > 0) & np.all(p[..., :3] == colour, axis=-1))+ at[part] = np.array([xs.mean(), ys.mean()])+ centres.append(at)+ n = len(frames)+ return {part: np.array([np.linalg.norm(centres[(k + 1) % n][part] - centres[k][part]) for k in range(n)]) for part in LEGS}+++def _legs_off(frames: list[Image.Image], loop: list[Image.Image]) -> float:+ """How far a step of the legs or the feet over `loop` as it plays is off the same step as filmed, at most, in their+ ordinary steps (the median of their steps as filmed that move)."""+ filmed, played = _legs(frames), _legs(loop)+ return max(float((np.abs(played[p] - filmed[p]) / np.median(filmed[p][filmed[p] > 0])).max()) for p in LEGS)+++def test_a_tail_that_rushes_on_at_the_wrap_is_spread_over_the_frames_either_side_and_refused_for_the_legs_beat():+ # 2.45.0 took this stretch. Its frames made spread the tail and keep the feet, the torso, the ground line and the rest+ # of the body within the stretch's own steps, and stop the legs where the tail is given its time: refused for the+ # legs' beat alone, and the loop is the flow repair's.n = WRAPframes = _walk(n, lag=LAG)flow_only, before = repair.repair_jumps(frames, Lerp(frames, lag=LAG, n=n), ease=False)@@ -116,48 +165,36 @@ def test_a_tail_that_rushes_on_at_the_wrap_is_eased_over_the_frames_either_side(lerp = Lerp(frames, lag=LAG, n=n)out, record = repair.repair_jumps(frames, lerp)round_ = _ease(record)- assert round_["outcome"] == "accepted" and round_["width"] in repair.EASE_WIDTHS+ assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the body off its beat"]+ assert round_["width"] in repair.EASE_WIDTHSsegment = [*[round_["anchors"][0]], *round_["frames"], round_["anchors"][1]]assert [(k - segment[0]) % n for k in segment] == list(range(len(segment))) # one stretch, in play orderassert {n - 1, 0} <= set(round_["frames"]) and len(round_["frames"]) == 2 * round_["width"] + round_["surge"]["steps"] - 1- assert len(out) == n and all(out[k] is frames[k] for k in range(n) if k not in round_["frames"])# every frame is made from two frames as filmed, never from a frame made — the flow's frame at the wrap is made againflow_frames = set(before["rounds"][0]["frames"])assert flow_frames <= set(round_["frames"]) and round_["supersedes"] == sorted(flow_frames)assert all(not {a, b} & flow_frames for a, b, _ in lerp.calls)- # the rush is spread: the part's largest step over the stretch falls by a quarter or more, its speed changes less- # from step to step, and no rush is left+ # on the frames made, the rush is spread: the part's largest step over the stretch falls by a quarter or more, its+ # speed changes less from step to step, and no rush is left+ made = _made(flow_only, frames, round_, lerp.made)assert round_["part"]["step_max_after"] <= 3 / 4 * round_["part"]["step_max_before"]- assert round_["accel"]["chosen"] < round_["accel"]["before"] / 2 and record["surge_after"] is None+ assert round_["accel"]["chosen"] < round_["accel"]["before"] / 2 and repair.surges(made, box=repair.union_box(frames)) == []# no drift: the feet, the torso and the ground line move no further in a step than they did there, to within the pixel# they are read on — the torso read on the loop as filmed, as the round read it; the body's mass reported beside it;# and the rest of the body no fastertorso = repair.Torso(frames)assert record["torso"] == torso.said() and record["torso"]["regions"]- assert np.all(_drift(out, segment, torso) <= _drift(flow_only, segment, torso) + repair.DRIFT_PIXEL)+ assert np.all(_drift(made, segment, torso) <= _drift(flow_only, segment, torso) + repair.DRIFT_PIXEL)assert round_["drift"]["measures"] == ["foot_centre", "torso_centre", "ground"] and set(round_["drift"]["body_mass"]) == {"bound", "moved"}- _kept_rest(frames, flow_only, out, round_)- changed = {k for k in round_["frames"] if out[k] is not frames[k]}- assert round_["replaced"] == sorted(changed, key=round_["frames"].index) and len(changed) >= len(round_["frames"]) - 2- assert set(record["replaced"]) == flow_frames | changed---LEGS = {"near leg": (80, 70, 130), "far leg": (90, 80, 150), "feet": (40, 30, 30)} # the walker's colours, dark parts whole-BEAT = 0.25 # of a leg's ordinary step: a step of the legs this far off the same step as filmed is off the walk's beat---def _legs(frames: list[Image.Image]) -> dict[str, np.ndarray]:- """Per step of the loop as it plays (cyclic), how far each leg and the feet move: the centre of their colour, px."""- centres = []- for f in frames:- p = np.asarray(f)- at = {}- for part, colour in LEGS.items():- ys, xs = np.nonzero((p[..., 3] > 0) & np.all(p[..., :3] == colour, axis=-1))- at[part] = np.array([xs.mean(), ys.mean()])- centres.append(at)- n = len(frames)- return {part: np.array([np.linalg.norm(centres[(k + 1) % n][part] - centres[k][part]) for k in range(n)]) for part in LEGS}+ _kept_rest(frames, flow_only, made, round_)+ assert len({k for k in round_["frames"] if made[k] is not frames[k]}) >= len(round_["frames"]) - 2+ # and the legs off their beat there, read on their colours; the round reads it on the rest of the body, step by step+ assert _legs_off(frames, made) > BEAT and round_["beat"]["off_max"] > repair.EASE_BEAT+ assert len(round_["beat"]["off"]) == len(segment) - 1 and round_["beat"]["bound"] == repair.EASE_BEAT+ # delivered: the loop as the flow repair left it, the legs on their steps as filmed, the rush still named+ assert [f.tobytes() for f in out] == [f.tobytes() for f in flow_only] and "replaced" not in round_+ assert set(record["replaced"]) == flow_frames and record["surge_after"] is not None+ assert _legs_off(frames, out) <= BEAT@pytest.mark.parametrize("n, lag, hold", [(WRAP, LAG, 1), (SHORT, SHORT_LAG, 1), (14, 0.15, 1), (48, LAG, 2)])@@ -179,30 +216,48 @@ def test_whatever_spreads_a_rush_the_legs_keep_their_own_beat(n, lag, hold):assert off.max() <= BEAT, f"{name}, {part}: step {int(off.argmax())} is {off.max():.2f} of an ordinary step off"-def test_a_rush_the_flow_reading_lets_pass_is_eased_too():+def test_a_rush_the_flow_reading_lets_pass_is_read_too_and_refused_for_the_legs_beat():# A tail 15 % of a cycle short over 16 frames reads under the flow's limit: no frame is taken out, and it still rushes.+ # 2.45.0 took its stretch; the frames made spread the rush and stop the legs where the tail is given its time.frames = _walk(SHORT, lag=SHORT_LAG)assert repair.flow_targets(frames)["groups"] == []flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=SHORT_LAG, n=SHORT), ease=False)- out, record = repair.repair_jumps(frames, Lerp(frames, lag=SHORT_LAG, n=SHORT))+ lerp = Lerp(frames, lag=SHORT_LAG, n=SHORT)+ out, record = repair.repair_jumps(frames, lerp)round_ = _ease(record)- assert round_["outcome"] == "accepted" and round_["supersedes"] == []- assert record["surge"]["ratio"] >= repair.SURGE_RATIO and record["surge_after"] is None- assert len(out) == SHORT and all(out[k] is frames[k] for k in range(SHORT) if k not in round_["frames"])- _kept_rest(frames, flow_only, out, round_)+ assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the body off its beat"] and round_["supersedes"] == []+ assert record["surge"]["ratio"] >= repair.SURGE_RATIO and record["surge_after"] is not None+ made = _made(flow_only, frames, round_, lerp.made)+ assert repair.surges(made, box=repair.union_box(frames)) == [] and _legs_off(frames, made) > BEAT+ _kept_rest(frames, flow_only, made, round_)+ assert all(o is f for o, f in zip(out, frames)) and record["replaced"] == []+++def test_with_the_beat_bound_lifted_a_stretch_that_keeps_every_other_reading_is_played_as_made(monkeypatch):+ # The delivery itself. Lifted, the beat refuses nothing: the same stretch is taken, every frame of it shown as it was+ # made, every frame outside it the video's own, and no rush is left.+ monkeypatch.setattr(repair, "EASE_BEAT", math.inf)+ frames = _walk(SHORT, lag=SHORT_LAG)+ lerp = Lerp(frames, lag=SHORT_LAG, n=SHORT)+ out, record = repair.repair_jumps(frames, lerp)+ round_ = _ease(record)+ assert round_["outcome"] == "accepted" and record["surge_after"] is None+ assert all(o is m for o, m in zip(out, _made(frames, frames, round_, lerp.made)))+ assert all(out[k] is frames[k] for k in range(SHORT) if k not in round_["frames"])+ assert record["replaced"] == round_["replaced"] and round_["replaced"]def test_a_stretch_whose_frames_made_hurry_the_rest_of_the_body_is_refused():# Over 36 frames, easing a tail a tenth of a cycle short gives the rush more time and the steps around it less; the# search reads the rest of the body on the line between the frames as filmed and finds times that keep it, and the# frames made, which go the body's own way between them, step further there (a validator's counterexample on 9dd5050:- # the arm's window 1.736 to 2.030, the stretch taken). Read on the frames made, the stretch is refused for that alone,- # and the loop stays as the flow left it.+ # the arm's window 1.736 to 2.030, the stretch taken). Read on the frames made, the stretch is refused for that, and+ # for the legs' beat that the same frames made break; the loop stays as the flow left it.frames = _walk(lag=0.1)flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=0.1), ease=False)out, record = repair.repair_jumps(frames, Lerp(frames, lag=0.1))round_ = _ease(record)- assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the rest faster"]+ assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the rest faster", "the body off its beat"]faster = [i for i, w in enumerate(round_["rest"]["windows"]) if w in round_["rest"]["faster"]]assert faster and all(round_["rest"]["step_max_after"][i] > round_["rest"]["step_max_before"][i] for i in faster)assert [f.tobytes() for f in out] == [f.tobytes() for f in flow_only] and record["replaced"] == []@@ -222,18 +277,21 @@ def test_a_frame_flung_against_the_way_is_not_a_rush():assert repair.surges(_loop(flung={12: 12})) == [] and repair.surges(_walk(flung={12: 12})) == []-def test_on_twos_each_drawing_is_made_once_and_shown_for_both_its_frames():+def test_on_twos_each_drawing_is_made_once_for_both_its_frames_and_the_stretch_refused_for_the_legs_beat():+ # 2.45.0 took this stretch; read on its drawings, the frames made stop the legs where the tail is given its time.n = 48 # 24 drawings on twosframes = _walk(n, lag=LAG, hold=2)flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=LAG, hold=2, n=n), ease=False)lerp = Lerp(frames, lag=LAG, hold=2, n=n)out, record = repair.repair_jumps(frames, lerp)round_ = _ease(record)- assert record["hold"] == 2 and round_["outcome"] == "accepted"- assert all(len(d) == 2 and out[d[0]] is out[d[1]] for d in round_["drawings"])+ assert record["hold"] == 2 and round_["outcome"] == "rejected" and round_["rejected_for"] == ["the body off its beat"]+ made = _made(flow_only, frames, round_, lerp.made)+ assert all(len(d) == 2 and made[d[0]] is made[d[1]] for d in round_["drawings"])assert len(lerp.calls) <= len(round_["drawings"]) + len(record["rounds"][0]["drawings"])- assert len(out) == n and all(out[k] is frames[k] for k in range(n) if k not in round_["frames"])- _kept_rest(frames, flow_only, out, round_)+ assert _legs_off(frames, made) > BEAT+ _kept_rest(frames, flow_only, made, round_)+ assert [f.tobytes() for f in out] == [f.tobytes() for f in flow_only]def test_a_made_frame_that_moves_the_body_further_than_the_stretch_did_refuses_the_ease():@@ -285,7 +343,8 @@ def test_without_an_interpolator_a_rush_is_named_and_nothing_is_made():assert all(o is f for o, f in zip(out, frames)) and record["replaced"] == []-def test_it_eases_the_same_frames_whichever_way_the_body_faces():+def test_it_reads_the_same_stretch_and_refuses_it_alike_whichever_way_the_body_faces():+ # 2.45.0 took the same stretch either way; either way the frames made stop the legs where the tail is given its time.frames = _walk(SHORT, lag=SHORT_LAG)mirrored = [ImageOps.mirror(f) for f in frames]lerp = Lerp(frames, lag=SHORT_LAG, n=SHORT)@@ -296,8 +355,11 @@ def test_it_eases_the_same_frames_whichever_way_the_body_faces():_, record = repair.repair_jumps(frames, Lerp(frames, lag=SHORT_LAG, n=SHORT))_, flipped = repair.repair_jumps(mirrored, mirror_lerp)- assert _ease(flipped)["frames"] == _ease(record)["frames"] and _ease(flipped)["outcome"] == "accepted"- assert _ease(record)["outcome"] == "accepted"+ assert _ease(flipped)["frames"] == _ease(record)["frames"]+ assert _ease(flipped)["rejected_for"] == _ease(record)["rejected_for"] == ["the body off its beat"]+ # the path's grid drops the box's columns past its last whole block and its windows round down, on one side only:+ # mirrored, the body reads a little apart+ assert _ease(flipped)["beat"]["off"] == pytest.approx(_ease(record)["beat"]["off"], abs=0.05)# --- the search (repair.ease_times) -------------------------------------------------------------------------------@@ -351,21 +413,25 @@ def _cut(tmp_path, frames: list[Image.Image], **kw):start=0, length=len(frames), anchor="none", **kw)-def test_video_loop_eases_the_rush_and_plays_the_frames_made(tmp_path, capsys):+def test_video_loop_reads_the_rush_refuses_the_stretch_for_the_legs_beat_and_plays_the_frames_as_cut(tmp_path, capsys):+ # 2.45.0 played this stretch's frames made; their legs stop where the tail is given its time. The stretch is made and+ # recorded, and the cells are the frames as cut.frames = _walk(SHORT, lag=SHORT_LAG)lerp = NearestLerp(frames, lag=SHORT_LAG, n=SHORT)rep = _cut(tmp_path, frames, interpolate=lerp)jr = rep["jump_repair"]round_ = _ease(jr)- assert round_["outcome"] == "accepted" and jr["applied"] is True and jr["surge_after"] is None- assert set(round_["replaced"]) <= set(jr["replaced"]) and rep["seam_measurement"] == "rendered-cells"+ assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the body off its beat"]+ assert round_["calls"] == len(lerp.made) > 0 and jr["ease_calls"] == round_["calls"]+ assert jr["applied"] is False and jr["replaced"] == [] and jr["surge_after"] is not None+ assert rep["seam_measurement"] == "source-frames" # no frame made is playedcells = sorted((tmp_path / "out" / "cycle").glob("frame-*.png"))assert len(cells) == SHORT # the loop keeps its length- made = {k: m for m in round_["made"] if m["proposal"] is not None for k in m["frames"]}- assert made and len(made) == len(lerp.made)- for k, image in zip(sorted(made, key=round_["frames"].index), lerp.made):- assert np.array_equal(np.asarray(Image.open(cells[k])), np.asarray(image))- assert json.loads((tmp_path / "w.json").read_text())["jump_repair"]["rounds"][-1]["why"] == "ease"+ cut = [Image.open(p).convert("RGBA") for p in sorted((tmp_path / "keyed").glob("*.png"))][:SHORT]+ for cell, frame in zip(cells, cut):+ assert np.array_equal(np.asarray(Image.open(cell)), np.asarray(frame))+ written = json.loads((tmp_path / "w.json").read_text())["jump_repair"]["rounds"][-1]+ assert written["why"] == "ease" and written["rejected_for"] == ["the body off its beat"]def test_a_rush_without_rife_is_cut_as_filmed_with_a_warning(tmp_path, monkeypatch, capsys):@@ -385,16 +451,20 @@ def test_repair_on_without_rife_refuses_a_rush(tmp_path, monkeypatch):_cut(tmp_path, _walk(lag=0.1), repair="on")-def test_a_stretch_never_leaves_more_than_half_the_loop_made(monkeypatch):+def test_a_stretch_never_takes_more_than_half_the_loop_and_is_refused_for_the_legs_beat(monkeypatch):# Fourteen frames: four drawings a side would remake eight, more than the seven video-loop-compare reads back as the- # repair's; three a side remakes six.+ # repair's; three a side remakes six. 2.45.0 took the six; their legs stop where the tail is given its time.frames = _walk(14, lag=0.15)monkeypatch.setattr(repair, "MADE_SHARE", 1.0)_, unbound = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14))assert _ease(unbound)["width"] == 4 and len(_ease(unbound)["frames"]) == 8monkeypatch.setattr(repair, "MADE_SHARE", 0.5)flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14), ease=False)- out, record = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14))+ lerp = Lerp(frames, lag=0.15, n=14)+ out, record = repair.repair_jumps(frames, lerp)round_ = _ease(record)- assert round_["outcome"] == "accepted" and round_["width"] == 3 and len(record["replaced"]) <= 14 // 2- _kept_rest(frames, flow_only, out, round_)+ assert round_["width"] == 3 and len(set(record["replaced"]) | set(round_["frames"])) <= 14 // 2+ assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the body off its beat"]+ made = _made(flow_only, frames, round_, lerp.made)+ _kept_rest(frames, flow_only, made, round_)+ assert _legs_off(frames, made) > BEAT and [f.tobytes() for f in out] == [f.tobytes() for f in flow_only]