Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(video): video-follow --on-fold lower gives each region a gain of its own
Soohong Kima673da3f6e47
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8de206c..7108eaa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,9 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased+- **Output change: `video-follow --on-fold lower` gives each region a gain of its own.** One gain for the strip, set by the region with the smallest radius, let the smallest ellipse decide for every part: a small one (an ear) lowered a large one (a chest) to its own gain, and where that gain went under 1 refused the whole strip, so the chest did not move either. Now a region that folds at `--gain` takes the largest gain that does not fold it (in steps of 0.01), a region that does not fold keeps `--gain`, and one that would have to go under 1, the mass as measured, is **held**: it does not move, and stderr names it by its `--region` (`video-follow: --region <cx,cy,rx,ry> folds at --gain <g>, under 1 (the mass as measured): it is held, and does not move`); a region lowered while another keeps its gain is named the same way (`video-follow: --gain 2.5 folds --region <cx,cy,rx,ry> (a move of <px> px); lowered to --gain <g> for it`). The strip is refused only when every region is held, and that message names the largest region (`… folds a region of radius <r> px over itself (the largest of <n> regions: every one folds), and --on-fold lower would have to go under --gain 1 …`); with one region it reads as before. Where regions of different gains overlap, the larger move wins (`follow.move_regions` takes groups of regions, each with its own offset), and the move there is nowhere steeper than either region's own. In the record each `fold.regions` entry gains `gain` (0 when held) and `held`, the strip's `gain` is the largest any region moved by (`dx_px`, `dy_px` and `reach_px` are that gain's), `fold.ratio` is the nearest any region came to folding and `fold.lowered` says whether any region was lowered or held; the printed summary gains `region_gains`. A strip whose regions all take one gain — one region, regions that fold alike, or none that folds — is written as before, strip, GIF, WebP and record byte for byte. `--on-fold refuse`, the default, is unchanged. [docs/video-pipeline.md](docs/video-pipeline.md#6-follow-through--video-follow).+- **`tests/video/test_follow.py`** moves a chest with a region on the head too small to move at gain 1: before this the strip was refused, now the head region is held and the strip is the chest's alone, byte for byte (and at `--gain 0.9` the chest still moves at 0.9). A head region that folds at 1.75 beside a chest that does not fold takes 1.75 while the chest keeps 2.5, each region's pixels those of the region moved alone at its gain; a small region inside a larger one folds nowhere; a strip whose every region is held is refused untouched; two regions that fold alike are written with the one-gain record and the strip their gain asked for gives; the command names the region it lowered and the one it held.+- **Fix: `video-loop --anchor motion-auto` reads the same motion from the same pixels on every machine.** A patch of its region search that sits on a flat fill matches equally at several offsets; the FFTs that score them round differently between numpy builds (about 1e-14), and that rounding chose the offset. From the same frames a synthetic walk was tracked differently on Linux than on macOS, its distance matrix differed in most entries, and `tests/video/test_loop_prop_seam_held.py` failed on CI only (first choice popping 2.4 there, 11.7 on macOS). Now costs within 1e-9 of the lowest are one match (`auto_motion.MATCH_TIE`) and the offset nearest the centre the search was put on is taken; regions whose qualities tie on that grain are ranked by their box; and the drift lines that move the analysis frames are least-squares lines with exactly rounded sums (`auto_motion._line`), not `np.polyfit`, whose LAPACK solve rounded differently and tipped bilinear samples of those frames. The tracked offsets, the analysis frames and the distance matrix are now bit-identical on macOS arm64 and Linux x86-64; only the last digits of reported costs and scores differ. Footage whose patches never tie is cut as before: the same regions, offsets and cut, its analysis translations moved only in their last bits (about 1e-14), which can tip a bilinear sample and move a score in its sixth decimal. Drawn, flat-filled frames, where ties are the rule, are tracked — and may be cut — otherwise than v2.36.0 tracked them on any one machine.- **`tests/video/test_auto_motion.py`** perturbs every FFT correlation by up to 1e-12 of its largest value, seed by seed: the tied offset taken on the flat torso, the regions found and every tracked offset are the same under all of them (under v2.36.0 eight seeds took six offsets, 14 px apart, and four seeds found four different region pairs), and the drift line is `np.polyfit`'s to 1e-12 and bit-identical in any order of its points. `test_a_held_part_that_swings_with_the_step_is_cut_one_step_long` draws its pennant on a 53-frame beat (phase 0.75): its first choice pops 12.6, no other candidate's top over 8.5, the cut taken closes below the hand at 0.0; on v2.35.0 with this fix it fails where v2.35.0 reads nothing below the top.diff --git a/docs/video-pipeline.md b/docs/video-pipeline.mdindex d6f1c48..b9cafe2 100644--- a/docs/video-pipeline.md+++ b/docs/video-pipeline.md@@ -1029,21 +1029,33 @@ sprite-gen video-follow --loop-dir set/front-walk/loop --region 136,164,60,50 [-at the centre and 0 at the rim (cos²), sampled as premultiplied bilinear colour; outside it nopixel changes. A move so large that the weight's slope folds the picture over(offset × π / (2 · radius) ≥ 1) is refused: lower `--gain` or give the region larger radii.-- **`--on-fold lower`** lowers the gain for you instead of refusing. The move is the gain times- the move at gain 1, so the gain at which a region folds is 2 · radius / (π · move at gain 1),- and the strip takes the largest gain under it, in steps of 0.01 and no more than `--gain`. One- gain for the strip, set by the region with the smallest radius: every part hangs on the same- body and answers the same motion, and the recorded `gain` is then the `--gain` that gives this- strip by itself. It does not go under 1, the mass as measured: a region too small for that is- refused (the message names the largest gain that would not fold), so a follow-through is never- quietly weaker than the motion it answers. The default stays `refuse`, with the same output and- the same message as 2.24.0; a strip that does not fold is the same under either.+- **`--on-fold lower`** lowers the gain for you instead of refusing, region by region. The move+ is the gain times the move at gain 1, so the gain at which a region folds is+ 2 · radius / (π · move at gain 1), and a region that folds at `--gain` takes the largest gain+ under that, in steps of 0.01; a region that does not fold keeps `--gain`. Every part hangs on+ the same body and answers the same motion, but how far a part can move before it folds is its+ own size, so a small part (an ear) does not hold a large one (a chest) back. No region goes under 1, the+ mass as measured: one that would have to is **held** — it does not move at all, so a+ follow-through is never quietly weaker than the motion it answers — and named, on stderr and in+ the record. The strip is refused only when every region is held (the message names the largest+ region and the largest gain that would not fold it). Where regions of different gains overlap,+ the larger move wins; the move there is nowhere steeper than either region's own, so nothing+ folds. A strip whose regions all take one gain — one region, or regions that fold alike, or+ none that folds — is written as before this was per region, byte for byte, and its `gain` is+ then the `--gain` that gives this strip by itself. The default stays `refuse`, with the same+ output and the same message as 2.24.0; a strip that does not fold is the same under either.- **What it writes**: the strip, GIF and WebP over the loop's own (both animations re-opened andchecked as `video-loop` checks them), and `follow` in `<name>.strip.json` (the regions, thesettings, the body's bob, `dx_px`/`dy_px` per cell, `reach_px`; `gain` is the gain used and`gain_requested` the one asked for, and `fold` says whether it was lowered, the move asked for,the move at gain 1, and per region its smaller radius, how near the used move is to folding it- (`ratio`, under 1) and the gain at which it folds). A lowered gain is also named on stderr.+ (`ratio`, under 1) and the gain at which it folds). Where the regions took gains of their own,+ each region's entry also says the gain it moved by (`gain`, 0 when held) and whether it was+ held (`held`); the strip's `gain` is then the largest of them, `dx_px`/`dy_px` and `reach_px`+ are that gain's (a region moves by its own gain over that one times them), and `fold.ratio` is+ the nearest any region came to folding. A lowered gain is also named on stderr — per region,+ with the `--region` it was given, where they differ — and so is a held region; the printed+ summary then carries `region_gains`.`cycle/` is left as cut. The strip as it was is kept as `follow.source.png`; running`video-follow` again reads from it, so a second run never moves a moved strip. `--board` writes the cells before and after where the partsits lowest and highest, on white.diff --git a/sprite_gen/video/follow.py b/sprite_gen/video/follow.pyindex 6f8f2e3..1bb86e5 100644--- a/sprite_gen/video/follow.py+++ b/sprite_gen/video/follow.py@@ -19,9 +19,10 @@ the body's bob. Inside it every pixel is moved by the offset times a weight thatcentre and falls to 0 at the rim (cos²); outside it no pixel changes. The move is sampled aspremultiplied bilinear colour, so an edge never picks up the colour under a transparent pixel.A move so large that the weight's slope folds the picture over (offset × π / (2 · radius) ≥ 1)-is refused. `--on-fold lower` takes the largest gain that does not fold instead (the offset-grows in a straight line with the gain, so that gain is known without a search), and never one-under 1, the mass as measured: a region too small for that is still refused.+is refused. `--on-fold lower` gives each region the largest gain that does not fold it instead+(the offset grows in a straight line with the gain, so that gain is known without a search), and+never one under 1, the mass as measured: a region too small for that is held — it does not move,+and the record names it — and the strip is refused only when every region is.The strip as it was before is kept as `follow.source.png` beside it; running the command againreads from there, so a second follow-through never moves a moved strip. `video-loop` (a new@@ -135,22 +136,32 @@ def _sample(premultiplied: np.ndarray, sx: np.ndarray, sy: np.ndarray) -> np.ndareturn (a * (1 - fx) + b * fx) * (1 - fy) + (c * (1 - fx) + d * fx) * fy-def move_regions(cell: Image.Image, regions: list[tuple[float, float, float, float]], shift: tuple[float, float],- offset: tuple[float, float]) -> Image.Image:- """`cell` with each region moved by `offset` (dx, dy), its centre carried by `shift`."""+def move_regions(cell: Image.Image, moves: list[tuple[list[tuple[float, float, float, float]], tuple[float, float]]],+ shift: tuple[float, float]) -> Image.Image:+ """`cell` with each group of regions moved by its own offset (dx, dy), their centres carried by `shift`.++ The groups' offsets are one motion times their gains, so where regions of different groups+ overlap the larger move wins: the move is the largest of the groups' weighted moves, which is+ nowhere steeper than the steepest group's own."""src = np.asarray(cell.convert("RGBA"), dtype=np.float32)h, w = src.shape[:2]yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)- weight = np.zeros((h, w), np.float32)- for cx, cy, rx, ry in regions:- r = np.sqrt(((xx - cx - shift[0]) / rx) ** 2 + ((yy - cy - shift[1]) / ry) ** 2)- weight = np.maximum(weight, np.where(r < 1, np.cos(r * math.pi / 2) ** 2, 0))- inside = weight > 0+ weights = []+ for regions, _ in moves:+ weight = np.zeros((h, w), np.float32)+ for cx, cy, rx, ry in regions:+ r = np.sqrt(((xx - cx - shift[0]) / rx) ** 2 + ((yy - cy - shift[1]) / ry) ** 2)+ weight = np.maximum(weight, np.where(r < 1, np.cos(r * math.pi / 2) ** 2, 0))+ weights.append(weight)+ inside = np.logical_or.reduce([weight > 0 for weight in weights])if not inside.any():return cell.copy()+ largest = np.argmax(np.stack([math.hypot(*offset) * weight for (_, offset), weight in zip(moves, weights)]), axis=0)[None]+ move_x = np.take_along_axis(np.stack([offset[0] * weight for (_, offset), weight in zip(moves, weights)]), largest, 0)[0]+ move_y = np.take_along_axis(np.stack([offset[1] * weight for (_, offset), weight in zip(moves, weights)]), largest, 0)[0]pm = src.copy()pm[..., :3] *= pm[..., 3:4] / 255- moved = _sample(pm, xx - offset[0] * weight, yy - offset[1] * weight)+ moved = _sample(pm, xx - move_x, yy - move_y)alpha = np.clip(np.round(moved[..., 3:4]), 0, 255)# Colour is read back from the premultiplied mix, and none is kept where the coverage# rounds to nothing (the WebP check refuses colour under alpha 0).@@ -199,26 +210,44 @@ def follow_loop(loop_dir: Path, regions: list[tuple[float, float, float, float]]dy = follow_offsets(rows, fps, freq=freq, zeta=zeta, gain=gain)dx = follow_offsets(cols, fps, freq=freq, zeta=zeta, gain=gain)reach = float(np.max(np.hypot(dx, dy)))- smallest = min(min(rx, ry) for _, _, rx, ry in regions)+ radii = [min(rx, ry) for _, _, rx, ry in regions]+ smallest = min(radii)requested, reach_requested = gain, reach# The offset is the gain times the answer at gain 1, so one move decides every gain.reach_per_gain = float(np.max(np.hypot(follow_offsets(cols, fps, freq=freq, zeta=zeta, gain=1.0),follow_offsets(rows, fps, freq=freq, zeta=zeta, gain=1.0))))+ gains, held = [requested] * len(regions), [False] * len(regions)if fold_ratio(reach, smallest) >= 1:if on_fold == "refuse":raise SystemExit(f"video-follow: a move of {reach:.1f} px folds a region of radius {smallest:g} px over itself; ""lower --gain or give the region larger radii")- # One gain for the strip, set by the smallest region: every part hangs on the same body and- # answers the same motion, and the report's `gain` is then the `--gain` that gives this strip.- gain = lowered_gain(requested, reach_per_gain, smallest)- if gain < GAIN_MEASURED:- raise SystemExit(f"video-follow: a move of {reach:.1f} px folds a region of radius {smallest:g} px over itself, and "+ # A gain for each region: every part hangs on the same body and answers the same motion, but+ # how far it moves before it folds is its own size, so a small part (an ear) does not hold a+ # large one (a chest) back. A region that does not fold keeps the gain asked for.+ folds = [fold_ratio(reach, radius) >= 1 for radius in radii]+ gains = [lowered_gain(requested, reach_per_gain, radius) if fold else requested for radius, fold in zip(radii, folds)]+ # One that would have to go under the mass as measured does not move at all; the strip is+ # refused only when that is every region.+ held = [bool(fold and g < GAIN_MEASURED) for fold, g in zip(folds, gains)] # numpy radii compare to numpy bools+ if all(held):+ k = radii.index(max(radii)) # the largest folds at the largest gain: if it is held, every region is+ every = f" (the largest of {len(regions)} regions: every one folds)" if len(regions) > 1 else ""+ raise SystemExit(f"video-follow: a move of {reach:.1f} px folds a region of radius {radii[k]:g} px over itself{every}, and "f"--on-fold lower would have to go under --gain {GAIN_MEASURED:g} (the mass as measured moves "- f"{reach_per_gain:.1f} px; the largest gain that does not fold is {gain:g}); give the region larger radii")- dy = follow_offsets(rows, fps, freq=freq, zeta=zeta, gain=gain)- dx = follow_offsets(cols, fps, freq=freq, zeta=zeta, gain=gain)- reach = float(np.max(np.hypot(dx, dy)))- out = [move_regions(c, regions, (cols[k] - cols[0], rows[k] - rows[0]), (dx[k], dy[k])) for k, c in enumerate(cells)]+ f"{reach_per_gain:.1f} px; the largest gain that does not fold is {gains[k]:g}); give the region larger radii")+ gains = [0.0 if hold else g for g, hold in zip(gains, held)]+ # One move per gain: a region's offset is its gain times the answer at gain 1. The strip's `gain`+ # is the largest any region moves by, and `dx_px`/`dy_px` and `reach_px` are that gain's.+ moves = {g: (follow_offsets(cols, fps, freq=freq, zeta=zeta, gain=g), follow_offsets(rows, fps, freq=freq, zeta=zeta, gain=g))+ for g, hold in zip(gains, held) if not hold}+ reaches = {g: float(np.max(np.hypot(*move))) for g, move in moves.items()}+ gain = max(moves)+ dx, dy = moves[gain]+ reach = reaches[gain]+ ratios = [0.0 if hold else fold_ratio(reaches[g], radius) for radius, g, hold in zip(radii, gains, held)]+ groups = [([r for r, rg, hold in zip(regions, gains, held) if rg == g and not hold], move) for g, move in moves.items()]+ out = [move_regions(c, [(group, (mx[k], my[k])) for group, (mx, my) in groups], (cols[k] - cols[0], rows[k] - rows[0]))+ for k, c in enumerate(cells)]joined = Image.new("RGBA", (w * n, h), (0, 0, 0, 0))for k, c in enumerate(out):joined.alpha_composite(c, (k * w, 0))@@ -236,11 +265,14 @@ def follow_loop(loop_dir: Path, regions: list[tuple[float, float, float, float]]"reach_px": round(reach, 2),"gain_requested": requested, "on_fold": on_fold,"fold": {- "lowered": gain != requested, "ratio": round(fold_ratio(reach, smallest), 3),+ "lowered": any(g != requested for g in gains), "ratio": round(max(ratios), 3),"reach_requested_px": round(reach_requested, 2), "reach_per_gain_px": round(reach_per_gain, 3),- "regions": [{"radius_px": min(rx, ry), "ratio": round(fold_ratio(reach, min(rx, ry)), 3),- "gain_limit": None if (limit := gain_limit(reach_per_gain, min(rx, ry))) is None else round(limit, 3)}- for _, _, rx, ry in regions],+ # Where the regions took gains of their own, each says the gain it moved by and whether it+ # was held still (gain 0); where they all took one, the strip's `gain` is every region's.+ "regions": [{"radius_px": radius, "ratio": round(ratio, 3),+ "gain_limit": None if (limit := gain_limit(reach_per_gain, radius)) is None else round(limit, 3)}+ | ({} if len(set(gains)) == 1 else {"gain": g, "held": hold})+ for radius, ratio, g, hold in zip(radii, ratios, gains, held)],},"gif": loop_mod.verify_animation(gif_path, expect_frames=n, check_stale=False),"webp": loop_mod.verify_animation(webp_path, expect_frames=n, check_stale=True),@@ -274,8 +306,9 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:help="cx,cy,rx,ry: an ellipse over the soft part in the strip's first cell, in cell pixels (repeatable)")parser.add_argument("--gain", type=float, default=GAIN_DEFAULT, help=f"times the physical answer (default {GAIN_DEFAULT:g}; 1 is the mass as measured, 0 leaves the strip as it was)")parser.add_argument("--on-fold", choices=ON_FOLD_MODES, default="refuse",- help=f"a move that folds a region over itself: refuse (default), or lower — take the largest gain that does not fold "- f"(one gain for the strip, set by the smallest region; never under {GAIN_MEASURED:g}, the mass as measured: that is still refused)")+ help=f"a move that folds a region over itself: refuse (default), or lower — each region takes the largest gain that does "+ f"not fold it, never under {GAIN_MEASURED:g} (the mass as measured): a region that would have to is held still and "+ "named in the record, and the strip is refused only when every region is")parser.add_argument("--freq", type=float, default=FREQ_DEFAULT, help=f"the part's own frequency in Hz (default {FREQ_DEFAULT:g})")parser.add_argument("--zeta", type=float, default=ZETA_DEFAULT, help=f"damping ratio (default {ZETA_DEFAULT:g}: lags and settles, no ringing)")parser.add_argument("--board", type=Path, help="write a before/after picture at the frames where the part sits lowest and highest")@@ -286,10 +319,22 @@ def run(**kwargs: object) -> int:gain=float(kwargs.get("gain", GAIN_DEFAULT)), freq=float(kwargs.get("freq", FREQ_DEFAULT)), # type: ignore[arg-type]zeta=float(kwargs.get("zeta", ZETA_DEFAULT)), board=kwargs.get("board"), # type: ignore[arg-type]on_fold=str(kwargs.get("on_fold") or "refuse"))- if result["fold"]["lowered"]:- print(f"video-follow: --gain {result['gain_requested']:g} folds a region (a move of {result['fold']['reach_requested_px']:g} px); "+ fold = result["fold"]+ own = "held" in fold["regions"][0] # the regions took gains of their own+ if own:+ for (cx, cy, rx, ry), entry in zip(result["regions"], fold["regions"]):+ name = f"--region {cx:g},{cy:g},{rx:g},{ry:g}"+ if entry["held"]:+ print(f"video-follow: {name} folds at --gain {entry['gain_limit']:g}, under {GAIN_MEASURED:g} (the mass as measured): "+ "it is held, and does not move", file=sys.stderr)+ elif entry["gain"] != result["gain_requested"]:+ print(f"video-follow: --gain {result['gain_requested']:g} folds {name} (a move of {fold['reach_requested_px']:g} px); "+ f"lowered to --gain {entry['gain']:g} for it", file=sys.stderr)+ elif fold["lowered"]:+ print(f"video-follow: --gain {result['gain_requested']:g} folds a region (a move of {fold['reach_requested_px']:g} px); "f"lowered to --gain {result['gain']:g}", file=sys.stderr)print(json.dumps({k: result[k] for k in ("strip", "regions", "gain", "gain_requested", "on_fold", "body_bob_px", "reach_px")}+ | ({"region_gains": [entry["gain"] for entry in fold["regions"]]} if own else {})| ({"board": result["board"]} if "board" in result else {}), ensure_ascii=False, indent=2))return 0diff --git a/tests/video/test_follow.py b/tests/video/test_follow.pyindex ac82fc3..63918ff 100644--- a/tests/video/test_follow.py+++ b/tests/video/test_follow.py@@ -141,6 +141,18 @@ def small_region(loop_dir, share):return (cx, cy, rx, share*reach*math.pi/2)+def head_region(loop_dir, share):+ """A round region on the crown, clear of the chest, whose radius folds at `share` of the default gain.++ On the head's top edge, so its move changes pixels: inside the flat head nothing would."""+ radius = small_region(loop_dir, share)[3]+ meta = json.loads((loop_dir/'walk.strip.json').read_text())+ first = cells(loop_dir/'walk.strip.png', meta)[0]+ head = np.all(np.abs(first[..., :3].astype(int)-(210, 150, 60)) < 40, axis=-1) & (first[..., 3] > 200)+ ys, xs = np.nonzero(head)+ return ((xs.min()+xs.max())/2, float(ys.min()), radius, radius)++def folds_nowhere(rec, meta):"""Every cell's picture runs forwards: along the move, the sample point never turns back."""h, w = meta['h'], meta['w']@@ -148,9 +160,11 @@ def folds_nowhere(rec, meta):worst = 0.0for dx, dy in zip(rec['dx_px'], rec['dy_px']):weight = np.zeros((h, w))- for cx, cy, rx, ry in rec['regions']:+ for (cx, cy, rx, ry), entry in zip(rec['regions'], rec['fold']['regions']):+ # A region that took a gain of its own moves by that share of the strip's move.+ share = entry.get('gain', rec['gain'])/rec['gain']r = np.sqrt(((xx-cx)/rx)**2+((yy-cy)/ry)**2)- weight = np.maximum(weight, np.where(r < 1, np.cos(r*math.pi/2)**2, 0))+ weight = np.maximum(weight, share*np.where(r < 1, np.cos(r*math.pi/2)**2, 0))gy, gx = np.gradient(weight)worst = max(worst, float(np.max(dx*gx+dy*gy)))return worst < 1, worst@@ -184,15 +198,109 @@ def test_lower_takes_the_largest_gain_that_does_not_fold(loop_dir):follow.follow_loop(loop_dir, [region], gain=rec['gain']+follow.GAIN_STEP)-def test_the_smallest_region_sets_the_one_gain(loop_dir):- small = small_region(loop_dir, 0.7)- cx, cy, rx, ry = chest_region(loop_dir)- alone = follow.follow_loop(loop_dir, [small], on_fold='lower')- both = follow.follow_loop(loop_dir, [(cx, cy+30, rx, ry), small], on_fold='lower')- assert both['gain'] == alone['gain']+def moved_alone(loop_dir, region, **kw):+ """The strip with `region` moved by itself, and the pixels that moved."""+ follow.follow_loop(loop_dir, [region], on_fold='lower', **kw)+ meta = json.loads((loop_dir/'walk.strip.json').read_text())+ strip = np.asarray(Image.open(loop_dir/'walk.strip.png').convert('RGBA'))+ source = np.asarray(Image.open(loop_dir/follow.SOURCE).convert('RGBA'))+ return strip, (strip != source).any(axis=-1), meta['follow']+++def test_each_region_takes_its_own_gain(loop_dir):+ small = head_region(loop_dir, 0.7) # folds at 1.75+ chest = chest_region(loop_dir) # does not fold at the default gain+ little_alone, little_moved, alone = moved_alone(loop_dir, small)+ chest_alone, chest_moved, chest_rec = moved_alone(loop_dir, chest)+ assert alone['fold']['lowered'] is True and follow.GAIN_MEASURED <= alone['gain'] < follow.GAIN_DEFAULT+ assert little_moved.any() and chest_moved.any() and not (little_moved & chest_moved).any()+ both = follow.follow_loop(loop_dir, [chest, small], on_fold='lower')large, little = both['fold']['regions']+ # The chest moves at the gain asked for; the small region at the gain it takes alone.+ assert both['gain'] == large['gain'] == both['gain_requested'] == follow.GAIN_DEFAULT+ assert little['gain'] == alone['gain'] and large['held'] is little['held'] is False+ assert both['fold']['lowered'] is Trueassert large['gain_limit'] > follow.GAIN_DEFAULT > little['gain_limit']- assert large['ratio'] < little['ratio'] < 1+ assert little['ratio'] == alone['fold']['ratio'] and 0.99 <= little['ratio'] < 1 and large['ratio'] < 1+ assert both['fold']['ratio'] == max(large['ratio'], little['ratio'])+ # The strip's move is the chest's, as when the chest moves alone.+ assert (both['dx_px'], both['dy_px'], both['reach_px']) == (chest_rec['dx_px'], chest_rec['dy_px'], chest_rec['reach_px'])+ meta = json.loads((loop_dir/'walk.strip.json').read_text())+ strip = np.asarray(Image.open(loop_dir/'walk.strip.png').convert('RGBA'))+ source = np.asarray(Image.open(loop_dir/follow.SOURCE).convert('RGBA'))+ # Each region's pixels are as when it moves alone at its own gain; nothing else changed.+ assert np.array_equal(strip[little_moved], little_alone[little_moved])+ assert np.array_equal(strip[chest_moved], chest_alone[chest_moved])+ assert np.array_equal(strip[~(little_moved | chest_moved)], source[~(little_moved | chest_moved)])+ ok, worst = folds_nowhere(meta['follow'], meta)+ assert ok, worst+++def test_overlapping_regions_of_different_gains_fold_nowhere(loop_dir):+ # A small region on the chest takes a lower gain than a large one over it: where they overlap the+ # larger move wins, and the move is nowhere steeper than either region's own.+ small = small_region(loop_dir, 0.7)+ cx, cy, rx, ry = chest_region(loop_dir)+ result = follow.follow_loop(loop_dir, [(cx, cy+6, rx+6, ry+6), small], on_fold='lower')+ large, little = result['fold']['regions']+ assert large['gain'] == follow.GAIN_DEFAULT > little['gain'] >= follow.GAIN_MEASURED+ meta = json.loads((loop_dir/'walk.strip.json').read_text())+ ok, worst = folds_nowhere(meta['follow'], meta)+ assert ok, worst+++def test_a_region_too_small_to_move_is_held_and_the_rest_move(loop_dir):+ # One gain for the strip, set by its smallest region, refused the whole strip for a region under the+ # mass as measured: the chest did not move because of an ear.+ chest = chest_region(loop_dir)+ ear = head_region(loop_dir, 0.3) # folds at 0.75: under the mass as measured+ chest_alone, chest_moved, _ = moved_alone(loop_dir, chest)+ result = follow.follow_loop(loop_dir, [chest, ear], on_fold='lower')+ meta = json.loads((loop_dir/'walk.strip.json').read_text())+ rec = meta['follow']+ moving, held = rec['fold']['regions']+ assert held['held'] is True and held['gain'] == 0 and held['ratio'] == 0 and held['gain_limit'] < follow.GAIN_MEASURED+ assert moving['held'] is False and moving['gain'] == rec['gain'] == follow.GAIN_DEFAULT+ assert rec['fold']['lowered'] is True and result['gain'] == rec['gain']+ # The held region does not move: the strip is the chest's alone, byte for byte.+ assert np.array_equal(np.asarray(Image.open(loop_dir/'walk.strip.png').convert('RGBA')), chest_alone)+ # A gain asked for under 1 that does not fold the chest still moves it; only the ear is held.+ low = follow.follow_loop(loop_dir, [chest, ear], gain=0.9, on_fold='lower')+ assert [e['gain'] for e in low['fold']['regions']] == [0.9, 0] and [e['held'] for e in low['fold']['regions']] == [False, True]+ # Refusing is still the default: the ear folds at the gain asked for.+ with pytest.raises(SystemExit, match='folds a region of radius'):+ follow.follow_loop(loop_dir, [chest, ear])+++def test_a_strip_whose_every_region_is_held_is_refused(loop_dir):+ ear, other = head_region(loop_dir, 0.3), small_region(loop_dir, 0.2)+ as_cut = (loop_dir/'walk.strip.png').read_bytes()+ meta = (loop_dir/'walk.strip.json').read_text()+ with pytest.raises(SystemExit) as refused:+ follow.follow_loop(loop_dir, [other, ear], on_fold='lower')+ # Named by the largest region, the last to fold: if it is held, so is every other.+ assert re.fullmatch(rf'video-follow: a move of \d+\.\d px folds a region of radius {ear[3]:g} px over itself '+ r'\(the largest of 2 regions: every one folds\), and --on-fold lower would have to go under --gain 1 '+ r'\(the mass as measured moves \d+\.\d px; the largest gain that does not fold is 0\.\d+\); give the region larger radii',+ str(refused.value))+ assert (loop_dir/'walk.strip.png').read_bytes() == as_cut+ assert (loop_dir/'walk.strip.json').read_text() == meta+++def test_regions_that_take_one_gain_are_written_as_one_gain(loop_dir):+ # Two regions of the same smaller radius fold at the same gain: the record is the one-gain record,+ # with no gain of its own per region, and the strip is the one that gain asked for gives.+ a, b = small_region(loop_dir, 0.7), head_region(loop_dir, 0.7)+ result = follow.follow_loop(loop_dir, [a, b], on_fold='lower')+ rec = json.loads((loop_dir/'walk.strip.json').read_text())['follow']+ assert rec['fold']['lowered'] is True and follow.GAIN_MEASURED <= rec['gain'] < follow.GAIN_DEFAULT+ assert [sorted(e) for e in rec['fold']['regions']] == [['gain_limit', 'radius_px', 'ratio']]*2+ assert sorted(rec) == ['body_bob_px', 'body_px', 'dx_px', 'dy_px', 'fold', 'freq_hz', 'gain', 'gain_requested', 'gif',+ 'harmonics', 'on_fold', 'reach_px', 'regions', 'source', 'webp', 'zeta']+ assert sorted(rec['fold']) == ['lowered', 'ratio', 'reach_per_gain_px', 'reach_requested_px', 'regions']+ lowered = (loop_dir/'walk.strip.png').read_bytes()+ follow.follow_loop(loop_dir, [a, b], gain=result['gain'])+ assert (loop_dir/'walk.strip.png').read_bytes() == lowereddef test_lower_does_not_go_under_the_mass_as_measured(loop_dir):@@ -230,11 +338,26 @@ def test_the_command_takes_on_fold_and_says_what_it_lowered_to(loop_dir, capsys)out, err = capsys.readouterr()printed = json.loads(out)assert printed['gain_requested'] == 2.5 and printed['on_fold'] == 'lower' and 1 <= printed['gain'] < 2.5- assert f"lowered to --gain {printed['gain']:g}" in err+ assert f"lowered to --gain {printed['gain']:g}" in err and 'region_gains' not in printedwith pytest.raises(SystemExit, match='unknown --on-fold'):follow.follow_loop(loop_dir, [region], on_fold='quietly')+def test_the_command_names_the_regions_it_lowered_and_held(loop_dir, capsys):+ regions = [chest_region(loop_dir), small_region(loop_dir, 0.7), head_region(loop_dir, 0.3)]+ texts = [','.join(f'{v:.3f}' for v in r) for r in regions]+ names = ['--region '+','.join(f'{float(v):g}' for v in t.split(',')) for t in texts]+ capsys.readouterr()+ assert follow.main(['--loop-dir', str(loop_dir), '--on-fold', 'lower', *(x for t in texts for x in ('--region', t))]) == 0+ out, err = capsys.readouterr()+ printed = json.loads(out)+ chest, small, ear = printed['region_gains']+ assert printed['gain'] == chest == 2.5 and 1 <= small < 2.5 and ear == 0+ lowered, held = err.strip().splitlines()+ assert lowered.startswith(f'video-follow: --gain 2.5 folds {names[1]} (a move of ') and lowered.endswith(f'lowered to --gain {small:g} for it')+ assert held.startswith(f'video-follow: {names[2]} folds at --gain 0.') and held.endswith('under 1 (the mass as measured): it is held, and does not move')++def test_bad_regions_are_refused(loop_dir):with pytest.raises(SystemExit, match='outside'):follow.follow_loop(loop_dir, [(9999, 10, 5, 5)])