"""Experiment A adapter #2: baseline-side materials from GT boxes. Subcommands ----------- crops Materialize IDENTICAL-CROP line images for the baseline recognizer. Geometry is bit-identical to the recognizer's crop rule (indra rec_preproc): expand by PAD_Y=0.2×h / PAD_X=0.3×h, white fill outside the page, floor/ceil int rounding — but saved at NATIVE resolution (and optionally the 32-px-height version), as PNGs, one per line: /__.png + manifest.jsonl. "Identical crops" fairness claim rests on these files (§A.2 step 2). baseline Run the deployed PP-OCRv5 Devanagari recognizer on those crops → eval-schema records for tools/indra_eval/eval_indra.py cer. *** PLUG: needs probe 4 (how ~/ocr/ocr-deva-ft exposes its rec) *** Given the probe facts, fill `_load_engine` with the deployed entry point (paddle.inference / tools/deva_rec_torch / paddleocr lib) — nothing else in this file hardcodes paddle behavior. """ from __future__ import annotations import argparse import sys from pathlib import Path import numpy as np sys.path.insert(0, str(Path(__file__).resolve().parent)) from common import append_jsonl, load_gray, read_jsonl, write_jsonl # noqa: E402 PAD_Y = 0.2 # = indra_ocr.rec_preproc.PAD_Y — keep the literal, import may absent PAD_X = 0.3 LINE_HEIGHT = 32 def crop_native(gray: np.ndarray, box, pad_y: float = PAD_Y, pad_x: float = PAD_X) -> np.ndarray: """Same expand geometry as rec_preproc.crop_line, BEFORE the 32-px resize.""" import math x0, y0, x1, y1 = (float(v) for v in box[:4]) h = max(1.0, y1 - y0) X0, X1 = x0 - pad_x * h, x1 + pad_x * h Y0, Y1 = y0 - pad_y * h, y1 + pad_y * h H, W = gray.shape ix0, iy0 = int(math.floor(X0)), int(math.floor(Y0)) ix1, iy1 = int(math.ceil(X1)), int(math.ceil(Y1)) cw, ch = max(1, ix1 - ix0), max(1, iy1 - iy0) crop = np.full((ch, cw), 255, np.uint8) sx0, sy0, sx1, sy1 = max(0, ix0), max(0, iy0), min(W, ix1), min(H, iy1) if sx1 > sx0 and sy1 > sy0: crop[sy0 - iy0 : sy1 - iy0, sx0 - ix0 : sx1 - ix0] = gray[sy0:sy1, sx0:sx1] return crop def _resolve_image(image: str, boxes_path: Path) -> Path: """Absolute, cwd-relative, or boxes-file-relative page path (same as run_rec).""" p = Path(image) if p.is_absolute(): return p for cand in (Path.cwd() / p, boxes_path.resolve().parent / p): if cand.exists(): return cand raise SystemExit(f"{boxes_path}: page not found: {image}") def cmd_crops(args) -> None: rows = [] for r in read_jsonl(args.boxes): image = r.get("image") or r.get("file") assert image, "boxes record without image/file field" gray = load_gray(_resolve_image(image, args.boxes)) stem = Path(image).stem for ln in r["lines"]: name = f"{stem}_line{int(ln.get('i', len(rows)))}.png" out = args.out_dir / name out.parent.mkdir(parents=True, exist_ok=True) crop = crop_native(gray, ln["box"]) import cv2 cv2.imwrite(str(out), crop) row = {"image": image, "i": int(ln.get("i", 0)), "text": ln["text"], "box": ln["box"], "crop": str(out)} if args.height32: from indra_ocr.rec_preproc import crop_line # the EXACT resize rule ink = crop_line(gray, ln["box"]) # [32,W] f32 0..1 img32 = (255 * (1.0 - ink)).round().astype(np.uint8) p32 = args.out_dir / f"{name}.h32.png" cv2.imwrite(str(p32), img32) row["crop32"] = str(p32) rows.append(row) write_jsonl(args.out_dir / "crops_manifest.jsonl", rows) print(f"crops: {len(rows)} lines -> {args.out_dir} (+ crops_manifest.jsonl)") # ------------------------------------------------------------- PLUG (probe 4) ------------------------------------------------- def _load_engine(args): """Return callable(list[PIL.Image|np.ndarray]) -> list[(text, conf)]. TODO(probe 4): fill with the deployed recognizer's ACTUAL entry point. Candidates once the probe answers, in order of preference: 1) tools/deva_rec_torch — if it is a script with a batch inference API, import it here and wrap it (weights already fine-tuned Devanagari). 2) paddle.inference predictor over the deployed rec model dir + PP-OCRv5 rec preprocessing (3×48×W, CTC/DB decode per their recipe). 3) paddleocr library (--paddle-model-dir), LAST resort — uses stock PP-OCRv5 preprocessing, may differ from the deployed pipeline's own adapter; results then describe the LIBRARY, not the deployment, and the run sheet says so. Keep preprocessing **at most** the deployed system's own (never invent). """ raise NotImplementedError( "baseline engine not wired yet — needs probe 4 (deployed rec invocation)." ) def cmd_baseline(args) -> None: engine = _load_engine(args) rows = read_jsonl(args.manifest) records_by_image: dict[str, dict] = {} import cv2 batch, batch_meta = [], [] def flush(): if not batch: return preds = engine(batch) # [(text, conf)] per crop for (text, conf), meta in zip(preds, batch_meta): img_key = meta["image"] rec = records_by_image.setdefault(img_key, { "image": img_key, "page": 1, "w": meta["w"], "h": meta["h"], "source": "rec-lines-baseline", "lines": [], "text": "", "mean_conf": 0.0, "timings": {}, }) rec["lines"].append({"box": meta["box"], "text": text, "conf": float(conf)}) batch.clear() batch_meta.clear() for row in rows: crop_path = row.get("crop") img = cv2.imread(crop_path, cv2.IMREAD_GRAYSCALE) batch.append(img) batch_meta.append(row) if len(batch) >= args.batch: flush() flush() out_records = [] for rec in records_by_image.values(): rec["lines"].sort(key=lambda l: (l["box"][1], l["box"][0])) rec["text"] = "\n".join(l["text"] for l in rec["lines"]) confs = [l["conf"] for l in rec["lines"]] rec["mean_conf"] = float(np.mean(confs)) if confs else 0.0 out_records.append(rec) write_jsonl(args.out, out_records) print(f"baseline records -> {args.out} ({len(out_records)} pages)") def main(argv=None): ap = argparse.ArgumentParser("a_ppocr_baseline") sub = ap.add_subparsers(dest="cmd", required=True) c = sub.add_parser("crops", help="materialize identical crops from boxes jsonl") c.add_argument("--boxes", type=Path, required=True) c.add_argument("--out-dir", type=Path, required=True) c.add_argument("--height32", action="store_true", help="also save 32-px ink-resized PNGs") c.set_defaults(fn=cmd_crops) b = sub.add_parser("baseline", help="run deployed rec on crops -> eval records") b.add_argument("--manifest", type=Path, required=True, help="crops_manifest.jsonl") b.add_argument("--out", type=Path, required=True) b.add_argument("--batch", type=int, default=16) b.add_argument("--paddle-model-dir", type=Path, default=None) b.set_defaults(fn=cmd_baseline) args = ap.parse_args(argv) args.fn(args) if __name__ == "__main__": main()