commit 5ac483c2486b6ac333254854debeb96e6d767803 Author: aarbit Date: Tue Nov 10 18:46:37 2026 -0600 Initial commit: CdCrop engine, review UI, and tuner diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1959cff --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.venv/ +.venv1/ +.idea/ +.claude/ +__pycache__/ +*.pyc + +# local machine state (regenerated by the app, has your local paths) +cdcrop_ui_last.json + +# scan test data, review previews/output — large binaries, not code +test_match/ diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..329cdd5 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,74 @@ +# CdCrop — project notes for Claude + +Alex is scanning a large CD collection: two items per flatbed scan, 600 dpi, +5100×7019 RGB BMPs. These tools find each item, straighten and crop it, and +save it as an otherwise unaltered lossless PNG (same mode and DPI, just a +smaller file than BMP). Everything runs locally; no cloud vision services. + +## Files +- `cdcrop.py` — the engine, which also runs from the command line (`python cdcrop.py scan.bmp -o out`). +- `cdcrop_ui.py` — the local review interface on 127.0.0.1:8765 (stdlib server and single-page JS). It writes `cdcrop_decisions.csv` to the output folder. Remembers the last-used scans/output folder and settings across restarts (`cdcrop_ui_last.json`). The detail view has a "Context" toggle (shows scan around the crop, darkened outside the boundary) and a "Crop edges" control (manual per-side px push in/out) for covers whose insert isn't quite square — the automatic crop is conservative (sized off whichever corner needs the most trim), so a non-rectangular insert can cost real art at the other corner; the manual push recovers it. +- `cdcrop_tune.py` — fits the angle-choice weights to Alex's picks in the decisions CSV. It saves `cdcrop_tuning.json` only if leave-one-out validation beats the current settings. +- `scanner_calibration.json` — the scanner's geometry correction (see Calibration below). +- `cdcrop_tuning.json` — optional tuned weights; delete it to revert. + +## Covers: what Alex wants +- The art is straight, with as much art kept as possible and no scanner lid. +- Rotation uses one Lanczos warp in float, rounded once. +- **Default alignment is the left edge**, unless horizontal lines near the top or bottom argue otherwise. +- Lines that should be parallel to the sides end up parallel. +- Lettering and deliberate borders matter more than blurry photo detail. +- Don't keep out-of-focus art at the edges. +- Borders should look as the artist intended (trim bleed so a frame's border is even). +- He prefers consistent results over chasing slim-margin picks, and wants calibration to his picks over time. + +## Discs +The output is a square of side 2(R+6) around the disc; lid showing in the corners is fine. + +Orientation rubric: +1. Text-heavy discs: put the text upright. +2. Otherwise: put the label logo upright. +3. Otherwise: center the rim text at the top. +4. A clear photo upright overrides these. +5. Otherwise he doesn't care. + +## How it works (engine) +**Finding items** +- The lid is flood-filled using the per-channel gradient max (threshold 2.0 at 1/3 scale). +- Items with circularity ≥ 0.9 are discs. + +**Fitting edges (`fit_edge`)** +- Takes the outermost strong peak group, then the innermost line within that group. +- Refines to sub-pixel, then fits a Huber line. +- Soft edges (strength < 6) borrow their angle from another edge. + +**Choosing the angle (`angle_components` → `combine_angle`)** +The choice weighs: +- art lines (LSD: verticals first, plus horizontals near the top or bottom); +- the salience of art that would be lost; +- a left-edge bias; +- edge consensus; +- a blur term (`W_BLUR`). + +**Calibration** +- Model: scan = M·true, with M = [[1,k],[0,sy]]. +- It is measured from two scans of the same item at different angles (SIFT matching plus an affine least-squares fit). +- Analysis runs in true space, and the correction is folded into the final render. +- Alex's calibration: stretch +0.457%, shear −0.105°. + +## Status (2026-10-13) +**Results on Alex's 12 reviewed covers:** 9 of 12 picks reproduced within 2 px of drift. The misses: +- DJ C (448_2): out-of-focus art along the top edge; he picks the bottom-edge angle. +- Simple Minds (451_1): trims bleed so the grey border is even. Subtle; likely stays manual. +- Dirty Pretty Things (447_1). + +**Blur detector:** compares the ratio of fine to medium detail at the edge with the same ratio 60 px inside. +- It already scales down the "art worth keeping" weight, which fixed DFC. +- An extra cost (`W_BLUR` > 0) breaks other covers, so it defaults to 0. The tuner can try 0, 0.25 and 0.5. + +**Open items** +- Flag more of the misses in review. +- Watch the scan folder (deferred until Alex asks). +- Scanner integration (discussed, not started). + +Alex runs this from PyCharm on Windows, in its venv (`python -m pip install` the dependencies there). \ No newline at end of file diff --git a/CdCrop.bat b/CdCrop.bat new file mode 100644 index 0000000..342f8ef --- /dev/null +++ b/CdCrop.bat @@ -0,0 +1,16 @@ +@echo off +REM Launches the CdCrop review UI (cdcrop_ui.py) using its project venv. +REM Double-click from the desktop, a shortcut, or anywhere else. + +set "PROJECT_DIR=C:\Users\aarbi\PycharmProjects\CdCrop" +set "PYTHON=%PROJECT_DIR%\.venv\Scripts\python.exe" + +if not exist "%PYTHON%" ( + echo Could not find %PYTHON% + echo Edit CdCrop.bat if the project or venv has moved. + pause + exit /b 1 +) + +cd /d "%PROJECT_DIR%" +start "CdCrop" "%PYTHON%" cdcrop_ui.py diff --git a/cdcrop.py b/cdcrop.py new file mode 100644 index 0000000..19d3f74 --- /dev/null +++ b/cdcrop.py @@ -0,0 +1,1460 @@ +#!/usr/bin/env python3 +""" +cdcrop.py - split a flatbed scan holding CD covers into individually +straightened, tightly cropped PNG files. + +For each cover it: + 1. finds the cover against the scanner background by its outline, + 2. measures all four edges to sub-pixel accuracy; edges with no crisp line + (folds, lifted or worn paper) are cropped where in-focus art begins, + 3. chooses the rotation (auto mode) the way you would by eye: + - vertical lines in the art (boxes, text, borders) come out vertical, + - horizontal lines running along the top/bottom edge come out level, + - the thin wedges any rotation trims cost as little bold art as possible, + - otherwise it leans toward the left edge, + and flags close calls with an A/B sheet in _review/, + 4. crops to the largest rectangle inside all four edges (no background), + 5. rotates + crops in ONE Lanczos resample in floating point (no + intermediate rounding, no second interpolation pass), + 6. writes a lossless PNG in the original colour mode with the original DPI. + +Nothing else is changed: no colour correction, sharpening, or resizing. + +Usage: + python cdcrop.py SCAN.bmp [SCAN2.bmp ...] -o OUTDIR + python cdcrop.py INPUT_FOLDER -o OUTDIR +Options: + --align auto|left|right|top|bottom default auto + --align-override "file.bmp:2=top" per cover: an edge, auto, or an angle like +0.15 + --count N covers per scan (default 2) + --inset PX minimum px trimmed inside each edge (default 1.0); soft or + highlighted edges are trimmed further automatically + --no-preview skip preview images +Every run appends to OUTDIR/cdcrop_log.csv (angles considered, what was chosen). +Requires: pip install opencv-python numpy pillow scipy +""" +import argparse +import json +import math +import os +import sys + +import cv2 +from scipy.ndimage import median_filter +import numpy as np +from PIL import Image + +# ----------------------------------------------------------------- loading + +def load_scan(path): + im = Image.open(path) + dpi = im.info.get("dpi") + mode = im.mode + if mode == "P": + # palette BMPs: work in RGB, write back as RGB (lossless widening) + im = im.convert("RGB") + mode = "RGB" + if mode not in ("RGB", "L", "RGBA"): + raise ValueError(f"{path}: unsupported image mode {mode}") + arr = np.asarray(im) + return arr, mode, dpi + + +def save_png(arr, mode, dpi, path): + im = Image.fromarray(arr, mode=mode) + kw = {} + if dpi: + kw["dpi"] = tuple(round(float(d)) for d in dpi) + im.save(path, format="PNG", **kw) + +# -------------------------------------------------------------- detection +# +# Covers are found by their outlines, not their colour: a cover's edge is a +# crisp transition (paper on glass), while the lid is smooth. This works for +# white covers on a white lid and copes with uneven lid shading. + +def coarse_rects(arr, count, scale=3, grad_thr=2.0, return_shape=False): + """Rough cover rectangles: flood-fill the smooth lid from the scan border; + whatever the flood can't reach (enclosed by sharp outlines) is a cover. + Edges are measured per colour channel (strongest wins), so a cream or + tinted cover on a white lid is found by its colour step even when its + brightness barely differs.""" + chans = [arr[..., c] for c in range(min(3, arr.shape[2]))] if arr.ndim == 3 else [arr] + g = None + for ch_ in chans: + ch_ = ch_.astype(np.float32) + h, w = ch_.shape + s = cv2.resize(ch_, (w // scale, h // scale), interpolation=cv2.INTER_AREA) + s = cv2.GaussianBlur(s, (0, 0), 1.0) + gc = np.hypot(cv2.Sobel(s, cv2.CV_32F, 1, 0, ksize=3), + cv2.Sobel(s, cv2.CV_32F, 0, 1, ksize=3)) / 8 + g = gc if g is None else np.maximum(g, gc) + edges = cv2.dilate((g > grad_thr).astype(np.uint8), np.ones((3, 3), np.uint8)) + n, lab = cv2.connectedComponents(1 - edges) + border = set(np.unique(np.concatenate([lab[0], lab[-1], lab[:, 0], lab[:, -1]]))) - {0} + obj = (~np.isin(lab, list(border))).astype(np.uint8) + obj = cv2.morphologyEx(obj, cv2.MORPH_OPEN, np.ones((15, 15), np.uint8)) + cnts, _ = cv2.findContours(obj, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) + cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:count] + cnts = [c for c in cnts if cv2.contourArea(c) > 0.02 * obj.size] + rects = [cv2.minAreaRect(cv2.convexHull(c).astype(np.float32) * scale + (scale - 1) / 2.0) + for c in cnts] + if not return_shape: + return rects + circ = [] + for c in cnts: + hull = cv2.convexHull(c) + A, P = cv2.contourArea(hull), cv2.arcLength(hull, True) + circ.append(4 * math.pi * A / (P * P)) # 1.0 circle, 0.785 square + return rects, circ + + +def rect_sides(rect): + """Return the 4 sides of a cv2 rect as (p0, p1, outward_normal), named + left/right/top/bottom in image orientation.""" + pts = cv2.boxPoints(rect) + c = pts.mean(axis=0) + named = {} + for i in range(4): + p0, p1 = pts[i], pts[(i + 1) % 4] + n = (p0 + p1) / 2 - c + n /= np.linalg.norm(n) + ang = math.degrees(math.atan2(n[1], n[0])) # 0 = right, 90 = down + key = ("right" if -45 <= ang < 45 else "bottom" if 45 <= ang < 135 + else "top" if -135 <= ang < -45 else "left") + named[key] = (p0, p1, n) + if len(named) != 4: + raise RuntimeError("could not name cover sides") + return named + + +def _band(arr, p0, td, n, ts, offs): + base = p0[None, :] + ts[:, None] * td[None, :] + xs = (base[:, 0:1] + offs[None, :] * n[0]).astype(np.float32) + ys = (base[:, 1:2] + offs[None, :] * n[1]).astype(np.float32) + b = cv2.remap(arr, xs, ys, cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) + return b if b.ndim == 3 else b[..., None] + + +def fit_edge(arr, p0, p1, n, inner=130, outer=60, trim=0.08, max_ang=1.5): + """Locate one cover edge near the coarse side p0-p1 (n = outward normal). + + 1. Sample a band across the side (inner px inside, outer px outside). + 2. Search angles/offsets for straight lines along which a sharp transition + runs for most of the side (30th-percentile edge strength, so short art + features don't count), measured against the local level so busy + halftone print doesn't masquerade as an edge. + 3. Of the strong candidates near the outline, take the INNERMOST one: the + paper's own edge, not its thickness shadow, a cut-edge line, or a page + sticking out from underneath. + 4. Refine to sub-pixel per sample and fit a robust line. + Returns (point, unit direction, stats) or None.""" + Ls = float(np.linalg.norm(p1 - p0)); td = (p1 - p0) / Ls + offs = np.arange(-inner, outer + 1, 1.0, dtype=np.float32) + ts = np.arange(trim * Ls, (1 - trim) * Ls, 4.0) + band = _band(arr, p0, td, n, ts, offs) + d = (cv2.Sobel(band, cv2.CV_32F, 1, 0, ksize=3) / 4) + dabs = np.abs(d).sum(axis=2) if d.ndim == 3 else np.abs(d) + tc = ts.mean() + best_prof, best_ang = None, None + angs = np.arange(-max_ang, max_ang + 1e-9, 0.05) + S = np.empty((len(angs), len(offs)), np.float32) + for k, ang in enumerate(angs): + sh = (ts - tc) * math.tan(math.radians(ang)) + M = np.empty_like(dabs) + for i in range(len(ts)): + M[i] = np.interp(offs + sh[i], offs, dabs[i], left=0, right=0) + S[k] = np.percentile(M, 30, axis=0) + raw = S.max(axis=0) + # subtract the local level: textured art has a high, flat floor of + # gradient everywhere; a real edge stands up out of it. + prof = raw - median_filter(raw, size=31, mode="nearest") + pk = [j for j in range(1, len(offs) - 1) + if prof[j] >= prof[j - 1] and prof[j] > prof[j + 1] and prof[j] >= 0.5 * prof.max()] + if not pk: + return None + # Group candidate lines that lie within 30 px of each other. A narrow + # group is one edge plus its junk (shadow, cut-edge line, a page peeking + # out); a gap wider than that means a printed border or frame INSIDE the + # cover. So: take the outermost group with a strong line, and within it + # the innermost line - the paper's own edge. + pk_sorted = sorted(pk, key=lambda j: -offs[j]) # outermost first + groups, cur = [], [pk_sorted[0]] + for j in pk_sorted[1:]: + if offs[cur[-1]] - offs[j] <= 30: + cur.append(j) + else: + groups.append(cur); cur = [j] + groups.append(cur) + top_ = max(prof[j] for j in pk) + grp = next(g_ for g_ in groups if max(prof[j] for j in g_) >= 0.5 * top_) + j0 = min(grp, key=lambda j: offs[j]) # innermost of that group + ang0 = math.radians(angs[S[:, j0].argmax()]) + strength = float(prof[j0]) + + # A cover edge is often a thin dark line (the paper's cut edge) or a step + # into a shadow: two transitions a few px apart. Average the SIGNED + # profile along the line and take the innermost transition, so the crop + # starts at the paper, never on the line itself or the lid beyond it. + dsig = d.sum(axis=2) if d.ndim == 3 else d + sh = (ts - tc) * math.tan(ang0) + Msig = np.empty_like(dsig) + for i in range(len(ts)): + Msig[i] = np.interp(offs + sh[i], offs, dsig[i], left=0, right=0) + ps = np.median(Msig, axis=0) + o0 = offs[j0] + w = [j for j in range(1, len(offs) - 1) if abs(offs[j] - o0) <= 6] + amax = max(abs(ps[j]) for j in w) + flank = [j for j in w if abs(ps[j]) >= abs(ps[j - 1]) and abs(ps[j]) > abs(ps[j + 1]) + and abs(ps[j]) >= 0.35 * amax] + jf = min(flank, key=lambda j: offs[j]) if flank else j0 + pol = 1.0 if ps[jf] >= 0 else -1.0 + o_sel = float(offs[jf]) + + # How far inward does the transition reach? (blurry folds, edge highlights) + # Measured on the median luminance profile along the line; the crop is + # moved in past it so no blended edge pixels survive. + lumb = band.sum(axis=2) if band.ndim == 3 else band[..., 0] + Ml = np.empty_like(lumb) + for i in range(len(ts)): + Ml[i] = np.interp(offs + sh[i], offs, lumb[i]) + pl = np.median(Ml, axis=0) + io = lambda o: int(np.clip(np.searchsorted(offs, o), 0, len(offs) - 1)) + seg_i = pl[io(o_sel - 16):io(o_sel - 9)] + interior = float(np.median(seg_i)) if len(seg_i) else float(pl[0]) + A = float(np.max(np.abs(pl[io(o_sel):io(o_sel + 4) + 1] - interior))) + reach = 0.0 + for r_ in range(0, 9): + seg = pl[io(o_sel - 9):io(o_sel - r_) + 1] + if np.all(np.abs(seg - interior) <= 0.12 * A + 1.0): + reach = float(r_) + break + else: + reach = 8.0 + edge_inset = reach + 0.5 + + # sub-pixel refinement around the chosen line, 1 px sampling along the side + ts2 = np.arange(trim * Ls, (1 - trim) * Ls, 2.0) + loc = np.arange(-3, 3.01, 0.25, dtype=np.float32) + o_line = o_sel + (ts2 - tc) * math.tan(ang0) + base = p0[None, :] + ts2[:, None] * td[None, :] + O = o_line[:, None] + loc[None, :] + xs = (base[:, 0:1] + O * n[0]).astype(np.float32) + ys = (base[:, 1:2] + O * n[1]).astype(np.float32) + b = cv2.remap(arr, xs, ys, cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) + lum = b.sum(axis=2) if b.ndim == 3 else b + sd = pol * np.gradient(lum, 0.25, axis=1) + mid = len(loc) // 2 + lo, hi = mid - 5, mid + 6 # +/-1.25 px + k = np.argmax(sd[:, lo:hi], axis=1) + lo + r = np.arange(len(ts2)) + ok = (sd[r, k] > 0) & (k > 0) & (k < len(loc) - 1) + y0, y1, y2 = sd[r, k - 1], sd[r, k], sd[r, np.minimum(k + 1, len(loc) - 1)] + den = y0 - 2 * y1 + y2 + frac = np.where(den != 0, 0.5 * (y0 - y2) / np.where(den != 0, den, 1), 0.0) + oo = o_line + loc[k] + np.clip(frac, -0.5, 0.5) * 0.25 + P = (base + oo[:, None] * n[None, :])[ok].astype(np.float32) + if len(P) < 30: + return None + keep = np.ones(len(P), bool) + for _ in range(4): + vx, vy, x0, y0 = cv2.fitLine(P[keep], cv2.DIST_HUBER, 0, 0.01, 0.01).ravel() + dvec = np.array([vx, vy]); pp = np.array([x0, y0]) + resid = np.abs((P - pp) @ np.array([-vy, vx])) + new = resid < max(0.75, 3.0 * np.median(resid[keep])) + if (new == keep).all(): + break + keep = new + if dvec @ td < 0: + dvec = -dvec + return pp, dvec, {"n": int(keep.sum()), "rms": float(np.sqrt(np.mean(resid[keep] ** 2))), + "strength": strength, "inset": edge_inset, "o_sel": o_sel, + "peaks": [(int(offs[j]), round(float(prof[j]), 1)) for j in pk]} + + +SOFT_STRENGTH = 6.0 # below this, a side has no crisp line to measure + + +def locate_soft_edge(arr, p0, p1, n, dvec, inner=150, outer=60, trim=0.08, tol=4.0): + """Place a soft side (fold, lifted or curled paper, worn edge) whose angle + is taken from another edge (dvec). Walking inward from the lid, skip the + lid and the blurred transition, and stop at the first stretch where the + cover's content is steady along the whole side. Everything outside that + point is lid, shadow or out-of-focus paper.""" + Ls = float(np.linalg.norm(p1 - p0)); td = (p1 - p0) / Ls + ts = np.arange(trim * Ls, (1 - trim) * Ls, 4.0); tc = ts.mean() + offs = np.arange(-inner, outer + 1, 1.0, dtype=np.float32) + tan_a = float(dvec @ n) / float(dvec @ td) + band = _band(arr, p0, td, n, ts, offs) # (T, O, C) + sh = (ts - tc) * tan_a + al = np.empty_like(band) + for i in range(len(ts)): + for c in range(band.shape[2]): + al[i, :, c] = np.interp(offs + sh[i], offs, band[i, :, c]) + C = np.median(al, axis=0) # (O, C) + lum = al.mean(axis=2) + T = np.median(np.abs(lum - cv2.GaussianBlur(lum, (0, 0), sigmaX=0.01, sigmaY=2.0)), axis=0) + # core of the soft transition: steepest smoothed change near the outline + g = np.abs(np.gradient(C, axis=0)).max(axis=1) + g = np.convolve(g, np.ones(7) / 7, mode="same") + io = lambda o: int(np.clip(np.searchsorted(offs, o), 0, len(offs) - 1)) + lo, hi = io(-75), io(25) + core = lo + int(np.argmax(g[lo:hi])) + # walk inward from the core until (a) the steep part of the blur is over + # (change per px below half its peak) and (b) on printed art, the fine + # halftone detail is back to full sharpness, i.e. the paper is on the glass. + Tm = np.convolve(T, np.ones(5) / 5, mode="same") + i0, i1 = max(0, core - 90), max(1, core - 45) + t_int = float(np.median(Tm[i0:i1])) + t_lid = float(np.median(Tm[io(outer - 25):])) + use_t = t_int > 3.0 * t_lid + 1.0 + g_lim = 0.5 * float(g[core]) + run, need = 0, 3 + o_clean = float(offs[i1]) + for k in range(core, i1, -1): + ok = (Tm[k] >= 0.75 * t_int) if use_t else (g[k] <= g_lim) + run = run + 1 if ok else 0 + if run >= need: + o_clean = float(offs[k + need - 1]) - 1.0 + break + pp = p0 + tc * td + o_clean * n + d = dvec if dvec @ td > 0 else -dvec + return pp, d, {"n": 0, "rms": float("nan"), "strength": 0.0, "inset": 0.5, + "o_sel": o_clean, "soft": True} + + +def intersect(pa, da, pb, db): + A = np.array([[da[0], -db[0]], [da[1], -db[1]]]) + t = np.linalg.solve(A, pb - pa) + return pa + t[0] * da + +# ------------------------------------------------------------- extraction + +OPP = {"left": "right", "right": "left", "top": "bottom", "bottom": "top"} +ADJ = {"left": ("top", "bottom"), "right": ("top", "bottom"), + "top": ("left", "right"), "bottom": ("left", "right")} + + +def analyze_cover(arr, rect): + """Fit all four edges of one cover (done once per cover).""" + sides = rect_sides(rect) + edges, soft = {}, [] + for name, (p0, p1, n) in sides.items(): + f = fit_edge(arr, p0, p1, n) + if f is None or f[2]["strength"] < SOFT_STRENGTH: + soft.append(name) + else: + edges[name] = f + borrowed = {} + for name in soft: + p0, p1, n = sides[name] + td = (p1 - p0) / np.linalg.norm(p1 - p0) + if OPP[name] in edges: + dv = edges[OPP[name]][1].copy(); src = OPP[name] + else: + adj = [edges[k][1] for k in ADJ[name] if k in edges] + if adj: + dv = np.mean([np.array([-v[1], v[0]]) * (1 if np.array([-v[1], v[0]]) @ td > 0 else -1) + for v in adj], axis=0) + src = "/".join(k for k in ADJ[name] if k in edges) + else: + dv = td.copy(); src = "outline" + dv = dv / np.linalg.norm(dv) + edges[name] = locate_soft_edge(arr, p0, p1, n, dv) + borrowed[name] = src + for k in ("left", "right"): # left/right point down + p, d, st = edges[k]; edges[k] = (p, d if d[1] > 0 else -d, st) + for k in ("top", "bottom"): # top/bottom point right + p, d, st = edges[k]; edges[k] = (p, d if d[0] > 0 else -d, st) + return {"edges": edges, "soft": soft, "borrowed": borrowed} + + +def axes(theta): + """Output frame for a rotation of theta degrees (+ = clockwise lean corrected).""" + t = math.radians(theta) + ey = np.array([math.sin(t), math.cos(t)]) + ex = np.array([math.cos(t), -math.sin(t)]) + return ex, ey + + +def edge_theta(name, d): + """Rotation (deg) that would make this edge exactly vertical/horizontal.""" + if name in ("left", "right"): + return math.degrees(math.atan2(d[0], d[1])) + return math.degrees(math.atan2(-d[1], d[0])) + + +CORNER_LENIENCY = 0.0 # 0..1 (or a {"left":.., "right":.., "top":.., "bottom":..} dict of + # those): how far each side's bound relaxes from the conservative + # (most-trimmed) corner toward the other one, recovering art on + # non-square inserts without a manual push. Fit by cdcrop_tune_crop.py. + + +def frame_at(cov, theta, inset, edge_adj=None): + """Largest crop rectangle at rotation theta that stays inside all edges. + Soft edges have no measurable angle, so they are taken as square to the + frame, anchored where their in-focus art begins. + + edge_adj (optional, px): a manual push on one or more sides beyond that + automatic boundary - positive lets some lid back in to recover art an + automatic fit had to trim because the insert itself isn't quite square + (the fit is conservative: it sizes every side off whichever corner needs + the most trim, which can cost real art at the other corner - CORNER_LENIENCY + relaxes that by a fraction automatically); negative trims further in. + Keys: "left", "right", "top", "bottom".""" + ex, ey = axes(theta) + E = dict(cov["edges"]) + for k in cov["soft"]: + p, d, st = E[k] + E[k] = (p, ey.copy() if k in ("left", "right") else ex.copy(), st) + tl = intersect(E["top"][0], E["top"][1], E["left"][0], E["left"][1]) + tr = intersect(E["top"][0], E["top"][1], E["right"][0], E["right"][1]) + bl = intersect(E["bottom"][0], E["bottom"][1], E["left"][0], E["left"][1]) + br = intersect(E["bottom"][0], E["bottom"][1], E["right"][0], E["right"][1]) + c = (tl + tr + bl + br) / 4 + uv = lambda p: ((p - c) @ ex, (p - c) @ ey) + (utl, vtl), (utr, vtr), (ubl, vbl), (ubr, vbr) = map(uv, (tl, tr, bl, br)) + ins = {k: max(inset, E[k][2]["inset"]) for k in E} + cl = CORNER_LENIENCY + beta = (lambda k: cl) if not isinstance(cl, dict) else (lambda k: cl.get(k, 0.0)) + gaps = {"left": abs(utl - ubl), "right": abs(utr - ubr), + "top": abs(vtl - vtr), "bottom": abs(vbl - vbr)} + u0 = max(utl, ubl) - beta("left") * gaps["left"] + ins["left"] + u1 = min(utr, ubr) + beta("right") * gaps["right"] - ins["right"] + v0 = max(vtl, vtr) - beta("top") * gaps["top"] + ins["top"] + v1 = min(vbl, vbr) + beta("bottom") * gaps["bottom"] - ins["bottom"] + adj = edge_adj or {} + u0 -= adj.get("left", 0.0); u1 += adj.get("right", 0.0) + v0 -= adj.get("top", 0.0); v1 += adj.get("bottom", 0.0) + W = max(1, int(math.floor(u1 - u0))); H = max(1, int(math.floor(v1 - v0))) + u0 += ((u1 - u0) - W) / 2; v0 += ((v1 - v0) - H) / 2 + origin = c + u0 * ex + v0 * ey + def dev(k): + return edge_theta(k, E[k][1]) - theta + return {"theta": theta, "ex": ex, "ey": ey, "origin": origin, "W": W, "H": H, + "ins": ins, "corners": (tl, tr, br, bl), "corner_gaps": gaps, + "crop_poly": [origin, origin + W * ex, origin + W * ex + H * ey, origin + H * ey], + "edge_dev_deg": {k: dev(k) for k in E}, "edge_adj": dict(adj)} + + +def render(arr, fr): + """Rotate + crop in a single Lanczos resample, rounding once at the end.""" + ex, ey, o = fr["ex"], fr["ey"], fr["origin"] + M = np.array([[ex[0], ey[0], o[0]], [ex[1], ey[1], o[1]]], dtype=np.float64) + out = cv2.warpAffine(arr, M, (fr["W"], fr["H"]), + flags=cv2.INTER_LANCZOS4 | cv2.WARP_INVERSE_MAP, + borderMode=cv2.BORDER_REPLICATE) + return np.clip(np.rint(out), 0, 255).astype(np.uint8) + + +def extract(arr, rect, align, inset, raw_inset=None): + """Fixed-edge crop (used for --align-override and diagnostics).""" + cov = analyze_cover(arr, rect) + src = cov["borrowed"].get(align, align) + theta = edge_theta(src, cov["edges"][src][1]) + if align in cov["soft"] and src not in ("left", "right", "top", "bottom"): + theta = 0.0 + fr = frame_at(cov, theta, inset) + if raw_inset is not None: + cov2 = {**cov, "edges": {k: (p, d, {**st, "inset": raw_inset}) for k, (p, d, st) in cov["edges"].items()}} + fr = frame_at(cov2, theta, raw_inset) + info = {"rotation_deg": theta, "size": (fr["W"], fr["H"]), "edge_dev_deg": fr["edge_dev_deg"], + "edge_fit": {k: v[2] for k, v in cov["edges"].items()}, "insets": fr["ins"], + "soft": cov["soft"], "borrowed": cov["borrowed"], "used": src, + "corners": fr["corners"], "crop_poly": fr["crop_poly"]} + return render(arr, fr), info + +# ----------------------------------------------------- art-aware angle choice +# +# Two things decide the rotation, the same two you'd judge by eye: +# * lines in the art that are meant to be square to the cover (boxes, text +# baselines, borders) should come out exactly horizontal/vertical; +# * the thin wedges every rotation trims off should cost as little +# *interesting* art as possible (plain paper is cheap, drawing/text is not). + +LINE_MIN_LEN = 120 # px at full res; shorter segments are too imprecise +LINE_MAX_DEV = 0.8 # deg; lines tilted more than this are deliberate +TAPER_CAP = 3.0 # px; a line off by more than this is treated as deliberate +H_NEAR = 0.03 # horizontal lines count only this close (fraction of height) + # to the top/bottom edge, e.g. a border or strip +W_V = 2.5 # 1 px of lean on vertical art lines = this many px-strips of art +# Calibration (Alex's picks), all reproduced for W_V 2.0-2.75 with DEEP_BLEND 0.5: +# 447_1 Dirty Pretty Things ~0.00 (box sides square + top-right sketch kept) +# 447_2 Jesus Jones +0.41 left edge (lettering at top > blurry photo) +# 448_2 DJ C ~+1.65 right/bottom edges (not the torn top-right corner) +# 449_2 DFC left edge by a slim margin (treated as either) +# 451_1 Simple Minds no preference +W_H = 1.25 # ... on horizontal lines along the top/bottom edge +SAL_FLOOR = 60 # percentile of cover detail treated as "plain" +PROBE_DEPTHS = (30, 70) # px inside the edge where a wedge's art is judged +DEEP_BLEND = 0.5 # how much the deeper probe can veto edge-only detail (wear, tears) +ART_BASE = 0.0 # value of plain art relative to average detail +SAL_POWER = 2.0 # >1 makes bold detail count much more than faint marks +W_LEFT = 0.1 # ... and of straying from the left edge (your default) +W_EDGE = 0.0 # ... and of straying from the edges' consensus (their median angle) +W_BLUR = 0.0 # ... and of keeping out-of-focus art along an edge (lifted paper) +BLUR_PROBE = 60 # px further in where the art's normal sharpness is measured +BLUR_SLACK = 0.15 # sharpness dips smaller than this are ignored +REVIEW_MARGIN = 1.0 # score gap under which a distinct alternative is flagged for review +ALT_MIN_SEP = 0.1 # deg; alternatives closer than this look the same +LEFT_REVIEW = 0.15 # deg; straying further than this from the left edge gets a review + + +def _inside(quad, pts, margin): + q = np.array(quad, np.float32) + return np.array([cv2.pointPolygonTest(q, (float(x), float(y)), True) >= margin for x, y in pts]) + + +def art_lines(gray, quad, theta0, side): + """Straight, near-axis lines inside the art. Returns (theta_i, weight_i, segs).""" + q = np.array(quad, np.float32) + x0, y0 = np.floor(q.min(axis=0)).astype(int); x1, y1 = np.ceil(q.max(axis=0)).astype(int) + x0, y0 = max(x0, 0), max(y0, 0) + sub = gray[y0:y1, x0:x1] + half = cv2.resize(sub, (sub.shape[1] // 2, sub.shape[0] // 2), interpolation=cv2.INTER_AREA) + lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD) + segs = lsd.detect(np.clip(half, 0, 255).astype(np.uint8))[0] + if segs is None: + return np.zeros(0), np.zeros(0), [] + segs = segs.reshape(-1, 4) * 2 + [x0, y0, x0, y0] + blur = cv2.GaussianBlur(gray, (0, 0), 1.2) + th, wt, kept = [], [], [] + loc = np.arange(-4, 4.01, 0.25, dtype=np.float32) + for xa, ya, xb, yb in segs: + P, Q = np.array([xa, ya]), np.array([xb, yb]) + L = float(np.linalg.norm(Q - P)) + if L < LINE_MIN_LEN: + continue + d = (Q - P) / L + vert = abs(d[1]) > abs(d[0]) + if vert and d[1] < 0: d = -d + if not vert and d[0] < 0: d = -d + t_s = math.degrees(math.atan2(d[0], d[1])) if vert else math.degrees(math.atan2(-d[1], d[0])) + if abs(t_s - theta0) > LINE_MAX_DEV: + continue + if not _inside(quad, [P, Q], 40).all(): + continue + # sub-pixel refinement at full resolution + nrm = np.array([-d[1], d[0]]) + ts = np.arange(0.05 * L, 0.95 * L, 2.0) + base = P[None, :] + ts[:, None] * d[None, :] + xs = (base[:, 0:1] + loc[None, :] * nrm[0]).astype(np.float32) + ys = (base[:, 1:2] + loc[None, :] * nrm[1]).astype(np.float32) + prof = cv2.remap(blur, xs, ys, cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) + g = np.gradient(prof, 0.25, axis=1) + sgn = 1.0 if np.median(g[:, len(loc) // 2]) >= 0 else -1.0 + g = g * sgn + k = np.argmax(g[:, 4:-4], axis=1) + 4 + r = np.arange(len(ts)) + ok = g[r, k] > 0 + y_0, y_1, y_2 = g[r, k - 1], g[r, k], g[r, k + 1] + den = y_0 - 2 * y_1 + y_2 + fr = np.where(den != 0, 0.5 * (y_0 - y_2) / np.where(den != 0, den, 1), 0) + off = loc[k] + np.clip(fr, -0.5, 0.5) * 0.25 + pts = (base + off[:, None] * nrm[None, :])[ok].astype(np.float32) + if len(pts) < 0.6 * len(ts) or len(pts) < 20: + continue + vx, vy, px, py = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01).ravel() + res = np.abs((pts - [px, py]) @ np.array([-vy, vx])) + rms = float(np.sqrt(np.mean(np.minimum(res, 3) ** 2))) + if rms > 1.0: + continue # wobbly (hand-drawn) - not a reference line + dd = np.array([vx, vy]) + if vert and dd[1] < 0: dd = -dd + if not vert and dd[0] < 0: dd = -dd + t_f = math.degrees(math.atan2(dd[0], dd[1])) if vert else math.degrees(math.atan2(-dd[1], dd[0])) + if abs(t_f - theta0) > LINE_MAX_DEV: + continue + contrast = float(np.median(g[r, k][ok])) + C = np.array([px, py]) + (((P + Q) / 2 - [px, py]) @ dd) * dd # refined centre + th.append(t_f); wt.append(L * min(1.0, contrast / 20.0)) + kept.append((P, Q, vert, L, C, dd)) + return merge_lines(np.array(th), np.array(wt), kept) + + +def merge_lines(th, wt, segs, max_dang=0.2, max_off=2.5): + """Join pieces of the same printed line (a divider or border broken up by + detail) into one line, so its lean is judged over its full length - the + way the eye sees it - rather than piece by piece.""" + if len(th) == 0: + return th, wt, segs + used = np.zeros(len(th), bool) + out_t, out_w, out_s = [], [], [] + order = np.argsort(-wt) + for i in order: + if used[i]: + continue + Pi, Qi, vi, Li, Ci, di = segs[i] + ni = np.array([-di[1], di[0]]) + grp = [i]; used[i] = True + for j in order: + if used[j] or segs[j][2] != vi or abs(th[j] - th[i]) > max_dang: + continue + if abs((segs[j][4] - Ci) @ ni) <= max_off: + grp.append(j); used[j] = True + if len(grp) == 1: + out_t.append(th[i]); out_w.append(wt[i]); out_s.append((Pi, Qi, vi, Li)); continue + pts = [] + for j in grp: + _, _, _, Lj, Cj, dj = segs[j] + pts += [Cj - dj * Lj / 2, Cj, Cj + dj * Lj / 2] + pts = np.array(pts, np.float32) + vx, vy, x0, y0 = cv2.fitLine(pts, cv2.DIST_L2, 0, 0.01, 0.01).ravel() + d = np.array([vx, vy]) + if vi and d[1] < 0: d = -d + if not vi and d[0] < 0: d = -d + t = math.degrees(math.atan2(d[0], d[1])) if vi else math.degrees(math.atan2(-d[1], d[0])) + proj = (pts - [x0, y0]) @ d + ext = float(proj.max() - proj.min()) + P = np.array([x0, y0]) + proj.min() * d; Q = np.array([x0, y0]) + proj.max() * d + out_t.append(t); out_w.append(float(sum(wt[j] for j in grp))); out_s.append((P, Q, vi, ext)) + return np.array(out_t), np.array(out_w), out_s + + +def _family_cost(thetas, th, w, L): + """Consensus within one family of lines: find the angle most of the line + length agrees on, drop lines that disagree with it (deliberately tilted + elements), then score every angle by the RMS taper (px) of the rest.""" + if len(th) == 0: + return np.zeros(len(thetas)), 0.0, None + taper = np.abs(th[None, :] - thetas[:, None]) * math.pi / 180 * L[None, :] + K = (w[None, :] * np.exp(-taper ** 2)).sum(axis=1) + i = int(np.argmax(K)) + agree = taper[i] <= TAPER_CAP / 2 + wa = w[agree] + cost = np.sqrt((wa[None, :] * np.minimum(taper[:, agree], 8.0) ** 2).sum(axis=1) / wa.sum()) + conf = min(1.0, float(wa.sum()) / 300.0) * math.sqrt(float(wa.sum()) / float(w.sum())) + return cost, conf, float(thetas[i]) + + +def line_profile(thetas, th, wt, segs, fr0): + """Line-straightness cost per candidate angle, following your priorities: + vertical lines in the art come first (judged against the sides, like your + left-edge default); horizontal lines only matter when they run along the + top or bottom edge. Returns (cost, conf_v, art_theta_v, cost_h, conf_h, art_theta_h).""" + H = float(fr0["H"]); ey, o = fr0["ey"], fr0["origin"] + vert = np.array([s_[2] for s_ in segs], bool) if segs else np.zeros(0, bool) + L = np.array([s_[3] for s_ in segs]) if segs else np.zeros(0) + near = np.zeros(len(segs), bool) + for i, (P, Q, v, _) in enumerate(segs): + if not v: + m = float(((P + Q) / 2 - o) @ ey) + near[i] = min(m, H - m) / H <= H_NEAR + # Lines confirmed by a perpendicular partner at the same angle form a + # frame, box or border - far stronger evidence than lone strokes, which + # are often lettering or collage set at deliberately varied angles. + wt = wt.copy() + if len(th): + base_w = wt.copy() + for i in range(len(th)): + other = (vert != vert[i]) & (np.abs(th - th[i]) <= 0.1) + wt[i] = base_w[i] * (1.0 + 2.0 * min(1.0, float(base_w[other].sum()) / 300.0)) + cv, conf_v, tv = _family_cost(thetas, th[vert], wt[vert], L[vert]) if len(th) else (np.zeros(len(thetas)), 0.0, None) + hs = (~vert) & near + ch, conf_h, thh = _family_cost(thetas, th[hs], wt[hs], L[hs]) if len(th) else (np.zeros(len(thetas)), 0.0, None) + return cv, conf_v, tv, ch, conf_h, thh + + +def detail_samples(gray, quad, band=45, probe=30, step=2): + """Sample points in the strip along the cover's outline (where different + rotations crop differently), each valued by how interesting the art is + just inside it. Probing ~30 px inward skips the cut edge, creases and + shadows, and tells plain background (cheap to lose) from drawing or + text (expensive). Detail is measured at ~75 dpi so halftone dots and + paper grain don't count as detail.""" + q = np.array(quad, np.float32) + x0, y0 = np.floor(q.min(axis=0)).astype(int) - 8; x1, y1 = np.ceil(q.max(axis=0)).astype(int) + 8 + x0, y0 = max(x0, 0), max(y0, 0) + sub = gray[y0:y1, x0:x1] + f = 8 + sm = cv2.resize(sub, (sub.shape[1] // f, sub.shape[0] // f), interpolation=cv2.INTER_AREA) + gm = np.hypot(cv2.Sobel(sm, cv2.CV_32F, 1, 0, ksize=3), cv2.Sobel(sm, cv2.CV_32F, 0, 1, ksize=3)) / 8 + I = cv2.blur(gm, (3, 3)) + mask = np.zeros(sm.shape, np.uint8) + cv2.fillConvexPoly(mask, ((q - [x0, y0]) / f).astype(np.int32), 1) + mask = cv2.erode(mask, np.ones((5, 5), np.uint8)) + # salience: faint smudges and grain count little, bold strokes/text a lot + floor = float(np.percentile(I[mask > 0], SAL_FLOOR)) + I = np.maximum(I - floor, 0) ** SAL_POWER + mean_I = float(I[mask > 0].mean()) + 1e-3 + # fine grid of points in the outline strip + yy, xx = np.mgrid[y0:y1:step, x0:x1:step].astype(np.float32) + P = np.stack([xx.ravel(), yy.ravel()], 1) + c = q.mean(axis=0) + dist = np.full(len(P), np.inf); nin = np.zeros_like(P) + for i in range(4): + a_, b_ = q[i], q[(i + 1) % 4] + t_ = (b_ - a_) / np.linalg.norm(b_ - a_); nn = np.array([-t_[1], t_[0]], np.float32) + if (c - a_) @ nn < 0: nn = -nn + d_ = (P - a_) @ nn + closer = d_ < dist + dist = np.where(closer, d_, dist); nin[closer] = nn + sel = (dist >= 0) & (dist <= band) + P, nin = P[sel], nin[sel] + # Probe at two depths and keep the lesser: real art (lettering, drawing, + # borders) carries on into the cover, while wear, tears and fibres at a + # damaged corner sit right at the edge and shouldn't be "saved". + from scipy.ndimage import map_coordinates + # out-of-focus art (paper lifted off the glass) counts as background: compare + # halftone-scale sharpness at each point with the art ~60 px further in + gs = gray[y0:y1, x0:x1].astype(np.float32) + g1 = cv2.GaussianBlur(gs, (0, 0), 1.0) + fine = cv2.blur(np.abs(gs - g1), (15, 15)) # halftone-scale detail + mid = cv2.blur(np.abs(g1 - cv2.GaussianBlur(gs, (0, 0), 4.0)), (15, 15)) + ratio = fine / np.maximum(mid, 0.5) # drops where art is out of focus, whatever its contrast + at = lambda pts, im: map_coordinates(im, [pts[:, 1] - y0, pts[:, 0] - x0], order=1, mode="nearest") + inner = P + BLUR_PROBE * nin + r_here, r_in = at(P, ratio), at(inner, ratio) + structured = (at(inner, mid) > 1.5) & (at(P, mid) > 0.8) + blur = np.where(structured, np.clip(1 - r_here / np.maximum(r_in, 1e-3) - BLUR_SLACK, 0, 1), 0.0) + blur[dist[sel] < 4] = 0.0 # the cut edge itself isn't "art" + val = None + for depth in PROBE_DEPTHS: + pp_ = P + depth * nin + px = ((pp_[:, 0] - x0) - (f - 1) / 2) / f; py = ((pp_[:, 1] - y0) - (f - 1) / 2) / f + v_ = map_coordinates(I, [py, px], order=1, mode="constant", cval=0.0) + val = v_ if val is None else (1 - DEEP_BLEND) * val + DEEP_BLEND * np.minimum(val, v_) + val = val + ART_BASE * mean_I # any art is worth something, even flat colour + val = val * (1 - blur) # blurry art is no loss to trim + return P[:, 0], P[:, 1], val * step * step, mean_I, blur * step * step + + +def lost_art(fr, X, Y, Iw, mean_I, side): + """Interesting art inside the cover but outside this crop, expressed as the + px-width of a strip of average art along one side.""" + d = np.stack([X, Y], 1) - fr["origin"] + u = d @ fr["ex"]; v = d @ fr["ey"] + out = (u < 0) | (u > fr["W"]) | (v < 0) | (v > fr["H"]) + return float(Iw[out].sum()) / (mean_I * side) + + +def angle_components(gray, cov, inset, step=0.002): + """Everything the automatic angle choice weighs, on a fine grid of angles. + Computed once per cover; the weights are applied in combine_angle(), so a + tuning run can try hundreds of weightings in no time.""" + E = cov["edges"] + named = {} + for k in ("left", "right", "top", "bottom"): + if k not in cov["soft"]: + named[k] = edge_theta(k, E[k][1]) + if not named: + named["outline"] = 0.0 + theta0 = float(np.median(list(named.values()))) + fr0 = frame_at(cov, theta0, inset) + quad = fr0["corners"] + side = float(max(fr0["W"], fr0["H"])) + th, wt, segs = art_lines(gray, quad, theta0, side) + strong = list(th[wt > 0.1 * wt.max()]) if len(th) else [] + lo = min(list(named.values()) + strong) - 0.15 + hi = max(list(named.values()) + strong) + 0.15 + lo, hi = max(lo, theta0 - 1.5), min(hi, theta0 + 1.5) + thetas = np.unique(np.concatenate([np.arange(lo, hi + 1e-9, step), np.array(list(named.values()))])) + cv, conf_v, tv, chh, conf_h, thh = line_profile(thetas, th, wt, segs, fr0) + frames = [frame_at(cov, t, inset) for t in thetas] + comp = {"thetas": thetas, "named": dict(named), "side": side, "quad": quad, "frames": frames, + "cv": cv, "conf_v": conf_v, "tv": tv, "chh": chh, "conf_h": conf_h, "thh": thh, + "segs": segs, "n_lines": int(len(th)), + "left_px": (np.abs(thetas - named["left"]) * math.pi / 180 * side) if "left" in named + else np.zeros_like(thetas), + "edge_px": np.abs(thetas - theta0) * math.pi / 180 * side} + comp["loss"] = component_loss(comp, gray) + return comp + + +def component_loss(comp, gray): + """Lost-art term for every angle in comp (depends on the detail settings); + also stores comp["blur"]: out-of-focus edge art each angle would keep.""" + X, Y, Iw, mean_I, Bw = detail_samples(gray, comp["quad"]) + loss, kept = [], [] + for f in comp["frames"]: + d = np.stack([X, Y], 1) - f["origin"] + u = d @ f["ex"]; v = d @ f["ey"] + out = (u < 0) | (u > f["W"]) | (v < 0) | (v > f["H"]) + loss.append(float(Iw[out].sum()) / (mean_I * comp["side"])) + kept.append(float(Bw[~out].sum()) / comp["side"]) + loss, kept = np.array(loss), np.array(kept) + comp["blur"] = kept - kept.min() + return loss - loss.min() + + +def combine_angle(comp, W=None): + """Apply the weights: pick the best angle, a runner-up for review, and an + explanation table. W overrides the module's weights (used by tuning).""" + g = globals() + W = W or {} + wv, wh = W.get("W_V", g["W_V"]), W.get("W_H", g["W_H"]) + wl, we = W.get("W_LEFT", g["W_LEFT"]), W.get("W_EDGE", g["W_EDGE"]) + loss = W.get("loss", comp["loss"]) + thetas, named = comp["thetas"], dict(comp["named"]) + wb = W.get("W_BLUR", g["W_BLUR"]) + blur = W.get("blur", comp.get("blur", np.zeros_like(loss))) + total = (loss + wv * comp["conf_v"] * comp["cv"] + wh * comp["conf_h"] * comp["chh"] + + wl * comp["left_px"] + we * comp["edge_px"] + wb * blur) + i = int(np.argmin(total)) + best = float(thetas[i]) + if comp["tv"] is not None and comp["conf_v"] > 0.15: + named["art verticals"] = comp["tv"] + if comp["thh"] is not None and comp["conf_h"] > 0.15: + named["edge horizontals"] = comp["thh"] + at = lambda t: int(np.argmin(np.abs(thetas - t))) + table = {name: {"theta": t, "total": float(total[at(t)]) - float(total[i]), + "loss": float(loss[at(t)]), "line": float(comp["cv"][at(t)])} + for name, t in named.items()} + # runner-up: a genuinely different option - another local minimum at least + # ALT_MIN_SEP away that scores nearly as well + mins = [j for j in range(1, len(thetas) - 1) if total[j] <= total[j - 1] and total[j] <= total[j + 1]] + if len(thetas) > 1: + mins += [j for j in (0, len(thetas) - 1) if total[j] <= total[1 if j == 0 else len(thetas) - 2]] + cand = [j for j in mins if abs(thetas[j] - best) >= ALT_MIN_SEP and total[j] - total[i] < REVIEW_MARGIN] + alt, alt_why = None, None + if cand: + alt = float(thetas[min(cand, key=lambda j: total[j])]); alt_why = "close call" + elif "left" in named and abs(best - named["left"]) > LEFT_REVIEW: + alt = float(named["left"]); alt_why = "left edge" # overriding your usual habit + return {"theta": best, "alt": alt, "alt_why": alt_why, "conf": max(comp["conf_v"], comp["conf_h"]), + "art_theta": comp["tv"], "art_theta_h": comp["thh"], "named": table, + "n_lines": comp["n_lines"], "segs": comp["segs"], "best_loss": float(loss[i]), + "conf_v": comp["conf_v"], "conf_h": comp["conf_h"], "best_line": float(comp["cv"][i]), + "alt_gap": float(total[at(alt)] - total[i]) if alt is not None else None} + + +def choose_angle(arr, gray, cov, inset): + """Automatic rotation for one cover (see angle_components / combine_angle).""" + return combine_angle(angle_components(gray, cov, inset)) + + +# ------------------------------------------------------------------- tuning +# +# The weights above can be tuned to your own picks with cdcrop_tune.py, which +# writes cdcrop_tuning.json next to this script; it is loaded automatically. + +TUNABLE = ("W_V", "W_H", "W_LEFT", "W_EDGE", "W_BLUR", "DEEP_BLEND", "SAL_POWER", "SAL_FLOOR", + "CORNER_LENIENCY") +TUNING_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cdcrop_tuning.json") + + +def load_tuning(path=TUNING_PATH): + if not os.path.exists(path): + return None + with open(path) as fh: + t = json.load(fh) + for k in TUNABLE: + if k in t.get("weights", {}): + v = t["weights"][k] + globals()[k] = v if isinstance(v, dict) else type(globals()[k])(v) + return t + + +# ------------------------------------------------------------------- discs +# +# A disc is found by its outline like a cover, recognised by being round, and +# then fitted as the circle that follows a sharp edge most consistently all +# the way round, sized near a CD's standard 120 mm (the clear plastic rim and +# a one-sided shadow can't fool that; the printed label isn't always centred). +# The output is a square centred on the disc; rotation trims nothing, so the +# only question is which way is up. + +DISC_CIRC = 0.90 # outline circularity above which an object is a disc +DISC_MM = 120.0 +DISC_MARGIN = 6 # px of lid kept around the disc edge +DISC_LINE_STRONG = 10.0 # straight-text evidence strong enough to level automatically +DISC_REVIEW_DEG = 15.0 # automatic turns bigger than this get a review + + +def fit_disc(arr, c0, dpi, coarse_d=None): + """Centre and radius of a disc's physical edge (sub-pixel).""" + from scipy.ndimage import map_coordinates + from scipy.optimize import minimize + R0 = DISC_MM / 2 / 25.4 * dpi + if coarse_d is not None and abs(coarse_d / 2 / R0 - 1) > 0.06: # mini CD or other size + R0 = coarse_d / 2 * 0.99 + x0, y0 = max(int(c0[0] - R0 - 80), 0), max(int(c0[1] - R0 - 80), 0) + sub = arr[y0:int(c0[1] + R0 + 80), x0:int(c0[0] + R0 + 80)] + G = None + for c in range(min(3, sub.shape[2]) if sub.ndim == 3 else 1): + ch = cv2.GaussianBlur((sub[..., c] if sub.ndim == 3 else sub).astype(np.float32), (0, 0), 0.8) + gm = np.hypot(cv2.Sobel(ch, cv2.CV_32F, 1, 0, ksize=3), cv2.Sobel(ch, cv2.CV_32F, 0, 1, ksize=3)) / 8 + G = gm if G is None else np.maximum(G, gm) + G = cv2.GaussianBlur(G, (0, 0), 1.2) + th = np.radians(np.arange(0, 360, 0.25)); ct, st = np.cos(th), np.sin(th) + Rlo, Rhi = R0 - 8, R0 + 6 + + def coverage(p): + cx, cy, R = p + if R < Rlo or R > Rhi: + return -1.0 + v = map_coordinates(G, [cy - y0 + R * st, cx - x0 + R * ct], order=1, mode="nearest") + return float(np.mean(np.sqrt(np.minimum(v, 40.0)))) + best = None + for R in np.arange(Rlo, Rhi + 0.01, 1.0): + for dx in range(-14, 15, 2): + for dy in range(-14, 15, 2): + sc = coverage((c0[0] + dx, c0[1] + dy, R)) + if best is None or sc > best[0]: + best = (sc, (c0[0] + dx, c0[1] + dy, R)) + res = minimize(lambda p: -coverage(p), best[1], method="Nelder-Mead", options={"xatol": 0.05, "fatol": 1e-5}) + cx, cy, R = res.x + return np.array([cx, cy]), float(R) + + +def disc_frame(center, R, theta, margin=DISC_MARGIN): + """Square output centred on the disc, rotated by theta.""" + S = int(math.ceil(2 * (R + margin))) + ex, ey = axes(theta) + origin = np.asarray(center, float) - (S - 1) / 2 * (ex + ey) + poly = [origin, origin + S * ex, origin + S * ex + S * ey, origin + S * ey] + return {"theta": theta, "ex": ex, "ey": ey, "origin": origin, "W": S, "H": S, "ins": {}, + "corners": poly, "crop_poly": poly, "edge_dev_deg": {}, "disc": True, + "center": np.asarray(center, float), "R": R} + + +def _wrap(t): + return (t + 180) % 360 - 180 + + +def disc_orientation(gray, center, R): + """Rotation candidates for a disc and an automatic pick. + - level: straight text/graphics made level (quarter-turn nearest as scanned) + - text arc at top: the biggest arc of text around the rim, centred at the top + The automatic pick levels strong straight text, else centres a text arc + that is already near the top, else leaves the disc as scanned - and flags + anything that isn't clear-cut.""" + c = np.asarray(center, float) + cand, notes = {"as scanned": 0.0}, [] + # straight lines on the label + x0, y0 = max(int(c[0] - R), 0), max(int(c[1] - R), 0) + sub = gray[y0:int(c[1] + R), x0:int(c[0] + R)] + half = cv2.resize(sub, (sub.shape[1] // 2, sub.shape[0] // 2), interpolation=cv2.INTER_AREA) + segs = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD).detect(np.clip(half, 0, 255).astype(np.uint8))[0] + strength = 0.0 + if segs is not None: + segs = segs.reshape(-1, 4) * 2 + [x0, y0, x0, y0] + mid = (segs[:, :2] + segs[:, 2:]) / 2 + rad = np.hypot(mid[:, 0] - c[0], mid[:, 1] - c[1]) + L = np.hypot(segs[:, 2] - segs[:, 0], segs[:, 3] - segs[:, 1]) + ok = (rad > 0.30 * R) & (rad < 0.93 * R) & (L > 25) + d = segs[ok, 2:] - segs[ok, :2] + ang = np.degrees(np.arctan2(d[:, 1], d[:, 0])) % 90 + hist = np.zeros(360) + for a_, w in zip(ang, L[ok]): + hist[int(a_ * 4) % 360] += w + hist = np.convolve(np.r_[hist[-6:], hist, hist[:6]], np.hanning(9), "same")[6:-6] + k = int(np.argmax(hist)) + strength = float(hist[k] / (np.median(hist) + 1e-6)) + # refine: length-weighted mean of nearby segment angles + sel = np.abs(((ang - k / 4.0) + 45) % 90 - 45) < 1.0 + if sel.any(): + w = L[ok][sel] + dev = ((ang[sel] - k / 4.0) + 45) % 90 - 45 + peak = k / 4.0 + float((dev * w).sum() / w.sum()) + else: + peak = k / 4.0 + lev = _wrap(-peak) + lev = ((lev + 45) % 90) - 45 # the quarter turn nearest "as scanned" + if strength >= 4: + cand["level text"] = lev + # text arc around the rim + th = np.radians(np.arange(0, 360, 0.25)); rr = np.arange(0.80 * R, 0.965 * R, 1.0, dtype=np.float32) + xs = (c[0] + np.cos(th)[:, None] * rr).astype(np.float32); ys = (c[1] + np.sin(th)[:, None] * rr).astype(np.float32) + P = cv2.remap(gray.astype(np.float32), xs, ys, cv2.INTER_LINEAR) + hp = np.abs(P - cv2.GaussianBlur(P, (0, 0), sigmaX=6, sigmaY=6)) + e = hp.mean(axis=1) + e = np.convolve(np.r_[e[-40:], e, e[:40]], np.ones(41) / 41, "same")[40:-40] + on = e > np.median(e) + 2.5 * np.median(np.abs(e - np.median(e))) + arc = None + if on.any() and not on.all(): + s2 = np.r_[on, on]; best = (0, 0); i = 0 + while i < len(on): + if s2[i]: + j = i + while j < i + len(on) and s2[j]: + j += 1 + if j - i > best[1] - best[0]: + best = (i, j) + i = j + else: + i += 1 + span = (best[1] - best[0]) * 0.25 + if span >= 20: + mid = ((best[0] + best[1]) / 2 * 0.25) % 360 # image angle of the arc centre + arc = (mid, span) + cand["rim text at top"] = _wrap(-90 - mid) + # automatic pick + if "level text" in cand and strength >= DISC_LINE_STRONG: + auto, why = cand["level text"], "straight text levelled" + elif arc and abs(cand["rim text at top"]) <= 30: + auto, why = cand["rim text at top"], "text arc centred at the top" + else: + auto, why = 0.0, "left as scanned (no clear straight text)" + flagged = (why.startswith("left") or abs(auto) > DISC_REVIEW_DEG) + return {"candidates": cand, "auto": float(auto), "why": why, "flagged": bool(flagged), + "line_strength": strength} + + +def find_objects(arr, count, dpi): + """Covers and discs in one scan: [(kind, rect, circularity)], in reading order.""" + rects, circ = coarse_rects(arr, count, return_shape=True) + objs = [("disc" if c >= DISC_CIRC else "cover", r, c) for r, c in zip(rects, circ)] + cents = np.array([o[1][0] for o in objs]) if objs else np.zeros((0, 2)) + axis = 0 if len(cents) < 2 or np.ptp(cents[:, 0]) >= np.ptp(cents[:, 1]) else 1 + order = np.argsort(cents[:, axis]) if len(cents) else [] + return [objs[i] for i in order] + +# ------------------------------------------------------- scanner calibration +# +# A flatbed's carriage may not travel exactly perpendicular to its sensor, and +# its step size along the travel may be slightly off, so a scan is the true +# object seen through scan = M . true, M = [[1, k], [0, sy]] +# (x = along the sensor, assumed exact; y = carriage travel). Squares come out +# as slightly skewed rectangles. Measure M once with --calibrate (two scans of +# the same flat cover, turned roughly a quarter turn between them); after that +# every crop is computed in true geometry and the correction is folded into +# the single final resample, so it costs no extra interpolation. + +CAL = None # {"sy": .., "k": ..} when a calibration is loaded + + +def load_calibration(path): + global CAL + with open(path) as fh: + CAL = json.load(fh) + return CAL + + +def true_space(arr, cal): + """(arr_true, A): arr resampled into true geometry for *analysis only*, and + A (2x3) mapping true coords -> scan coords, used for the final render.""" + if not cal: + return arr, None + sy, k = float(cal["sy"]), float(cal["k"]) + M = np.array([[1.0, k], [0.0, sy]]) + Mi = np.linalg.inv(M) + h, w = arr.shape[:2] + corners = np.array([[0, 0], [w, 0], [0, h], [w, h]], float) + tc = corners @ Mi.T + lo = tc.min(axis=0) + Wt, Ht = int(math.ceil(tc[:, 0].max() - lo[0])), int(math.ceil(tc[:, 1].max() - lo[1])) + A = np.hstack([M, (M @ lo).reshape(2, 1)]) # scan = M (t + lo) + arr_t = cv2.warpAffine(arr, A, (Wt, Ht), flags=cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP, + borderMode=cv2.BORDER_REPLICATE) + return arr_t, A + + +def frame_to_scan(fr, A): + """Express an output frame defined in true geometry in scan coordinates.""" + if A is None: + return fr + L, t = A[:, :2], A[:, 2] + f2 = dict(fr) + f2["origin"] = L @ np.asarray(fr["origin"], float) + t + f2["ex"] = L @ np.asarray(fr["ex"], float) + f2["ey"] = L @ np.asarray(fr["ey"], float) + return f2 + + +def calibrate_pair(path1, path2, verbose=True): + """Measure the scanner's stretch and shear from two scans of the same flat, + detailed item (a cover), turned roughly 90 degrees between scans.""" + from scipy.optimize import least_squares + imgs = [] + for p in (path1, path2): + a8, _, _ = load_scan(p) + g = a8[..., :3].mean(axis=2).astype(np.float32) if a8.ndim == 3 else a8.astype(np.float32) + imgs.append(g) + # 1. match features on the cover at 1/2 scale, 2. fit a full affine to all + # inliers (thousands of matches pin it to ~1e-6) + h = 2 + sm = [cv2.resize(g, (g.shape[1] // h, g.shape[0] // h), interpolation=cv2.INTER_AREA) for g in imgs] + sm = [np.clip(g, 0, 255).astype(np.uint8) for g in sm] + masks = [] + for g in imgs: + rect = coarse_rects(np.dstack([g] * 3), 1)[0] + m = np.zeros(sm[0].shape[:2] if g is imgs[0] else sm[1].shape[:2], np.uint8) + box = cv2.boxPoints(((rect[0][0] / h, rect[0][1] / h), (rect[1][0] / h * 0.97, rect[1][1] / h * 0.97), rect[2])) + cv2.fillConvexPoly(m, box.astype(np.int32), 255) + masks.append(m) + det = cv2.SIFT_create(nfeatures=20000) + k1, d1 = det.detectAndCompute(sm[0], masks[0]); k2, d2 = det.detectAndCompute(sm[1], masks[1]) + mt = cv2.BFMatcher(cv2.NORM_L2).knnMatch(d1, d2, k=2) + good = [m for m, n in (x for x in mt if len(x) == 2) if m.distance < 0.75 * n.distance] + if len(good) < 50: + raise RuntimeError("couldn't match the two scans - use the same flat, detailed cover in both") + P1 = np.float64([k1[m.queryIdx].pt for m in good]) * h + P2 = np.float64([k2[m.trainIdx].pt for m in good]) * h + A0, inl = cv2.estimateAffine2D(P1, P2, method=cv2.RANSAC, ransacReprojThreshold=3.0, maxIters=5000) + inl = inl.ravel().astype(bool) + for _ in range(3): # least squares on inliers, then tighten + X = np.hstack([P1[inl], np.ones((inl.sum(), 1))]) + sol_, *_ = np.linalg.lstsq(X, P2[inl], rcond=None) + A0 = sol_.T + err = np.linalg.norm(P2 - (P1 @ A0[:, :2].T + A0[:, 2]), axis=1) + inl = err < max(1.0, 2.5 * np.median(err[inl])) + Al = A0[:, :2] + cc = float(inl.sum()); rms_px = float(np.sqrt(np.mean(err[inl] ** 2))) + # 3. solve Al M = M R(phi) for M = [[1,k],[0,sy]] + def resid(p): + k, sy, phi = p + M = np.array([[1, k], [0, sy]]); Rm = np.array([[math.cos(phi), -math.sin(phi)], [math.sin(phi), math.cos(phi)]]) + return (Al @ M - M @ Rm).ravel() + phi0 = math.atan2(Al[1, 0] - Al[0, 1], Al[0, 0] + Al[1, 1]) + sol = least_squares(resid, [0.0, 1.0, phi0]) + k, sy, phi = sol.x + turn = math.degrees(phi) + out = {"sy": float(sy), "k": float(k), "shear_deg": math.degrees(math.atan2(k, sy)), + "y_stretch_pct": (sy - 1) * 100, "turn_deg": turn, "matches": int(cc), "match_rms_px": rms_px, + "fit_residual": float(np.abs(sol.fun).max()), "scans": [os.path.basename(path1), os.path.basename(path2)]} + if abs(abs(turn) - 90) > 45: + out["warning"] = (f"the cover was turned {turn:.0f} deg between scans; turn it roughly a quarter " + f"turn for a reliable measurement") + if verbose: + print(f"scanner calibration from {out['scans'][0]} + {out['scans'][1]} (turned {turn:+.1f} deg):") + print(f" carriage-direction stretch {out['y_stretch_pct']:+.3f}% shear {out['shear_deg']:+.4f} deg" + f" ({int(cc)} matched points, {rms_px:.2f} px rms, model residual {out['fit_residual']:.1e})") + if "warning" in out: + print(" !! " + out["warning"]) + return out + + +def add_calibration(path, res, reset=False): + """Add one measured pair to the calibration file and store the average of + all pairs measured so far (each new pair refines it).""" + hist = [] + if not reset and os.path.exists(path): + with open(path) as fh: + old = json.load(fh) + hist = old.get("history") or [{k: old[k] for k in ("sy", "k", "shear_deg", "y_stretch_pct", "scans") if k in old}] + hist.append({k: res[k] for k in ("sy", "k", "shear_deg", "y_stretch_pct", "scans", "turn_deg") if k in res}) + sy = float(np.mean([h["sy"] for h in hist])); k = float(np.mean([h["k"] for h in hist])) + out = {"sy": sy, "k": k, "shear_deg": math.degrees(math.atan2(k, sy)), "y_stretch_pct": (sy - 1) * 100, + "scans": [f"average of {len(hist)} pair(s)"] if len(hist) > 1 else hist[0]["scans"], + "pairs": len(hist), "history": hist} + if len(hist) > 1: + out["spread_shear_deg"] = float(np.ptp([h["shear_deg"] for h in hist])) + out["spread_stretch_pct"] = float(np.ptp([h["y_stretch_pct"] for h in hist])) + with open(path, "w") as fh: + json.dump(out, fh, indent=2) + return out + + +# ---------------------------------------------------------------- preview + +def make_preview(arr8, infos, outs, path): + rgb = arr8 if arr8.ndim == 3 else np.stack([arr8] * 3, axis=2) + rgb = np.ascontiguousarray(rgb[..., :3]) + h, w = rgb.shape[:2] + s = 1000 / max(h, w) + small = cv2.resize(rgb, (int(w * s), int(h * s)), interpolation=cv2.INTER_AREA) + small = cv2.cvtColor(small, cv2.COLOR_RGB2BGR) + for i, inf in enumerate(infos): + poly = (np.array(inf["corners"]) * s).astype(np.int32) + cv2.polylines(small, [poly], True, (0, 0, 255), 1, cv2.LINE_AA) + crop = (np.array(inf["crop_poly"]) * s).astype(np.int32) + cv2.polylines(small, [crop], True, (0, 200, 0), 1, cv2.LINE_AA) + cx, cy = crop.mean(axis=0).astype(int) + cv2.putText(small, str(i + 1), (cx - 10, cy + 10), cv2.FONT_HERSHEY_SIMPLEX, + 1.2, (0, 0, 255), 3, cv2.LINE_AA) + # corner zooms: 4x of each crop's corners + tiles = [] + for o in outs: + o3 = o if o.ndim == 3 else np.stack([o] * 3, axis=2) + o3 = cv2.cvtColor(np.ascontiguousarray(o3[..., :3]), cv2.COLOR_RGB2BGR) + z = 40 + cs = [o3[:z, :z], o3[:z, -z:], o3[-z:, :z], o3[-z:, -z:]] + cs = [np.pad(cv2.resize(c, (z * 4, z * 4), interpolation=cv2.INTER_NEAREST), + ((3, 3), (3, 3), (0, 0)), constant_values=255) for c in cs] + tiles.append(np.vstack([np.hstack(cs[:2]), np.hstack(cs[2:])])) + if tiles: + zoom = np.hstack([np.pad(t, ((4, 4), (4, 4), (0, 0)), constant_values=128) for t in tiles]) + if zoom.shape[1] > small.shape[1]: + f = small.shape[1] / zoom.shape[1] + zoom = cv2.resize(zoom, (small.shape[1], int(zoom.shape[0] * f)), interpolation=cv2.INTER_NEAREST) + pad = small.shape[1] - zoom.shape[1] + zoom = np.pad(zoom, ((0, 0), (0, pad), (0, 0)), constant_values=255) + small = np.vstack([small, zoom]) + cv2.imwrite(path, small, [cv2.IMWRITE_JPEG_QUALITY, 90]) + +# ------------------------------------------------------------------- main + +def describe_frame(fr, cov): + return {"rotation_deg": fr["theta"], "size": (fr["W"], fr["H"]), "edge_dev_deg": fr["edge_dev_deg"], + "edge_fit": {k: v[2] for k, v in cov["edges"].items()}, "insets": fr["ins"], + "soft": cov["soft"], "borrowed": cov["borrowed"], + "corners": fr["corners"], "crop_poly": fr["crop_poly"]} + + +def review_sheet(arr8, frs, labels, segs, path): + """A/B sheet for a close call. For each candidate: a small overview, the + key art line at 2x with an exactly vertical/horizontal guide (cyan) so any + tilt is obvious, and the four corners at 1:1 - the places where the two + crops actually differ.""" + a = arr8.astype(np.float32) + key = None + if segs: + vs = [s_ for s_ in segs if s_[2]] or list(segs) + key = max(vs, key=lambda s_: s_[3]) + cols = [] + for fr, lab in zip(frs, labels): + o = render(a, fr) + bgr = lambda im: cv2.cvtColor(np.ascontiguousarray(im), cv2.COLOR_RGB2BGR) + ov = bgr(cv2.resize(o, (520, int(520 * fr["H"] / fr["W"])), interpolation=cv2.INTER_AREA)) + parts = [ov] + if key is not None: + P, Q, vert, L = key + uv = lambda p_: np.array([(p_ - fr["origin"]) @ fr["ex"], (p_ - fr["origin"]) @ fr["ey"]]) + p_, q_ = uv(P), uv(Q) + if vert: + e0, e1 = (p_, q_) if p_[1] < q_[1] else (q_, p_) + lean = e1[0] - e0[0] + else: + e0, e1 = (p_, q_) if p_[0] < q_[0] else (q_, p_) + lean = e1[1] - e0[1] + zs = [] + for e in (e0, e1): + cx, cy = int(round(e[0])), int(round(e[1])) + x0, y0 = max(cx - 40, 0), max(cy - 40, 0) + z = bgr(np.ascontiguousarray(o[y0:y0 + 80, x0:x0 + 80])) + z = cv2.resize(z, (240, 240), interpolation=cv2.INTER_NEAREST) + if vert: + gx = int(round((e0[0] - x0 + 0.5) * 3)); cv2.line(z, (gx, 0), (gx, 239), (255, 255, 0), 1) + else: + gy = int(round((e0[1] - y0 + 0.5) * 3)); cv2.line(z, (0, gy), (239, gy), (255, 255, 0), 1) + zs.append(np.pad(z, ((2, 2), (2, 2), (0, 0)), constant_values=255)) + z = np.hstack(zs); z = np.pad(z, ((0, 0), (0, 520 - z.shape[1]), (0, 0)), constant_values=255) + cap = np.full((26, 520, 3), 255, np.uint8) + what = "top / bottom" if vert else "left / right" + cv2.putText(cap, f"key art line, {what} ends 3x: leans {abs(lean):.1f} px over {L:.0f} px", + (6, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.47, (0, 0, 0), 1, cv2.LINE_AA) + parts += [cap, z] + n = 256 + cs = [o[:n, :n], o[:n, -n:], o[-n:, :n], o[-n:, -n:]] + cs = [np.pad(bgr(c), ((2, 2), (2, 2), (0, 0)), constant_values=255) for c in cs] + grid = np.vstack([np.hstack(cs[:2]), np.hstack(cs[2:])]) + grid = cv2.resize(grid, (520, 520), interpolation=cv2.INTER_AREA) + cap = np.full((26, 520, 3), 255, np.uint8) + cv2.putText(cap, "corners, 1:1 (top-left, top-right / bottom-left, bottom-right)", (6, 18), + cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 0), 1, cv2.LINE_AA) + parts += [cap, grid] + head = np.full((40, 520, 3), 255, np.uint8) + cv2.putText(head, lab, (6, 28), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2, cv2.LINE_AA) + cols.append(np.vstack([head] + parts)) + h = max(c.shape[0] for c in cols) + cols = [np.pad(c, ((0, h - c.shape[0]), (10, 10), (0, 0)), constant_values=255) for c in cols] + cv2.imwrite(path, np.hstack(cols), [cv2.IMWRITE_JPEG_QUALITY, 92]) + + +LOG_FIELDS = ["file", "cover", "chosen_deg", "mode", "alt_deg", "flagged", "line_conf_v", "line_conf_h", + "art_vertical_deg", "edge_horizontal_deg", "left_deg", "right_deg", "top_deg", "bottom_deg", + "soft_edges", "width", "height"] + + +def process(path, outdir, align, overrides, count, inset, preview, log_rows): + arr8, mode, dpi = load_scan(path) + scan_px = arr8.astype(np.float32) + # all measuring happens in true geometry; the final render maps back to the + # original scan pixels, so the calibration costs no extra resample + arr, CALA = true_space(scan_px, CAL) + arr8v = np.clip(np.rint(arr), 0, 255).astype(np.uint8) if CALA is not None else arr8 + gray = arr[..., :3].mean(axis=2) if arr.ndim == 3 else arr + rects = coarse_rects(arr, count) + base = os.path.splitext(os.path.basename(path))[0] + if len(rects) < count: + print(f"!! {base}: found {len(rects)} cover(s), expected {count}") + cents = np.array([r[0] for r in rects]) if rects else np.zeros((0, 2)) + axis = 0 if len(cents) < 2 or np.ptp(cents[:, 0]) >= np.ptp(cents[:, 1]) else 1 + order = np.argsort(cents[:, axis]) if len(cents) else [] + infos, outs = [], [] + kinds = {i: "cover" for i in range(len(rects))} + if rects: + _, circ = coarse_rects(arr, count, return_shape=True) + kinds = {i: ("disc" if c >= DISC_CIRC else "cover") for i, c in enumerate(circ)} + for idx, ri in enumerate(order, start=1): + if kinds.get(ri) == "disc": + (cx0, cy0), (w0, h0), _ = rects[ri] + center, R = fit_disc(arr, (cx0, cy0), (dpi or (600, 600))[0], (w0 + h0) / 2) + ori = disc_orientation(gray, center, R) + ov = overrides.get((os.path.basename(path), idx)) + theta = float(ov) if ov not in (None, "auto") and ov not in ("left", "right", "top", "bottom") else ori["auto"] + fr = disc_frame(center, R, theta) + out = render(scan_px, frame_to_scan(fr, CALA)) + save_png(out, mode, dpi, os.path.join(outdir, f"{base}_{idx}.png")) + infos.append({"corners": fr["corners"], "crop_poly": fr["crop_poly"]}); outs.append(out) + print(f" {base}_{idx}.png disc {2 * R / (dpi or (600,))[0] * 25.4:.1f} mm {fr['W']}x{fr['H']} " + f"rotated {theta:+.2f}° ({ori['why']})") + if ori["flagged"]: + print(" ^ orientation needs a look: candidates " + + ", ".join(f"{k} {v:+.1f}°" for k, v in ori["candidates"].items())) + log_rows.append({"file": os.path.basename(path), "cover": idx, "chosen_deg": f"{theta:.4f}", + "mode": "disc: " + ori["why"], "alt_deg": "", "flagged": int(ori["flagged"]), + "line_conf_v": "", "line_conf_h": "", "art_vertical_deg": "", "edge_horizontal_deg": "", + "left_deg": "", "right_deg": "", "top_deg": "", "bottom_deg": "", "soft_edges": "", + "width": fr["W"], "height": fr["H"]}) + continue + cov = analyze_cover(arr, rects[ri]) + ov = overrides.get((os.path.basename(path), idx), align) + ch = None + if ov == "auto": + ch = choose_angle(arr, gray, cov, inset) + theta, how = ch["theta"], "auto" + elif ov in ("left", "right", "top", "bottom"): + src = cov["borrowed"].get(ov, ov) + theta = edge_theta(src, cov["edges"][src][1]) if src in cov["edges"] else 0.0 + how = f"{ov} edge" + (f" (soft, used {src})" if ov in cov["soft"] else "") + else: + theta, how = float(ov), "fixed angle" + fr = frame_at(cov, theta, inset) + out = render(scan_px, frame_to_scan(fr, CALA)) + out_path = os.path.join(outdir, f"{base}_{idx}.png") + save_png(out, mode, dpi, out_path) + info = describe_frame(fr, cov) + infos.append(info); outs.append(out) + + named = {k: edge_theta(k, cov["edges"][k][1]) for k in ("left", "right", "top", "bottom") + if k not in cov["soft"]} + print(f" {base}_{idx}.png {fr['W']}x{fr['H']} rotated {theta:+.3f}° ({how})") + print(" edge angles: " + ", ".join( + f"{k} {'soft' if k in cov['soft'] else f'{named[k]:+.3f}°'}" for k in ("left", "right", "top", "bottom"))) + flagged = False + if ch is not None: + bits = [] + if ch["art_theta"] is not None and ch["conf_v"] > 0.15: + bits.append(f"art verticals {ch['art_theta']:+.3f}° (confidence {ch['conf_v']:.0%})") + if ch["art_theta_h"] is not None and ch["conf_h"] > 0.15: + bits.append(f"edge horizontals {ch['art_theta_h']:+.3f}° (confidence {ch['conf_h']:.0%})") + print(" art lines: " + ("; ".join(bits) if bits else "none usable - chose by edges and art kept")) + if ch["alt"] is not None: + flagged = True + rdir = os.path.join(outdir, "_review"); os.makedirs(rdir, exist_ok=True) + fa = frame_at(cov, ch["alt"], inset) + save_png(render(arr, fa), mode, dpi, os.path.join(rdir, f"{base}_{idx}_alt.png")) + bl = "left edge" if ch["alt_why"] == "left edge" else "runner-up" + review_sheet(arr8, [fr, fa], + [f"A (used): {theta:+.3f} deg", f"B ({bl}): {ch['alt']:+.3f} deg"], + ch["segs"], os.path.join(rdir, f"{base}_{idx}_A-vs-B.jpg")) + if ch["alt_why"] == "left edge": + driver = ("art lines" if ch["best_line"] * W_V * ch["conf_v"] > + ch["named"].get("left", {}).get("loss", 0) else "art kept near the edges") + why = f"turned {theta - ch['alt']:+.2f}° from the left edge ({driver})" + else: + why = "close call" + print(f" ^ {why} - B = {ch['alt']:+.3f}°: see _review/{base}_{idx}_A-vs-B.jpg; " + f"if B looks better, use _review/{base}_{idx}_alt.png") + for k in cov["soft"]: + print(f" ^ {k} edge has no crisp line (fold, lifted or worn paper); " + f"cropped where the in-focus art begins.") + W, H = fr["W"], fr["H"] + if abs(W / H - 1) > 0.02: + print(f" ^ not square ({W/H:.3f}). Fine for a back inlay or digipak; on a jewel-case " + f"front it usually means art blends into the lid along one edge - check the preview.") + log_rows.append({ + "file": os.path.basename(path), "cover": idx, "chosen_deg": f"{theta:.4f}", "mode": how, + "alt_deg": "" if not ch or ch["alt"] is None else f"{ch['alt']:.4f}", "flagged": int(flagged), + "line_conf_v": "" if not ch else f"{ch['conf_v']:.2f}", "line_conf_h": "" if not ch else f"{ch['conf_h']:.2f}", + "art_vertical_deg": "" if not ch or ch["art_theta"] is None else f"{ch['art_theta']:.4f}", + "edge_horizontal_deg": "" if not ch or ch["art_theta_h"] is None else f"{ch['art_theta_h']:.4f}", + **{f"{k}_deg": (f"{named[k]:.4f}" if k in named else "soft") for k in ("left", "right", "top", "bottom")}, + "soft_edges": "/".join(cov["soft"]), "width": W, "height": H}) + if preview and infos: + pdir = os.path.join(outdir, "_previews") + os.makedirs(pdir, exist_ok=True) + make_preview(arr8v, infos, outs, os.path.join(pdir, f"{base}_preview.jpg")) + + +def _run_one(job): + """Process one scan, capturing its report so parallel runs print cleanly.""" + import io, contextlib, warnings + global CAL + CAL = job[7] + job = job[:7] + f = job[0] + buf, rows = io.StringIO(), [] + with contextlib.redirect_stdout(buf), warnings.catch_warnings(): + warnings.simplefilter("ignore") + print(os.path.basename(f)) + try: + process(*job, rows) + except Exception as e: + print(f"!! {f}: {e}") + return buf.getvalue(), rows + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("inputs", nargs="*") + ap.add_argument("-o", "--outdir", default="cropped") + ap.add_argument("--align", default="auto", choices=["auto", "left", "right", "top", "bottom"], + help="auto (default) weighs art lines, art kept and the left edge; " + "or force one edge for every cover") + ap.add_argument("--align-override", action="append", default=[], + help='per cover: "scan012.bmp:2=top", "=left", "=auto" or an angle "=+0.15"') + ap.add_argument("--count", type=int, default=2) + ap.add_argument("--inset", type=float, default=1.0, + help="minimum px trimmed inside each edge; soft edges get more automatically") + ap.add_argument("--no-preview", action="store_true") + ap.add_argument("--calibrate", nargs=2, metavar=("SCAN_A", "SCAN_B"), + help="measure the scanner from two scans of the same flat cover turned ~90 deg, save, and exit") + ap.add_argument("--calibration", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), + "scanner_calibration.json"), + help="scanner calibration file (default: scanner_calibration.json next to this script, if present)") + ap.add_argument("--no-calibration", action="store_true", help="ignore any scanner calibration") + ap.add_argument("--calibrate-reset", action="store_true", + help="with --calibrate: discard earlier pairs instead of averaging with them") + ap.add_argument("--jobs", type=int, default=1, + help="scans processed in parallel (each needs ~1.5 GB RAM at 600 dpi)") + a = ap.parse_args() + + if a.calibrate: + res = calibrate_pair(*a.calibrate) + out = add_calibration(a.calibration, res, reset=a.calibrate_reset) + if out["pairs"] > 1: + print(f"average of {out['pairs']} pairs: stretch {out['y_stretch_pct']:+.3f}%, shear {out['shear_deg']:+.4f} deg " + f"(pairs differ by up to {out['spread_stretch_pct']:.3f}% / {out['spread_shear_deg']:.4f} deg)") + print(f"saved to {a.calibration} - it will be applied to every crop from now on") + return + if not a.no_calibration and os.path.exists(a.calibration): + cal = load_calibration(a.calibration) + print(f"scanner calibration: stretch {cal['y_stretch_pct']:+.3f}%, shear {cal['shear_deg']:+.4f} deg " + f"(from {os.path.basename(a.calibration)})") + + if not a.inputs: + ap.error("give scan files or a folder (or --calibrate SCAN_A SCAN_B)") + overrides = {} + for o in a.align_override: + lhs, side = o.split("=", 1) + f, n = lhs.rsplit(":", 1) + overrides[(f, int(n))] = side.strip() + + files = [] + for p in a.inputs: + if os.path.isdir(p): + files += sorted(os.path.join(p, f) for f in os.listdir(p) if f.lower().endswith(".bmp")) + else: + files.append(p) + os.makedirs(a.outdir, exist_ok=True) + log_rows = [] + jobs = [(f, a.outdir, a.align, overrides, a.count, a.inset, not a.no_preview, CAL) for f in files] + if a.jobs > 1 and len(files) > 1: + from multiprocessing import Pool + with Pool(a.jobs) as pool: + for text, rows in pool.imap(_run_one, jobs): + print(text, end=""); log_rows += rows + else: + for j in jobs: + text, rows = _run_one(j) + print(text, end=""); log_rows += rows + if log_rows: + import csv + lp = os.path.join(a.outdir, "cdcrop_log.csv") + new = not os.path.exists(lp) + with open(lp, "a", newline="") as fh: + w = csv.DictWriter(fh, fieldnames=LOG_FIELDS) + if new: + w.writeheader() + w.writerows(log_rows) + n_flag = sum(r["flagged"] for r in log_rows) + print(f"\n{len(log_rows)} covers cropped, {n_flag} close call(s) in _review/. Log: {lp}") + + +TUNING = load_tuning() + +if __name__ == "__main__": + main() diff --git a/cdcrop_tune.py b/cdcrop_tune.py new file mode 100644 index 0000000..111ce6c --- /dev/null +++ b/cdcrop_tune.py @@ -0,0 +1,233 @@ +#!/usr/bin/env python3 +""" +cdcrop_tune.py - tune cdcrop's automatic angle choice to your own picks. + + python cdcrop_tune.py --scans "C:\\path\\to\\scans" --decisions "C:\\path\\to\\output\\cdcrop_decisions.csv" + +Runs on your computer with your scans, so the result matches exactly what the +cropper does there. For every cover you reviewed in the interface, it +measures everything the automatic choice weighs, then tries a few thousand +weightings and keeps the one that reproduces your picks best - preferring +settings in the middle of a good region, so small differences on new covers +don't flip results. The winner is saved to cdcrop_tuning.json next to +cdcrop.py and used automatically from then on (delete that file to go back +to the built-in weights). Run it again whenever you've reviewed a new batch. + +A pick counts as reproduced when the automatic angle lands within --tol px +(default 2) of yours, measured as drift across the whole cover. New weights +are only saved if they predict covers they weren't tuned on (leave-one-out) +better than the current ones - otherwise they'd just be memorising. +""" +import argparse +import csv +import itertools +import json +import math +import os +import sys +import time +import warnings + +import numpy as np + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import cdcrop # noqa: E402 + +DETAIL_GRID = {"DEEP_BLEND": (0.0, 0.5, 1.0), "SAL_POWER": (1.0, 2.0), "SAL_FLOOR": (50, 60, 70)} +WEIGHT_GRID = {"W_V": (0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 6.0, 8.0), + "W_LEFT": (0.0, 0.05, 0.1, 0.2, 0.4), + "W_EDGE": (0.0, 0.01, 0.02, 0.05), + "W_BLUR": (0.0, 0.25, 0.5)} +H_RATIO = 0.5 # W_H = W_V * H_RATIO (horizontal lines count half, as calibrated so far) + + +def read_picks(path): + """Latest reviewed angle per cover (discs are skipped - they're tuned by hand).""" + 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 + picks[row["cover"]] = {"chosen": float(row["chosen_deg"]), "how": row["how"], + "logged": {k: row.get(f"{k}_deg") for k in ("left", "right", "top", "bottom")}} + return picks + + +def analyse(scans_dir, picks, inset, cal): + """angle_components for every reviewed cover, with one loss per detail variant.""" + by_scan = {} + for cid in picks: + base, idx = cid.rsplit("_", 1) + by_scan.setdefault(base, []).append(int(idx)) + variants = [dict(zip(DETAIL_GRID, v)) for v in itertools.product(*DETAIL_GRID.values())] + 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) + gray = arr[..., :3].mean(axis=2) if arr.ndim == 3 else arr + 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) + comp = cdcrop.angle_components(gray, cov, inset) + losses, blurs = [], [] + for v in variants: + saved = {k: getattr(cdcrop, k) for k in v} + for k, val in v.items(): + setattr(cdcrop, k, val) + losses.append(cdcrop.component_loss(comp, gray)) + blurs.append(comp["blur"]) + for k, val in saved.items(): + setattr(cdcrop, k, val) + comp["losses"], comp["blurs"] = losses, blurs + # sanity check: do our edge angles match what the interface logged? + mism = [] + for k, v in picks[cid]["logged"].items(): + if v not in (None, "", "soft") and k in comp["named"]: + if abs(float(v) - comp["named"][k]) > 0.01: + mism.append(f"{k} {float(v):+.3f} logged vs {comp['named'][k]:+.3f} now") + covers[cid] = comp + note = (" !! differs from the logged run: " + "; ".join(mism)) if mism else "" + print(f" {cid}: measured{note}") + print(f" ({base}: {time.time() - t0:.0f}s)") + return covers, variants + + +def evaluate(covers, picks, variants, tol): + """Score every weighting: how many picks it reproduces, and how closely.""" + names = list(WEIGHT_GRID) + grid = list(itertools.product(range(len(variants)), *[range(len(v)) for v in WEIGHT_GRID.values()])) + results = [] + cids = sorted(covers) + for gi in grid: + vi, wi = gi[0], gi[1:] + W = {n: WEIGHT_GRID[n][j] for n, j in zip(names, wi)} + errs = [] + for cid in cids: + c = covers[cid] + total = (c["losses"][vi] + W["W_V"] * c["conf_v"] * c["cv"] + + W["W_V"] * H_RATIO * c["conf_h"] * c["chh"] + + W["W_LEFT"] * c["left_px"] + W["W_EDGE"] * c["edge_px"] + + W["W_BLUR"] * c["blurs"][vi]) + best = c["thetas"][int(np.argmin(total))] + errs.append(abs(best - picks[cid]["chosen"]) * math.pi / 180 * c["side"]) + errs = np.array(errs) + results.append((gi, int((errs <= tol).sum()), float(np.minimum(errs, 20).sum()), errs)) + return results, cids + + +def robust_best(results, mask=None, tol=2.0): + """Most picks reproduced (only covers in mask, if given); among those, the + setting whose grid neighbours do as well (the middle of a good region), + then the smallest total error.""" + sel = (lambda e: e) if mask is None else (lambda e: e[mask]) + score = {r[0]: int((sel(r[3]) <= tol).sum()) for r in results} + err = {r[0]: float(np.minimum(sel(r[3]), 20).sum()) for r in results} + top = max(score.values()) + def support(gi): + n = 0 + for d in range(len(gi)): + for s in (-1, 1): + nb = list(gi); nb[d] += s + if score.get(tuple(nb)) == top: + n += 1 + return n + cands = [r for r in results if score[r[0]] == top] + return max(cands, key=lambda r: (support(r[0]), -err[r[0]])), top + + +def leave_one_out(results, n, tol): + """Tune on all covers but one, check the one left out; repeat for each. + The honest measure of whether tuning learned your taste or just fit + these particular covers.""" + hits = 0 + for k in range(n): + mask = np.ones(n, bool); mask[k] = False + (r, _) = robust_best(results, mask, tol) + hits += int(r[3][k] <= tol) + return hits + + +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 drift across the cover that counts as a match") + ap.add_argument("--inset", type=float, default=1.0) + ap.add_argument("--no-calibration", action="store_true", help="tune 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 weights even if they don't beat the current ones on unseen covers") + a = ap.parse_args() + warnings.simplefilter("ignore") + + 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_picks(a.decisions) + print(f"{len(picks)} reviewed covers in {os.path.basename(a.decisions)}; measuring them…") + covers, variants = analyse(a.scans, picks, a.inset, cal) + if len(covers) < 3: + print("!! need at least 3 reviewed covers to tune"); return + + results, cids = evaluate(covers, picks, variants, a.tol) + # the current settings, for comparison + cur = {k: getattr(cdcrop, k) for k in cdcrop.TUNABLE} + cur_errs = [] + for cid in cids: + c = covers[cid] + ch = cdcrop.combine_angle(c) # uses the loss computed at current detail settings + cur_errs.append(abs(ch["theta"] - picks[cid]["chosen"]) * math.pi / 180 * c["side"]) + cur_errs = np.array(cur_errs) + (gi, n, tot, errs), top = robust_best(results, None, a.tol) + cur_hits = int((cur_errs <= a.tol).sum()) + loo = leave_one_out(results, len(cids), a.tol) + W = {"W_V": WEIGHT_GRID["W_V"][gi[1]], "W_LEFT": WEIGHT_GRID["W_LEFT"][gi[2]], "W_EDGE": WEIGHT_GRID["W_EDGE"][gi[3]], + "W_BLUR": WEIGHT_GRID["W_BLUR"][gi[4]]} + W["W_H"] = W["W_V"] * H_RATIO + W.update(variants[gi[0]]) + + print(f"\ncurrent settings reproduce {int((cur_errs <= a.tol).sum())} of {len(cids)} picks; " + f"tuned settings reproduce {n} of {len(cids)}") + print(f"{'cover':22s} {'your pick':>10s} {'current':>9s} {'tuned':>9s} (px of drift from your pick)") + for cid, e_now, e_new in zip(cids, cur_errs, errs): + c = covers[cid] + print(f"{cid:22s} {picks[cid]['chosen']:+10.3f} {e_now:8.1f}px {e_new:8.1f}px" + + (" <- still off" if e_new > a.tol else "")) + print("\ntuned weights: " + ", ".join(f"{k}={v:g}" for k, v in W.items())) + print(f"check on unseen covers (leave-one-out): tuned settings predict {loo} of {len(cids)}, " + f"current settings {cur_hits} of {len(cids)}") + if loo <= cur_hits and not a.force: + print("-> tuning doesn't predict unseen covers any better than the current settings 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, **W} # keep any tunables this script doesn't touch (e.g. CORNER_LENIENCY, + # fit by cdcrop_tune_crop.py) at their current value instead of dropping them + out = {"weights": W, "picks": len(cids), "reproduced": n, "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): + os.replace(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() diff --git a/cdcrop_tune_crop.py b/cdcrop_tune_crop.py new file mode 100644 index 0000000..de3c3e8 --- /dev/null +++ b/cdcrop_tune_crop.py @@ -0,0 +1,188 @@ +#!/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() diff --git a/cdcrop_ui.py b/cdcrop_ui.py new file mode 100644 index 0000000..6960b60 --- /dev/null +++ b/cdcrop_ui.py @@ -0,0 +1,897 @@ +#!/usr/bin/env python3 +""" +cdcrop_ui.py - local review interface for cdcrop. + + python cdcrop_ui.py (opens http://127.0.0.1:8765 in your browser) + python cdcrop_ui.py --port 9000 --no-browser + +Everything runs on this computer: the server only listens on 127.0.0.1 and +nothing is sent anywhere. Put cdcrop.py next to this file. + +Workflow + 1. Choose a scans folder and an output folder, press Process. Every cover is + cropped at the automatic choice and written straight away, so covers you + never open are already done. + 2. Review: flagged covers come first. Open one, pick an angle, press Enter. + The full-resolution PNG is re-rendered and your choice is logged to + OUTDIR/cdcrop_decisions.csv (used to calibrate the automatic choice). + 3. Close the window and re-open later: reviewing resumes where you left off. + +Keys in the detail view + 1-9 pick a candidate (shown on its button) A / B automatic / alternative + < > nudge 0.01 deg (Shift: 0.05, Ctrl: 0.1) + Space blink: flip to the previous angle and back + G grid overlay Z zoom panel on/off + Enter accept & save Esc back to grid + N / P next / previous cover +""" +import argparse +import csv +import io +import json +import math +import os +import pickle +import sys +import threading +import time +import traceback +import webbrowser +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from urllib.parse import parse_qs, urlparse + +import cv2 +import numpy as np + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import cdcrop # noqa: E402 + +PREVIEW_SHRINK = 3 # cached preview source is 1/3 scale +REGION_MARGIN = 120 # px of scan kept around each cover (for re-rendering) +CTX_MARGIN = 100 # px of that margin the "context" view is allowed to show +LAST_RUN_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cdcrop_ui_last.json") + +# ---------------------------------------------------------------- processing + + +def _cover_entry(arr, gray, cov, base, idx, path, outdir, mode, dpi, inset, align, scan_px=None, A=None): + """Crop one cover at the automatic angle, write it, cache what review needs.""" + ch = cdcrop.choose_angle(arr, gray, cov, inset) + theta = ch["theta"] + if align != "auto": + src = cov["borrowed"].get(align, align) + if src in cov["edges"]: + theta = cdcrop.edge_theta(src, cov["edges"][src][1]) + fr = cdcrop.frame_at(cov, theta, inset) + scan_px = arr if scan_px is None else scan_px + out = cdcrop.render(scan_px, cdcrop.frame_to_scan(fr, A)) + cdcrop.save_png(out, mode, dpi, os.path.join(outdir, f"{base}_{idx}.png")) + + # cache the scan region around the cover (uint8) + a 1/3-scale copy for previews + q = np.array([_to_scan(p_, A) for p_ in fr["corners"]]) + x0, y0 = np.maximum(np.floor(q.min(axis=0)).astype(int) - REGION_MARGIN, 0) + x1 = min(int(np.ceil(q[:, 0].max())) + REGION_MARGIN, scan_px.shape[1]) + y1 = min(int(np.ceil(q[:, 1].max())) + REGION_MARGIN, scan_px.shape[0]) + region = np.clip(np.rint(scan_px[y0:y1, x0:x1]), 0, 255).astype(np.uint8) + small = cv2.resize(region, (region.shape[1] // PREVIEW_SHRINK, region.shape[0] // PREVIEW_SHRINK), + interpolation=cv2.INTER_AREA) + cid = f"{base}_{idx}" + cdir = os.path.join(outdir, "_cache") + os.makedirs(cdir, exist_ok=True) + cv2.imwrite(os.path.join(cdir, cid + "_small.jpg"), cv2.cvtColor(small, cv2.COLOR_RGB2BGR), + [cv2.IMWRITE_JPEG_QUALITY, 95]) + + named = {k: float(v["theta"]) for k, v in ch["named"].items()} + key = None + if ch["segs"]: + vs = [s for s in ch["segs"] if s[2]] or list(ch["segs"]) + P, Q, vert, L = max(vs, key=lambda s: s[3]) + key = {"P": [float(P[0]), float(P[1])], "Q": [float(Q[0]), float(Q[1])], "vert": bool(vert), "L": float(L)} + with open(os.path.join(cdir, cid + "_cov.pkl"), "wb") as fh: + pickle.dump({"cov": cov, "offset": (int(x0), int(y0)), "end": (int(x1), int(y1)), + "mode": mode, "dpi": dpi, "inset": inset, "key": key, + "scan_path": os.path.abspath(path), "A": None if A is None else A.tolist()}, fh) + + notes = [] + for k in cov["soft"]: + notes.append(f"{k} edge has no crisp line (fold / lifted / worn); cropped where in-focus art begins") + if abs(fr["W"] / fr["H"] - 1) > 0.02: + notes.append(f"not square ({fr['W'] / fr['H']:.3f}) - fine for inlays/digipaks") + flagged = ch["alt"] is not None + if flagged: + notes.insert(0, ("close call" if ch["alt_why"] == "close call" + else f"turned {theta - ch['alt']:+.2f} deg from the left edge") + + f" - alternative {ch['alt']:+.3f} deg") + return { + "id": cid, "scan": os.path.basename(path), "cover": idx, + "auto": float(theta), "alt": None if ch["alt"] is None else float(ch["alt"]), + "alt_why": ch["alt_why"], "named": named, "soft": cov["soft"], + "edges": {k: (None if k in cov["soft"] else float(cdcrop.edge_theta(k, cov["edges"][k][1]))) + for k in ("left", "right", "top", "bottom")}, + "flagged": flagged, "notes": notes, "size": [fr["W"], fr["H"]], + "chosen": None, "status": "auto", "edge_adj": {}, + } + + +def _to_scan(p, A): + p = np.asarray(p, float) + return p if A is None else np.asarray(A)[:, :2] @ p + np.asarray(A)[:, 2] + + +def _cache_region(arr, poly, cid, outdir, A=None): + q = np.array([_to_scan(p_, A) for p_ in poly]) + x0, y0 = np.maximum(np.floor(q.min(axis=0)).astype(int) - REGION_MARGIN, 0) + x1 = min(int(np.ceil(q[:, 0].max())) + REGION_MARGIN, arr.shape[1]) + y1 = min(int(np.ceil(q[:, 1].max())) + REGION_MARGIN, arr.shape[0]) + region = np.clip(np.rint(arr[y0:y1, x0:x1]), 0, 255).astype(np.uint8) + small = cv2.resize(region, (region.shape[1] // PREVIEW_SHRINK, region.shape[0] // PREVIEW_SHRINK), + interpolation=cv2.INTER_AREA) + cdir = os.path.join(outdir, "_cache") + os.makedirs(cdir, exist_ok=True) + cv2.imwrite(os.path.join(cdir, cid + "_small.jpg"), cv2.cvtColor(small, cv2.COLOR_RGB2BGR), + [cv2.IMWRITE_JPEG_QUALITY, 95]) + return (int(x0), int(y0)), (int(x1), int(y1)) + + +def _disc_entry(arr, gray, rect, base, idx, path, outdir, mode, dpi, scan_px=None, A=None): + (cx0, cy0), (w0, h0), _ = rect + center, R = cdcrop.fit_disc(arr, (cx0, cy0), (dpi or (600, 600))[0], (w0 + h0) / 2) + ori = cdcrop.disc_orientation(gray, center, R) + theta = ori["auto"] + fr = cdcrop.disc_frame(center, R, theta) + scan_px = arr if scan_px is None else scan_px + cdcrop.save_png(cdcrop.render(scan_px, cdcrop.frame_to_scan(fr, A)), mode, dpi, + os.path.join(outdir, f"{base}_{idx}.png")) + cid = f"{base}_{idx}" + off, end = _cache_region(scan_px, fr["corners"], cid, outdir, A) + with open(os.path.join(outdir, "_cache", cid + "_cov.pkl"), "wb") as fh: + pickle.dump({"type": "disc", "center": [float(center[0]), float(center[1])], "R": float(R), + "offset": off, "end": end, "mode": mode, "dpi": dpi, + "scan_path": os.path.abspath(path), "A": None if A is None else A.tolist()}, fh) + mm = 2 * R / (dpi or (600,))[0] * 25.4 + notes = [f"disc {mm:.1f} mm - {ori['why']}"] + if ori["flagged"]: + notes.insert(0, "orientation needs a look") + return {"id": cid, "scan": os.path.basename(path), "cover": idx, "type": "disc", + "auto": float(theta), "alt": None, "alt_why": None, + "named": {k: float(v) for k, v in ori["candidates"].items()}, "soft": [], + "edges": {}, "flagged": ori["flagged"], "notes": notes, "size": [fr["W"], fr["H"]], + "chosen": None, "status": "auto"} + + +def process_scan(job): + """Worker: one scan -> list of cover entries (runs in a separate process).""" + path, outdir, count, inset, align, cal = job + import warnings + warnings.simplefilter("ignore") + try: + arr8, mode, dpi = cdcrop.load_scan(path) + scan_px = arr8.astype(np.float32) + arr, A = cdcrop.true_space(scan_px, cal) # measure in true geometry + gray = arr[..., :3].mean(axis=2) if arr.ndim == 3 else arr + objs = cdcrop.find_objects(arr, count, dpi) + base = os.path.splitext(os.path.basename(path))[0] + entries = [] + for idx, (kind, rect, _) in enumerate(objs, start=1): + if kind == "disc": + entries.append(_disc_entry(arr, gray, rect, base, idx, path, outdir, mode, dpi, scan_px, A)) + else: + cov = cdcrop.analyze_cover(arr, rect) + entries.append(_cover_entry(arr, gray, cov, base, idx, path, outdir, mode, dpi, inset, align, + scan_px, A)) + err = None if len(objs) == count else f"found {len(objs)} item(s), expected {count}" + return {"scan": os.path.basename(path), "entries": entries, "error": err} + except Exception as e: + return {"scan": os.path.basename(path), "entries": [], "error": f"{e}\n{traceback.format_exc()}"} + + +class Session: + """All state for one output folder. Persists to OUTDIR/_cache/index.json.""" + + def __init__(self): + self.lock = threading.Lock() + self.outdir = None + self.scans_dir = None + self.entries = {} # id -> entry + self.order = [] # ids in scan order + self.progress = {"running": False, "done": 0, "total": 0, "current": "", "errors": []} + self.settings = {"count": 2, "inset": 1.0, "jobs": 2, "align": "auto"} + self._cache = {} # id -> (region, small, meta) small LRU + + # -- persistence + def index_path(self): + return os.path.join(self.outdir, "_cache", "index.json") + + def save(self): + if not self.outdir: + return + os.makedirs(os.path.join(self.outdir, "_cache"), exist_ok=True) + with self.lock: + data = {"scans_dir": self.scans_dir, "settings": self.settings, + "order": self.order, "entries": self.entries} + tmp = self.index_path() + ".tmp" + with open(tmp, "w") as fh: + json.dump(data, fh) + os.replace(tmp, self.index_path()) + + def open(self, outdir, scans_dir=None): + self.outdir = os.path.abspath(outdir) + self.entries, self.order, self._cache = {}, [], {} + if os.path.exists(self.index_path()): + with open(self.index_path()) as fh: + data = json.load(fh) + self.entries = data.get("entries", {}) + self.order = sorted(data.get("order", [])) + self.settings.update(data.get("settings", {})) + self.scans_dir = data.get("scans_dir") + if scans_dir: + self.scans_dir = os.path.abspath(scans_dir) + with open(LAST_RUN_PATH, "w") as fh: # so the next launch reopens this folder + json.dump({"outdir": self.outdir}, fh) + + # -- processing + def start(self, scans_dir, outdir, settings, redo=False): + if self.progress["running"]: + return "already processing" + os.makedirs(os.path.join(os.path.abspath(outdir), "_cache"), exist_ok=True) + self.open(outdir, scans_dir) + self.settings.update(settings) + files = sorted(f for f in os.listdir(self.scans_dir) if f.lower().endswith(".bmp")) + done_scans = {e["scan"] for e in self.entries.values()} + todo = [f for f in files if redo or f not in done_scans] + self.progress = {"running": True, "done": 0, "total": len(todo), "current": "", "errors": []} + threading.Thread(target=self._run, args=(todo,), daemon=True).start() + return None + + def _run(self, todo): + s = self.settings + cal = self.calibration() + jobs = [(os.path.join(self.scans_dir, f), self.outdir, int(s["count"]), float(s["inset"]), s["align"], cal) + for f in todo] + + def absorb(res): + with self.lock: + for e in res["entries"]: + old = self.entries.get(e["id"]) + if old and old.get("status") == "reviewed": + e["chosen"], e["status"] = old["chosen"], "reviewed" + e["edge_adj"] = old.get("edge_adj", {}) + self.entries[e["id"]] = e + if e["id"] not in self.order: + self.order.append(e["id"]) + self.order.sort() + self._cache = {} + if res["error"]: + self.progress["errors"].append(f"{res['scan']}: {res['error'].splitlines()[0]}") + self.progress["done"] += 1 + if res["entries"]: + ids = [e["id"] for e in res["entries"]] + chosen = [self.entries[i] for i in ids if self.entries[i]["status"] == "reviewed"] + for e in chosen: # keep an earlier manual choice after re-processing + self.render_full(e["id"], e["chosen"], e.get("edge_adj")) + self.save() + + try: + if int(s["jobs"]) > 1 and len(jobs) > 1: + from multiprocessing import get_context + with get_context("spawn").Pool(int(s["jobs"])) as pool: + for res in pool.imap(process_scan, jobs): + absorb(res) + else: + for j in jobs: + self.progress["current"] = os.path.basename(j[0]) + absorb(process_scan(j)) + finally: + self.progress["running"] = False + self.progress["current"] = "" + self.save() + + # -- scanner calibration (scanner_calibration.json next to cdcrop.py) + CAL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "scanner_calibration.json") + + def calibration(self): + if self.settings.get("use_cal", True) and os.path.exists(self.CAL_PATH): + with open(self.CAL_PATH) as fh: + return json.load(fh) + return None + + # -- rendering + def load(self, cid): + """Metadata + 1/3-scale preview from the cache; full-resolution pixels + are read from your original scan when needed (zooms, saving).""" + if cid in self._cache: + return self._cache[cid] + cdir = os.path.join(self.outdir, "_cache") + with open(os.path.join(cdir, cid + "_cov.pkl"), "rb") as fh: + meta = pickle.load(fh) + small = cv2.cvtColor(cv2.imread(os.path.join(cdir, cid + "_small.jpg")), cv2.COLOR_BGR2RGB) + if len(self._cache) > 12: + self._cache.pop(next(iter(self._cache))) + self._cache[cid] = (_LazyRegion(meta), small, meta) + return self._cache[cid] + + def frame(self, cid, theta, edge_adj=None): + region, small, meta = self.load(cid) + if meta.get("type") == "disc": + return cdcrop.disc_frame(meta["center"], meta["R"], float(theta)), meta + return cdcrop.frame_at(meta["cov"], float(theta), meta["inset"], edge_adj), meta + + def scan_frame(self, cid, theta, edge_adj=None): + """Output frame in true geometry, expressed in the original scan's pixels.""" + fr, meta = self.frame(cid, theta, edge_adj) + A = meta.get("A") + return cdcrop.frame_to_scan(fr, None if A is None else np.array(A)), meta + + def preview(self, cid, theta, width, margin=0.0, edge_adj=None): + """margin: extra context shown around the crop, in crop px - clamped to + CTX_MARGIN so it never reaches past the cached region.""" + region, small, meta = self.load(cid) + fr, _ = self.scan_frame(cid, theta, edge_adj) + ox, oy = meta["offset"] + s = width / fr["W"] # output px per crop px + m = min(margin, CTX_MARGIN) + k = PREVIEW_SHRINK + ex, ey = fr["ex"], fr["ey"] + o_full = fr["origin"] - m * ex - m * ey + size = (int(round((fr["W"] + 2 * m) * s)), int(round((fr["H"] + 2 * m) * s))) + src = small if s * k <= 1.0 else np.asarray(region) + if src is small: + o = (o_full - [ox, oy]) / k + M = np.array([[ex[0] / (s * k), ey[0] / (s * k), o[0]], + [ex[1] / (s * k), ey[1] / (s * k), o[1]]]) + else: + o = (o_full - [ox, oy]) + M = np.array([[ex[0] / s, ey[0] / s, o[0]], [ex[1] / s, ey[1] / s, o[1]]]) + return cv2.warpAffine(src, M, size, flags=cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP, + borderMode=cv2.BORDER_REPLICATE) + + def patch(self, cid, theta, cx, cy, n, edge_adj=None): + """n x n patch of the crop at 1:1 (Lanczos), top-left at crop coords (cx, cy).""" + region, small, meta = self.load(cid) + fr, _ = self.scan_frame(cid, theta, edge_adj) + ox, oy = meta["offset"] + o = fr["origin"] - [ox, oy] + cx * fr["ex"] + cy * fr["ey"] + ex, ey = fr["ex"], fr["ey"] + M = np.array([[ex[0], ey[0], o[0]], [ex[1], ey[1], o[1]]]) + return cv2.warpAffine(np.asarray(region), M, (n, n), flags=cv2.INTER_LANCZOS4 | cv2.WARP_INVERSE_MAP, + borderMode=cv2.BORDER_REPLICATE) + + def info(self, cid, theta, edge_adj=None): + fr, meta = self.frame(cid, theta, edge_adj) + if meta.get("type") == "disc": + return {"size": [fr["W"], fr["H"]], "disc": True, "edge_dev": {}} + res = {"size": [fr["W"], fr["H"]], + "edge_dev": {k: (None if k in meta["cov"]["soft"] else round(float(v), 3)) + for k, v in fr["edge_dev_deg"].items()}} + key = meta.get("key") + if key: + P, Q = np.array(key["P"]), np.array(key["Q"]) + uv = lambda p: np.array([(p - fr["origin"]) @ fr["ex"], (p - fr["origin"]) @ fr["ey"]]) + p, q = uv(P), uv(Q) + if key["vert"]: + e0, e1 = (p, q) if p[1] < q[1] else (q, p); lean = e1[0] - e0[0] + else: + e0, e1 = (p, q) if p[0] < q[0] else (q, p); lean = e1[1] - e0[1] + res["key"] = {"vert": key["vert"], "L": round(key["L"]), "lean": round(float(lean), 2), + "e0": [float(e0[0]), float(e0[1])], "e1": [float(e1[0]), float(e1[1])]} + return res + + def render_full(self, cid, theta, edge_adj=None): + region, small, meta = self.load(cid) + fr, _ = self.scan_frame(cid, theta, edge_adj) + ox, oy = meta["offset"] + f2 = dict(fr) + f2["origin"] = fr["origin"] - [ox, oy] + out = cdcrop.render(np.asarray(region).astype(np.float32), f2) + cdcrop.save_png(out, meta["mode"], meta["dpi"], os.path.join(self.outdir, cid + ".png")) + return fr["W"], fr["H"] + + def accept(self, cid, theta, how, edge_adj=None): + edge_adj = edge_adj or {} + with self.lock: + e = self.entries[cid] + W, H = self.render_full(cid, theta, edge_adj) + with self.lock: + e["chosen"], e["status"], e["size"], e["how"] = float(theta), "reviewed", [W, H], how + e["edge_adj"] = edge_adj + log = os.path.join(self.outdir, "cdcrop_decisions.csv") + new = not os.path.exists(log) + with open(log, "a", newline="") as fh: + w = csv.writer(fh) + if new: + w.writerow(["time", "cover", "chosen_deg", "how", "auto_deg", "alt_deg", "flagged", + "left_deg", "right_deg", "top_deg", "bottom_deg", + "left_push_px", "right_push_px", "top_push_px", "bottom_push_px", "candidates"]) + ed = e["edges"] + w.writerow([time.strftime("%Y-%m-%d %H:%M:%S"), cid, f"{theta:.4f}", how, f"{e['auto']:.4f}", + "" if e["alt"] is None else f"{e['alt']:.4f}", int(e["flagged"]), + *[("" if k not in ed else "soft" if ed[k] is None else f"{ed[k]:.4f}") + for k in ("left", "right", "top", "bottom")], + *[f"{edge_adj.get(k, 0.0):.1f}" for k in ("left", "right", "top", "bottom")], + json.dumps({k: round(v, 4) for k, v in e["named"].items()})]) + self.save() + return W, H + + +_SCANS = {} # path -> uint8 array; the two most recent scans stay in memory +_SCANS_LOCK = threading.Lock() + + +def _scan_pixels(path): + with _SCANS_LOCK: + if path not in _SCANS: + if not os.path.exists(path): + raise FileNotFoundError(f"original scan not found: {path} (moved or renamed?)") + arr, _, _ = cdcrop.load_scan(path) + if len(_SCANS) >= 2: + _SCANS.pop(next(iter(_SCANS))) + _SCANS[path] = arr + return _SCANS[path] + + +class _LazyRegion: + """The cover's region of the original scan, loaded on first use.""" + def __init__(self, meta): + self.meta = meta + self._a = None + + def __array__(self, dtype=None, copy=None): + if self._a is None: + x0, y0 = self.meta["offset"]; x1, y1 = self.meta["end"] + self._a = _scan_pixels(self.meta["scan_path"])[y0:y1, x0:x1] + return self._a if dtype is None else self._a.astype(dtype) + + +S = Session() + +# --------------------------------------------------------------------- server + + +def jpeg(img_rgb, q=88): + ok, buf = cv2.imencode(".jpg", cv2.cvtColor(np.ascontiguousarray(img_rgb), cv2.COLOR_RGB2BGR), + [cv2.IMWRITE_JPEG_QUALITY, q]) + return buf.tobytes() + + +def png(img_rgb): + ok, buf = cv2.imencode(".png", cv2.cvtColor(np.ascontiguousarray(img_rgb), cv2.COLOR_RGB2BGR)) + return buf.tobytes() + + +def _edge_adj(q): + """Parse the optional ?edge={"top":12,...} query param into a plain dict.""" + try: + e = json.loads(q.get("edge") or "{}") + return {k: float(v) for k, v in e.items() if k in ("left", "right", "top", "bottom")} + except (ValueError, TypeError): + return {} + + +class Handler(BaseHTTPRequestHandler): + def log_message(self, *a): + pass + + def _send(self, code, body, ctype="application/json", cache=False): + if isinstance(body, (dict, list)): + body = json.dumps(body).encode() + elif isinstance(body, str): + body = body.encode() + self.send_response(code) + self.send_header("Content-Type", ctype) + self.send_header("Content-Length", str(len(body))) + self.send_header("Cache-Control", "max-age=3600" if cache else "no-store") + self.end_headers() + self.wfile.write(body) + + def do_GET(self): + u = urlparse(self.path) + q = {k: v[0] for k, v in parse_qs(u.query).items()} + try: + if u.path == "/": + return self._send(200, PAGE, "text/html; charset=utf-8") + if u.path == "/api/state": + with S.lock: + ents = [S.entries[i] for i in S.order if i in S.entries] + return self._send(200, {"outdir": S.outdir, "scans_dir": S.scans_dir, "settings": S.settings, + "progress": S.progress, "entries": ents, + "home": os.path.expanduser("~"), "calibration": S.calibration(), + "cal_file": os.path.exists(S.CAL_PATH)}) + if u.path == "/api/ls": + p = os.path.abspath(os.path.expanduser(q.get("path") or "~")) + if not os.path.isdir(p): + return self._send(200, {"path": p, "error": "not a folder", "dirs": [], "bmps": 0}) + dirs, bmps, files = [], 0, [] + try: + for name in sorted(os.listdir(p), key=str.lower): + full = os.path.join(p, name) + if name.startswith(".") or name.startswith("_"): + continue + if os.path.isdir(full): + dirs.append(name) + elif name.lower().endswith(".bmp"): + bmps += 1 + files.append(name) + except PermissionError: + pass + return self._send(200, {"path": p, "parent": os.path.dirname(p), "dirs": dirs, "bmps": bmps, + "files": files[:400]}) + if u.path == "/api/info": + return self._send(200, S.info(q["id"], float(q["theta"]), _edge_adj(q))) + if u.path == "/img/preview": + img = S.preview(q["id"], float(q["theta"]), int(q.get("w", 900)), float(q.get("margin", 0)), + _edge_adj(q)) + return self._send(200, jpeg(img, 90), "image/jpeg", cache=True) + if u.path == "/img/corner": + fr, _ = S.frame(q["id"], float(q["theta"]), _edge_adj(q)) + n = int(q.get("n", 160)) + c = q["c"] + cx = 0 if c[1] == "l" else fr["W"] - n + cy = 0 if c[0] == "t" else fr["H"] - n + return self._send(200, png(S.patch(q["id"], float(q["theta"]), cx, cy, n, _edge_adj(q))), + "image/png", cache=True) + if u.path == "/img/keyend": + inf = S.info(q["id"], float(q["theta"]), _edge_adj(q)) + if "key" not in inf: + return self._send(404, {"error": "no key line"}) + e = inf["key"]["e0" if q["end"] == "0" else "e1"] + n = 60 + return self._send(200, png(S.patch(q["id"], float(q["theta"]), int(round(e[0])) - n // 2, + int(round(e[1])) - n // 2, n, _edge_adj(q))), + "image/png", cache=True) + return self._send(404, {"error": "not found"}) + except Exception as ex: + return self._send(500, {"error": str(ex), "trace": traceback.format_exc()}) + + def do_POST(self): + u = urlparse(self.path) + n = int(self.headers.get("Content-Length") or 0) + body = json.loads(self.rfile.read(n) or b"{}") + try: + if u.path == "/api/process": + err = S.start(body["scans_dir"], body["outdir"], body.get("settings", {}), body.get("redo", False)) + return self._send(200 if not err else 409, {"error": err}) + if u.path == "/api/calibrate": + res = cdcrop.calibrate_pair(body["a"], body["b"], verbose=False) + out = cdcrop.add_calibration(S.CAL_PATH, res, reset=bool(body.get("reset"))) + out["this_pair"] = {k: res[k] for k in ("y_stretch_pct", "shear_deg")} + if "warning" in res: + out["warning"] = res["warning"] + return self._send(200, out) + if u.path == "/api/usecal": + S.settings["use_cal"] = bool(body.get("on", True)); S.save() + return self._send(200, {"ok": True}) + if u.path == "/api/open": + S.open(body["outdir"]) + return self._send(200, {"ok": True}) + if u.path == "/api/accept": + W, H = S.accept(body["id"], float(body["theta"]), body.get("how", "manual"), body.get("edge")) + return self._send(200, {"ok": True, "size": [W, H]}) + return self._send(404, {"error": "not found"}) + except Exception as ex: + return self._send(500, {"error": str(ex), "trace": traceback.format_exc()}) + + +PAGE = r""" + +CD Crop Review + +

