#!/usr/bin/env python3 """ cdcrop_tune_crop.py - tune how far cdcrop's crop relaxes from the conservative corner on each side, to your own manual "Crop edges" pushes. python cdcrop_tune_crop.py --scans "C:\\path\\to\\scans" --decisions "C:\\path\\to\\output\\cdcrop_decisions.csv" The automatic crop sizes every side off whichever of its two corners needs the most trim (see frame_at's docstring in cdcrop.py) - safe, but on a non-rectangular insert it can cost real art at the other corner, which is what the interface's "Crop edges" push is for. For every cover you reviewed, this measures how much slack existed at each side (the gap between its two corners) and fits CORNER_LENIENCY per side: the fraction of that slack to give back automatically, so fewer covers need a manual push. Saved into the same cdcrop_tuning.json cdcrop_tune.py uses (merged in, not overwritten) and used automatically from then on. Only saved if leave-one-out validation beats the current setting - otherwise it would just be memorising your pushes. Run it again whenever you've reviewed a new batch. """ import argparse import csv import json import math import os import shutil import sys import time import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import cdcrop # noqa: E402 SIDES = ("left", "right", "top", "bottom") BETA_GRID = tuple(round(b, 2) for b in np.arange(0.0, 1.0001, 0.05)) def read_pushes(path): """Latest reviewed push per cover and side, px (0 if none pushed). Discs skipped.""" picks = {} with open(path, newline="") as fh: for row in csv.DictReader(fh): cands = json.loads(row.get("candidates") or "{}") if "as scanned" in cands: # a disc continue push = {k: (float(row[f"{k}_push_px"]) if row.get(f"{k}_push_px") else 0.0) for k in SIDES} picks[row["cover"]] = {"theta": float(row["chosen_deg"]), "push": push} return picks def analyse(scans_dir, picks, inset, cal): """Per-side corner gap (px) for every reviewed cover, at its chosen angle.""" by_scan = {} for cid in picks: base, idx = cid.rsplit("_", 1) by_scan.setdefault(base, []).append(int(idx)) covers = {} for base, idxs in sorted(by_scan.items()): path = next((os.path.join(scans_dir, f) for f in os.listdir(scans_dir) if os.path.splitext(f)[0] == base and f.lower().endswith(".bmp")), None) if not path: print(f" !! {base}.bmp not found in the scans folder - skipping its covers") continue t0 = time.time() arr8, _, dpi = cdcrop.load_scan(path) arr, _ = cdcrop.true_space(arr8.astype(np.float32), cal) objs = cdcrop.find_objects(arr, max(max(idxs), 2), dpi) for idx, (kind, rect, _) in enumerate(objs, start=1): cid = f"{base}_{idx}" if idx not in idxs or kind != "cover": continue cov = cdcrop.analyze_cover(arr, rect) fr = cdcrop.frame_at(cov, picks[cid]["theta"], inset) covers[cid] = fr["corner_gaps"] print(f" ({base}: {time.time() - t0:.0f}s)") return covers def evaluate(covers, picks, cids, tol): """error[beta_index, cover_index, side_index] = |beta*gap - actual push| (px).""" gaps = np.array([[covers[cid][s] for s in SIDES] for cid in cids]) # (N,4) push = np.array([[picks[cid]["push"][s] for s in SIDES] for cid in cids]) # (N,4) betas = np.array(BETA_GRID) err = np.abs(betas[:, None, None] * gaps[None] - push[None]) return err, gaps, push def best_betas(err, mask, tol): """Per-side beta that matches the most covers; ties broken toward a grid neighbour that does as well (the middle of a good region), then smallest error.""" e = err if mask is None else err[:, mask, :] hits = (e <= tol).sum(axis=1) # (nbeta, 4) tot = np.minimum(e, 20).sum(axis=1) # (nbeta, 4) out = [] for s in range(len(SIDES)): top = hits[:, s].max() cand = [i for i in range(len(BETA_GRID)) if hits[i, s] == top] def support(i): return sum(1 for d in (-1, 1) if 0 <= i + d < len(BETA_GRID) and hits[i + d, s] == top) out.append(max(cand, key=lambda i: (support(i), -tot[i, s]))) return out def leave_one_out(err, n, tol): hits = 0 for k in range(n): mask = np.ones(n, bool); mask[k] = False idx = best_betas(err, mask, tol) hits += sum(int(err[idx[s], k, s] <= tol) for s in range(len(SIDES))) return hits, n * len(SIDES) def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--scans", required=True, help="folder with the original scan BMPs") ap.add_argument("--decisions", required=True, help="cdcrop_decisions.csv from the interface's output folder") ap.add_argument("--tol", type=float, default=2.0, help="px of push left unexplained that still counts as a match") ap.add_argument("--inset", type=float, default=1.0) ap.add_argument("--no-calibration", action="store_true", help="measure without the scanner calibration") ap.add_argument("--dry-run", action="store_true", help="report only; don't save cdcrop_tuning.json") ap.add_argument("--force", action="store_true", help="save the tuned setting even if it doesn't beat the current one on unseen covers") a = ap.parse_args() cal_path = os.path.join(os.path.dirname(os.path.abspath(cdcrop.__file__)), "scanner_calibration.json") cal = None if not a.no_calibration and os.path.exists(cal_path): with open(cal_path) as fh: cal = json.load(fh) print(f"scanner calibration: stretch {cal['y_stretch_pct']:+.3f}%, shear {cal['shear_deg']:+.4f} deg") else: print("no scanner calibration in use") picks = read_pushes(a.decisions) print(f"{len(picks)} reviewed covers in {os.path.basename(a.decisions)}; measuring them…") covers = analyse(a.scans, picks, a.inset, cal) cids = sorted(covers) if len(cids) < 6: print("!! need at least a handful of reviewed covers to tune"); return err, gaps, push = evaluate(covers, picks, cids, a.tol) cur = {k: getattr(cdcrop, k) for k in cdcrop.TUNABLE} cur_cl = cur["CORNER_LENIENCY"] cur_beta = {s: (cur_cl if not isinstance(cur_cl, dict) else cur_cl.get(s, 0.0)) for s in SIDES} cur_err = np.abs(np.array([cur_beta[s] for s in SIDES])[None, :] * gaps - push) cur_hits = int((cur_err <= a.tol).sum()) total = len(cids) * len(SIDES) idx = best_betas(err, None, a.tol) tuned_beta = {s: BETA_GRID[idx[i]] for i, s in enumerate(SIDES)} tuned_err = np.stack([err[idx[i], :, i] for i in range(len(SIDES))], axis=1) # (N,4) tuned_hits = int((tuned_err <= a.tol).sum()) loo_hits, loo_total = leave_one_out(err, len(cids), a.tol) print(f"\ncurrent setting ({', '.join(f'{s}={cur_beta[s]:g}' for s in SIDES)}) " f"matches {cur_hits} of {total} side-pushes within {a.tol:.0f}px") print(f"tuned setting ({', '.join(f'{s}={tuned_beta[s]:g}' for s in SIDES)}) " f"matches {tuned_hits} of {total}") print(f"{'cover':22s}" + "".join(f"{s:>9s}" for s in SIDES) + " (px of push left unexplained, tuned)") for i, cid in enumerate(cids): row = tuned_err[i] flag = " <- still needs a push" if (row > a.tol).any() else "" print(f"{cid:22s}" + "".join(f"{v:9.1f}" for v in row) + flag) print(f"\ncheck on unseen covers (leave-one-out): tuned setting predicts {loo_hits} of {loo_total} " f"side-pushes, current setting {cur_hits} of {total}") if loo_hits <= cur_hits and not a.force: print("-> tuning doesn't predict unseen covers any better than the current setting yet, so it was NOT " "saved (it would only be fitting these particular covers). Review more covers and run this again; " "use --force to save anyway.") return if a.dry_run: return W = {**cur, "CORNER_LENIENCY": tuned_beta} out = {"weights": W, "picks": len(cids), "reproduced": tuned_hits, "of": total, "tolerance_px": a.tol, "calibrated": cal is not None, "date": time.strftime("%Y-%m-%d %H:%M"), "previous": cur} if os.path.exists(cdcrop.TUNING_PATH): shutil.copy2(cdcrop.TUNING_PATH, cdcrop.TUNING_PATH + ".bak") with open(cdcrop.TUNING_PATH, "w") as fh: json.dump(out, fh, indent=2) print(f"saved to {cdcrop.TUNING_PATH} (previous version kept as .bak). Re-process to apply.") if __name__ == "__main__": main()