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(prop-plate): video-frames finds the plate in each frame, moved or turned, and erases it and the line its edge blends into the key there, names the frames it was not found in or meets the subject in, and learns the palette without it

Soohong Kim8a9bd0fe44e6
diff --git a/sprite_gen/video/frames.py b/sprite_gen/video/frames.py
index aa786de..fc53ad7 100644
--- a/sprite_gen/video/frames.py
+++ b/sprite_gen/video/frames.py
@@ -53,9 +53,11 @@ and stay: the outline's loose pixels one transparent pixel off, a shadow drawn u
shoe's part the matte cut loose. How many each frame lost is in its report row (`specks`).
A **plate** painted into the canvas (`video-canvas --prop-plate`, a low wall for the subject to clear) is taken out of
-every keyed frame of a run that passed: `--prop-plate` names that canvas's report, whose box is the plate's
-(`prop_plate.read`), and the erase runs on the frames before they are handed over (`prop_plate.erase`; the report's
-`prop_plate`). A refused run's frames keep it.
+every keyed frame of a run that passed: `--prop-plate` names that canvas's report, whose box is where the plate is
+searched for in the first frame (`prop_plate.read`). It is found in each frame as decoded, where the camera carried it
+or the subject turned it (`prop_plate.find`), its place is left out of what the edge pass learns the subject's palette
+from (`prop_plate.not_subject`), and the erase runs on the frames before they are handed over (`prop_plate.erase`; the
+report's `prop_plate`, a row a frame). A refused run's frames keep it.
"""
from __future__ import annotations
@@ -67,7 +69,7 @@ import shutil
import subprocess
import sys
from pathlib import Path
-from typing import Any
+from typing import Any, Callable
from PIL import Image
@@ -484,7 +486,8 @@ def key_frames(
"""Key `raw_files` into `keyed_dir` and read their edges; the report. The frames are keyed in a working folder
beside it and handed over as `keyed_dir` when the run passes; a run that does not pass leaves them as
`refused_dir(keyed_dir)` and no `keyed_dir`. A refusal raises `KeyingRefused`, carrying the report. A `plate`
- (`prop_plate.read`) is erased from the frames of a run that passed before they are handed over."""
+ (`prop_plate.read`) is found in each frame (`prop_plate.find`), left out of the edge pass's palette, and erased from
+ the frames of a run that passed before they are handed over."""
from . import effect
if alpha_mode not in effect.ALPHA_MODES:
raise SystemExit(f"video-frames: unknown --alpha-mode {alpha_mode!r}")
@@ -496,11 +499,19 @@ def key_frames(
raise SystemExit(f"video-frames: unknown --decontam {decontam!r}; expected one of {', '.join(DECONTAM_MODES)}")
work = _withdraw(keyed_dir)
try:
+ found = plate_mod.find(raw_files, plate) if plate is not None else None
+ not_subject = None
+ if found is not None:
+ with Image.open(raw_files[0]) as first:
+ size = first.size # one size for every frame of a clip
+
+ def not_subject(k: int) -> np.ndarray | None:
+ return plate_mod.not_subject(found[k], size)
report = (effect.key_frames(raw_files, work) if alpha_mode == "brightness" else
_key_chroma(raw_files, work, key=key, check_edges=check_edges, spill=spill, allow_subject=allow_subject,
- decontam=decontam))
- if plate is not None:
- report["prop_plate"] = plate_mod.erase([work / f.name for f in raw_files], plate)
+ decontam=decontam, not_subject=not_subject))
+ if found is not None:
+ report["prop_plate"] = plate_mod.erase([work / f.name for f in raw_files], raw_files, found, plate)
except BaseException:
shutil.rmtree(refused_dir(keyed_dir), ignore_errors=True)
work.rename(refused_dir(keyed_dir))
@@ -513,7 +524,9 @@ def key_frames(
def _key_chroma(raw_files: list[Path], keyed_dir: Path, *, key: str, check_edges: bool, spill: str, allow_subject: bool,
- decontam: str) -> dict[str, Any]:
+ decontam: str, not_subject: Callable[[int], np.ndarray | None] | None = None) -> dict[str, Any]:
+ # `not_subject(k)`: the pixels of frame k that are not the subject's (a prop plate's place), which the edge pass's
+ # palette is learned without on the frame it is learned on
clip_palette: dict[str, Any] | None = None
clip_history: dict[str, Any] = {} # the last frame's calls, in clip order
decontam_first: dict[str, Any] | None = None
@@ -565,9 +578,11 @@ def _key_chroma(raw_files: list[Path], keyed_dir: Path, *, key: str, check_edges
for k, src in enumerate(raw_files):
dst = keyed_dir / src.name
+ learn_without = not_subject(k) if not_subject is not None and decontam != OFF and clip_palette is None else None
stats = cutout(src, dst, key=key, spill_max_fraction=spill_max, spill_min_tint=spill_tint,
spill_require_hue=spill == "full", decontam=decontam, decontam_fit=DECONTAM_FIT,
decontam_palette=clip_palette, decontam_edge_band=EDGE_ROWS, decontam_history=clip_history,
+ decontam_not_subject=learn_without,
# one exact key for the whole clip: an unrecognised background is refused below and its
# still normalized by `video-canvas` (docs/chroma-alpha.md "The measured background")
measure_unrecognised=False, refuse_empty=False)
@@ -783,7 +798,7 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--source-manifest", type=Path, help="write exact clip/canvas/keyed-file digests and source timestamps (requires --reference)")
parser.add_argument("--spill", choices=SPILL_MODES, default="small", help="small: correct only small key-tinted clusters (default); full: every key tint in the subject is spill; auto: decide from --reference")
parser.add_argument("--reference", type=Path, help="the still the clip was made from (required by --spill auto)")
- parser.add_argument("--prop-plate", type=Path, help="the `video-canvas --report` of a canvas painted with --prop-plate: the plate's box it holds is erased from every keyed frame (the subject crossing in front of it stays); the report's `prop_plate` says what went")
+ parser.add_argument("--prop-plate", type=Path, help="the `video-canvas --report` of a canvas painted with --prop-plate: the plate is searched for from the box it holds, found in each frame where the camera carried it or the subject turned it, and erased from every keyed frame (the subject crossing in front of it stays); the report's `prop_plate` says where it was and what went, a row a frame")
parser.add_argument("--decontam", choices=DECONTAM_MODES, default=DECONTAM_DEFAULT, help="auto (default): re-explain key-tinted edges with the subject's own colours (one palette per clip, video fit) where it applies, the reason recorded where not (a white key, a subject with no interior; brightness alpha is off); palette: the same, failing where it cannot run; off: the matte as is")
@@ -799,6 +814,8 @@ def run(**kwargs: object) -> int:
line = decontam_warning(payload["decontam"])
if line:
print(f"video-frames: warning: {line}", file=sys.stderr)
+ for line in plate_mod.warnings(payload.get("prop_plate") or {}):
+ print(f"video-frames: warning: {line}", file=sys.stderr)
return 0
diff --git a/sprite_gen/video/prop_plate.py b/sprite_gen/video/prop_plate.py
index 3638b3d..b3b711b 100644
--- a/sprite_gen/video/prop_plate.py
+++ b/sprite_gen/video/prop_plate.py
@@ -1,50 +1,76 @@
# SPDX-License-Identifier: Apache-2.0
-"""A flat magenta plate painted into a clip's still for the subject to clear, and taken out of its keyed frames
-again (`video-canvas --prop-plate`, `video-frames --prop-plate`).
+"""A flat magenta plate painted into a clip's still for the subject to clear, found in each frame of the clip and taken
+out of its keyed frames again (`video-canvas --prop-plate`, `video-frames --prop-plate`).
A vault needs something to vault. Asked in words for a wall in the background colour, a video model draws a black
slab, and keying leaves the slab in the frames. So the wall is drawn into the still instead, as a plate the key
does not take and the erase does: flat magenta on the green key, standing on the ground in front of the subject.
-One box is the plate. `place` sets it on the canvas, and `video-canvas` paints it and writes it into its report
-(`prop_plate.box`, canvas px, right and bottom one past). `video-frames` reads it from that report (`read`) and
-erases inside it on every keyed frame (`erase`), the box carried onto the clip's frames by the ratio of their size
-to the canvas's where the video model returned another size.
+`place` sets the plate's box on the canvas, and `video-canvas` paints it and writes it into its report
+(`prop_plate.box`, canvas px, right and bottom one past). `video-frames` reads it from that report (`read`).
The plate, of the subject's height on the canvas (`canvas.subject_box`): a wall as high as the thighs (HEIGHT), a
fifth of the height thick (WIDTH), its near side GAP ahead of the subject's front (the side it faces), its foot on
the subject's foot row.
-The erase: inside the box and a rim of RIM px, every pixel led by red and blue over green (by more than
-MAGENTA_LEAD, red and blue within MAGENTA_BALANCE of each other: the plate, its rim blended into the key too) goes,
-as does every pixel within STILL_TOLERANCE on each channel of what that pixel shows over the whole clip (the median:
-the plate's keyed rim, a ground line drawn under it), since the subject crosses the box for part of the clip only.
-Alpha is one of those channels, so a pixel keyed out over most of the clip is not read as black: the subject's dark
-outline crossing the rim stays, as does the rest of the subject crossing in front of the plate.
+A video model told to keep the camera still pans it anyway, after the body, and turns the plate the body pushes off:
+after the first frames the plate is not where the canvas has it. So it is found in each frame (`find`), on the frame
+as decoded. Its pieces are the pixels of its colour (each channel within PLATE_TOLERANCE of RGB) joined side by side or
+at corners, each at least PIECE_SHARE of the plate's area on the frame, near where it was last found: within the
+plate's own diagonal of that box, and on the first frame of the canvas's box carried onto the frame (`frame_box`). The
+plate is every pixel of its colour in the box its pieces span, so a plate the subject in front of it cuts into pieces
+is found whole. A frame with no piece has no plate found, and the next frame is searched from where it was last found.
-A plate the video model draws a pixel off in a frame leaves its edge there, the codec's blend of magenta and green
-(grey, so neither rule takes it), a line a pixel wide down each side.
+The erase (`erase`) runs on each keyed frame its plate was found in, within RIM px of the plate's pixels, and decides
+on the frame as decoded, not on what the matte or the edge pass made of it. A pixel goes where red and blue lead green
+there (by more than MAGENTA_LEAD, red and blue within MAGENTA_BALANCE of each other: the plate, its edge, an edge pixel
+of the subject it tinted), or where it is the line the plate's edge blends into the key. A codec's 4:2:0 chroma leaves
+that line neither colour (a grey, a dark magenta, a pale green), so it is read by where and what it is: within
+BLEND_REACH px of a pixel of the key's colour (each channel within PLATE_TOLERANCE of it), its light within LUMA_SLACK
+of the span between the plate's and the key's, and its hue on the axis between them (red and blue within BLEND_BALANCE
+plus BLEND_SLOPE of how far their mean is from green). The subject in front of the plate is none of these: its skin,
+cloth and hair lie off that axis, its dark outline below that light and its white above it. A grey of the subject's
+own between the plate and the key goes with the line. The codec can also ring a blue grey into that line, which no
+colour tells from denim: what is left within RIM px of the plate after the two rules, in a piece (opaque pixels joined
+side by side or at corners) that lies wholly there, is the plate's edge as well. The subject reaches beyond the
+plate's rim and stays, with the rest of the line where its pixels touch it; a piece of the subject the plate cuts off
+within its rim would go.
+
+The edge pass (`--decontam`) learns the subject's palette on the first frame it runs on, where the plate stands as
+opaque as the subject: `not_subject` is the plate's place there (its pixels and RIM px round them), left out of what the
+palette is learned from, so the plate's magenta is not taken for a colour the subject owns.
"""
from __future__ import annotations
import json
+import math
from pathlib import Path
from typing import Any
from PIL import Image
from sprite_gen._deps import np
-from sprite_gen.frames.cutout import KEY_TARGETS, MAGENTA
+from sprite_gen.frames.cutout import GREEN, KEY_TARGETS, MAGENTA
+from sprite_gen.frames.decontam import LUMA
RGB = KEY_TARGETS[MAGENTA]
+KEY = KEY_TARGETS[GREEN]
HEIGHT = 0.49 # of the subject's height
WIDTH = 0.20
GAP = 0.84
-RIM = 3 # px added round the box on each side, on the clip's frames
+# px round the plate's pixels the erase reaches: a decoded edge runs on past the plate's colour into a dark magenta and
+# then the line blended into the key, a pixel or two each
+RIM = 4
+BLEND_REACH = 2 # px from a pixel of the key's colour: the line blended into the key lies against it
+PLATE_TOLERANCE = 48 # on each channel: the plate's colour (and the key's) as a codec returns it
+PIECE_SHARE = 0.01 # of the plate's area on the frame: the least a piece of it holds
MAGENTA_LEAD = 35
MAGENTA_BALANCE = 90
-STILL_TOLERANCE = 40
+LUMA_SLACK = 40
+BLEND_BALANCE = 40
+BLEND_SLOPE = 0.35
+NAMED = 6 # frames a warning names before it counts the rest
CANVAS_REPORT = "sprite-gen-video-canvas-report"
@@ -92,37 +118,177 @@ def read(report: Path) -> dict[str, Any]:
def frame_box(box: list[int], canvas: list[int], frame: tuple[int, int]) -> tuple[int, int, int, int]:
- """The canvas `box` on a frame of size `frame`: scaled by frame / canvas each way, rounded outward, widened by
- RIM px, kept on the frame."""
+ """The canvas `box` on a frame of size `frame`: scaled by frame / canvas each way, rounded outward, kept on the
+ frame."""
(x0, y0, x1, y1), (cw, ch), (fw, fh) = box, canvas, frame
- x0, y0 = x0 * fw // cw, y0 * fh // ch
- x1, y1 = -(-x1 * fw // cw), -(-y1 * fh // ch)
- return max(0, x0 - RIM), max(0, y0 - RIM), min(fw, x1 + RIM), min(fh, y1 + RIM)
+ return max(0, x0 * fw // cw), max(0, y0 * fh // ch), min(fw, -(-x1 * fw // cw)), min(fh, -(-y1 * fh // ch))
+
+
+def _colour(rgb: np.ndarray, target: tuple[int, int, int]) -> np.ndarray:
+ """Where `rgb` (H, W, 3 int) is within PLATE_TOLERANCE of `target` on each channel."""
+ return np.abs(rgb - np.asarray(target, dtype=np.int32)).max(axis=2) <= PLATE_TOLERANCE
+
+
+def _decoded(path: Path) -> np.ndarray:
+ with Image.open(path) as im:
+ return np.asarray(im.convert("RGB")).astype(np.int32)
+
+
+def _tilt(ys: np.ndarray, xs: np.ndarray) -> float:
+ """How far the long axis of the pixels at `ys`, `xs` is turned from upright: degrees, clockwise on the frame
+ positive (its top to the right)."""
+ if len(xs) < 2:
+ return 0.0
+ _, vectors = np.linalg.eigh(np.cov(np.stack([xs - xs.mean(), ys - ys.mean()])))
+ vx, vy = vectors[:, 1]
+ if vy > 0:
+ vx, vy = -vx, -vy
+ return round(math.degrees(math.atan2(vx, -vy)), 1) + 0.0
-def erase(files: list[Path], plate: dict[str, Any]) -> dict[str, Any]:
- """Erase the plate from every keyed frame in `files` (in place); the frames report's `prop_plate`."""
+def find(files: list[Path], plate: dict[str, Any]) -> list[dict[str, Any]]:
+ """Where the plate (`read`) is in each frame of `files`, the clip's frames as decoded: a row a frame, `found` and,
+ where it was, the `box` its pieces span (frame px, right and bottom one past), its pixels' count (`plate_px`), how
+ far it is turned (`tilt`, `_tilt`), and its pixels in the box (`pixels`, for `region`)."""
+ from sprite_gen.video.frames import _components
with Image.open(files[0]) as first:
- x0, y0, x1, y1 = frame_box(plate["box"], plate["canvas"], first.size)
- crops = []
+ size = first.size
+ last = frame_box(plate["box"], plate["canvas"], size)
+ width, height = last[2] - last[0], last[3] - last[1]
+ reach = math.ceil(math.hypot(width, height))
+ least = max(2, round(PIECE_SHARE * width * height))
+ rows: list[dict[str, Any]] = []
for f in files:
- with Image.open(f) as im:
- crops.append(np.asarray(im.convert("RGBA"))[y0:y1, x0:x1])
- still = np.median(np.stack(crops), axis=0).astype(np.int32)
+ rgb = _decoded(f)
+ h, w = rgb.shape[:2]
+ wx0, wy0 = max(0, last[0] - reach), max(0, last[1] - reach)
+ wx1, wy1 = min(w, last[2] + reach), min(h, last[3] + reach)
+ own = _colour(rgb[wy0:wy1, wx0:wx1], RGB)
+ ys, starts, stops, labels, sizes = _components(own, diagonal=True)
+ piece = sizes[labels] >= least if len(ys) else np.zeros(0, dtype=bool)
+ if not piece.any():
+ rows.append({"frame": f.name, "found": False})
+ continue
+ x0, x1 = int(starts[piece].min()), int(stops[piece].max())
+ y0, y1 = int(ys[piece].min()), int(ys[piece].max()) + 1
+ pixels = own[y0:y1, x0:x1]
+ py, px = np.nonzero(pixels)
+ box = (x0 + wx0, y0 + wy0, x1 + wx0, y1 + wy0)
+ rows.append({"frame": f.name, "found": True, "box": list(box), "plate_px": int(len(px)),
+ "tilt": _tilt(py.astype(np.float64), px.astype(np.float64)), "pixels": pixels})
+ last = box
+ return rows
+
+
+def region(row: dict[str, Any], size: tuple[int, int]) -> tuple[tuple[int, int, int, int], np.ndarray]:
+ """Where the erase reaches in a frame of `size` whose plate `find` found (`row`): a box (left, top, right, bottom)
+ and, in it, every pixel within RIM px of the plate's, each way and diagonally."""
+ from sprite_gen.video.frames import _near
+ x0, y0, x1, y1 = row["box"]
+ bx0, by0, bx1, by1 = max(0, x0 - RIM), max(0, y0 - RIM), min(size[0], x1 + RIM), min(size[1], y1 + RIM)
+ mask = np.zeros((by1 - by0, bx1 - bx0), dtype=bool)
+ mask[y0 - by0:y1 - by0, x0 - bx0:x1 - bx0] = row["pixels"]
+ return (bx0, by0, bx1, by1), _near(mask, RIM)
+
+
+def not_subject(row: dict[str, Any], size: tuple[int, int]) -> np.ndarray | None:
+ """The plate's place on a frame of `size` (`region`, the whole frame), for the edge pass to learn the subject's
+ palette without; None where `find` found no plate."""
+ if not row["found"]:
+ return None
+ (x0, y0, x1, y1), mask = region(row, size)
+ out = np.zeros((size[1], size[0]), dtype=bool)
+ out[y0:y1, x0:x1] = mask
+ return out
+
+
+def _blend_line(rgb: np.ndarray, near_key: np.ndarray) -> np.ndarray:
+ """Where `rgb` (H, W, 3 int, as decoded) is the line the plate's edge blends into the key: `near_key`, its light
+ within LUMA_SLACK of the span between the plate's and the key's, its hue on the axis between them."""
+ r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
+ low, high = sorted((float(np.dot(RGB, LUMA)), float(np.dot(KEY, LUMA))))
+ light = rgb @ LUMA
+ return (near_key & (light >= low - LUMA_SLACK) & (light <= high + LUMA_SLACK)
+ & (np.abs(r - b) <= BLEND_BALANCE + BLEND_SLOPE * np.abs(r + b - 2 * g)))
+
+
+def _within(left: np.ndarray, reach: np.ndarray) -> np.ndarray:
+ """The pixels of `left` in a piece (joined side by side or at corners) that lies wholly in `reach`."""
+ from sprite_gen.video.frames import _components
+ ys, starts, stops, labels, _ = _components(left, diagonal=True)
+ out = np.zeros(len(ys), dtype=bool) # by piece: some pixel of it outside `reach`
+ for y, x0, x1, label in zip(ys, starts, stops, labels):
+ if not reach[y, x0:x1].all():
+ out[label] = True
+ inside = np.zeros(left.shape, dtype=bool)
+ for y, x0, x1, label in zip(ys, starts, stops, labels):
+ if not out[label]:
+ inside[y, x0:x1] = True
+ return inside
+
+
+def erase(files: list[Path], decoded: list[Path], rows: list[dict[str, Any]], plate: dict[str, Any]) -> dict[str, Any]:
+ """Erase the plate `find` found (`rows`) from every keyed frame in `files` (in place), each read on its frame as
+ decoded (`decoded`, in the same order); the frames report's `prop_plate`."""
+ from sprite_gen.video.frames import _near
+ with Image.open(decoded[0]) as first:
+ size = first.size
+ report_rows, not_found, touching = [], [], []
changed = erased = 0
- for f in files:
+ for f, src, row in zip(files, decoded, rows):
+ kept = {k: v for k, v in row.items() if k != "pixels"}
+ report_rows.append(kept)
+ if not row["found"]:
+ not_found.append(row["frame"])
+ continue
+ (rx0, ry0, rx1, ry1), reach = region(row, size)
+ # seen a pixel wider than the reach's box, so a piece that leaves the box is seen to leave the reach
+ x0, y0, x1, y1 = max(0, rx0 - 1), max(0, ry0 - 1), min(size[0], rx1 + 1), min(size[1], ry1 + 1)
+ wide = np.zeros((y1 - y0, x1 - x0), dtype=bool)
+ wide[ry0 - y0:ry1 - y0, rx0 - x0:rx1 - x0] = reach
+ reach = wide
+ rgb = _decoded(src)
+ # a pixel of the key's colour within BLEND_REACH px of the reach's own: seen in a box that much wider
+ kx0, ky0 = max(0, x0 - BLEND_REACH), max(0, y0 - BLEND_REACH)
+ kx1, ky1 = min(size[0], x1 + BLEND_REACH), min(size[1], y1 + BLEND_REACH)
+ near_key = _near(_colour(rgb[ky0:ky1, kx0:kx1], KEY), BLEND_REACH)[y0 - ky0:y1 - ky0, x0 - kx0:x1 - kx0]
+ seen = rgb[y0:y1, x0:x1]
+ r, g, b = seen[..., 0], seen[..., 1], seen[..., 2]
+ tinted = (np.minimum(r, b) - g > MAGENTA_LEAD) & (np.abs(r - b) < MAGENTA_BALANCE)
+ gone = reach & (tinted | _blend_line(seen, near_key))
with Image.open(f) as im:
a = np.array(im.convert("RGBA"))
- reg = a[y0:y1, x0:x1]
- rgba = reg.astype(np.int32)
- r, g, b = rgba[..., 0], rgba[..., 1], rgba[..., 2]
- magenta = (np.minimum(r, b) - g > MAGENTA_LEAD) & (np.abs(r - b) < MAGENTA_BALANCE)
- gone = magenta | (np.abs(rgba - still).max(axis=2) < STILL_TOLERANCE)
- if not reg[gone].any():
- continue
- erased += int((reg[gone][:, 3] > 0).sum())
- reg[gone] = 0
- Image.fromarray(a, "RGBA").save(f)
- changed += 1
- return {"canvas_report": plate["report"], "erase_box": [x0, y0, x1, y1], "rim": RIM, "frames": len(files),
- "frames_changed": changed, "erased_px": erased}
+ part = a[y0:y1, x0:x1]
+ opaque = part[..., 3] > 0
+ gone |= _within(opaque & ~gone, reach)
+ took, touch = int((gone & opaque).sum()), int((reach & ~gone & opaque).sum())
+ kept.update(erased_px=took, touch_px=touch)
+ if touch:
+ touching.append(row["frame"])
+ if took:
+ part[gone] = 0
+ Image.fromarray(a, "RGBA").save(f)
+ changed += 1
+ erased += took
+ return {"canvas_report": plate["report"], "start_box": list(frame_box(plate["box"], plate["canvas"], size)), "rim": RIM,
+ "frames": len(files), "frames_changed": changed, "erased_px": erased, "not_found": not_found,
+ "touching": touching, "rows": report_rows}
+
+
+def _names(names: list[str]) -> str:
+ shown = ", ".join(names[:NAMED])
+ return shown if len(names) <= NAMED else f"{shown} and {len(names) - NAMED} more"
+
+
+def warnings(done: dict[str, Any]) -> list[str]:
+ """The lines to read after an erase (`erase`'s report): the frames no plate was found in, which nothing was
+ erased from, and those the plate meets the subject in, whose pixels beside it were judged one by one."""
+ lines = []
+ if done.get("not_found"):
+ lines.append(f"--prop-plate: no plate found in {len(done['not_found'])} frame(s) ({_names(done['not_found'])}): "
+ "nothing was erased there, so any of the plate those frames show is still in them")
+ if done.get("touching"):
+ lines.append(f"--prop-plate: the plate meets the subject in {len(done['touching'])} frame(s) "
+ f"({_names(done['touching'])}): within {RIM} px of the plate the subject's pixels stay, but those the "
+ "plate tinted magenta and a grey between the plate and the key went with it; look at those frames")
+ return lines
diff --git a/tests/video/test_prop_plate.py b/tests/video/test_prop_plate.py
index 99cd9b4..96bb2df 100644
--- a/tests/video/test_prop_plate.py
+++ b/tests/video/test_prop_plate.py
@@ -1,15 +1,18 @@
# SPDX-License-Identifier: Apache-2.0
-"""A magenta plate painted into the still (`video-canvas --prop-plate`) and erased from the keyed frames by the same
-box (`video-frames --prop-plate`), so a clip filmed over it keeps no plate and the cut `video-loop` makes has none.
+"""A magenta plate painted into the still (`video-canvas --prop-plate`), found in each frame of the clip filmed over
+it and erased from the keyed frames (`video-frames --prop-plate`), so the frames keep no plate and the cut
+`video-loop` makes has none.
The pictures are synthetic: an outlined figure on the green key, filmed by a "model" that returns the canvas at twice
-its size, keeps the plate where the canvas has it (a pixel off in two frames), draws a ground line under it and runs
-the figure across in front of it.
+its size and runs the figure across in front of the plate. One clip keeps the plate where the canvas has it (a pixel
+off in two frames); another pans the camera so the plate moves left frame by frame, turns it in one frame and leaves it
+out of another, as a video model does with a plate it is told to keep still.
"""
from __future__ import annotations
import json
+import math
import shutil
import subprocess
from pathlib import Path
@@ -29,19 +32,23 @@ SKIN = (238, 206, 178)
SHIRT = (245, 245, 240)
NAVY = (40, 52, 110)
HAIR = (170, 52, 36)
-GROUND = (60, 60, 60)
+PINK = (230, 100, 190) # a colour the figure of the palette clip owns, read as magenta by the erase's own rule
SS = 4 # supersamples per pixel side: the figure's edge is antialiased as a drawn one is
STILL = (160, 120)
AT = (10, 20) # the figure's place on the still
SCALE = 2 # the clip's frames are the canvas at twice its size
FRAMES = 16
-STEP = 22 # frame px the figure runs each frame: in front of the plate in frames 8..12
-OFF = {3: 1, 11: 1} # frames the model draws the plate a pixel right in
+STEP = 22 # frame px the figure runs each frame over the ground: in front of the plate in frames 8..12 of the still clip
+OFF = {3: 1, 11: 1} # frames the model draws the plate a pixel right in (the still clip)
+PAN = 4 # frame px the panned clip's camera moves right each frame: its ground, the plate on it, moves left
+TILT = (4, 12.0) # the panned clip's frame the plate is turned in, and by how many degrees (clockwise, about its foot)
+HIDDEN = (14,) # the panned clip's frames the model leaves the plate out of
OUTLINE_MAX = 60 # an outline pixel: no channel above it
-def figure(scale: int) -> Image.Image:
- """A 40 x 96 runner at `scale`, RGBA: a dark outline round a head, a shirt, shorts, legs and shoes."""
+def figure(scale: int, *, patch: tuple[int, int, int] | None = None) -> Image.Image:
+ """A 40 x 96 runner at `scale`, RGBA: a dark outline round a head, a shirt, shorts, legs and shoes; `patch` paints a
+ square of that colour on the shirt."""
k = SS * scale
im = Image.new("RGBA", (40 * k, 96 * k))
d = ImageDraw.Draw(im)
@@ -53,12 +60,14 @@ def figure(scale: int) -> Image.Image:
d.rectangle(((x - 2) * k, 88 * k, (x + 11) * k, 96 * k - 1), fill=SHIRT, outline=OUTLINE, width=line)
d.rectangle((10 * k, 26 * k, 34 * k, 54 * k), fill=SHIRT, outline=OUTLINE, width=line)
d.rectangle((10 * k, 52 * k, 34 * k, 66 * k), fill=NAVY, outline=OUTLINE, width=line)
+ if patch is not None:
+ d.rectangle((15 * k, 31 * k, 29 * k, 47 * k), fill=patch)
return im.resize((40 * scale, 96 * scale), Image.BOX)
-def body_mask(scale: int) -> np.ndarray:
- """Where the figure at `scale` covers at least half a pixel."""
- return np.asarray(figure(scale))[..., 3] >= 128
+def body_mask(scale: int, *, least: int = 128) -> np.ndarray:
+ """Where the figure at `scale` covers at least `least` of 255."""
+ return np.asarray(figure(scale))[..., 3] >= least
def still(path: Path) -> Path:
@@ -75,37 +84,69 @@ def make_canvas(tmp: Path, *, facing: str = "right", plate: bool = True, report:
prop_plate=plate, **kwargs)
-def film(tmp: Path, report: dict) -> tuple[Path, list[tuple[int, int]]]:
- """The clip a model makes of the canvas: the canvas at SCALE, the plate kept (a pixel to the right in frames 3
- and 11), a ground line under it, the figure running right across in front of it. Returns the clip and the
- figure's place in each frame."""
+def plate_mask(size: tuple[int, int], box: list[int], dx: int, tilt: float) -> np.ndarray:
+ """The plate as the model draws it on a frame of `size`: the canvas's `box` at the frame's scale, moved `dx` px
+ right, turned `tilt` degrees clockwise about the middle of its foot."""
+ im = Image.new("L", size, 0)
+ d = ImageDraw.Draw(im)
+ x0, y0, x1, y1 = box
+ if tilt == 0:
+ d.rectangle((x0 + dx, y0, x1 - 1 + dx, y1 - 1), fill=255)
+ else:
+ cx, cy, t = (x0 + x1) / 2 + dx, y1, math.radians(tilt)
+ corners = [(x + dx - cx, y - cy) for x, y in ((x0, y0), (x1, y0), (x1, y1), (x0, y1))]
+ d.polygon([(cx + u * math.cos(t) - v * math.sin(t), cy + u * math.sin(t) + v * math.cos(t)) for u, v in corners], fill=255)
+ return np.asarray(im) > 0
+
+
+class Filmed:
+ """A clip a model made of the canvas, keyed with the plate erased (`erased`) and without (`kept`)."""
+
+ def __init__(self, tmp: Path, report: dict, clip: Path, places: list[tuple[int, int]], plates: list[np.ndarray | None],
+ erased: dict, kept: dict):
+ self.tmp, self.report, self.clip, self.places, self.plates, self.erased, self.kept = (
+ tmp, report, clip, places, plates, erased, kept)
+
+
+def film(tmp: Path, report: dict, *, pan: int = 0, off: dict[int, int] | None = None, tilt: tuple[int, float] | None = None,
+ hidden: tuple[int, ...] = (), runner: Image.Image | None = None) -> tuple[Path, list[tuple[int, int]], list[np.ndarray | None]]:
+ """The clip a model makes of the canvas: the canvas at SCALE, its camera moving `pan` px right a frame (the plate
+ with the ground), the plate a pixel right in the frames `off` names, turned in the frame `tilt` names and left out of
+ the `hidden` ones, the figure running right across in front of it. Returns the clip, the figure's place and the plate
+ as drawn (None where left out) in each frame."""
if not shutil.which("ffmpeg"):
pytest.skip("ffmpeg required")
w, h = report["canvas"]
- x0, y0, x1, y1 = (v * SCALE for v in report["prop_plate"]["box"])
+ size = (w * SCALE, h * SCALE)
+ box = [v * SCALE for v in report["prop_plate"]["box"]]
ox, oy = report["offset"]
made = tmp / "made"
made.mkdir()
- runner = figure(SCALE)
- places = []
+ runner = runner if runner is not None else figure(SCALE)
+ places, plates = [], []
for k in range(FRAMES):
- im = Image.new("RGB", (w * SCALE, h * SCALE), GREEN)
- d = ImageDraw.Draw(im)
- off = OFF.get(k, 0)
- d.rectangle((x0 + off, y0, x1 - 1 + off, y1 - 1), fill=prop_plate.RGB)
- d.rectangle((x0 - 2, y1, x1 + 1, y1 + 1), fill=GROUND)
- at = ((ox + AT[0]) * SCALE + STEP * k, (oy + AT[1]) * SCALE)
+ frame = np.zeros((size[1], size[0], 3), dtype=np.uint8)
+ frame[...] = GREEN
+ drawn = None if k in hidden else plate_mask(size, box, (off or {}).get(k, 0) - pan * k,
+ tilt[1] if tilt is not None and tilt[0] == k else 0.0)
+ if drawn is not None:
+ frame[drawn] = prop_plate.RGB
+ im = Image.fromarray(frame, "RGB")
+ at = ((ox + AT[0]) * SCALE + (STEP - pan) * k, (oy + AT[1]) * SCALE)
im.paste(runner, at, runner)
places.append(at)
+ plates.append(drawn)
im.save(made / f"frame-{k:04d}.png")
subprocess.run(["ffmpeg", "-v", "error", "-framerate", "24", "-i", str(made / "frame-%04d.png"), "-c:v", "libx264",
"-pix_fmt", "yuv420p", "-crf", "12", str(tmp / "clip.mp4")], check=True)
- return tmp / "clip.mp4", places
+ return tmp / "clip.mp4", places, plates
-def key(tmp: Path, clip: Path, out: str, plate: Path | None) -> dict:
+def key(tmp: Path, clip: Path, out: str, plate: Path | None, *, decontam: str = "off") -> dict:
+ """The clip keyed into `tmp / out`. The erase reads the frames as decoded, so the edge pass is left off where two
+ keyings are compared pixel for pixel; with it on, the plate keyed with the clip changes the subject's palette."""
return frames_mod.run_frames(clip, tmp / out, key="green", allow_edge_contact=False, report_path=None, spill="auto",
- reference=tmp / "canvas.png", prop_plate=plate)
+ reference=tmp / "canvas.png", prop_plate=plate, decontam=decontam)
def keyed(tmp: Path, out: str) -> list[np.ndarray]:
@@ -132,15 +173,22 @@ def shrunk(mask: np.ndarray, px: int) -> np.ndarray:
return ~grown(~mask, px)
[email protected](scope="module")
-def filmed(tmp_path_factory):
- """One clip keyed twice: with the plate erased by the canvas report's box, and without."""
- tmp = tmp_path_factory.mktemp("plate")
+def shoot(tmp: Path, **kwargs) -> Filmed:
report = make_canvas(tmp)
- clip, places = film(tmp, report)
- with_plate = key(tmp, clip, "erased", tmp / "canvas.json")
- without = key(tmp, clip, "kept", None)
- return tmp, report, places, with_plate, without
+ clip, places, plates = film(tmp, report, **kwargs)
+ return Filmed(tmp, report, clip, places, plates, key(tmp, clip, "erased", tmp / "canvas.json"), key(tmp, clip, "kept", None))
+
+
[email protected](scope="module")
+def filmed(tmp_path_factory) -> Filmed:
+ """The plate kept where the canvas has it, a pixel off in two frames."""
+ return shoot(tmp_path_factory.mktemp("plate"), off=OFF)
+
+
[email protected](scope="module")
+def panned(tmp_path_factory) -> Filmed:
+ """The camera panning right: the plate moves left PAN px a frame, is turned in one frame and left out of one."""
+ return shoot(tmp_path_factory.mktemp("panned"), pan=PAN, tilt=TILT, hidden=HIDDEN)
# --- video-canvas ------------------------------------------------------------------------------------------------
@@ -201,80 +249,160 @@ def magenta(a: np.ndarray) -> np.ndarray:
return (a[..., 3] > 0) & (np.minimum(r, b) - g > prop_plate.MAGENTA_LEAD) & (np.abs(r - b) < prop_plate.MAGENTA_BALANCE)
-def test_the_plate_is_gone_from_every_keyed_frame(filmed) -> None:
- """Gone where the model kept the plate where the canvas has it. A frame it is drawn a pixel off in keeps the
- codec's grey blend of its sides, a line a pixel wide down each (the erase's limit, in its docstring)."""
- tmp, report, places, with_plate, without = filmed
- frames = keyed(tmp, "erased")
+def left_of_the_plate(shot: Filmed) -> list[tuple[int, int]]:
+ """Each erased frame's pixels the figure does not cover (grown by 3 px) that are opaque within 3 px of the plate as
+ drawn, and those that are magenta anywhere."""
+ frames = keyed(shot.tmp, "erased")
size = (frames[0].shape[1], frames[0].shape[0])
- x0, y0, x1, y1 = prop_plate.frame_box(report["prop_plate"]["box"], report["canvas"], size)
- assert with_plate["prop_plate"]["erase_box"] == [x0, y0, x1, y1]
- assert with_plate["prop_plate"]["canvas_report"] == str((tmp / "canvas.json").resolve())
- px0, py0, px1, py1 = (v * SCALE for v in report["prop_plate"]["box"])
- for k, (a, at) in enumerate(zip(frames, places)):
- left = (a[y0:y1, x0:x1, 3] > 0) & ~grown(placed(body_mask(SCALE), at, size), 3)[y0:y1, x0:x1]
- ys, xs = np.nonzero(left)
- assert not (left & magenta(a[y0:y1, x0:x1])).any(), f"frame {k}: magenta left in the box"
- if k not in OFF:
- assert len(xs) == 0, f"frame {k}: {len(xs)} plate pixels left in the box"
+ out = []
+ for a, at, drawn in zip(frames, shot.places, shot.plates):
+ free = (a[..., 3] > 0) & ~grown(placed(body_mask(SCALE), at, size), 3)
+ near = grown(drawn, 3) if drawn is not None else np.zeros(free.shape, dtype=bool)
+ out.append((int((free & near).sum()), int((free & magenta(a)).sum())))
+ return out
+
+
[email protected]("clip", ["filmed", "panned"])
+def test_the_plate_is_gone_from_every_keyed_frame(clip: str, request) -> None:
+ """Gone where the model kept the plate where the canvas has it, drew it a pixel off, carried it off with the camera
+ or turned it: no pixel of it or of the line its edge blends into the key is left, and no magenta anywhere."""
+ shot: Filmed = request.getfixturevalue(clip)
+ left = left_of_the_plate(shot)
+ assert all(n == 0 and m == 0 for n, m in left), f"(plate px, magenta px) left, frame by frame: {left}"
+ kept = [int(((a[..., 3] > 0) & drawn).sum()) for a, drawn in zip(keyed(shot.tmp, "kept"), shot.plates) if drawn is not None]
+ assert min(kept) > 0.5 * min(int(d.sum()) for d in shot.plates if d is not None), "keyed without the erase, the plate is there"
+
+
+def test_each_frame_reports_where_its_plate_was_found(panned: Filmed) -> None:
+ """A row a frame: the box the plate was found in (the part of the drawn plate the figure leaves showing, but for the
+ pixel or two the codec blends at its edge, a turned plate's sharp corners), and, where the figure is clear of it,
+ how far it was turned; a frame it was left out of is named as one with no plate found, and the frames the figure
+ crosses it in as ones where it meets the subject."""
+ plate = panned.erased["prop_plate"]
+ rows = plate["rows"]
+ names = [f"frame-{k + 1:04d}.png" for k in range(FRAMES)]
+ assert [r["frame"] for r in rows] == names and plate["frames"] == FRAMES
+ size = (panned.plates[0].shape[1], panned.plates[0].shape[0])
+ assert plate["canvas_report"] == str((panned.tmp / "canvas.json").resolve())
+ assert plate["start_box"] == list(prop_plate.frame_box(panned.report["prop_plate"]["box"], panned.report["canvas"], size))
+ meets = []
+ for k, (row, drawn, at) in enumerate(zip(rows, panned.plates, panned.places)):
+ if drawn is None:
+ assert row == {"frame": names[k], "found": False}
+ continue
+ body = placed(body_mask(SCALE, least=1), at, size)
+ showing = drawn & ~body
+ ys, xs = np.nonzero(showing)
+ assert row["found"] is True
+ assert all(abs(a - b) <= 2 for a, b in zip(row["box"], (xs.min(), ys.min(), xs.max() + 1, ys.max() + 1))), (k, row["box"])
+ if (grown(body, prop_plate.RIM + 1) & drawn).any():
+ meets.append(names[k])
continue
- sides = (px0 + OFF[k], px1 - 1 + OFF[k]) # the first and last column of the plate as drawn in frame k
- assert all(min(abs(x0 + x - side) for side in sides) <= 1 for x in xs), f"frame {k}: left off the plate's sides"
- assert len(xs) <= 2 * 2 * (py1 - py0 + 2), f"frame {k}: {len(xs)} px left, more than a line down each side"
- kept = [int((a[y0:y1, x0:x1, 3] > 0).sum()) for a in keyed(tmp, "kept")]
- assert min(kept) > (x1 - x0 - 2 * prop_plate.RIM) * (y1 - y0 - 2 * prop_plate.RIM) // 2, "keyed without the erase, the plate is there"
-
-
-def test_the_figure_crossing_in_front_of_the_plate_keeps_its_pixels(filmed) -> None:
- """The figure crosses the plate and its rim, its dark outline over the rim's pixels that are keyed out over most of
- the clip. None of it goes but the few edge pixels the codec tinted with the plate's magenta."""
- tmp, report, places, with_plate, without = filmed
- erased, kept = keyed(tmp, "erased"), keyed(tmp, "kept")
+ assert abs(row["plate_px"] - int(drawn.sum())) <= 0.05 * drawn.sum(), (k, row["plate_px"], int(drawn.sum()))
+ assert abs(row["tilt"] - (TILT[1] if k == TILT[0] else 0.0)) <= 1.0, (k, row["tilt"])
+ assert plate["not_found"] == [names[k] for k in HIDDEN]
+ assert meets and plate["touching"] == meets
+ assert [r["frame"] for r in rows if r.get("touch_px")] == meets
+ assert plate["frames_changed"] == FRAMES - len(HIDDEN)
+ assert plate["erased_px"] == sum(r.get("erased_px", 0) for r in rows)
+
+
+def test_the_warnings_name_the_frames_with_no_plate_and_those_where_it_meets_the_subject(panned: Filmed) -> None:
+ plate = panned.erased["prop_plate"]
+ lines = prop_plate.warnings(plate)
+ assert len(lines) == 2
+ assert all(name in lines[0] for name in plate["not_found"]) and "no plate found" in lines[0]
+ assert plate["touching"][0] in lines[1] and "meets the subject" in lines[1]
+ assert prop_plate.warnings({**plate, "not_found": [], "touching": []}) == []
+
+
[email protected]("clip", ["filmed", "panned"])
+def test_the_figure_crossing_in_front_of_the_plate_keeps_its_pixels(clip: str, request) -> None:
+ """The figure crosses the plate and the line round it, its dark outline over that line. None of it goes but the few
+ edge pixels the codec tinted with the plate's magenta."""
+ shot: Filmed = request.getfixturevalue(clip)
+ erased, kept = keyed(shot.tmp, "erased"), keyed(shot.tmp, "kept")
size = (erased[0].shape[1], erased[0].shape[0])
- x0, y0, x1, y1 = with_plate["prop_plate"]["erase_box"]
- px0, py0, px1, py1 = (v * SCALE for v in report["prop_plate"]["box"])
- rim = np.zeros(size[::-1], dtype=bool)
- rim[y0:y1, x0:x1] = True
- rim[py0:py1, px0:px1] = False
lost, tinted, over_rim = [], [], []
- for a, b, at in zip(erased, kept, places):
+ for a, b, at, drawn in zip(erased, kept, shot.places, shot.plates):
core = shrunk(placed(body_mask(SCALE), at, size), 2)
gone = core & (a != b).any(axis=2)
lost.append(int(gone.sum()))
tinted.append(int((gone & magenta(b)).sum()))
+ rim = grown(drawn, prop_plate.RIM) & ~drawn if drawn is not None else np.zeros(core.shape, dtype=bool)
over_rim.append(int((core & rim & (b[..., :3].astype(np.int32).max(axis=2) < OUTLINE_MAX)).sum()))
assert sum(1 for n in over_rim if n) >= 4, f"the outline crosses the rim in too few frames: {over_rim}"
assert lost == tinted, f"figure pixels erased that the codec did not tint magenta: lost {lost}, tinted {tinted}"
- assert max(lost) <= 2, f"figure pixels erased in the box, frame by frame: {lost}"
+ assert max(lost) <= 2, f"figure pixels erased, frame by frame: {lost}"
-def test_outside_the_box_the_frames_are_the_frames_keyed_without_it(filmed) -> None:
- tmp, report, places, with_plate, without = filmed
- x0, y0, x1, y1 = with_plate["prop_plate"]["erase_box"]
- for a, b in zip(keyed(tmp, "erased"), keyed(tmp, "kept")):
- a, b = a.copy(), b.copy()
- a[y0:y1, x0:x1] = b[y0:y1, x0:x1] = 0
- assert (a == b).all()
- assert "prop_plate" not in without
[email protected]("clip", ["filmed", "panned"])
+def test_away_from_each_frame_s_plate_the_frames_are_the_frames_keyed_without_it(clip: str, request) -> None:
+ shot: Filmed = request.getfixturevalue(clip)
+ for a, b, drawn in zip(keyed(shot.tmp, "erased"), keyed(shot.tmp, "kept"), shot.plates):
+ away = ~grown(drawn, prop_plate.RIM + 2) if drawn is not None else np.ones(a.shape[:2], dtype=bool)
+ assert (a[away] == b[away]).all()
+ assert "prop_plate" not in shot.kept
-def test_the_erase_reads_the_box_the_canvas_report_holds(filmed, tmp_path: Path) -> None:
- """One box: moved in the canvas report, the erase moves with it."""
- tmp, report, places, with_plate, without = filmed
- moved = json.loads((tmp / "canvas.json").read_text())
- moved["prop_plate"]["box"] = [v + 5 for v in moved["prop_plate"]["box"]]
+def test_the_search_starts_from_the_box_the_canvas_report_holds(filmed: Filmed, tmp_path: Path) -> None:
+ """The canvas report's box is where the first frame is searched: moved more than the plate's own size away, no frame
+ finds it, and nothing is erased."""
+ moved = json.loads((filmed.tmp / "canvas.json").read_text())
+ x0, y0, x1, y1 = moved["prop_plate"]["box"]
+ moved["prop_plate"]["box"] = [2, y0, 2 + x1 - x0, y1]
(tmp_path / "moved.json").write_text(json.dumps(moved))
- files = sorted((tmp / "erased" / "keyed").glob("*.png"))
- for f in files:
+ raw = sorted((filmed.tmp / "erased" / "raw").glob("*.png"))
+ files = []
+ for f in sorted((filmed.tmp / "kept" / "keyed").glob("*.png")):
shutil.copy(f, tmp_path / f.name)
- done = prop_plate.erase(sorted(tmp_path.glob("frame-*.png")), prop_plate.read(tmp_path / "moved.json"))
- size = Image.open(files[0]).size
- assert done["erase_box"] == list(prop_plate.frame_box(moved["prop_plate"]["box"], moved["canvas"], size))
- assert done["erase_box"] != with_plate["prop_plate"]["erase_box"]
-
-
-def test_a_canvas_report_without_a_plate_is_refused_before_anything_is_keyed(filmed, tmp_path: Path) -> None:
- tmp = filmed[0]
+ files.append(tmp_path / f.name)
+ plate = prop_plate.read(tmp_path / "moved.json")
+ done = prop_plate.erase(files, raw, prop_plate.find(raw, plate), plate)
+ assert done["not_found"] == [f.name for f in raw] and done["erased_px"] == 0 and done["frames_changed"] == 0
+ for f in files:
+ assert (np.asarray(Image.open(f)) == np.asarray(Image.open(filmed.tmp / "kept" / "keyed" / f.name))).all()
+
+
+DENIM = (66, 92, 140)
+RING = (70, 100, 125) # a blue grey a codec can ring beside a magenta plate on green, off the plate-key axis as denim is
+# A plate's side as a codec can decode it, outward from its colour: dark magenta, then the line blended into the key
+EDGE = ((205, 40, 190), (225, 65, 215), (90, 160, 110), (115, 180, 135))
+
+
+def test_the_plate_s_edge_and_a_blue_grey_line_beside_it_go_and_the_denim_crossing_its_rim_stays(tmp_path: Path) -> None:
+ """The left side is a plate's edge as decoded: the line blended into the key ends 4 px past the plate's colour.
+ On the right, no colour tells the codec's blue grey from denim; it lies wholly within the plate's rim and goes. A
+ denim bar crossing the rim from outside it stays, the part in front of the plate too."""
+ w, h, box = 120, 80, (60, 20, 76, 70)
+ (tmp_path / "canvas.json").write_text(json.dumps({"kind": prop_plate.CANVAS_REPORT, "canvas": [w, h],
+ "prop_plate": {"box": list(box)}}))
+ decoded = np.zeros((h, w, 3), dtype=np.uint8)
+ decoded[...] = GREEN
+ decoded[box[1]:box[3], box[0]:box[2]] = prop_plate.RGB
+ decoded[box[1]:box[3], box[2]] = RING
+ for k, colour in enumerate(EDGE):
+ decoded[box[1]:box[3], box[0] - 1 - k] = colour
+ decoded[40:46, 50:63] = DENIM
+ Image.fromarray(decoded, "RGB").save(tmp_path / "frame-0001.png")
+ keyed_frame = np.zeros((h, w, 4), dtype=np.uint8)
+ solid = (decoded != GREEN).any(axis=2)
+ keyed_frame[solid, :3], keyed_frame[solid, 3] = decoded[solid], 255
+ (tmp_path / "keyed").mkdir()
+ Image.fromarray(keyed_frame, "RGBA").save(tmp_path / "keyed" / "frame-0001.png")
+ plate = prop_plate.read(tmp_path / "canvas.json")
+ raw = [tmp_path / "frame-0001.png"]
+ done = prop_plate.erase([tmp_path / "keyed" / "frame-0001.png"], raw, prop_plate.find(raw, plate), plate)
+ out = np.asarray(Image.open(tmp_path / "keyed" / "frame-0001.png"))
+ bar = (decoded == DENIM).all(axis=2)
+ assert (out[..., 3][bar] == 255).all(), "the denim bar keeps every pixel"
+ assert not (out[..., 3] > 0)[~bar].any(), f"left beside the bar: {np.argwhere((out[..., 3] > 0) & ~bar)[:5].tolist()}"
+ assert done["touching"] == ["frame-0001.png"] and done["rows"][0]["touch_px"] == int(
+ (bar & prop_plate.not_subject(prop_plate.find(raw, plate)[0], (w, h))).sum())
+
+
+def test_a_canvas_report_without_a_plate_is_refused_before_anything_is_keyed(filmed: Filmed, tmp_path: Path) -> None:
+ tmp = filmed.tmp
bare = json.loads((tmp / "canvas.json").read_text())
del bare["prop_plate"]
(tmp_path / "bare.json").write_text(json.dumps(bare))
@@ -286,24 +414,53 @@ def test_a_canvas_report_without_a_plate_is_refused_before_anything_is_keyed(fil
assert not (tmp_path / "out" / "raw").exists()
-def test_frame_box_at_the_canvas_size_is_the_box_and_its_rim() -> None:
- assert prop_plate.frame_box([700, 729, 777, 919], [1920, 1080], (1920, 1080)) == (697, 726, 780, 922)
- assert prop_plate.frame_box([10, 10, 21, 31], [100, 50], (300, 150)) == (27, 27, 66, 96)
- assert prop_plate.frame_box([0, 40, 5, 50], [100, 50], (100, 50)) == (0, 37, 8, 50)
+def test_frame_box_is_the_canvas_box_on_the_frame_rounded_outward() -> None:
+ assert prop_plate.frame_box([700, 729, 777, 919], [1920, 1080], (1920, 1080)) == (700, 729, 777, 919)
+ assert prop_plate.frame_box([10, 10, 21, 31], [100, 50], (300, 150)) == (30, 30, 63, 93)
+ assert prop_plate.frame_box([1500, 700, 1650, 1000], [2400, 1350], (1280, 720)) == (800, 373, 880, 534)
+
+
+# --- the edge pass learns the subject's palette without the plate --------------------------------------------------
+
+
[email protected](scope="module")
+def palette_clip(tmp_path_factory) -> Filmed:
+ """The still clip keyed with the edge pass on, its figure wearing a pink patch: a magenta of its own."""
+ tmp = tmp_path_factory.mktemp("palette")
+ report = make_canvas(tmp)
+ clip, places, plates = film(tmp, report, runner=figure(SCALE, patch=PINK))
+ return Filmed(tmp, report, clip, places, plates, key(tmp, clip, "erased", tmp / "canvas.json", decontam="palette"),
+ key(tmp, clip, "kept", None, decontam="palette"))
+
+
+def nearest(palette: list[list[int]], colour: tuple[int, int, int]) -> int:
+ """How far the palette colour nearest `colour` is from it, on its farthest channel."""
+ return int(np.abs(np.asarray(palette) - np.asarray(colour)).max(axis=1).min())
+
+
+def test_the_edge_pass_learns_the_palette_without_the_plate(palette_clip: Filmed) -> None:
+ """The palette is learned on the first frame, where the plate stands as opaque as the figure: keyed without the
+ plate's report it takes the plate's magenta for a colour the subject owns; with it, the plate's place is left out
+ and the figure's own pink stays."""
+ with_plate, without = palette_clip.erased["decontam"], palette_clip.kept["decontam"]
+ assert with_plate["applied_frames"] == without["applied_frames"] == FRAMES
+ assert nearest(without["palette"], prop_plate.RGB) <= prop_plate.PLATE_TOLERANCE
+ assert nearest(with_plate["palette"], prop_plate.RGB) > prop_plate.PLATE_TOLERANCE, with_plate["palette"]
+ assert nearest(with_plate["palette"], PINK) <= 24, with_plate["palette"]
+ left = left_of_the_plate(palette_clip)
+ assert all(n == 0 for n, _ in left), f"plate px left with the edge pass on, frame by frame: {left}"
# --- video-loop: the cut is made of the erased frames -------------------------------------------------------------
-def test_the_cut_of_the_erased_frames_has_no_plate(filmed) -> None:
- tmp = filmed[0]
- magenta = []
+def test_the_cut_of_the_erased_frames_has_no_plate(filmed: Filmed) -> None:
+ tmp = filmed.tmp
+ counts = []
for out in ("erased", "kept"):
loop_mod.run_loop(tmp / out / "keyed", tmp / f"loop-{out}", fps=24, state="vault", min_len=None, max_len=None,
n_out=None, seam_max=0.001, name="vault", report_path=None, cycle_mode="once", start=0,
length=FRAMES, fill_held="off")
- strip = np.asarray(Image.open(tmp / f"loop-{out}" / "vault.strip.png").convert("RGBA")).astype(np.int32)
- r, g, b = strip[..., 0], strip[..., 1], strip[..., 2]
- magenta.append(int(((strip[..., 3] > 0) & (np.minimum(r, b) - g > prop_plate.MAGENTA_LEAD)
- & (np.abs(r - b) < prop_plate.MAGENTA_BALANCE)).sum()))
- assert magenta[0] == 0 and magenta[1] > 0, magenta
+ strip = np.asarray(Image.open(tmp / f"loop-{out}" / "vault.strip.png").convert("RGBA"))
+ counts.append(int(magenta(strip).sum()))
+ assert counts[0] == 0 and counts[1] > 0, counts