CD Crop Review

+
+ +
+
+
+
+ +
+
+ + ~1.5 GB RAM each
+
+
+
+
Scanner calibration +
Corrects your scanner's own stretch and shear so squares stay square. Scan one flat, detailed cover, + turn it roughly a quarter turn on the glass, scan it again, then pick the two scans and press Measure. It is stored next to cdcrop.py and + applied to everything processed afterwards (use “Re-process all” for earlier scans).
+
+
+
+
+
+
How it works. Every cover is cropped at the automatic choice and written to the output folder right away. + Review only what you want - flagged covers first. Your choices re-render the full-resolution PNG and are logged to + cdcrop_decisions.csv so the automatic choice can be calibrated to your eye. You can close this window and come back later.
+
+ +
+
+
+
+
+ +
+
+
+

+
+
+
    +

    Candidates

    +

    Fine tune

    + + +
    + +
    + +
    +
    +

    Key art line

    +
    +

    Crop edges push a side out to recover trimmed art (a non-square insert may show a sliver of lid on the other side), or in to trim more

    +
    +
    Top0px
    +
    Bottom0px
    +
    Left0px
    +
    Right0px
    +
    +
    +

    Corners at 1:1 (TL TR / BL BR)

    +

    Edges vs. this angle

    +

    Keys

    1–9 candidates · A/B auto/alt · ←→ ±0.01° (Shift 0.05, Ctrl 0.1)
    + Space blink · G grid · C context · Enter accept · N/P next/prev · Esc grid
    + Discs: ←→ ±0.1° (Shift 1°, Ctrl 0.01°) · Q/E quarter turn · L level a line · T point to top
    +
    +
    + + +""" + + +def main(): + ap = argparse.ArgumentParser(description="Local review interface for cdcrop.") + ap.add_argument("--port", type=int, default=8765) + ap.add_argument("--out", help="open this output folder for review on start") + ap.add_argument("--no-browser", action="store_true") + a = ap.parse_args() + outdir = a.out + if not outdir and os.path.exists(LAST_RUN_PATH): # reopen wherever the last run left off + with open(LAST_RUN_PATH) as fh: + outdir = json.load(fh).get("outdir") + if outdir and os.path.isdir(outdir): + S.open(outdir) + srv = ThreadingHTTPServer(("127.0.0.1", a.port), Handler) + url = f"http://127.0.0.1:{a.port}/" + print(f"CD Crop Review running at {url} (only this computer can reach it; Ctrl+C to stop)") + if not a.no_browser: + threading.Timer(0.6, lambda: webbrowser.open(url)).start() + try: + srv.serve_forever() + except KeyboardInterrupt: + pass + + +if __name__ == "__main__": + main() diff --git a/scanner_calibration.json b/scanner_calibration.json new file mode 100644 index 0000000..13d9f91 --- /dev/null +++ b/scanner_calibration.json @@ -0,0 +1,35 @@ +{ + "sy": 1.0045673391446617, + "k": -0.0018456983223492352, + "shear_deg": -0.10526980224259717, + "y_stretch_pct": 0.4567339144661675, + "scans": [ + "average of 2 pair(s)" + ], + "pairs": 2, + "history": [ + { + "sy": 1.004341498347653, + "k": -0.0019650586352377205, + "shear_deg": -0.11210272881360409, + "y_stretch_pct": 0.4341498347653072, + "scans": [ + "test457.bmp", + "test458.bmp" + ] + }, + { + "sy": 1.0047931799416703, + "k": -0.0017263380094607497, + "shear_deg": -0.09843994426373696, + "y_stretch_pct": 0.4793179941670278, + "scans": [ + "test455.bmp", + "test456.bmp" + ], + "turn_deg": -89.39149175232839 + } + ], + "spread_shear_deg": 0.013662784549867121, + "spread_stretch_pct": 0.04516815940172059 +} \ No newline at end of file