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-sway): --read-on-cut reads each cut by its record of its cells alone: video-loop writes cell_record in the strip record, bound to the strip PNG's sha256 (each cell's source frame, where its anchor moved it, the factor each hold scaled it by, and whether it is as filmed by its pixels against the receipt taken before any repair, now written at --anchor motion and motion-auto too); the first alignment keeps the cut's strip record beside its frames as filmed; a cut with no cell record, a record of another strip or not as video-loop writes it, a paired cell scaled or not as filmed, and two paired cells not the same pixels at the shift are refused no-match

Soohong Kim0920e7259e5f
diff --git a/sprite_gen/video/align.py b/sprite_gen/video/align.py
index 1c085bf..0392614 100644
--- a/sprite_gen/video/align.py
+++ b/sprite_gen/video/align.py
@@ -41,7 +41,10 @@ of `cycle.source/` (`{"source": i}`, as many times as it is taken) or a frame ma
`follow.cut_origin`: its sha256, cell size, scale and crop origin, its cells, and the body's motion as
`video-follow` reads it there), and every later one keeps it: regions somebody located on the cut's
first cell are carried to the aligned loop by these two (`video-follow --read-on`), not read again on a
-first cell that is another. Neither changes a pixel of the strip, the GIF or the WebP. A loop never aligned whose
+first cell that is another. The first also keeps the cut's own strip record beside its frames as filmed
+(`cycle.source/<name>.strip.json`, the record of each of its cells with it: `loop.CELL_RECORD`), which the
+aligned strip does not carry over, as it is another strip; `video-sway --read-on-cut` opens it there.
+Neither changes a pixel of the strip, the GIF or the WebP. A loop never aligned whose
strip as cut is not there (`cut_strip`) is refused by name before any loop of the set is touched: no later
alignment could record its origin.
@@ -1025,8 +1028,11 @@ def _rebuild(loop_dir: Path, meta_path: Path, meta: dict[str, Any], frames: list
record: dict[str, Any], *, cells_from: list[dict[str, Any]] | None = None,
origin: dict[str, Any] | None = None) -> dict[str, Any]:
"""Write the aligned cycle, strip, GIF and WebP over the loop's own, at the loop's own cell size rules. `cells_from`
- says where each of `frames` is from; the record keeps it per cell of the strip, with the cut's `origin`."""
+ says where each of `frames` is from; the record keeps it per cell of the strip, with the cut's `origin`. On a loop
+ never aligned, the cut's own strip record is kept beside its frames as filmed first (`cycle.source/`), as it was."""
name = meta_path.name[: -len(".strip.json")]
+ if "cycle_align" not in meta:
+ shutil.copyfile(meta_path, loop_dir / SOURCE_DIR / meta_path.name)
cycle_dir = loop_dir / "cycle"
cycle_dir.mkdir(exist_ok=True)
for old in cycle_dir.glob("frame-*.png"):
@@ -1045,7 +1051,9 @@ def _rebuild(loop_dir: Path, meta_path: Path, meta: dict[str, Any], frames: list
record["follow_cleared"] = True
(loop_dir / loop_mod.FOLLOW_SOURCE).unlink(missing_ok=True)
# a loop one cycle of which was taken out (`--cycles` k > 1) no longer holds the steps it was counted with
- merged = {**{k: v for k, v in meta.items() if k not in strip_meta and k not in ("cycle_align", *loop_mod.SWAY_RECORD_KEYS)
+ # and the record of the cut's cells is the cut's strip's, not this one's
+ merged = {**{k: v for k, v in meta.items() if k not in strip_meta
+ and k not in ("cycle_align", loop_mod.CELL_RECORD, *loop_mod.SWAY_RECORD_KEYS)
and not (k == "steps" and "cycle_taken" in record)}, **strip_meta}
if meta.get("foot_anchor") and meta.get("foot_anchor") != "feet":
merged["foot_anchor"] = meta["foot_anchor"]
diff --git a/sprite_gen/video/loop.py b/sprite_gen/video/loop.py
index fbb4e79..603990d 100644
--- a/sprite_gen/video/loop.py
+++ b/sprite_gen/video/loop.py
@@ -155,6 +155,18 @@ FOLLOW_SOURCE = "follow.source.png"
CYCLE_REFUSED_DIR = "cycle.refused"
SWAY_RECORD = "follow"
SWAY_RECORD_KEYS = ("sway", "follow")
+# The strip's record of each of its cells, in its `<name>.strip.json` and bound to it by its PNG's sha256 (`strip_sha256`):
+# per cell the source frame it is, where the anchor moved that frame before the crop (`anchor_move_px`, across and down),
+# the factor each step that holds a clip's frames to one size scaled it by about its feet before the cut (`scale`; 1
+# where the step did not hold them), and whether it is that frame as filmed but for that move (`as_filmed`: its pixels at
+# the end of the cut are its pixels anchored, before any repair, and nothing held the clip's frames). `video-sway
+# --read-on-cut` pairs two cuts of one clip by it alone. An alignment does not carry it over to the strip it makes; it
+# keeps the cut's own strip record beside the cut's frames (`cycle.source/<name>.strip.json`).
+CELL_RECORD = "cell_record"
+CELL_SCALE_STEPS = {"size_hold": "the size hold", "gait_fallback": "the gait fallback"} # each, in words
+# How the frames of a cut at --anchor motion or motion-auto are made before any repair, as their receipt names it
+# (`source_projection.recipe`; a cut at --anchor none or body is made by `source.RECIPE`): laid by the correction.
+MOTION_RECIPE = "motion-anchor-correction-v1"
def sway_recorded(meta: dict) -> bool:
@@ -1736,11 +1748,13 @@ def _counted(cycle: dict[str, Any], steps: int, *, window: CutWindow, D: np.ndar
def _motion_auto(frames: list[Image.Image], files: list[Path], D: np.ndarray, trajectory: np.ndarray, analysis: dict[str, Any],
*, fps: float, window: CutWindow, prof: LoopProfile, steps: int | None, seam_max: float,
seam_pop_max: float | None, scratch: _Scratch, said: dict[str, Any],
- turn: body_turn_mod.Turn | None = None) -> tuple[dict[str, Any], list[Path], dict[str, Any]]:
+ turn: body_turn_mod.Turn | None = None
+ ) -> tuple[dict[str, Any], list[Path], dict[str, Any], list[float] | None]:
"""`--anchor motion-auto`'s cut of `frames` — the clip after its lead-in, held, `files` written of them and `D`,
`trajectory` and `analysis` read on them (auto_motion.analyse): the search, the gait fallback after a first search
that finds nothing, then the count, given (`steps`, `_counted`) or the clip's crossings (`_by_steps`). Returns the
- cut, and the files and the analysis it was read on (the gait fallback's, where it scaled the frames back); `said`
+ cut, the files and the analysis it was read on (the gait fallback's, where it scaled the frames back), and the factor
+ the gait fallback scaled each frame by (None where it did not); `said`
takes what the report adds (`automatic_motion_analysis` read again, `gait_fallback`), whether a cut is found or the
search raises. With `turn` (body_turn.Turn), no cut that holds a frame of a body turn is taken."""
lo, hi = window.lo, window.hi
@@ -1749,6 +1763,7 @@ def _motion_auto(frames: list[Image.Image], files: list[Path], D: np.ndarray, tr
searched: dict[str, Any] = {"min_len": lo, "max_len": hi}
refused: ValueError | None = None
cycle = None
+ undone: list[float] | None = None
# The cuts the search chooses among, read before one is cut as the seam gate and the seam pop gate read the loop.
cells = CutOnCells(frames, pop_max=seam_pop_max)
try:
@@ -1765,6 +1780,7 @@ def _motion_auto(frames: list[Image.Image], files: list[Path], D: np.ndarray, tr
if abs(drift["drift"]) >= gait_fallback.SCALE_DRIFT_MIN:
fallback["padding_ltrb"] = list(gait_fallback.undo_padding(frames, drift))
frames = gait_fallback.undo_scale(frames, drift, pad=tuple(fallback["padding_ltrb"]))
+ undone = [gait_fallback.frame_hold(k, drift)["scale"] for k in range(len(frames))]
files = gait_fallback.write_frames(frames, [f.name for f in files], scratch.new())
D, trajectory, analysis = auto_motion.analyse(frames, fps=fps)
said["automatic_motion_analysis"] = analysis
@@ -1799,7 +1815,7 @@ def _motion_auto(frames: list[Image.Image], files: list[Path], D: np.ndarray, tr
else:
cycle = _by_steps(cycle, refused, frames=frames, analysis=analysis, D=D, trajectory=trajectory,
window=window, searched=searched, detect=detect, seam_max=seam_max, cells=cells, turn=turn)
- return cycle, files, analysis
+ return cycle, files, analysis, undone
def _from_lead_in(record: Any, lead: int) -> Any:
@@ -1853,12 +1869,13 @@ def _blend_wrap(frames: list[Image.Image], pin_file: Path, *, mode: str, anchor:
return wrap_blend_mod.apply(frames, pin, fps=fps, seam_max=seam_max)
-def _size_hold(frames: list[Image.Image], *, lo: int, hi: int,
- cut: tuple[int, int] | None = None) -> tuple[list[Image.Image] | None, dict[str, Any], Any]:
+def _size_hold(frames: list[Image.Image], *, lo: int, hi: int, cut: tuple[int, int] | None = None
+ ) -> tuple[list[Image.Image] | None, dict[str, Any], Any, list[float] | None]:
"""A walk or run held at its first frame's size, and its feet on the first frame's row, before its cut is chosen
(`--size-hold auto`): the frames held (None: the clip changes size by less than SIZE_HOLD_MIN, read one cycle on,
- and is cut as filmed), the report's `size_hold`, and where a box of a frame as filmed lands in its held frame
- (`gait_fallback.held_box`, None when not held).
+ and is cut as filmed), the report's `size_hold`, where a box of a frame as filmed lands in its held frame
+ (`gait_fallback.held_box`), and the factor each frame was scaled by (`gait_fallback.frame_hold`); the last two None
+ when not held.
A clip filmed from its first frame only grows or shrinks as it plays, so the frame one cycle on is not the size of
the first and the loop pops at the wrap; a walk away from the camera climbs the frame as well, and held at its size
@@ -1873,14 +1890,14 @@ def _size_hold(frames: list[Image.Image], *, lo: int, hi: int,
**{k: measured[k] for k in ("height_first_px", "height_last_px", "drift", "method",
"lag", "pairs", "per_lag", "pose_match", "drift_fitted_line")}}
if abs(measured["drift"]) < gait_fallback.SIZE_HOLD_MIN:
- return None, hold, None
+ return None, hold, None, None
feet = gait_fallback.foot_drift(frames, measured)
if cut is not None:
seen = hold["at_cut"] = gait_fallback.at_cut(frames, measured, feet, start=cut[0], length=cut[1])
if not seen["size"]:
hold["why"] = ("the cut does not change size a cycle of its length apart as the clip reads it (frames that are "
"not the walk, or a clip too short to show its cycle twice): cut as filmed")
- return None, hold, None
+ return None, hold, None, None
if feet["held"] and not seen["feet"]:
feet = {**feet, "held": False, "lift": None,
"why": "the cut's feet do not move a cycle of its length apart as the clip reads them"}
@@ -1892,7 +1909,8 @@ def _size_hold(frames: list[Image.Image], *, lo: int, hi: int,
# The move each frame of the cut was held by, exact (`gait_fallback.frame_hold`): `video-loop-repair` holds the
# cut's source frames by it again before it reads them (sprite_gen/video/source.py).
hold["cut_frames"] = [gait_fallback.frame_hold(k, measured, lift) for k in range(cut[0], cut[0] + cut[1])]
- return held, hold, partial(gait_fallback.held_box, measured=measured, pad=pad, lift=lift)
+ return (held, hold, partial(gait_fallback.held_box, measured=measured, pad=pad, lift=lift),
+ [gait_fallback.frame_hold(k, measured, lift)["scale"] for k in range(len(frames))])
class _Scratch:
@@ -1926,6 +1944,29 @@ def _refuse_overlap(frames_dir: Path, files: list[Path], out_dir: Path, name: st
refuse_overlap({"--frames-dir": frames_dir, **{f"{f.name} in --frames-dir": f for f in files}}, outputs, verb="video-loop")
+def _cell_record(strip_bytes: bytes, cells: list[Image.Image], *, start: int, length: int, samples: list[int],
+ anchored: list[str], wrap_dx: int, motion: dict[str, Any] | None,
+ scales: dict[str, list[float]]) -> dict[str, Any]:
+ """The strip's record of its cells (`CELL_RECORD`), bound to the strip by its PNG's sha256: for each cell, the frame of
+ the cut it shows at the end of the cut (`cells`, frame `samples[c]` of a cut `length` frames long from clip frame
+ `start`), the source frame it is, where the anchor moved that frame before the crop (the body anchor's ramp,
+ `ramp_offset` of `wrap_dx`; the motion correction's place, `motion_anchor.laid_at`; nothing at --anchor none or feet),
+ the factor each step that held the clip's frames to one size scaled it by (`scales`, per clip frame; 1 where none did),
+ and whether it is that frame as filmed but for the anchor's move: its pixels are its pixels anchored, before any
+ repair (`anchored`, the cut's receipt), and no step held the clip's frames. What made a cell otherwise — a repair, a
+ blend at the wrap, a hold — is not listed: the cell's pixels say it."""
+ def moved(k: int) -> list[int]:
+ if motion is not None:
+ return list(motion_anchor.laid_at(motion, k))
+ return [ramp_offset(wrap_dx, k, length), 0]
+
+ return {"strip_sha256": evidence_mod.digest(strip_bytes), "cells": [
+ {"source_frame": start + k, "anchor_move_px": moved(k),
+ "scale": {step: scales[step][start + k] if step in scales else 1.0 for step in CELL_SCALE_STEPS},
+ "as_filmed": not scales and evidence_mod.digest(cell.tobytes()) == anchored[k]}
+ for k, cell in zip(samples, cells)]}
+
+
def run_loop(frames_dir: Path, out_dir: Path, **options: Any) -> dict[str, Any]:
"""Cut the loop (`_cut_loop`, which takes the options). The frames a size hold or the gait fallback scales back
are written in folders of the run's own (`_Scratch`), removed however the run ends."""
@@ -2076,15 +2117,19 @@ def _cut_loop(
shifted = False
clip_files, clip_D = files, D
files, D = files[skip:], D[skip:, skip:]
+ # Per step that holds a clip's frames to one size (`CELL_SCALE_STEPS`), the factor it scaled each frame of the clip by,
+ # where it held them (`cell_record`).
+ clip_scales: dict[str, list[float]] = {}
try:
if anchor == "motion-auto":
source_frames = gait_frames[skip:]
if size_hold == "auto":
# Held before the search (`_size_hold`), the cut is chosen on frames of one size whose feet stand on one
# row. The gait fallback below then finds nothing to undo.
- held, report_base["size_hold"], _ = _size_hold(source_frames, lo=lo, hi=hi)
+ held, report_base["size_hold"], _, scales = _size_hold(source_frames, lo=lo, hi=hi)
if held is not None:
source_frames = held
+ clip_scales["size_hold"] = [1.0] * skip + scales
files = gait_fallback.write_frames(held, [f.name for f in files], scratch.new())
else:
report_base["size_hold"] = {"applied": False, "why": "--size-hold off"}
@@ -2130,7 +2175,9 @@ def _cut_loop(
print(f"video-loop: warning: {body_turn_mod.unfound(turning)}", file=sys.stderr)
raise
report_base.update(said)
- cycle, files, analysis = found
+ cycle, files, analysis, undone = found
+ if undone is not None:
+ clip_scales["gait_fallback"] = [1.0] * skip + undone
if turn is not None:
cut = (cycle["start"] + skip, cycle["length"])
holds = turn.held(cycle["start"], cycle["length"])
@@ -2161,9 +2208,10 @@ def _cut_loop(
# A walk or run cut the caller names is held as a search's cut is (`_size_hold`), read over the whole clip,
# and cut on the frames held: the cut's distances, its count and the seam the gate reads are the frames
# that play. The clip is then the clip held (a fixed cut leaves no lead-in out).
- held, report_base["size_hold"], box_of = _size_hold(gait_frames, lo=lo, hi=hi, cut=(start, length))
+ held, report_base["size_hold"], box_of, scales = _size_hold(gait_frames, lo=lo, hi=hi, cut=(start, length))
if held is not None:
gait_frames = held
+ clip_scales["size_hold"] = scales
files = gait_fallback.write_frames(held, [f.name for f in files], scratch.new())
D = distance_matrix(files)
clip_files, clip_D = files, D
@@ -2295,11 +2343,14 @@ def _cut_loop(
refuse(target, out_dir, name, {**report_base, "status": "failed", "error": error, "cycle": cycle})
raise SystemExit(error) from exc
report_base["motion_anchor"] = motion
- if anchor in ("none", "body") and cycle["kind"] != "lifetime":
+ # Each frame of the cut anchored, before any repair, pixel for pixel: its receipt, and what each cell's record reads
+ # whether it is that frame as filmed (`cell_record`).
+ anchored = [evidence_mod.digest(f.tobytes()) for f in frames]
+ if anchor != "feet" and cycle["kind"] != "lifetime":
from sprite_gen.video.source import RECIPE
report_base["source_projection"] = {
- "recipe": RECIPE, "wrap_dx_px": wrap_dx,
- "before_repair_pixels_sha256": [evidence_mod.digest(f.tobytes()) for f in frames],
+ "recipe": MOTION_RECIPE if motion is not None else RECIPE, "wrap_dx_px": wrap_dx,
+ "before_repair_pixels_sha256": anchored,
}
if prof.gait:
# The ghost screen of the cut as filmed (docs/loop-repair.md section 4), read before the jump repair: each
@@ -2551,7 +2602,11 @@ def _cut_loop(
n_out = min(n_out, int(strip_meta["frames"]))
strip_path = out_dir / f"{name}.strip.png"
strip.save(strip_path)
- atomic_write_text(out_dir / f"{name}.strip.json", json.dumps(strip_meta, indent=2) + "\n")
+ cell_record = _cell_record(strip_path.read_bytes(), [frames[k] for k in strip_meta["sample_indices"]], start=i,
+ length=L, samples=strip_meta["sample_indices"], anchored=anchored, wrap_dx=wrap_dx,
+ motion=motion, scales=clip_scales)
+ # The record of its cells is the strip's own: written beside it, not into the report.
+ atomic_write_text(out_dir / f"{name}.strip.json", json.dumps({**strip_meta, CELL_RECORD: cell_record}, indent=2) + "\n")
# resampled GIF/WebP frames: same crop as the strip, n_out evenly across the cycle
single_play = cycle["kind"] in nonloop.KINDS
diff --git a/sprite_gen/video/sway.py b/sprite_gen/video/sway.py
index 5f42516..9279316 100644
--- a/sprite_gen/video/sway.py
+++ b/sprite_gen/video/sway.py
@@ -53,13 +53,15 @@ the cut's cell pixel for pixel (a crop or a scale changed, and nothing records h
carry: refused `uncertain`, never carried by the nearest whole cell. Another cut of this loop's own clip,
named by `--read-on-cut STRIP` with its record beside it, is `recut`: paired by the source frame its cell 0
is, its regions and masks moved onto this loop's cell that is that frame by the whole px shift the two
-crops set it apart by on the frame as filmed — each crop less where its cut's anchor moved that frame, as
-its record gives it (`anchor_move`; refused `no-match` where a record does not) — never scaled
-(`recut_pair`). The frame each cut pairs must be that frame as filmed but for its anchor's move, as its
-cut's record and the report `video-loop` wrote of that cut beside it say (`as_filmed`, `align.cut_report`):
-a cut the size hold or the gait fallback scaled, a frame RIFE made, given way or blended at the wrap, and a
-cut whose report is not beside it or is another cut's are refused `no-match`, saying which. Any other strip
-is refused `no-match`. Without `--read-on` nothing of this is read or written.
+crops set it apart by on the frame as filmed — each crop less where its cut's anchor moved that frame —
+never scaled (`recut_pair`). Each cut is read by its record of its cells alone, as `video-loop` writes it
+in the strip's record, bound to the strip's PNG by its sha256 (`cell_record`): the source frame each cell
+is, where the anchor moved it, the factor the size hold and the gait fallback scaled it by, and whether its
+pixels are that frame as filmed but for that move (on a loop aligned from its cut, the cut's own record as
+the alignment kept it beside the cut's frames). A cut with no such record (one video-loop made before it
+wrote one), a record of another strip, a cell scaled or not as filmed, and two paired cells that are not
+the same pixels where they overlap at the shift (`recut_same`) are refused `no-match`, saying which. Any
+other strip is refused `no-match`. Without `--read-on` nothing of this is read or written.
`--stretch-floor D` asks every region's move to leave each pixel at least D of its area. The weight's
steepest slope is π/2 over each radius, so a move (ox, oy) leaves 1 − (π/2)·hypot(ox/rx, oy/ry) of it —
@@ -192,7 +194,6 @@ from sprite_gen.frames import folder as folder_mod
from sprite_gen.spec.runio import atomic_write_text
from sprite_gen.util.gif_utils import save_clean_gif
from sprite_gen.video import loop as loop_mod
-from sprite_gen.video import motion_anchor
from sprite_gen.util.console import utf8_streams
VERB = "video-sway"
@@ -223,8 +224,9 @@ TRANSPORT_POLICY = "cut-reading/whole-cell/1"
# How regions read on another cut of the same clip are paired with this loop's cut (`--read-on` with `--read-on-cut`): by
# the source frame the cut's cell 0 is, onto the cell of this loop's cut that is that frame, moved by the whole px the two
# crops set that frame's pixels apart, each crop taken on the frame as filmed (less where its cut's anchor moved it); never
-# scaled or grown, and only where that frame is in each cut as filmed, as the cut's report says (`as_filmed`).
-RECUT_POLICY = "same-clip/source-frame/whole-px-shift/1"
+# scaled or grown, only where each cut's record of its cells calls that cell the frame as filmed (`cell_record`), and only
+# where the two cells are the same pixels where they overlap at the shift (`recut_same`).
+RECUT_POLICY = "same-clip/source-frame/whole-px-shift/2"
RECUT_DRIFT = 1.0 # px: the most a pixel of the one cut may lie off the whole shift from the same pixel of the other
# How `--stretch-floor` reads a region's least area: the cos² weight's closed form, by direction, never above the
# fold rule's gain.
@@ -577,207 +579,138 @@ def _clip(cut: dict[str, Any]) -> str:
return str(source.get("sha256", source)) if isinstance(source, dict) else str(source)
-def _cut_frame(record: dict[str, Any], cell: int) -> tuple[int, int]:
- """The frame of its cut a cut's cell `cell` is, and the cut's length, read off its record (`source_cut`). A ValueError
- where the record names no whole start and length of its cut that hold that cell."""
- cut = record["source_cut"]
- start, length = cut.get("start"), cut.get("length")
- k = int(cut["samples"][cell]) - start if type(start) is int else None
- if type(length) is not int or k is None or not 0 <= k < length:
- raise ValueError(f"it names no whole start and length of its cut that hold its cell {cell} (source_cut)")
- return k, length
-
-
-def _report_of(loop_dir: Path, meta_path: Path, meta: dict[str, Any]) -> dict[str, Any] | None:
- """The report `video-loop` wrote of a loop's cut, where `video-cycle-align` reads one (`align.cut_report`: in the
- loop's folder or the one above it); None where none is."""
- from sprite_gen.video import align as align_mod # align imports this module
- found = align_mod.cut_report(loop_dir, meta_path, meta)
- return None if found is None else found[1]
-
-
-def as_filmed(record: dict[str, Any], report: dict[str, Any] | None, cell: int) -> None:
- """Whether the frame a cut's cell `cell` is, is in that cut as filmed but for where its anchor moved it (`anchor_move`),
- as the cut's records say: its record (`record`) and the report `video-loop` wrote of the cut (`report`, beside its
- strip as `align.cut_report` finds one; None where none is), whose strip's `source_cut` is the record's. The record
- says nothing of the size hold, the gait fallback or RIFE; the report says what each did. A cut the size hold or the
- gait fallback held to one size was cut from frames scaled about their feet (`size_hold.applied`,
- `gait_fallback.scale_undone`): its crop is not on the frame as filmed, and no whole px shift undoes a scale. A frame
- the jump repair or the seam bridge made again or gave way (`jump_repair.replaced`), the overlapped wrap made
- (`wrap_overlap.frames`) or the wrap blend redrew toward the first frame's drawing (`wrap_blend.cells`) is not the
- frame its source frame number says. Each step is read as video-loop writes it, never guessed: the size hold on every
- cut at --anchor motion-auto and every walk or run cut it is given, the wrap blend on every pinned cut and the jump
- repair on every cut, whether they ran or not (`_always_written`, as the report's strip says how its cut was cut); the
- gait fallback and the overlapped wrap only where they ran or were asked. A step that did not run says so: `applied`
- false and no frame named, or no frame in the list. A step not in the report where video-loop writes it, a record or a
- field of one missing or not of its kind, and an `applied` that is not whether the list names a frame are what the
- records do not give. A ValueError says which, or what the records do not give."""
- if report is None:
- raise ValueError("no report video-loop wrote of its cut is beside its strip (in its folder or the one above it), and "
- "its record does not say whether the size hold, the gait fallback or RIFE changed its frames")
- strip = report.get("strip") if isinstance(report.get("strip"), dict) else {}
- if strip.get("source_cut") != record.get("source_cut"):
- raise ValueError("the report beside its strip is another cut's: its strip's source_cut is not the record's")
- k, _ = _cut_frame(record, cell)
-
- def step_record(key: str, step: str) -> dict[str, Any] | None:
- """The report's record of a step (`key`); None where it holds none and video-loop writes one only where the step
- ran or was asked."""
- if key not in report:
- where = _always_written(strip, key, step)
- if where is not None:
- raise ValueError(f"its report holds no record of {step} ({key}), which video-loop writes of {where}, whether "
- "it ran or not")
- return None
- if not isinstance(report[key], dict):
- raise ValueError(f"its report does not say what {step} did to its frames: its record of it is not one ({key})")
- return report[key]
-
- for key, said, step in (("size_hold", "applied", "the size hold"), ("gait_fallback", "scale_undone", "the gait fallback")):
- done = step_record(key, step)
- if done is None:
- continue
- if type(done.get(said)) is not bool:
- raise ValueError(f"its report does not say whether {step} held its frames to one size ({key}.{said})")
- if done[said]:
- raise ValueError(f"{step} held its frames to one size, scaled about their feet, before it was cut ({key}): its "
- "crop is not on the frame as filmed, and no whole px shift undoes a scale")
- repair = report.get("jump_repair")
- if not isinstance(repair, dict):
- raise ValueError("its report does not say what the jump repair made of its frames (jump_repair)")
- bridged = repair.get("reading") == "seam"
- made = (("jump_repair", "replaced", "the seam bridge" if bridged else "the jump repair",
- "made again by RIFE round the wrap (the seam bridge)" if bridged else
- "made again by RIFE, or given way to the frame beside it for a filmed ghost (the jump repair)"),
- ("wrap_overlap", "frames", "the overlapped wrap",
- "made again beside the wrap from the clip's frames a cycle apart (the overlapped wrap)"),
- ("wrap_blend", "cells", "the wrap blend",
- "redrawn toward the first frame's drawing before the wrap (the wrap blend)"))
- for key, field, step, how in made:
- done = repair if key == "jump_repair" else step_record(key, step)
- if done is None:
- continue
- says = key != "wrap_overlap" # the overlapped wrap's record names the frames it made, not whether it made one
- applied = done.get("applied") if says else None
- if says and type(applied) is not bool:
- raise ValueError(f"its report does not say whether {step} changed a frame of its cut ({key}.applied)")
- if applied is False and field not in done:
- continue # what video-loop writes of the step where it did not run
- frames = done.get(field)
- if not isinstance(frames, list) or any(type(f) is not int for f in frames):
- raise ValueError(f"its report does not name the frames of its cut it changed as whole frames ({key}.{field})")
- if says and applied != bool(frames):
- raise ValueError(f"its report says {step} changed {'a frame' if applied else 'no frame'} of its cut "
- f"({key}.applied) and names {'none' if applied else 'frames it changed'} ({key}.{field})")
- if k in frames:
- raise ValueError(f"its cell {cell} is frame {k} of its cut, {how} ({key}.{field})")
-
-
-def _always_written(strip: dict[str, Any], key: str, step: str) -> str | None:
- """The cuts video-loop writes its record of a step (`key`) of in their report whatever the step did, where the cut the
- report's strip is (`strip`) is one of them (`run_loop`): every cut at --anchor motion-auto and every walk or run cut it
- is given (--cycle fixed), for the size hold; every pinned cut, for the wrap blend. None where the cut is not, and for
- the steps video-loop writes only where they ran or were asked (the gait fallback, the overlapped wrap). A ValueError
- where the strip does not say what tells (its anchor, its kind, the state given)."""
-
- def cut_as(field: str) -> str | None:
- value = strip.get(field)
- if not isinstance(value, str) and not (field == "state" and value is None): # a cut given no state names none
- raise ValueError(f"its report holds no record of {step} ({key}), and its strip does not say how its cut was cut, "
- f"which tells whether video-loop writes one of it (strip.{field})")
- return value
-
- if key == "wrap_blend":
- return "every pinned cut" if cut_as("kind") == "pinned" else None
- if key != "size_hold":
+def cell_record(record: dict[str, Any], strip_sha256: str) -> list[dict[str, Any]]:
+ """Each cell of a cut's strip as `video-loop` records it in the strip's record (`record`; `loop.CELL_RECORD`): the
+ source frame it is, where its cut's anchor moved that frame before the crop, the factor each step that holds a clip's
+ frames to one size scaled it by, and whether it is that frame as filmed but for that move. Read only where the
+ record is of the strip whose PNG is `strip_sha256`. A ValueError says what it does not give: none (a cut made before
+ video-loop wrote one), another strip's, or a field of it missing or not of its kind, naming it."""
+ key = loop_mod.CELL_RECORD
+ if key not in record:
+ raise ValueError(f"it holds no record of them ({key}), as a cut video-loop made before it wrote one does")
+ said = record[key]
+ if not isinstance(said, dict):
+ raise ValueError(f"its record of them is not one ({key})")
+ if said.get("strip_sha256") != strip_sha256:
+ raise ValueError(f"its record of them is another strip's ({key}.strip_sha256 {said.get('strip_sha256')}; the strip is "
+ f"{strip_sha256})")
+ cells, n = said.get("cells"), record.get("frames")
+ if not isinstance(cells, list) or len(cells) != n:
+ raise ValueError(f"its record of them names {len(cells) if isinstance(cells, list) else 'no'} cells, and the strip "
+ f"has {n} ({key}.cells)")
+
+ def number(v: Any) -> bool:
+ return type(v) in (int, float) and math.isfinite(v) and v > 0
+
+ for c, cell in enumerate(cells):
+ at = f"{key}.cells[{c}]"
+ if not isinstance(cell, dict):
+ raise ValueError(f"its record of cell {c} is not one ({at})")
+ move, scale = cell.get("anchor_move_px"), cell.get("scale")
+ if type(cell.get("source_frame")) is not int:
+ raise ValueError(f"its record of cell {c} names no whole source frame ({at}.source_frame)")
+ if not (isinstance(move, list) and len(move) == 2 and all(type(v) is int for v in move)):
+ raise ValueError(f"its record of cell {c} gives no whole move of its anchor ({at}.anchor_move_px)")
+ if not (isinstance(scale, dict) and all(number(scale.get(step)) for step in loop_mod.CELL_SCALE_STEPS)):
+ raise ValueError(f"its record of cell {c} does not give the factor each hold scaled it by ({at}.scale: "
+ f"{', '.join(loop_mod.CELL_SCALE_STEPS)})")
+ if type(cell.get("as_filmed")) is not bool:
+ raise ValueError(f"its record of cell {c} does not say whether it is that frame as filmed ({at}.as_filmed)")
+ return cells
+
+
+def _not_as_filmed(cell: dict[str, Any], c: int) -> str | None:
+ """Why cell `c`, as its cut's record gives it (`cell_record`), is not its source frame as filmed but for its anchor's
+ move: a step scaled it, or video-loop did not cut it as filmed. None where it is."""
+ at = f"{loop_mod.CELL_RECORD}.cells[{c}]"
+ for step, words in loop_mod.CELL_SCALE_STEPS.items():
+ if cell["scale"][step] != 1:
+ return (f"{words} scaled it by {cell['scale'][step]:g} about its feet before the cut ({at}.scale.{step}), and "
+ "no whole px shift undoes a scale")
+ if not cell["as_filmed"]:
+ return (f"video-loop did not cut it as filmed ({at}.as_filmed): its pixels at the end of the cut are not its pixels "
+ "anchored, before any repair, or a step held the clip's frames to one size")
+ return None
+
+
+def aligned_from(loop_dir: Path, meta_path: Path, meta: dict[str, Any], verb: str = VERB) -> dict[str, Any] | None:
+ """On a loop aligned from its cut (`cycle_align`), the cut's own strip record, as the first alignment kept it beside
+ the cut's frames as filmed (`cycle.source/<name>.strip.json`, `align._rebuild`); None on a loop never aligned, and
+ where none is kept."""
+ if (meta.get("cycle_align") or {}).get("origin") is None:
+ return None
+ path = loop_dir / loop_mod.CYCLE_SOURCE_DIR / meta_path.name
+ if not path.is_file():
return None
- if cut_as("foot_anchor") == "motion-auto":
- return "every cut at --anchor motion-auto"
- state = cut_as("kind") == "fixed" and cut_as("state")
- return f"every {state} cut it is given (--cycle fixed)" if state and loop_mod.profile_for(state).gait else None
-
-
-def anchor_move(record: dict[str, Any], cell: int) -> tuple[int, int]:
- """Where the anchor of a cut moved the frame its cell `cell` is before the cut was cropped, across and down in px of
- the frame as filmed, read off the cut's record as `video-loop` writes one (`record`; `foot_anchor`, `source_cut`):
- at --anchor none nothing; at --anchor body the ramp's move of that frame of the cut (`loop.ramp_offset` of
- `wrap_dx_px`); at --anchor motion and motion-auto where the correction laid it on the canvas it gives every frame
- (`motion_anchor.laid_at`). A ValueError says what the record does not give, where it does not give it whole."""
- anchor = record.get("foot_anchor")
- if anchor == "none":
- return 0, 0
- if anchor not in ("body", "motion", "motion-auto"):
- raise ValueError("it does not say how its frames were anchored (foot_anchor)" if anchor is None else
- f"its anchor (--anchor {anchor}) keeps no move of each frame")
- k, length = _cut_frame(record, cell) # the frame of the cut the cell is
- if anchor == "body":
- wrap = record.get("wrap_dx_px")
- if type(wrap) is not int:
- raise ValueError("it was cut at --anchor body and records no whole wrap offset (wrap_dx_px)")
- return loop_mod.ramp_offset(wrap, k, length), 0
- motion = record.get("motion_anchor") or {}
- shifts, padding = motion.get("shifts_xy"), motion.get("padding_ltrb")
- if not (isinstance(shifts, list) and len(shifts) == length and isinstance(shifts[k], list) and len(shifts[k]) == 2
- and isinstance(padding, list) and len(padding) == 4 and all(type(v) is int for v in (*padding[:2], *shifts[k]))):
- raise ValueError(f"it was cut at --anchor {anchor} and records no whole shift of its frame {k} of {length} and no "
- "padding (motion_anchor)")
- return motion_anchor.laid_at(motion, k)
-
-
-def recut_pair(meta: dict[str, Any], cut: dict[str, Any], read_on: str, size: tuple[int, int], *,
- reports: tuple[dict[str, Any] | None, dict[str, Any] | None], verb: str = VERB) -> dict[str, Any]:
+ try:
+ kept = json.loads(path.read_text(encoding="utf-8"))
+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:
+ raise SystemExit(f"{verb}: {path}: not a strip record ({exc})") from exc
+ if not isinstance(kept, dict):
+ raise SystemExit(f"{verb}: {path}: not a strip record")
+ return kept
+
+
+def recut_pair(meta: dict[str, Any], cut: dict[str, Any], read_on: str, *, strip_sha256: str,
+ kept: dict[str, Any] | None = None, verb: str = VERB) -> dict[str, Any]:
"""How the cut the regions were read on (`cut`: its record, `--read-on-cut`; `read_on` its strip's sha256) pairs with
- this loop's cut (`meta`; cells `size`) when both are cut from one source clip (`source_cut`): the source frame the
- cut's cell 0 is, the cell of this loop's cut that is that frame, and the whole px across and down a pixel of that
- frame lies from where it lies in the cut — their crops and scales (`source_rect`) taken on the frame as filmed, each
- less where its cut's anchor moved that frame before the crop (`anchor_move`); a pixel lies at most RECUT_DRIFT px
- off that shift anywhere in the cell. `reports` are the reports `video-loop` wrote of the two cuts, the cut's then
- this loop's (None where none is beside its strip). Refused `no-match` where they are not one clip's, the frame is
- not this cut's, it is not in a cut as filmed or its records do not say it is (`as_filmed`), a record does not give
- where its anchor moved the frame, or the strip named is a loop aligned from a cut (its cell 0 is not the frame its
- cut starts on), saying which; `uncertain` where their crops do not set the frame apart by one whole shift."""
- then, now = cut.get("source_cut"), meta.get("source_cut")
+ this loop's cut, both cut from one source clip, by each cut's record of its cells (`cell_record`) and nothing else:
+ the source frame the cut's cell 0 is, the cell of this loop's cut that is that frame, and the whole px across and down
+ a pixel of that frame lies from where it lies in the cut — their crops and scales (`source_rect`) taken on the frame
+ as filmed, each less where its cut's anchor moved that frame before the crop (`anchor_move_px`); a pixel lies at
+ most RECUT_DRIFT px off that shift anywhere in the cell. This loop's cut is its own record (`meta`, of its strip as
+ cut, `strip_sha256`), or on a loop aligned from its cut, the cut's own record the alignment kept (`kept`,
+ `aligned_from`), whose strip the alignment's origin names. Refused `no-match` where the strip named is a loop
+ aligned from a cut, a record of the cells is not there, is another strip's or does not give them as video-loop
+ writes it, the two are not one clip's, this loop's cut does not hold the frame, or either cell is not that frame as
+ filmed (`_not_as_filmed`), saying which; `uncertain` where their crops do not set the frame apart by one whole shift.
+ Whether the two cells are that frame's pixels at the shift is `recut_same`'s."""
def no_match(why: str) -> SystemExit:
return SystemExit(f"{verb}: --read-on {read_on}: no-match — the regions were read on another cut, {why}; locate them "
"again on this strip")
- for whose, record in (("the cut's (--read-on-cut)", then), ("this loop's", now)):
- if not isinstance(record, dict) or not record.get("samples"):
- raise no_match(f"and {whose} record names no source frame its cells are (source_cut)")
if cut.get("cycle_align") is not None:
raise no_match("but the strip --read-on-cut names is a loop aligned from its cut (cycle_align), whose cells are not "
"the cut's frames in order")
- if then.get("source") != now.get("source"):
+ origin = (meta.get("cycle_align") or {}).get("origin")
+ own, own_sha256 = (meta, strip_sha256) if origin is None else (kept, origin["strip_sha256"])
+ cells = []
+ for whose, record, sha in (("the cut's (--read-on-cut)", cut, read_on), ("this loop's", own, own_sha256)):
+ if record is None:
+ raise no_match("and this loop is aligned from its cut (cycle_align), whose strip record is not kept beside its "
+ f"frames as filmed ({loop_mod.CYCLE_SOURCE_DIR}/<name>.strip.json)")
+ try:
+ cells.append(cell_record(record, sha))
+ except ValueError as exc:
+ raise no_match(f"but {whose} strip record does not give its cells: {exc}") from None
+ then, now = cut.get("source_cut"), own.get("source_cut")
+ for whose, record in (("the cut's (--read-on-cut)", then), ("this loop's", now)):
+ if not isinstance(record, dict) or "source" not in record:
+ raise no_match(f"and {whose} record names no clip it is cut from (source_cut)")
+ if then["source"] != now["source"]:
raise no_match(f"of another clip ({_clip(then)}; this loop's is {_clip(now)})")
- frame, held = int(then["samples"][0]), [int(f) for f in now["samples"]]
+ frame, held = cells[0][0]["source_frame"], [c["source_frame"] for c in cells[1]]
if frame not in held:
raise no_match(f"whose cell 0 is source frame {frame}, which this loop's cut does not hold (it holds source frames "
f"{min(held)} to {max(held)})")
cell = held.index(frame)
- for whose, record, report, at in (("the cut's (--read-on-cut)", cut, reports[0], 0), ("this loop's", meta, reports[1], cell)):
- try:
- as_filmed(record, report, at)
- except ValueError as exc:
- raise no_match(f"whose cell 0 is source frame {frame} (this loop's cut's cell {cell}), but {whose} records do not "
- f"give that frame as filmed: {exc}") from None
+ for whose, record, at in (("the cut's (--read-on-cut)", cells[0], 0), ("this loop's", cells[1], cell)):
+ why = _not_as_filmed(record[at], at)
+ if why is not None:
+ raise no_match(f"whose cell 0 is source frame {frame} (this loop's cut's cell {cell}), but {whose} cell {at} is "
+ f"not that frame as filmed: {why}")
def uncertain(why: str) -> SystemExit:
return SystemExit(f"{verb}: --read-on {read_on} is a cut of this loop's clip, paired at source frame {frame} (this "
f"loop's cut's cell {cell}), but uncertain: {why}; the regions are not moved — only by a whole px "
"shift, never scaled")
- rects = cut.get("source_rect"), meta.get("source_rect")
+ rects = cut.get("source_rect"), own.get("source_rect")
if rects[0] is None or rects[1] is None:
raise uncertain("a cut records no crop (laid by its feet, each frame is placed on its own)")
- moves = []
- for whose, record, at in (("the cut's (--read-on-cut)", cut, 0), ("this loop's", meta, cell)):
- try:
- moves.append(anchor_move(record, at))
- except ValueError as exc:
- raise no_match(f"whose cell 0 is source frame {frame} (this loop's cut's cell {cell}), but where the anchor moved "
- f"that frame before the crop cannot be read off {whose} record: {exc}") from None
- sizes = (int(cut["w"]), int(cut["h"])), size
+ moves = cells[0][0]["anchor_move_px"], cells[1][cell]["anchor_move_px"]
+ sizes = (int(cut["w"]), int(cut["h"])), (int(own["w"]), int(own["h"]))
shift, drift = [], []
for axis in (0, 1):
# each crop on the frame as filmed: where it lies on the frame its anchor moved, less that move
@@ -796,6 +729,28 @@ def recut_pair(meta: dict[str, Any], cut: dict[str, Any], read_on: str, size: tu
"w": sizes[0][0], "h": sizes[0][1], "source_rect": list(rects[0])}}
+def recut_same(pair: dict[str, Any], cut_cell: Image.Image, cell: Image.Image, read_on: str, *, verb: str = VERB) -> None:
+ """Whether the cut's cell 0 (`cut_cell`) and this loop's cell paired with it (`cell`, `recut_pair`) are the same
+ pixels where they overlap, the one laid on the other at the pair's whole shift: refused `no-match` where they are
+ not, saying how many differ: a record that calls a cell the frame as filmed is held to that frame's pixels, and two
+ cells whose crops and scales set a pixel off the whole shift (`drift_px`) are resampled apart and refused alike."""
+ a, b = np.asarray(cut_cell.convert("RGBA")), np.asarray(cell.convert("RGBA"))
+ sx, sy = pair["shift_px"]
+ x0, x1 = max(0, -sx), min(a.shape[1], b.shape[1] - sx)
+ y0, y1 = max(0, -sy), min(a.shape[0], b.shape[0] - sy)
+ seen = max(0, x1 - x0) * max(0, y1 - y0)
+ differ = int(np.any(a[y0:y1, x0:x1] != b[y0 + sy:y1 + sy, x0 + sx:x1 + sx], axis=-1).sum()) if seen else 0
+ if seen and not differ:
+ return
+ off = max(pair["drift_px"])
+ raise SystemExit(f"{verb}: --read-on {read_on}: no-match — the regions were read on another cut, whose cell 0 is source "
+ f"frame {pair['source_frame']} (this loop's cut's cell {pair['cut_cell']}), but the two cells are not "
+ f"the same pixels where they overlap at the shift ({sx}, {sy}): "
+ + (f"{differ} of {seen} px differ" if seen else "they do not overlap")
+ + (f" (their crops and scales set a pixel up to {off:g} px off the shift)" if off else "")
+ + "; locate them again on this strip")
+
+
def _recut_mask(mask: np.ndarray, shift: list[int], size: tuple[int, int], what: str, verb: str) -> np.ndarray:
"""A mask drawn on another cut's cell, moved by the whole px `shift` onto a cell of `size`: the same pixels; refused
where any would leave the cell."""
@@ -811,6 +766,17 @@ def _recut_mask(mask: np.ndarray, shift: list[int], size: tuple[int, int], what:
return out
+def _cut_cell(cut: dict[str, Any], pair: dict[str, Any], read_on: str, verb: str = VERB) -> Image.Image:
+ """The cut's cell 0, from its strip (`read_cut`) at the size its record gives it (`recut_pair`); refused `no-match`
+ where the strip is not that record's size."""
+ w, h, n = pair["cut"]["w"], pair["cut"]["h"], int(cut["meta"]["frames"])
+ if cut["strip"].size != (w * n, h):
+ raise SystemExit(f"{verb}: --read-on {read_on}: no-match — the regions were read on another cut, but its strip is "
+ f"{cut['strip'].size[0]}x{cut['strip'].size[1]} and its record says {n} cells of {w}x{h}; locate "
+ "them again on this strip")
+ return cut["strip"].crop((0, 0, w, h))
+
+
def recut_ask(ask: "Ask", pair: dict[str, Any], size: tuple[int, int], *, verb: str = VERB) -> "Ask":
"""`ask`, read on another cut of the clip (`recut_pair`), moved onto this loop's cell that is the same source frame:
each region's centre by the whole shift, a region mask and a hold mask drawn on that cell by the shift, pixel for
@@ -1503,8 +1469,8 @@ class Request:
meta_bytes: bytes
meta: dict[str, Any]
ask: Ask
- read_on_cut: dict[str, Any] | None = None # the record of the strip the regions were read on (`--read-on-cut`)
- loop_report: dict[str, Any] | None = None # with it, the report video-loop wrote of this loop's cut (`_report_of`)
+ read_on_cut: dict[str, Any] | None = None # the strip the regions were read on and its record (`--read-on-cut`)
+ aligned_from: dict[str, Any] | None = None # with it, on a loop aligned from its cut, the cut's record (`aligned_from`)
@property
def strip_path(self) -> Path:
@@ -1543,20 +1509,20 @@ def read_request(loop_dir: Path, regions: list[tuple[float, ...]], *, gain: floa
if read_on_cut is None:
return Request(loop_dir, name, meta_path, meta_bytes, meta, ask)
return Request(loop_dir, name, meta_path, meta_bytes, meta, ask, read_cut(read_on_cut, ask.read_on, verb),
- _report_of(loop_dir, meta_path, meta))
+ aligned_from(loop_dir, meta_path, meta, verb))
def read_cut(path: Path, read_on: str | None, verb: str = VERB) -> dict[str, Any]:
"""The strip the regions were read on (`--read-on-cut`, with `--read-on`): its PNG, whose bytes are the sha256
- `--read-on` names, its record beside it as `video-loop` writes one (`<name>.strip.json` by `<name>.strip.png`), and
- the report `video-loop` wrote of its cut where one is beside it (`_report_of`; None where none is)."""
+ `--read-on` names, and its record beside it as `video-loop` writes one (`<name>.strip.json` by `<name>.strip.png`)."""
if read_on is None:
raise SystemExit(f"{verb}: --read-on-cut names the strip --read-on names by its sha256; it needs --read-on")
path = Path(path).expanduser()
record = path.with_suffix(".json")
if not path.is_file() or not record.is_file():
raise SystemExit(f"{verb}: --read-on-cut {path}: wants the strip's PNG and its record beside it ({record.name})")
- digest = hashlib.sha256(path.read_bytes()).hexdigest()
+ data = path.read_bytes()
+ digest = hashlib.sha256(data).hexdigest()
if digest != read_on:
raise SystemExit(f"{verb}: --read-on-cut {path} is {digest}, not the strip --read-on names ({read_on})")
try:
@@ -1565,7 +1531,9 @@ def read_cut(path: Path, read_on: str | None, verb: str = VERB) -> dict[str, Any
raise SystemExit(f"{verb}: --read-on-cut {record}: not a strip record ({exc})") from exc
if not isinstance(meta, dict):
raise SystemExit(f"{verb}: --read-on-cut {record}: not a strip record")
- return {"path": str(path), "sha256": digest, "meta": meta, "report": _report_of(record.absolute().parent, record, meta)}
+ with Image.open(io.BytesIO(data)) as im:
+ strip = im.convert("RGBA")
+ return {"path": str(path), "sha256": digest, "meta": meta, "strip": strip}
@dataclass(frozen=True, eq=False)
@@ -1908,12 +1876,14 @@ def solve(request: Request, strip: Image.Image, *, strip_name: str, verb: str =
if read_on is not None and read_on["relation"] == "recut":
# Read on another cut of the clip: paired by the source frame its cell 0 is, the regions moved onto this loop's
# cut's cell that is that frame, and every cell carried from there (`Answer.carry`).
- pair = recut_pair(meta, request.read_on_cut["meta"], ask.read_on, (w, h),
- reports=(request.read_on_cut["report"], request.loop_report), verb=verb)
+ pair = recut_pair(meta, request.read_on_cut["meta"], ask.read_on, strip_sha256=strip_sha256,
+ kept=request.aligned_from, verb=verb)
+ cut_cell = _cut_cell(request.read_on_cut, pair, ask.read_on, verb)
read_on.update(pair)
- ask = recut_ask(ask, pair, (w, h), verb=verb)
if not aligned: # carried from that cell by its own pixels; on an aligned loop, as the alignment carries the cut's
+ recut_same(pair, cut_cell, cells[pair["cut_cell"]], ask.read_on, verb=verb)
read_on.update(policy=RECUT_POLICY, base_cell=pair["cut_cell"])
+ ask = recut_ask(ask, pair, (w, h), verb=verb)
def transported() -> tuple[np.ndarray, np.ndarray, int, list[int]]:
# The cut's own reading, cell for cell: the regions are where they were read, on the cut's cell 0 (or, read on
@@ -1927,6 +1897,8 @@ def solve(request: Request, strip: Image.Image, *, strip_name: str, verb: str =
f"{paired['source_frame']} with the cut this loop was aligned from, but uncertain: no cell "
f"of this loop is that cut's cell {paired['cut_cell']}; the regions are not moved")
read_on["base_cell"] = cut_cells.index(paired["cut_cell"])
+ # that cell is the cut's cell as drawn (`transported_motion`), so the cut's frame as filmed
+ recut_same(paired, cut_cell, cells[read_on["base_cell"]], ask.read_on, verb=verb)
read_on.update(policy=TRANSPORT_POLICY, cut_cells=cut_cells)
return rows, cols, height0, worn
@@ -3660,13 +3632,14 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
"it is another cut of this loop's source clip (a cut again from the same clip, not a new take): "
"paired by the source frame its cell 0 is with the cell of this loop's cut that is that frame, the "
"regions and the region mask moved onto that cell by the whole px the two crops set it apart, each "
- "taken on the frame as filmed (less where its cut's anchor moved that frame, as its record gives "
- "it), pixel for pixel, and carried from there (relation recut); refused no-match where the clip "
- "differs, this loop's cut does not hold that frame, the report video-loop wrote of either cut is "
- "not beside its strip or does not give that frame as filmed (the size hold or the gait fallback "
- "scaled the cut, or RIFE, the wrap overlap or the wrap blend made it), a record does not give its "
- "anchor's move of it or the strip is a loop aligned from its cut, uncertain where the crops do not "
- "set it apart by one whole shift. Off by default")
+ "taken on the frame as filmed (less where its cut's anchor moved that frame), pixel for pixel, "
+ "and carried from there (relation recut), each cut read by its record of its cells as video-loop "
+ "writes it (cell_record, bound to its strip's PNG); refused no-match where a cut has no such "
+ "record (made before video-loop wrote one) or it is another strip's, the clip differs, this "
+ "loop's cut does not hold that frame, either cell is not that frame as filmed (the size hold or "
+ "the gait fallback scaled it, or video-loop did not cut it as filmed), the two cells are not the "
+ "same pixels where they overlap at the shift, or the strip is a loop aligned from its cut, "
+ "uncertain where the crops do not set it apart by one whole shift. Off by default")
parser.add_argument("--band", type=parse_band, default=None, metavar=f"PX|{AUTO}",
help="move each region as one mass: 1 over its ellipse grown by --band-margin, falling to 0 (cos²) over PX px "
f"past it ({AUTO}: ceil(reach x π/2) + 1, the narrowest the move does not fold), carried into each cell by "
diff --git a/tests/video/test_sway_read_on_recut.py b/tests/video/test_sway_read_on_recut.py
index f555ab3..d358aba 100644
--- a/tests/video/test_sway_read_on_recut.py
+++ b/tests/video/test_sway_read_on_recut.py
@@ -5,18 +5,19 @@ source frames in other cells and at other places. The regions read on the first
there, are paired by the source frame that cell is with the cell of the loop's cut that is that frame, moved onto it by
the whole px the two crops set that frame apart, the mask pixel for pixel, and carried from there; on a loop aligned from
its cut, through the alignment's record of where each cell is from. Each crop is taken on the frame as filmed: less where
-its cut's anchor moved that frame (the body anchor's ramp, the motion anchor's correction), as its record gives it. A cut
-whose frame the loop's cut does not hold, a cut whose record names no source frame, a cut of another clip, a record that
-does not give where its anchor moved the frame and a loop aligned from its cut are refused no-match, saying which, and
-leave the loop as it was. So is a frame that is not in its cut as filmed, or that nothing says is: the strip's record says
-nothing of the size hold, the gait fallback or RIFE, the report video-loop wrote beside it does, so a cut with no report
-of its own beside its strip, a cut held to one size, and a frame RIFE made again, gave way or blended at the wrap are
-refused too, and so is a report that does not say what a step did as video-loop writes it: a step's record, or a field
-of one, missing or not of its kind, a step's record missing where video-loop writes it whether the step ran or not (the
-size hold of a walk cut), and an `applied` the frames it names do not bear out. What video-loop writes of a step that
-did not run leaves the frame paired. Synthetic figures only: the read-on test's runner, cut from a clip of two strides
-at two starts and two crops, by hand or by video-loop, each with the report video-loop writes beside it; and the walk
-away from the camera of the gait fallback's tests, cut by video-loop as a walk.
+its cut's anchor moved that frame (the body anchor's ramp, the motion anchor's correction).
+
+Each cut is read by its record of its cells alone, as video-loop writes it in the strip's record, bound to the strip's
+PNG by its sha256 (`cell_record`): the source frame each cell is, where its anchor moved it, the factor the size hold and
+the gait fallback scaled it by, and whether its pixels are that frame as filmed but for that move; on a loop aligned from
+its cut, the cut's own record, which the alignment keeps beside the cut's frames. A cut with no such record (one
+video-loop cut before it wrote one), a record of another strip or one that does not give its cells as video-loop writes
+them (naming the field), a cut whose frame the loop's cut does not hold, a cut of another clip, a loop aligned from its
+cut named by --read-on-cut, a cell scaled or not cut as filmed, and two paired cells that are not the same pixels where
+they overlap at the shift are refused no-match, saying which, and leave the loop as it was: a record changed to call a
+cell the frame as filmed is held to that frame's pixels. Synthetic figures only: the read-on test's runner, cut from a
+clip of two strides at two starts and two crops, by hand or by video-loop; the walk away from the camera of the gait
+fallback's tests, cut by video-loop as a walk; and the ghost walk of the ghost repair's tests.
Each case first reads what a sway writes with the new key (its cells, its record, whether it refuses and what it says),
so on a release without it the same input fails on what comes out.
@@ -28,14 +29,17 @@ import numpy as np
import pytest
from PIL import Image
from sprite_gen import cli
-from sprite_gen.video import loop, sway
+from sprite_gen.video import align, loop, motion_anchor, sway
from tests.video.test_follow_read_on import H, N, PATCH, SHIFT, W, aligned, cells_of, copy, cut, files, meta_of, runner, sha
from tests.video.test_gait_fallback import walking_away
+from tests.video.test_ghost_given_way import _cut as ghost_cut, _keyed as ghost_keyed
+from tests.video.test_loop_repair import _no_rife
from tests.video.test_sway_region import mask_png
CLIP = 2*N # the clip's frames: the runner's stride twice
FPS = 24
+CR = 'cell_record' # the strip record's record of its cells (`loop.CELL_RECORD`)
def clip():
@@ -51,12 +55,30 @@ def bbox(frames):
return int(xs.min()), int(ys.min()), int(xs.max())+1, int(ys.max())+1
-def cut_of(path, start, rect, *, source='clip-a', record=True, name='run', anchor='none', report=None, made=()):
+def as_cut(loop_dir, start, *, source='clip-a', made=(), cells=True, name='run'):
+ """A loop's record as video-loop writes it of a cut from source frame `start` of the clip `source`: the clip and the
+ cut (`source_cut`, `cycle_drawings`), and where `cells`, the record of each cell (`cell_record`, bound to the strip's
+ PNG): the source frame it is, moved by no anchor, scaled by no hold, as filmed but where it is in `made`."""
+ path = loop_dir/f'{name}.strip.json'
+ meta = json.loads(path.read_text())
+ n = meta['frames']
+ meta['source_cut'] = {'source': {'sha256': source, 'frames': CLIP, 'fps': FPS}, 'start': start, 'length': n,
+ 'samples': [start+k for k in range(n)]}
+ meta['cycle_drawings'] = {'start': start, 'length': n}
+ if cells:
+ meta[CR] = {'strip_sha256': sha(loop_dir/f'{name}.strip.png'), 'cells': [
+ {'source_frame': start+k, 'anchor_move_px': [0, 0], 'scale': {'size_hold': 1.0, 'gait_fallback': 1.0},
+ 'as_filmed': k not in made} for k in range(n)]}
+ path.write_text(json.dumps(meta, indent=2)+'\n')
+ return loop_dir
+
+
+def cut_of(path, start, rect, *, source='clip-a', record=True, name='run', anchor='none', made=(), cells=True):
"""A loop directory as video-loop leaves one, cut from the clip at `start` with the crop `rect` at scale 1: its strip,
- and its record with how its frames were anchored (`foot_anchor`; None: not said) and the source frame each cell is
- (`source_cut`, and its cut: `cycle_drawings`) where `record`, the report video-loop wrote of the cut beside them
- (`report_beside`: `report` what it says of a step in place of off; False: none). A cell in `made` is drawn between
- the frames either side of its own, half of each, as an interpolator makes it."""
+ and its record with how its frames were anchored (`foot_anchor`; None: not said), and where `record`, the source
+ frame each cell is and the record of each cell (`as_cut`, none where not `cells`). A cell in `made` is drawn between
+ the frames either side of its own, half of each, as an interpolator makes it, and its record says it is not the
+ frame as filmed."""
frames = clip()
x0, y0, x1, y1 = rect
w, h = x1-x0, y1-y0
@@ -68,33 +90,29 @@ def cut_of(path, start, rect, *, source='clip-a', record=True, name='run', ancho
meta = {'frames': N, 'sample_indices': list(range(N)), 'source_rect': list(rect), 'w': w, 'h': h, 'kind': 'periodic',
'loop': True, 'scale': 1.0, 'delay_ms': round(1000/FPS, 2), 'cycle_frames': N, 'cycle_seconds': N/FPS,
'subsampled': False} | ({} if anchor is None else {'foot_anchor': anchor})
- if record:
- meta['source_cut'] = {'source': {'sha256': source, 'frames': CLIP, 'fps': FPS}, 'start': start, 'length': N,
- 'samples': [start+k for k in range(N)]}
- meta['cycle_drawings'] = {'start': start, 'length': N}
path.mkdir(parents=True)
strip.save(path/f'{name}.strip.png')
(path/f'{name}.strip.json').write_text(json.dumps(meta, indent=2)+'\n')
- if record and report is not False:
- report_beside(path, name, **(report or {}))
- return path
+ return as_cut(path, start, source=source, made=made, cells=cells, name=name) if record else path
-GONE = object() # what a report does not hold (`report_beside`)
+GONE = object() # what a record does not hold (`recorded`)
-def report_beside(loop_dir, name='run', **steps):
- """The report video-loop writes beside a loop's strip (`<name>.loop.report.json`), passed, of the cut its record names
- (its strip's record, and its cut: `cycle`): the size hold and the jump repair off, and each of `steps` as given (GONE:
- not in it; `strip`: what its strip says of the cut in place of its record's, GONE where it says nothing)."""
- meta = json.loads((loop_dir/f'{name}.strip.json').read_text())
- strip = {'path': str(loop_dir/f'{name}.strip.png'), **meta} | steps.pop('strip', {})
- report = {'kind': 'sprite-gen-video-loop-report', 'status': 'passed', 'fps': FPS,
- 'cycle': {key: meta['cycle_drawings'][key] for key in ('start', 'length')},
- 'size_hold': {'applied': False, 'why': '--size-hold off'}, 'jump_repair': {'applied': False, 'why': '--repair off'},
- 'strip': {key: said for key, said in strip.items() if said is not GONE}} | steps
- report = {key: said for key, said in report.items() if said is not GONE}
- (loop_dir/f'{name}.loop.report.json').write_text(json.dumps(report, indent=2)+'\n')
+def recorded(loop_dir, *at, to, name='run'):
+ """The loop's record changed at the keys `at` (the record's, then each one's within it) to `to` (GONE: taken out; a
+ function: of what is there)."""
+ path = loop_dir/f'{name}.strip.json'
+ meta = json.loads(path.read_text())
+ *within, last = at
+ holder = meta
+ for key in within:
+ holder = holder[key]
+ if to is GONE:
+ del holder[last]
+ else:
+ holder[last] = to(holder[last]) if callable(to) else to
+ path.write_text(json.dumps(meta, indent=2)+'\n')
return loop_dir
@@ -159,6 +177,17 @@ def read_on_cut(a, b):
return region, kw, answer
+def refused(loop_dir, kw, said, region=None):
+ """`video-sway` on `loop_dir` asked `kw` (`read_on`), refused with `said` (the refusal after the regions were read on
+ another cut, to its end), the loop as it was."""
+ region = region or (0.0, 0.0, 18.0, 18.0)
+ before = files(loop_dir)
+ with pytest.raises(SystemExit, match=r"no-match — the regions were read on another cut, " + said
+ + r"; locate them again on this strip$"):
+ sway.sway_loop(loop_dir, [region], **kw)
+ assert files(loop_dir) == before
+
+
@pytest.fixture
def cuts(tmp_path):
"""The first cut (`a`, at frame 4, cropped to the clip's frames) and its region mask on its cell 0, the patch."""
@@ -169,14 +198,22 @@ def cuts(tmp_path):
return a, tight, (cx, cy, 17.0, 17.0), mask_png(tmp_path/'mask.png', patch)
+def wider(rect):
+ """`rect` cropped again 3 px wider left, 2 px higher, 2 px wider right and 1 px lower."""
+ return (rect[0]-3, rect[1]-2, rect[2]+2, rect[3]+1)
+
+
KW = dict(gain=1.0, on_fold='lower', region_shrink=0)
+PAIRED = r"whose cell 0 is source frame 4 \(this loop's cut's cell 3\), " # the read cut from 4, this loop's from 1
+NOT_ANCHORED = (r": its pixels at the end of the cut are not its pixels anchored, before any repair, or a step held the "
+ r"clip's frames to one size")
# ---------------------------------------------------------------- moved by the source frame
def test_on_a_cut_again_at_the_same_frame_the_regions_are_the_ones_drawn_there_moved_by_the_crop(cuts, tmp_path):
a, tight, region, mask = cuts
- wide = (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1) # cut again, cropped 3 px wider left, 2 px higher
+ wide = wider(tight)
loop_dir = cut_of(tmp_path/'b', 4, wide)
record = sway.sway_loop(loop_dir, [region], region_mask=mask, read_on=sha(a/'run.strip.png'),
read_on_cut=a/'run.strip.png', **KW)
@@ -194,7 +231,7 @@ def test_on_a_cut_again_at_the_same_frame_the_regions_are_the_ones_drawn_there_m
def test_on_a_cut_again_at_another_start_the_mask_is_moved_onto_the_cell_that_is_that_frame(cuts, tmp_path):
a, tight, region, mask = cuts
- wide = (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1)
+ wide = wider(tight)
loop_dir = cut_of(tmp_path/'b', 1, wide) # holds source frames 1 to N: frame 4 is its cell 3
source = cells_of(loop_dir)
request = sway.read_request(loop_dir, [region], region_mask=mask, read_on=sha(a/'run.strip.png'),
@@ -218,25 +255,51 @@ def test_on_a_cut_again_at_another_start_the_mask_is_moved_onto_the_cell_that_is
assert np.array_equal(out[k][outside], source[k][outside]), k
-def test_on_a_loop_aligned_from_the_cut_again_the_pair_is_carried_through_the_alignment(tmp_path):
- # The loop's cut as video-loop cuts one (its own crop), from frame 1, its report beside it; the regions read on a cut
- # from frame 4 cropped 3 px in from its left and 2 px from its top.
- b = cut(tmp_path/'b', clip()[1:1+N])
+def test_a_cell_its_record_calls_scaled_or_not_as_filmed_leaves_another_cell_paired(cuts, tmp_path):
+ # Only the two cells paired are read for the frame as filmed: this loop's cut's cell 5 scaled and not as filmed, and
+ # the cut's cell 2 not as filmed, leave frame 4 paired as ever.
+ a, tight, region, mask = cuts
+ read = recorded(cut_of(tmp_path/'read', 4, tight), CR, 'cells', 2, 'as_filmed', to=False)
+ loop_dir = cut_of(tmp_path/'b', 1, wider(tight))
+ recorded(loop_dir, CR, 'cells', 5, 'scale', 'size_hold', to=0.9)
+ recorded(loop_dir, CR, 'cells', 5, 'as_filmed', to=False)
+ _, _, answer = read_on_cut(read, loop_dir)
+ assert (answer.read_on['relation'], answer.read_on['cut_cell'], answer.read_on['shift_px']) == ('recut', 3, [3, 2])
+ assert np.array_equal(answer.mask.worn[0], patch_in(loop_dir, 3))
+
+
+def aligned_cut(tmp_path, made=(), read_made=()):
+ """The loop's cut as video-loop cuts one (its own crop), from frame 1, with its record of its cells (a cell in `made`
+ drawn between the frames either side, recorded so), aligned on its own; and the regions read on a cut from frame 4
+ cropped 3 px in from its left and 2 px from its top (its cells in `read_made` drawn so): the cut, the cut the regions
+ were read on, the aligned loop, the region and its mask."""
+ frames = clip()[1:1+N]
+ for k in made:
+ frames[k] = Image.blend(clip()[k], clip()[k+2], 0.5)
+ b = as_cut(cut(tmp_path/'b', frames), 1, made=made)
meta = meta_of(b)
assert meta['scale'] == 1.0 and meta['sample_indices'] == list(range(N))
- meta['source_cut'] = {'source': {'sha256': 'clip-a', 'frames': CLIP, 'fps': FPS}, 'start': 1, 'length': N,
- 'samples': [1+k for k in range(N)]}
- meta['cycle_drawings'] = {'start': 1, 'length': N}
- (b/'run.strip.json').write_text(json.dumps(meta, indent=2)+'\n')
- report_beside(b)
left, top, right, bottom = meta['source_rect']
inner = (left+3, top+2, right, bottom)
- a = cut_of(tmp_path/'a', 4, inner)
+ a = cut_of(tmp_path/'a', 4, inner, made=read_made)
(cx, cy), patch = patch_at(4, inner)
- region, mask = (cx, cy, 17.0, 17.0), mask_png(tmp_path/'mask.png', patch)
- loop_dir = aligned(b, tmp_path/'turned')
+ return b, a, aligned(b, tmp_path/'turned'), (cx, cy, 17.0, 17.0), mask_png(tmp_path/'mask.png', patch)
+
+
+def test_on_a_loop_aligned_from_the_cut_again_the_pair_is_carried_through_the_alignment(tmp_path):
+ b, a, loop_dir, region, mask = aligned_cut(tmp_path)
record = meta_of(loop_dir)['cycle_align']
assert record['origin']['strip_sha256'] == sha(b/'run.strip.png') and record['turned_by'] != 0
+ # The cut's own record, kept beside its frames as filmed as it was, byte for byte, bound to the strip the origin names;
+ # the aligned strip is another, and its record does not carry the cut's record of its cells. Aligned again, the cut's
+ # is kept as the first alignment kept it.
+ kept = loop_dir/loop.CYCLE_SOURCE_DIR/'run.strip.json'
+ assert kept.read_bytes() == (b/'run.strip.json').read_bytes() and CR not in meta_of(loop_dir)
+ assert json.loads(kept.read_text())[CR]['strip_sha256'] == record['origin']['strip_sha256']
+ again = copy(loop_dir, tmp_path/'again')
+ align.align_set([again], length=17, between='nearest', multi_cycle='warn')
+ assert (again/loop.CYCLE_SOURCE_DIR/'run.strip.json').read_bytes() == kept.read_bytes()
+ assert CR not in meta_of(again)
result = sway.sway_loop(loop_dir, [region], region_mask=mask, read_on=sha(a/'run.strip.png'),
read_on_cut=a/'run.strip.png', **KW)
read_on = result['read_on']
@@ -247,6 +310,38 @@ def test_on_a_loop_aligned_from_the_cut_again_the_pair_is_carried_through_the_al
assert read_on['cut_cell'] == 3 and read_on['base_cell'] == cut_cells.index(3) and read_on['source_frame'] == 4
+ALIGNED = { # what the alignment kept of the cut's record, or what its record says of the cell paired, and the refusal
+ 'kept-gone': (lambda kept, a: kept.unlink(),
+ r"and this loop is aligned from its cut \(cycle_align\), whose strip record is not kept beside its frames "
+ r"as filmed \(cycle\.source/<name>\.strip\.json\)"),
+ 'kept-no-cells': (lambda kept, a: recorded(kept.parent, CR, to=GONE),
+ r"but this loop's strip record does not give its cells: it holds no record of them \(cell_record\), "
+ r"as a cut video-loop made before it wrote one does"),
+ # bound to another strip than the one the alignment's origin names: here the aligned strip's
+ 'kept-another-strip': (lambda kept, a: recorded(kept.parent, CR, 'strip_sha256',
+ to=sha(kept.parent.parent/'run.strip.png')),
+ r"but this loop's strip record does not give its cells: its record of them is another strip's "
+ r"\(cell_record\.strip_sha256 [0-9a-f]{64}; the strip is <origin>\)"),
+ 'made': (None, PAIRED + r"but this loop's cell 3 is not that frame as filmed: video-loop did not cut it as filmed "
+ r"\(cell_record\.cells\[3\]\.as_filmed\)" + NOT_ANCHORED),
+ # the cut the regions were read on drew its cell 0 between the frames either side, its record changed to say not
+ 'read-made-said-as-filmed': (lambda kept, a: recorded(a, CR, 'cells', 0, 'as_filmed', to=True),
+ PAIRED + r"but the two cells are not the same pixels where they overlap at the shift \(3, 2\): "
+ r"\d+ of \d+ px differ")}
+
+
[email protected]('case', list(ALIGNED))
+def test_a_loop_aligned_from_a_cut_whose_kept_record_does_not_give_the_frame_as_filmed_is_refused_no_match(tmp_path, case):
+ change, said = ALIGNED[case]
+ b, a, loop_dir, region, mask = aligned_cut(tmp_path, made=(3,) if case == 'made' else (),
+ read_made=(0,) if case.startswith('read-made') else ())
+ said = said.replace('<origin>', meta_of(loop_dir)['cycle_align']['origin']['strip_sha256'])
+ if change is not None:
+ change(loop_dir/loop.CYCLE_SOURCE_DIR/'run.strip.json', a)
+ refused(loop_dir, dict(region_mask=mask, read_on=sha(a/'run.strip.png'), read_on_cut=a/'run.strip.png', **KW), said,
+ region)
+
+
# ---------------------------------------------------------------- the frame where each cut's anchor moved it
def test_two_cuts_at_anchor_body_pair_the_frame_where_each_ramp_moved_it(tmp_path):
@@ -258,6 +353,7 @@ def test_two_cuts_at_anchor_body_pair_the_frame_where_each_ramp_moved_it(tmp_pat
ma, mb = meta_of(a), meta_of(b)
assert ma['wrap_dx_px'] == mb['wrap_dx_px'] == -24 and ma['source_cut']['samples'][0] == mb['source_cut']['samples'][3] == 4
assert ma['source_rect'][0] - mb['source_rect'][0] == 6 and ma['source_rect'][1] == mb['source_rect'][1]
+ assert mb[CR]['cells'][3]['anchor_move_px'] == [-6, 0] and ma[CR]['cells'][0]['anchor_move_px'] == [0, 0]
there = patch_in(b, 3) # frame 4's patch where the second cut drew it
region, kw, answer = read_on_cut(a, b)
pair = answer.read_on
@@ -284,11 +380,11 @@ def test_two_cuts_at_anchor_body_whose_ramp_moves_nothing_pair_by_their_crops_al
assert there.sum() > 100 and np.array_equal(answer.mask.worn[0], there)
[email protected]('anchor', ['motion', 'motion-auto'])
-def test_two_cuts_at_anchor_motion_pair_the_frame_where_each_correction_laid_it(tmp_path, anchor):
+def test_two_cuts_at_anchor_motion_pair_the_frame_where_each_correction_laid_it(tmp_path):
# The runner walking 2 px a frame, cut by video-loop at --anchor motion from frame 4 and from frame 1 on its head and
# torso in each cut's first frame: each correction lays its frame k on a canvas padded for every frame, at the padding
- # plus that frame's shift, so the crops alone do not say where frame 4 lies in the second cut; the corrections do.
+ # plus that frame's shift, so the crops alone do not say where frame 4 lies in the second cut; the corrections do, and
+ # each cell's record gives it.
clip_dir = walked(tmp_path/'clip', (2, 0), (220, 220))
def regions(start):
@@ -298,15 +394,15 @@ def test_two_cuts_at_anchor_motion_pair_the_frame_where_each_correction_laid_it(
a = loop_cut(clip_dir, tmp_path/'a', 4, 'motion', anchor_regions=regions(4))
b = loop_cut(clip_dir, tmp_path/'b', 1, 'motion', anchor_regions=regions(1))
- if anchor == 'motion-auto':
- # --anchor motion-auto finds its regions and its cut itself (it takes no --cycle fixed), and records its
- # correction as --anchor motion does, the same `motion_anchor` under its own `foot_anchor`: these two cuts' records.
- for loop_dir in (a, b):
- (loop_dir/'run.strip.json').write_text(json.dumps(meta_of(loop_dir) | {'foot_anchor': anchor}, indent=2)+'\n')
ma, mb = meta_of(a), meta_of(b)
- assert ma['foot_anchor'] == mb['foot_anchor'] == anchor and ma['source_cut']['samples'][0] == mb['source_cut']['samples'][3] == 4
- laid = [m['motion_anchor']['padding_ltrb'][axis] + m['motion_anchor']['shifts_xy'][k][axis]
- for m, k in ((ma, 0), (mb, 3)) for axis in (0, 1)]
+ assert ma['source_cut']['samples'][0] == mb['source_cut']['samples'][3] == 4
+ for m in (ma, mb):
+ assert [c['anchor_move_px'] for c in m[CR]['cells']] == [list(motion_anchor.laid_at(m['motion_anchor'], k))
+ for k in range(N)]
+ assert all(c['as_filmed'] for c in m[CR]['cells'])
+ report = json.loads((b/'run.loop.report.json').read_text())
+ assert report['source_projection']['recipe'] == loop.MOTION_RECIPE
+ laid = [*ma[CR]['cells'][0]['anchor_move_px'], *mb[CR]['cells'][3]['anchor_move_px']]
crops = [ma['source_rect'][axis] - mb['source_rect'][axis] for axis in (0, 1)]
assert laid[:2] != laid[2:] # the two corrections lay frame 4 apart
there = patch_in(b, 3)
@@ -317,95 +413,233 @@ def test_two_cuts_at_anchor_motion_pair_the_frame_where_each_correction_laid_it(
assert there.sum() == patch_in(a, 0).sum() > 100 and np.array_equal(answer.mask.worn[0], there)
+# ---------------------------------------------------------------- what video-loop records of each cell
+
+def test_video_loop_records_each_cell_of_its_strip_in_the_strips_record_bound_to_its_png(tmp_path):
+ # --anchor body: each cell the source frame it is, moved across by its ramp, scaled by no hold, as filmed.
+ clip_dir = walked(tmp_path/'clip', (2, 0), (220, 220))
+ b = loop_cut(clip_dir, tmp_path/'b', 1, 'body')
+ meta = meta_of(b)
+ record = meta[CR]
+ assert loop.CELL_RECORD == CR and set(record) == {'strip_sha256', 'cells'}
+ assert record['strip_sha256'] == sha(b/'run.strip.png')
+ assert [set(c) for c in record['cells']] == [{'source_frame', 'anchor_move_px', 'scale', 'as_filmed'}]*N
+ assert [c['source_frame'] for c in record['cells']] == meta['source_cut']['samples'] == [1+k for k in range(N)]
+ assert [c['anchor_move_px'] for c in record['cells']] == [[loop.ramp_offset(-24, k, N), 0] for k in range(N)]
+ assert all(c['scale'] == {'size_hold': 1, 'gait_fallback': 1} and c['as_filmed'] is True for c in record['cells'])
+ # Written beside the strip and not into the report, which keeps each frame's receipt the record is read against.
+ report = json.loads((b/'run.loop.report.json').read_text())
+ assert CR not in report and CR not in report['strip']
+ assert len(report['source_projection']['before_repair_pixels_sha256']) == N
+
+
+def test_a_cell_the_jump_repair_gave_way_is_recorded_not_as_filmed(tmp_path, monkeypatch):
+ # The ghost walk with a filmed ghost at frame 10, no RIFE: frame 10 gives way to frame 11, shown twice in a row. Its
+ # cell's pixels are not its pixels before the repair; every other cell's are.
+ _no_rife(monkeypatch, tmp_path)
+ report = ghost_cut(ghost_keyed(tmp_path, 'E', (10,)), tmp_path/'E')
+ assert report['jump_repair']['replaced'] == [10]
+ meta = json.loads((tmp_path/'E'/'walk.strip.json').read_text())
+ assert 10 in meta['sample_indices']
+ assert [c['as_filmed'] for c in meta[CR]['cells']] == [k != 10 for k in meta['sample_indices']]
+ assert meta[CR]['strip_sha256'] == sha(tmp_path/'E'/'walk.strip.png')
+
+
# ---------------------------------------------------------------- refused, saying why
-def test_a_cut_whose_frame_the_loop_does_not_hold_or_that_names_none_is_refused_no_match(cuts, tmp_path):
+def test_a_cut_whose_frame_the_loop_does_not_hold_or_that_names_no_clip_is_refused_no_match(cuts, tmp_path):
a, tight, region, mask = cuts
+ kw = dict(region_mask=mask, read_on=sha(a/'run.strip.png'), read_on_cut=a/'run.strip.png', **KW)
later = cut_of(tmp_path/'later', 7, tight) # source frames 7 to N+6: frame 4 is not among them
- before = files(later)
- with pytest.raises(SystemExit, match=r'no-match — the regions were read on another cut, whose cell 0 is source frame 4, '
- r'which this loop\'s cut does not hold \(it holds source frames 7 to 19\)'):
- sway.sway_loop(later, [region], region_mask=mask, read_on=sha(a/'run.strip.png'), read_on_cut=a/'run.strip.png', **KW)
- assert files(later) == before
- bare = cut_of(tmp_path/'bare', 5, tight, record=False, name='walk')
- with pytest.raises(SystemExit, match=r"no-match — .*the cut's \(--read-on-cut\) record names no source frame"):
- sway.sway_loop(cut_of(tmp_path/'b', 4, tight), [region], region_mask=mask, read_on=sha(bare/'walk.strip.png'),
- read_on_cut=bare/'walk.strip.png', **KW)
+ refused(later, kw, r"whose cell 0 is source frame 4, which this loop's cut does not hold \(it holds source frames 7 to "
+ r"19\)", region)
+ nameless = recorded(cut_of(tmp_path/'nameless', 5, tight, name='walk'), 'source_cut', to=GONE, name='walk')
+ refused(cut_of(tmp_path/'b', 4, tight), dict(kw, read_on=sha(nameless/'walk.strip.png'),
+ read_on_cut=nameless/'walk.strip.png'),
+ r"and the cut's \(--read-on-cut\) record names no clip it is cut from \(source_cut\)", region)
# Without the cut's record nothing says which frame its cell 0 is: refused as ever, saying so.
+ before = files(later)
with pytest.raises(SystemExit, match=r'no-match — .*no record of it says which source frame its cell 0 is \(--read-on-cut'):
sway.sway_loop(later, [region], region_mask=mask, read_on=sha(a/'run.strip.png'), **KW)
assert files(later) == before
-UNREAD = { # how the cut the regions were read on and this loop's cut are recorded, and what the refusal says of them
- 'cut-says-no-anchor': (dict(name='walk', anchor=None), {},
- r"the cut's \(--read-on-cut\) record: it does not say how its frames were anchored \(foot_anchor\)"),
- 'body-no-wrap': ({}, dict(anchor='body'),
- r"this loop's record: it was cut at --anchor body and records no whole wrap offset \(wrap_dx_px\)"),
- 'motion-no-correction': ({}, dict(anchor='motion'),
- r"this loop's record: it was cut at --anchor motion and records no whole shift of its frame 3 of 13 "
- r"and no padding \(motion_anchor\)")}
-
-
[email protected]('case', list(UNREAD))
-def test_a_record_that_does_not_give_where_its_anchor_moved_the_frame_is_refused_no_match(cuts, tmp_path, case):
+WHOSE = {'read-on-cut': r"the cut's \(--read-on-cut\)", 'this-loop': "this loop's"}
+CELLS = r"its record of cell 2 "
+UNGIVEN = { # a cut's record of its cells changed (`recorded`: where, to what), on the cut the regions were read on or on
+ # this loop's, so that it does not give them as video-loop writes it; the refusal's words
+ 'none': ('read-on-cut', (CR,), GONE,
+ r"it holds no record of them \(cell_record\), as a cut video-loop made before it wrote one does"),
+ 'none-here': ('this-loop', (CR,), GONE,
+ r"it holds no record of them \(cell_record\), as a cut video-loop made before it wrote one does"),
+ 'null': ('this-loop', (CR,), None, r"its record of them is not one \(cell_record\)"),
+ 'list': ('read-on-cut', (CR,), [], r"its record of them is not one \(cell_record\)"),
+ 'another-strip': ('read-on-cut', (CR, 'strip_sha256'), 'ab'*32,
+ r"its record of them is another strip's \(cell_record\.strip_sha256 (ab){32}; the strip is "
+ r"[0-9a-f]{64}\)"),
+ 'another-strip-here': ('this-loop', (CR, 'strip_sha256'), 'cd'*32,
+ r"its record of them is another strip's \(cell_record\.strip_sha256 (cd){32}; the strip is "
+ r"[0-9a-f]{64}\)"),
+ 'unbound': ('this-loop', (CR, 'strip_sha256'), GONE,
+ r"its record of them is another strip's \(cell_record\.strip_sha256 None; the strip is [0-9a-f]{64}\)"),
+ 'cells-gone': ('read-on-cut', (CR, 'cells'), GONE,
+ r"its record of them names no cells, and the strip has 13 \(cell_record\.cells\)"),
+ 'cells-short': ('this-loop', (CR, 'cells'), lambda cells: cells[:-1],
+ r"its record of them names 12 cells, and the strip has 13 \(cell_record\.cells\)"),
+ 'cell-null': ('read-on-cut', (CR, 'cells', 2), None, CELLS + r"is not one \(cell_record\.cells\[2\]\)"),
+ 'frame-gone': ('this-loop', (CR, 'cells', 2, 'source_frame'), GONE,
+ CELLS + r"names no whole source frame \(cell_record\.cells\[2\]\.source_frame\)"),
+ 'frame-float': ('read-on-cut', (CR, 'cells', 2, 'source_frame'), 6.0,
+ CELLS + r"names no whole source frame \(cell_record\.cells\[2\]\.source_frame\)"),
+ 'frame-text': ('this-loop', (CR, 'cells', 2, 'source_frame'), '3',
+ CELLS + r"names no whole source frame \(cell_record\.cells\[2\]\.source_frame\)"),
+ 'frame-bool': ('read-on-cut', (CR, 'cells', 2, 'source_frame'), True,
+ CELLS + r"names no whole source frame \(cell_record\.cells\[2\]\.source_frame\)"),
+ 'move-gone': ('this-loop', (CR, 'cells', 2, 'anchor_move_px'), GONE,
+ CELLS + r"gives no whole move of its anchor \(cell_record\.cells\[2\]\.anchor_move_px\)"),
+ 'move-short': ('read-on-cut', (CR, 'cells', 2, 'anchor_move_px'), [0],
+ CELLS + r"gives no whole move of its anchor \(cell_record\.cells\[2\]\.anchor_move_px\)"),
+ 'move-float': ('this-loop', (CR, 'cells', 2, 'anchor_move_px'), [0.5, 0],
+ CELLS + r"gives no whole move of its anchor \(cell_record\.cells\[2\]\.anchor_move_px\)"),
+ 'move-text': ('read-on-cut', (CR, 'cells', 2, 'anchor_move_px'), '0,0',
+ CELLS + r"gives no whole move of its anchor \(cell_record\.cells\[2\]\.anchor_move_px\)"),
+ 'scale-gone': ('this-loop', (CR, 'cells', 2, 'scale'), GONE,
+ CELLS + r"does not give the factor each hold scaled it by \(cell_record\.cells\[2\]\.scale: size_hold, "
+ r"gait_fallback\)"),
+ 'scale-step-gone': ('read-on-cut', (CR, 'cells', 2, 'scale', 'gait_fallback'), GONE,
+ CELLS + r"does not give the factor each hold scaled it by \(cell_record\.cells\[2\]\.scale: "
+ r"size_hold, gait_fallback\)"),
+ 'scale-zero': ('this-loop', (CR, 'cells', 2, 'scale', 'size_hold'), 0,
+ CELLS + r"does not give the factor each hold scaled it by \(cell_record\.cells\[2\]\.scale: size_hold, "
+ r"gait_fallback\)"),
+ 'scale-text': ('read-on-cut', (CR, 'cells', 2, 'scale', 'size_hold'), '1',
+ CELLS + r"does not give the factor each hold scaled it by \(cell_record\.cells\[2\]\.scale: size_hold, "
+ r"gait_fallback\)"),
+ 'scale-bool': ('this-loop', (CR, 'cells', 2, 'scale', 'gait_fallback'), True,
+ CELLS + r"does not give the factor each hold scaled it by \(cell_record\.cells\[2\]\.scale: size_hold, "
+ r"gait_fallback\)"),
+ 'scale-nan': ('read-on-cut', (CR, 'cells', 2, 'scale', 'size_hold'), float('nan'),
+ CELLS + r"does not give the factor each hold scaled it by \(cell_record\.cells\[2\]\.scale: size_hold, "
+ r"gait_fallback\)"),
+ 'filmed-gone': ('this-loop', (CR, 'cells', 2, 'as_filmed'), GONE,
+ CELLS + r"does not say whether it is that frame as filmed \(cell_record\.cells\[2\]\.as_filmed\)"),
+ 'filmed-null': ('read-on-cut', (CR, 'cells', 2, 'as_filmed'), None,
+ CELLS + r"does not say whether it is that frame as filmed \(cell_record\.cells\[2\]\.as_filmed\)"),
+ 'filmed-int': ('this-loop', (CR, 'cells', 2, 'as_filmed'), 1,
+ CELLS + r"does not say whether it is that frame as filmed \(cell_record\.cells\[2\]\.as_filmed\)"),
+ 'filmed-text': ('read-on-cut', (CR, 'cells', 2, 'as_filmed'), 'true',
+ CELLS + r"does not say whether it is that frame as filmed \(cell_record\.cells\[2\]\.as_filmed\)")}
+
+
[email protected]('case', list(UNGIVEN))
+def test_a_record_of_its_cells_not_there_or_not_as_video_loop_writes_it_is_refused_no_match_naming_it(cuts, tmp_path, case):
+ # The frames are as filmed, and the record does not say so as video-loop writes it: none (as a cut made before
+ # video-loop wrote one), another strip's, or a field of it, of any cell, missing or not of its kind. Not read as off.
a, tight, region, mask = cuts
- read_kw, loop_kw, said = UNREAD[case]
- read = cut_of(tmp_path/'read', 4, tight, **read_kw) if read_kw else a
- loop_dir = cut_of(tmp_path/'b', 1, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1), **loop_kw)
- strip = next(read.glob('*.strip.png'))
- before = files(loop_dir)
- with pytest.raises(SystemExit, match=r"no-match — the regions were read on another cut, whose cell 0 is source frame 4 "
- r"\(this loop's cut's cell 3\), but where the anchor moved that frame before the crop "
- r"cannot be read off " + said + r"; locate them again on this strip$"):
- sway.sway_loop(loop_dir, [region], region_mask=mask, read_on=sha(strip), read_on_cut=strip, **KW)
- assert files(loop_dir) == before
+ side, at, to, said = UNGIVEN[case]
+ read = cut_of(tmp_path/'read', 4, tight)
+ loop_dir = cut_of(tmp_path/'b', 1, wider(tight))
+ recorded(read if side == 'read-on-cut' else loop_dir, *at, to=to)
+ region, kw = read_on(read)
+ refused(loop_dir, kw, rf"but {WHOSE[side]} strip record does not give its cells: {said}", region)
+
+
+NOT_FILMED = { # a cut's record of the cell paired, on the cut the regions were read on (cell 0) or on this loop's
+ # (cell 3): the field changed and to what, and the refusal's words
+ 'held': ('read-on-cut', ('scale', 'size_hold'), 0.85,
+ r"the size hold scaled it by 0\.85 about its feet before the cut \(cell_record\.cells\[0\]\.scale\.size_hold\), "
+ r"and no whole px shift undoes a scale"),
+ 'held-here': ('this-loop', ('scale', 'size_hold'), 1.25,
+ r"the size hold scaled it by 1\.25 about its feet before the cut \(cell_record\.cells\[3\]\.scale\."
+ r"size_hold\), and no whole px shift undoes a scale"),
+ 'fallback': ('read-on-cut', ('scale', 'gait_fallback'), 1.1,
+ r"the gait fallback scaled it by 1\.1 about its feet before the cut \(cell_record\.cells\[0\]\.scale\."
+ r"gait_fallback\), and no whole px shift undoes a scale"),
+ 'fallback-here': ('this-loop', ('scale', 'gait_fallback'), 0.95,
+ r"the gait fallback scaled it by 0\.95 about its feet before the cut \(cell_record\.cells\[3\]\.scale\."
+ r"gait_fallback\), and no whole px shift undoes a scale"),
+ 'not-filmed': ('read-on-cut', ('as_filmed',), False,
+ r"video-loop did not cut it as filmed \(cell_record\.cells\[0\]\.as_filmed\)"),
+ 'not-filmed-here': ('this-loop', ('as_filmed',), False,
+ r"video-loop did not cut it as filmed \(cell_record\.cells\[3\]\.as_filmed\)")}
+
+
[email protected]('case', list(NOT_FILMED))
+def test_a_cell_paired_its_record_calls_scaled_or_not_as_filmed_is_refused_no_match_saying_which(cuts, tmp_path, case):
+ a, tight, region, mask = cuts
+ side, field, to, said = NOT_FILMED[case]
+ read = cut_of(tmp_path/'read', 4, tight)
+ loop_dir = cut_of(tmp_path/'b', 1, wider(tight))
+ cell = 0 if side == 'read-on-cut' else 3
+ recorded(read if side == 'read-on-cut' else loop_dir, CR, 'cells', cell, *field, to=to)
+ region, kw = read_on(read)
+ refused(loop_dir, kw, PAIRED + rf"but {WHOSE[side]} cell {cell} is not that frame as filmed: {said}"
+ + (NOT_ANCHORED if field == ('as_filmed',) else ''), region)
+
+
+MADE = { # a cell an interpolator made, frame 4 as the cut the regions were read on (cell 0) or this loop's cut (cell 3)
+ # draws it: its record as video-loop writes it, or changed to call it the frame as filmed; the refusal's words
+ 'read-on-cut': ('read-on-cut', False, r"but the cut's \(--read-on-cut\) cell 0 is not that frame as filmed: video-loop "
+ r"did not cut it as filmed \(cell_record\.cells\[0\]\.as_filmed\)" + NOT_ANCHORED),
+ 'this-loop': ('this-loop', False, r"but this loop's cell 3 is not that frame as filmed: video-loop did not cut it as "
+ r"filmed \(cell_record\.cells\[3\]\.as_filmed\)" + NOT_ANCHORED),
+ 'read-on-cut-said-as-filmed': ('read-on-cut', True, r"but the two cells are not the same pixels where they overlap at "
+ r"the shift \(3, 2\): \d+ of \d+ px differ"),
+ 'this-loop-said-as-filmed': ('this-loop', True, r"but the two cells are not the same pixels where they overlap at the "
+ r"shift \(3, 2\): \d+ of \d+ px differ")}
+
+
[email protected]('case', list(MADE))
+def test_a_cell_an_interpolator_made_is_refused_no_match_whatever_its_record_says(cuts, tmp_path, case):
+ # Frame 4, drawn between frames 3 and 5 in one cut: not the patch drawn on the other cut's cell 0 moved by the crops.
+ # Its record says so as video-loop writes it; changed to say it is the frame as filmed, its pixels say it is not.
+ a, tight, region, mask = cuts
+ side, said_as_filmed, said = MADE[case]
+ wide = wider(tight)
+ read = cut_of(tmp_path/'read', 4, tight, made=(0,) if side == 'read-on-cut' else ())
+ loop_dir = cut_of(tmp_path/'b', 1, wide, made=(3,) if side == 'this-loop' else ())
+ assert not np.array_equal(patch_in(loop_dir, 3), moved(patch_in(read, 0), [3, 2], wide))
+ if said_as_filmed:
+ recorded(read if side == 'read-on-cut' else loop_dir, CR, 'cells', 0 if side == 'read-on-cut' else 3, 'as_filmed',
+ to=True)
+ region, kw = read_on(read, patch_at(4, tight)[1]) # the patch where frame 4 as filmed draws it
+ refused(loop_dir, kw, PAIRED + said, region)
def test_a_loop_aligned_from_its_cut_named_by_read_on_cut_is_refused_no_match(tmp_path):
# The cut from frame 1 aligned on its own (video-cycle-align): its record keeps the cut's, source frames 1 to N in
# order, but its cell 0 is another of them, so the frame the record's cut starts on is not where the regions were read.
- o = cut(tmp_path/'o', clip()[1:1+N])
- (o/'run.strip.json').write_text(json.dumps(meta_of(o) | {'source_cut': {
- 'source': {'sha256': 'clip-a', 'frames': CLIP, 'fps': FPS}, 'start': 1, 'length': N, 'samples': [1+k for k in range(N)]}},
- indent=2)+'\n')
+ o = as_cut(cut(tmp_path/'o', clip()[1:1+N]), 1)
turned = aligned(o, tmp_path/'turned')
assert meta_of(turned)['source_cut']['samples'][0] == 1 and meta_of(turned)['cycle_align']['cells_from'][0]['source'] != 0
drawn = patch_in(turned, 0)
ys, xs = np.nonzero(drawn)
loop_dir = cut_of(tmp_path/'b', 1, bbox(clip()))
- before = files(loop_dir)
- with pytest.raises(SystemExit, match=r'no-match — the regions were read on another cut, but the strip --read-on-cut names '
- r'is a loop aligned from its cut \(cycle_align\), whose cells are not the cut.s frames '
- r'in order; locate them again on this strip$'):
- sway.sway_loop(loop_dir, [(float(xs.mean()), float(ys.mean()), 18.0, 18.0)],
- region_mask=mask_png(tmp_path/'mask.png', drawn), read_on=sha(turned/'run.strip.png'),
- read_on_cut=turned/'run.strip.png', **KW)
- assert files(loop_dir) == before
+ refused(loop_dir, dict(region_mask=mask_png(tmp_path/'mask.png', drawn), read_on=sha(turned/'run.strip.png'),
+ read_on_cut=turned/'run.strip.png', **KW),
+ r"but the strip --read-on-cut names is a loop aligned from its cut \(cycle_align\), whose cells are not the "
+ r"cut.s frames in order", (float(xs.mean()), float(ys.mean()), 18.0, 18.0))
def test_a_cut_of_another_clip_is_refused_no_match(cuts, tmp_path):
a, tight, region, mask = cuts
- other = cut_of(tmp_path/'other', 4, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1), source='clip-b')
- before = files(other)
- with pytest.raises(SystemExit, match=r'no-match — the regions were read on another cut, of another clip \(clip-a; this '
- r"loop's is clip-b\)"):
- sway.sway_loop(other, [region], region_mask=mask, read_on=sha(a/'run.strip.png'), read_on_cut=a/'run.strip.png', **KW)
- assert files(other) == before
+ other = cut_of(tmp_path/'other', 4, wider(tight), source='clip-b')
+ refused(other, dict(region_mask=mask, read_on=sha(a/'run.strip.png'), read_on_cut=a/'run.strip.png', **KW),
+ r"of another clip \(clip-a; this loop's is clip-b\)", region)
def test_what_the_key_is_refused_for_leaves_the_loop_as_it_was(cuts, tmp_path):
a, tight, region, mask = cuts
- loop_dir = cut_of(tmp_path/'b', 1, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1))
+ loop_dir = cut_of(tmp_path/'b', 1, wider(tight))
before = files(loop_dir)
text = ','.join(f'{v:g}' for v in region)
for args, said in (
(['--read-on-cut', str(a/'run.strip.png')], r'--read-on-cut names the strip --read-on names by its sha256; it needs --read-on$'),
(['--read-on', 'ab'*32, '--read-on-cut', str(a/'run.strip.png')], r'is [0-9a-f]{64}, not the strip --read-on names'),
(['--read-on', sha(a/'run.strip.png'), '--read-on-cut', str(tmp_path/'none.png')], r'wants the strip.s PNG and its record')):
- with pytest.raises(SystemExit, match=said) as refused:
+ with pytest.raises(SystemExit, match=said) as refusal:
cli.main(['video-sway', '--loop-dir', str(loop_dir), '--region', text, '--region-mask', str(mask), *args])
- assert refused.value.code not in (0, None), args
+ assert refusal.value.code not in (0, None), args
assert files(loop_dir) == before
# And on the command line it is taken as the call takes it.
assert cli.main(['video-sway', '--loop-dir', str(loop_dir), '--region', text, '--region-mask', str(mask), '--region-shrink',
@@ -414,205 +648,7 @@ def test_what_the_key_is_refused_for_leaves_the_loop_as_it_was(cuts, tmp_path):
assert meta_of(loop_dir)['follow']['read_on']['relation'] == 'recut'
-# ---------------------------------------------------------------- only the frame as filmed, as the cut's report says
-
-def test_a_cell_rife_made_is_refused_no_match_not_paired_as_the_frame_filmed(cuts, tmp_path):
- # The loop's cell 3, frame 4 of the clip, made again by RIFE between the frames either side (the jump repair,
- # `jump_repair.replaced`): it is not frame 4 as filmed, and the patch there is not the patch drawn on the cut's cell 0
- # moved by the crops. The strip's record says nothing of it; the report beside it does.
- a, tight, region, mask = cuts
- wide = (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1)
- loop_dir = cut_of(tmp_path/'b', 1, wide, made=(3,), report={'jump_repair': {'applied': True, 'replaced': [3]}})
- assert not np.array_equal(patch_in(loop_dir, 3), moved(patch_in(a, 0), [3, 2], wide))
- assert 'jump_repair' not in meta_of(loop_dir)
- before = files(loop_dir)
- with pytest.raises(SystemExit, match=r"no-match — the regions were read on another cut, whose cell 0 is source frame 4 "
- r"\(this loop's cut's cell 3\), but this loop's records do not give that frame as "
- r"filmed: its cell 3 is frame 3 of its cut, made again by RIFE, or given way to the "
- r"frame beside it for a filmed ghost \(the jump repair\) \(jump_repair\.replaced\); "
- r"locate them again on this strip$"):
- sway.sway_loop(loop_dir, [region], region_mask=mask, read_on=sha(a/'run.strip.png'), read_on_cut=a/'run.strip.png', **KW)
- assert files(loop_dir) == before
-
-
-WHOSE = {'read-on-cut': r"the cut's \(--read-on-cut\)", 'this-loop': "this loop's"}
-BESIDE = { # what is beside a cut's strip in place of the report video-loop wrote of that cut, and the refusal's words
- 'none': r"no report video-loop wrote of its cut is beside its strip \(in its folder or the one above it\), and its "
- r"record does not say whether the size hold, the gait fallback or RIFE changed its frames",
- 'another-cuts': r"the report beside its strip is another cut's: its strip's source_cut is not the record's"}
-
-
[email protected]('side', list(WHOSE))
[email protected]('beside', list(BESIDE))
-def test_a_cut_with_no_report_of_its_own_beside_its_strip_is_refused_no_match(cuts, tmp_path, side, beside):
- # The strip's record says which source frame each cell is and nothing of the size hold, the gait fallback or RIFE:
- # with no report video-loop wrote of the cut beside it, or only the report of the same cut of another take (its
- # strip's source_cut names another clip), nothing says the frame is as filmed.
- a, tight, region, mask = cuts
- report = {'none': False, 'another-cuts': None}[beside] # no report, or its own (made another take's below)
- read = cut_of(tmp_path/'read', 4, tight, report=report) if side == 'read-on-cut' else a
- loop_dir = cut_of(tmp_path/'b', 1, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1),
- report=report if side == 'this-loop' else None)
- if beside == 'another-cuts':
- path = (read if side == 'read-on-cut' else loop_dir)/'run.loop.report.json'
- other = json.loads(path.read_text())
- other['strip']['source_cut']['source']['sha256'] = 'clip-b' # the same cut of another take
- path.write_text(json.dumps(other, indent=2)+'\n')
- before = files(loop_dir)
- with pytest.raises(SystemExit, match=r"no-match — the regions were read on another cut, whose cell 0 is source frame 4 "
- r"\(this loop's cut's cell 3\), but " + WHOSE[side] + r" records do not give that "
- r"frame as filmed: " + BESIDE[beside] + r"; locate them again on this strip$"):
- sway.sway_loop(loop_dir, [region], region_mask=mask, read_on=sha(read/'run.strip.png'),
- read_on_cut=read/'run.strip.png', **KW)
- assert files(loop_dir) == before
-
-
-CHANGED = { # what the report says a step of video-loop did to a cut's frames, in place of off: on the cut the regions
- # were read on or on this loop's, the frames the two are cut from, and the refusal's words
- 'seam-bridge': ('read-on-cut', 4, 1, {'jump_repair': {'applied': True, 'reading': 'seam', 'replaced': [12, 0]}},
- r"its cell 0 is frame 0 of its cut, made again by RIFE round the wrap \(the seam bridge\) "
- r"\(jump_repair\.replaced\)"),
- 'overlapped-wrap': ('this-loop', 4, 1, {'wrap_overlap': {'frames': [0, 1, 2, 3]}},
- r"its cell 3 is frame 3 of its cut, made again beside the wrap from the clip's frames a cycle apart "
- r"\(the overlapped wrap\) \(wrap_overlap\.frames\)"),
- 'wrap-blend': ('this-loop', 13, 1, {'wrap_blend': {'applied': True, 'K': 3, 'cells': [10, 11, 12]}},
- r"its cell 12 is frame 12 of its cut, redrawn toward the first frame's drawing before the wrap "
- r"\(the wrap blend\) \(wrap_blend\.cells\)"),
- 'gait-fallback': ('this-loop', 4, 1, {'gait_fallback': {'scale_undone': True}},
- r"the gait fallback held its frames to one size, scaled about their feet, before it was cut "
- r"\(gait_fallback\): its crop is not on the frame as filmed, and no whole px shift undoes a scale"),
- 'repair-unsaid': ('read-on-cut', 4, 1, {'jump_repair': None},
- r"its report does not say what the jump repair made of its frames \(jump_repair\)"),
- 'frames-not-whole': ('this-loop', 4, 1, {'jump_repair': {'applied': True, 'replaced': [3.0]}},
- r"its report does not name the frames of its cut it changed as whole frames "
- r"\(jump_repair\.replaced\)")}
-
-
[email protected]('case', list(CHANGED))
-def test_a_frame_the_report_says_video_loop_changed_is_refused_no_match_saying_which(tmp_path, case):
- side, read_at, loop_at, steps, said = CHANGED[case]
- tight = bbox(clip())
- read = cut_of(tmp_path/'read', read_at, tight, report=steps if side == 'read-on-cut' else None)
- loop_dir = cut_of(tmp_path/'b', loop_at, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1),
- report=steps if side == 'this-loop' else None)
- region, kw = read_on(read)
- before = files(loop_dir)
- with pytest.raises(SystemExit, match=rf"no-match — the regions were read on another cut, whose cell 0 is source frame "
- rf"{read_at} \(this loop's cut's cell {read_at - loop_at}\), but {WHOSE[side]} "
- rf"records do not give that frame as filmed: {said}; locate them again on this strip$"):
- sway.sway_loop(loop_dir, [region], **kw)
- assert files(loop_dir) == before
-
-
-# The refusal's words where a report does not say what a step did as video-loop writes it (`sway.as_filmed`)
-NOT_ONE = r"its report does not say what the {} did to its frames: its record of it is not one \({}\)"
-NOT_HELD = r"its report does not say whether the {} held its frames to one size \({}\.{}\)"
-NOT_CHANGED = r"its report does not say whether the {} changed a frame of its cut \({}\.applied\)"
-NOT_WHOLE = r"its report does not name the frames of its cut it changed as whole frames \({}\.{}\)"
-NOT_THERE = r"its report holds no record of the {} \({}\), which video-loop writes of every {}, whether it ran or not"
-NOT_TOLD = (r"its report holds no record of the {} \({}\), and its strip does not say how its cut was cut, which tells "
- r"whether video-loop writes one of it \(strip\.{}\)")
-NAMES = r"its report says the {} changed {} of its cut \({}\.applied\) and names {} \({}\.{}\)"
-WALK = {'kind': 'fixed', 'state': 'walk'} # a walk cut it is given (--cycle fixed), as the report's strip says it
-
-UNSAID = { # what a report says of a step of video-loop, on the cut the regions were read on or on this loop's, in place
- # of off (GONE: nothing; `strip`: how its strip says the cut was cut), not as video-loop writes it; the refusal's words
- 'hold-applied-gone': ('read-on-cut', {'size_hold': {'why': '--size-hold off'}},
- NOT_HELD.format('size hold', 'size_hold', 'applied')),
- 'hold-applied-null': ('this-loop', {'size_hold': {'applied': None}}, NOT_HELD.format('size hold', 'size_hold', 'applied')),
- 'hold-applied-zero': ('read-on-cut', {'size_hold': {'applied': 0}}, NOT_HELD.format('size hold', 'size_hold', 'applied')),
- 'hold-applied-text': ('this-loop', {'size_hold': {'applied': 'false'}},
- NOT_HELD.format('size hold', 'size_hold', 'applied')),
- 'hold-null': ('read-on-cut', {'size_hold': None}, NOT_ONE.format('size hold', 'size_hold')),
- 'hold-list': ('this-loop', {'size_hold': []}, NOT_ONE.format('size hold', 'size_hold')),
- 'hold-text': ('read-on-cut', {'size_hold': 'held'}, NOT_ONE.format('size hold', 'size_hold')),
- 'hold-gone-walk': ('this-loop', {'size_hold': GONE, 'strip': WALK},
- NOT_THERE.format('size hold', 'size_hold', r'walk cut it is given \(--cycle fixed\)')),
- 'hold-gone-motion-auto': ('read-on-cut', {'size_hold': GONE, 'strip': {'foot_anchor': 'motion-auto'}},
- NOT_THERE.format('size hold', 'size_hold', 'cut at --anchor motion-auto')),
- 'hold-gone-anchor-untold': ('this-loop', {'size_hold': GONE, 'strip': {'foot_anchor': GONE}},
- NOT_TOLD.format('size hold', 'size_hold', 'foot_anchor')),
- 'hold-gone-state-not-text': ('read-on-cut', {'size_hold': GONE, 'strip': {'kind': 'fixed', 'state': 5}},
- NOT_TOLD.format('size hold', 'size_hold', 'state')),
- 'fallback-undone-gone': ('this-loop', {'gait_fallback': {'scale_drift_min': 0.05}},
- NOT_HELD.format('gait fallback', 'gait_fallback', 'scale_undone')),
- 'fallback-undone-null': ('read-on-cut', {'gait_fallback': {'scale_undone': None}},
- NOT_HELD.format('gait fallback', 'gait_fallback', 'scale_undone')),
- 'fallback-undone-zero': ('this-loop', {'gait_fallback': {'scale_undone': 0}},
- NOT_HELD.format('gait fallback', 'gait_fallback', 'scale_undone')),
- 'fallback-null': ('read-on-cut', {'gait_fallback': None}, NOT_ONE.format('gait fallback', 'gait_fallback')),
- 'fallback-text': ('this-loop', {'gait_fallback': 'scaled'}, NOT_ONE.format('gait fallback', 'gait_fallback')),
- 'repair-replaced-gone': ('read-on-cut', {'jump_repair': {'applied': True}}, NOT_WHOLE.format('jump_repair', 'replaced')),
- 'repair-empty': ('this-loop', {'jump_repair': {}}, NOT_CHANGED.format('jump repair', 'jump_repair')),
- 'repair-applied-null': ('read-on-cut', {'jump_repair': {'applied': None}},
- NOT_CHANGED.format('jump repair', 'jump_repair')),
- 'repair-off-applied-gone': ('this-loop', {'jump_repair': {'why': '--repair off'}},
- NOT_CHANGED.format('jump repair', 'jump_repair')),
- 'bridge-replaced-gone': ('read-on-cut', {'jump_repair': {'applied': True, 'reading': 'seam'}},
- NOT_WHOLE.format('jump_repair', 'replaced')),
- 'repair-names-none': ('this-loop', {'jump_repair': {'applied': True, 'replaced': []}},
- NAMES.format('jump repair', 'a frame', 'jump_repair', 'none', 'jump_repair', 'replaced')),
- 'repair-off-names-one': ('read-on-cut', {'jump_repair': {'applied': False, 'replaced': [5]}},
- NAMES.format('jump repair', 'no frame', 'jump_repair', 'frames it changed', 'jump_repair',
- 'replaced')),
- 'overlap-frames-gone': ('this-loop', {'wrap_overlap': {'seam': 3.1}}, NOT_WHOLE.format('wrap_overlap', 'frames')),
- 'overlap-null': ('read-on-cut', {'wrap_overlap': None}, NOT_ONE.format('overlapped wrap', 'wrap_overlap')),
- 'overlap-list': ('this-loop', {'wrap_overlap': []}, NOT_ONE.format('overlapped wrap', 'wrap_overlap')),
- 'overlap-text': ('read-on-cut', {'wrap_overlap': 'made'}, NOT_ONE.format('overlapped wrap', 'wrap_overlap')),
- 'blend-cells-gone': ('this-loop', {'wrap_blend': {'applied': True, 'K': 3}}, NOT_WHOLE.format('wrap_blend', 'cells')),
- 'blend-applied-gone': ('read-on-cut', {'wrap_blend': {'K': 3}}, NOT_CHANGED.format('wrap blend', 'wrap_blend')),
- 'blend-null': ('this-loop', {'wrap_blend': None}, NOT_ONE.format('wrap blend', 'wrap_blend')),
- 'blend-text': ('read-on-cut', {'wrap_blend': 'blended'}, NOT_ONE.format('wrap blend', 'wrap_blend')),
- 'blend-names-none': ('this-loop', {'wrap_blend': {'applied': True, 'K': 3, 'cells': []}},
- NAMES.format('wrap blend', 'a frame', 'wrap_blend', 'none', 'wrap_blend', 'cells')),
- 'blend-gone-pinned': ('read-on-cut', {'strip': {'kind': 'pinned'}},
- NOT_THERE.format('wrap blend', 'wrap_blend', 'pinned cut')),
- 'blend-gone-kind-untold': ('this-loop', {'strip': {'kind': GONE}}, NOT_TOLD.format('wrap blend', 'wrap_blend', 'kind'))}
-
-
[email protected]('case', list(UNSAID))
-def test_a_report_that_does_not_say_what_a_step_did_as_video_loop_writes_it_is_refused_no_match_naming_it(tmp_path, case):
- # The frames are as filmed; the report does not say they are, as video-loop says it: a step's record, or a field of
- # one, missing or not of its kind; a step's record missing where video-loop writes one whether the step ran or not, or
- # where the strip does not say how the cut was cut; an `applied` the frames it names do not bear out. Not read as off.
- side, steps, said = UNSAID[case]
- tight = bbox(clip())
- read = cut_of(tmp_path/'read', 4, tight, report=steps if side == 'read-on-cut' else None)
- loop_dir = cut_of(tmp_path/'b', 1, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1),
- report=steps if side == 'this-loop' else None)
- region, kw = read_on(read)
- before = files(loop_dir)
- with pytest.raises(SystemExit, match=rf"no-match — the regions were read on another cut, whose cell 0 is source frame 4 "
- rf"\(this loop's cut's cell 3\), but {WHOSE[side]} records do not give that frame "
- rf"as filmed: {said}; locate them again on this strip$"):
- sway.sway_loop(loop_dir, [region], **kw)
- assert files(loop_dir) == before
-
-
-NOT_RUN = { # what video-loop writes of a step that did not run, in both cuts' reports in place of off (GONE: nothing;
- # `strip`: how its strip says the cut was cut)
- 'as-written': {},
- 'hold-off-walk': {'strip': WALK},
- 'hold-gone-idle': {'size_hold': GONE, 'strip': {'kind': 'fixed', 'state': 'idle'}},
- 'hold-gone-given-no-state': {'size_hold': GONE, 'strip': {'kind': 'fixed'}},
- 'fallback-not-undone': {'gait_fallback': {'scale_undone': False}},
- 'repair-replaced-none-motion-auto': {'jump_repair': {'applied': False, 'replaced': [], 'reading': 'flow'},
- 'strip': {'foot_anchor': 'motion-auto'}},
- 'bridge-replaced-none': {'jump_repair': {'applied': False, 'reading': 'seam', 'replaced': []}},
- 'overlap-skipped': {'wrap_overlap': {'frames': [], 'skipped': '--overlap-wrap off'}},
- 'blend-off-pinned': {'wrap_blend': {'applied': False, 'why': '--wrap-blend off'}, 'strip': {'kind': 'pinned'}},
- 'blend-kept-pinned': {'wrap_blend': {'applied': False, 'K': 3, 'cells': []}, 'strip': {'kind': 'pinned'}}}
-
-
[email protected]('case', list(NOT_RUN))
-def test_a_step_its_report_says_did_not_run_as_video_loop_writes_it_leaves_the_frame_paired(tmp_path, case):
- tight = bbox(clip())
- a = cut_of(tmp_path/'a', 4, tight, report=NOT_RUN[case])
- b = cut_of(tmp_path/'b', 1, (tight[0]-3, tight[1]-2, tight[2]+2, tight[3]+1), report=NOT_RUN[case])
- _, _, answer = read_on_cut(a, b)
- assert (answer.read_on['relation'], answer.read_on['cut_cell'], answer.read_on['shift_px']) == ('recut', 3, [3, 2])
- assert patch_in(b, 3).sum() > 100 and np.array_equal(answer.mask.worn[0], patch_in(b, 3))
-
+# ---------------------------------------------------------------- a walk the size hold held to one size
WALK_TORSO = (20, 90, 180) # the walker's torso (tests/video/test_gait_fallback.py)
@@ -638,73 +674,103 @@ def walk_cuts(tmp_path_factory):
for start in (24, 20) for hold in ('auto', 'off')}
-HOLDS = { # how each cut was held (--size-hold): the cut the regions were read on, then this loop's; whose report says it
[email protected]('start', [24, 20])
+def test_video_loop_records_the_factor_the_size_hold_scaled_each_cell_by(walk_cuts, start):
+ held, filmed = meta_of(walk_cuts[start, 'auto'])[CR]['cells'], meta_of(walk_cuts[start, 'off'])[CR]['cells']
+ report = json.loads((walk_cuts[start, 'auto']/'run.loop.report.json').read_text())
+ assert report['size_hold']['applied'] is True
+ assert [c['scale']['size_hold'] for c in held] == [f['scale'] for f in report['size_hold']['cut_frames']]
+ assert all(c['scale']['size_hold'] != 1 and c['as_filmed'] is False for c in held)
+ assert all(c['scale'] == {'size_hold': 1, 'gait_fallback': 1} and c['as_filmed'] is True for c in filmed)
+ assert [c['source_frame'] for c in held] == [c['source_frame'] for c in filmed] == list(range(start, start+24))
+
+
+HELD = r"the size hold scaled it by [0-9.]+ about its feet before the cut \(cell_record\.cells\[{}\]\.scale\.size_hold\), " \
+ r"and no whole px shift undoes a scale"
+HOLDS = { # how each cut was held (--size-hold): the cut the regions were read on, then this loop's; whose record says it
'read-on-cut-held': ('auto', 'off', 'read-on-cut'),
'this-loop-held': ('off', 'auto', 'this-loop'),
'both-held': ('auto', 'auto', 'read-on-cut')}
+WALK_PAIRED = r"whose cell 0 is source frame 24 \(this loop's cut's cell 4\), "
@pytest.mark.slow
@pytest.mark.parametrize('case', list(HOLDS))
def test_a_walk_cut_the_size_hold_held_to_one_size_is_refused_no_match(walk_cuts, tmp_path, case):
# Frame 24 is the first cut's cell 0 and the second's cell 4. A cut held to one size was cut from frames scaled about
- # their feet: its strip's record does not say so, the report beside it does. Held in one cut and not in the other,
- # frame 24's torso is not the same size in the two, and no whole px shift carries the one onto the other.
+ # their feet, and its record of its cells says by how much. Held in one cut and not in the other, frame 24's torso is
+ # not the same size in the two, and no whole px shift carries the one onto the other.
read_hold, loop_hold, side = HOLDS[case]
a, b = copy(walk_cuts[24, read_hold], tmp_path/'a'), copy(walk_cuts[20, loop_hold], tmp_path/'b')
assert meta_of(a)['source_cut']['samples'][0] == meta_of(b)['source_cut']['samples'][4] == 24
- for loop_dir, hold in ((a, read_hold), (b, loop_hold)):
- assert json.loads((loop_dir/'run.loop.report.json').read_text())['size_hold']['applied'] is (hold == 'auto')
- assert 'size_hold' not in meta_of(loop_dir)
drawn = torso_in(a, 0)
assert drawn.sum() > 100
if read_hold != loop_hold:
assert drawn.sum() != torso_in(b, 4).sum()
region, kw = read_on(a, drawn)
- before = files(b)
- with pytest.raises(SystemExit, match=r"no-match — the regions were read on another cut, whose cell 0 is source frame 24 "
- r"\(this loop's cut's cell 4\), but " + WHOSE[side] + r" records do not give that "
- r"frame as filmed: the size hold held its frames to one size, scaled about their "
- r"feet, before it was cut \(size_hold\): its crop is not on the frame as filmed, and "
- r"no whole px shift undoes a scale; locate them again on this strip$"):
- sway.sway_loop(b, [region], **kw)
- assert files(b) == before
-
-
-UNSAID_HOLDS = { # a walk cut the size hold held to one size, its report changed so that it no longer says so: whose
- # report it is (the other cut as filmed), what is taken out of it, and the refusal's words
- 'read-on-cut-applied-gone': ('read-on-cut', 'applied', NOT_HELD.format('size hold', 'size_hold', 'applied')),
- 'read-on-cut-record-gone': ('read-on-cut', 'record',
- NOT_THERE.format('size hold', 'size_hold', r'walk cut it is given \(--cycle fixed\)')),
- 'this-loop-applied-gone': ('this-loop', 'applied', NOT_HELD.format('size hold', 'size_hold', 'applied')),
- 'this-loop-record-gone': ('this-loop', 'record',
- NOT_THERE.format('size hold', 'size_hold', r'walk cut it is given \(--cycle fixed\)'))}
+ cell = 0 if side == 'read-on-cut' else 4
+ refused(b, kw, WALK_PAIRED + rf"but {WHOSE[side]} cell {cell} is not that frame as filmed: " + HELD.format(cell), region)
+
+
+def erase_trace(loop_dir):
+ """Every trace of the size hold and of the walk in a cut's report and its strip's record taken out: the report's
+ size hold, the state its strip's record, the report and the report's strip say."""
+ report_path = loop_dir/'run.loop.report.json'
+ report = json.loads(report_path.read_text())
+ for holder, key in ((report, 'size_hold'), (report, 'state'), (report['strip'], 'state')):
+ holder.pop(key, None)
+ report_path.write_text(json.dumps(report, indent=2)+'\n')
+ return recorded(loop_dir, 'state', to=GONE)
+
+
+def said_as_filmed(loop_dir):
+ """A cut's record of its cells changed to call every cell the frame as filmed, scaled by no hold."""
+ return recorded(loop_dir, CR, 'cells', to=lambda cells: [
+ c | {'scale': {'size_hold': 1.0, 'gait_fallback': 1.0}, 'as_filmed': True} for c in cells])
+
+
+def erase_scale(loop_dir):
+ """A cut's record of its cells with neither the factor each hold scaled a cell by nor whether it is as filmed."""
+ return recorded(loop_dir, CR, 'cells', to=lambda cells: [
+ {k: v for k, v in c.items() if k not in ('scale', 'as_filmed')} for c in cells])
+
+
+NOT_SAME = r"but the two cells are not the same pixels where they overlap at the shift \(-?\d+, -?\d+\): \d+ of \d+ px differ"
+ERASED = { # a walk cut the size hold held to one size, the other cut as filmed: whose cut it is, what is taken out of its
+ # records or changed in them, and the refusal's words
+ 'trace': ('read-on-cut', (erase_trace,), WALK_PAIRED + r"but the cut's \(--read-on-cut\) cell 0 is not that frame as "
+ r"filmed: " + HELD.format(0)),
+ 'trace-here': ('this-loop', (erase_trace,), WALK_PAIRED + r"but this loop's cell 4 is not that frame as filmed: "
+ + HELD.format(4)),
+ 'trace-and-scale': ('read-on-cut', (erase_trace, erase_scale),
+ r"but the cut's \(--read-on-cut\) strip record does not give its cells: its record of cell 0 does "
+ r"not give the factor each hold scaled it by \(cell_record\.cells\[0\]\.scale: size_hold, "
+ r"gait_fallback\)"),
+ 'trace-and-record': ('read-on-cut', (erase_trace, lambda d: recorded(d, CR, to=GONE)),
+ r"but the cut's \(--read-on-cut\) strip record does not give its cells: it holds no record of "
+ r"them \(cell_record\), as a cut video-loop made before it wrote one does"),
+ 'said-as-filmed': ('read-on-cut', (erase_trace, said_as_filmed), WALK_PAIRED + NOT_SAME),
+ 'said-as-filmed-here': ('this-loop', (erase_trace, said_as_filmed), WALK_PAIRED + NOT_SAME)}
@pytest.mark.slow
[email protected]('case', list(UNSAID_HOLDS))
-def test_a_held_walk_cut_whose_report_no_longer_says_it_was_held_is_refused_no_match(walk_cuts, tmp_path, case):
[email protected]('case', list(ERASED))
+def test_a_held_walk_cut_whose_records_no_longer_say_it_was_held_is_refused_no_match(walk_cuts, tmp_path, case):
# Frame 24 is the first cut's cell 0 and the second's cell 4, held to one size in one cut and as filmed in the other:
- # its torso is not the same size in the two, and no whole px shift carries the one onto the other. video-loop writes
- # the size hold's record of every walk cut it is given, held or not: a report with no record of it, or one that does
- # not say whether it held, does not give the frame as filmed, and is not read as a cut as filmed.
- side, taken, said = UNSAID_HOLDS[case]
+ # its torso is not the same size in the two. The report and the strip's record are no longer read for it: with every
+ # trace of the hold and of the walk taken out of them, the record of each cell still says the hold scaled it; with
+ # that taken out too, the record does not give the cells; changed to say each cell is as filmed, the pixels say not.
+ side, changes, said = ERASED[case]
read_hold, loop_hold = ('auto', 'off') if side == 'read-on-cut' else ('off', 'auto')
a, b = copy(walk_cuts[24, read_hold], tmp_path/'a'), copy(walk_cuts[20, loop_hold], tmp_path/'b')
- path = (a if side == 'read-on-cut' else b)/'run.loop.report.json'
- report = json.loads(path.read_text())
- assert report['size_hold']['applied'] is True and (report['strip']['kind'], report['strip']['state']) == ('fixed', 'walk')
- if taken == 'applied':
- del report['size_hold']['applied']
- else:
- del report['size_hold']
- path.write_text(json.dumps(report, indent=2)+'\n')
+ held = a if side == 'read-on-cut' else b
+ for change in changes:
+ change(held)
+ report = json.loads((held/'run.loop.report.json').read_text())
+ assert 'size_hold' not in report and 'state' not in report and 'state' not in report['strip']
+ assert 'state' not in meta_of(held) and 'size_hold' not in meta_of(held)
drawn = torso_in(a, 0)
assert drawn.sum() > 100 and drawn.sum() != torso_in(b, 4).sum()
region, kw = read_on(a, drawn)
- before = files(b)
- with pytest.raises(SystemExit, match=rf"no-match — the regions were read on another cut, whose cell 0 is source frame 24 "
- rf"\(this loop's cut's cell 4\), but {WHOSE[side]} records do not give that frame "
- rf"as filmed: {said}; locate them again on this strip$"):
- sway.sway_loop(b, [region], **kw)
- assert files(b) == before
+ refused(b, kw, said, region)