Add installable offline CLI, lossless PDF quarter-turns, fast mode, edge-matched padding and reproducible OCR and DocLayNet evaluations
a4cdb00 verified Download evaluation/evaluate_modes.py from foliokestrel/folio-orient: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/foliokestrel/folio-orient/resolve/main/evaluation/evaluate_modes.py
- Command line
-
hf download hf://foliokestrel/folio-orient/evaluation/evaluate_modes.py
-
curl -L -o evaluate_modes.py https://huggingface.co/foliokestrel/folio-orient/resolve/main/evaluation/evaluate_modes.py
2.68 kB
| """Measure default and fast quarter-turn decisions on the identical decoded views.""" | |
| import argparse | |
| from pathlib import Path | |
| import hashlib | |
| import json | |
| import sys | |
| from time import perf_counter | |
| from PIL import Image | |
| HERE = Path(__file__).resolve().parent | |
| sys.path.insert(0, str(HERE.parent)) | |
| from folio_orient import PageOrienter, transpose_cw | |
| def run(args): | |
| model = PageOrienter(args.model) | |
| if args.cohort == "contemporary": | |
| rows = json.loads((args.images / "manifest.json").read_text()) | |
| samples = [ | |
| (r["page_hash"] + ".png", r["category"], "print") | |
| for r in rows | |
| if r["reason"] == "eligible" | |
| ] | |
| else: | |
| rows = json.loads((HERE / "pages.json").read_text()) | |
| samples = [ | |
| (r["file"], r["book"], r["page_type"]) for r in rows if r["split"] == "test" | |
| ] | |
| results = [] | |
| for filename, category, page_type in samples: | |
| with Image.open(args.images / filename) as image: | |
| page = image.convert("RGB") | |
| seed = int.from_bytes(hashlib.sha256(filename.encode()).digest()[:4], "big") | |
| for tilt in (0, (1.5 + seed % 550 / 100) * (-1 if seed % 2 else 1)): | |
| tested = ( | |
| page.rotate( | |
| tilt, Image.Resampling.BICUBIC, fillcolor=page.getpixel((0, 0)) | |
| ) | |
| if tilt | |
| else page | |
| ) | |
| for turn in (0, 90, 180, 270): | |
| view = transpose_cw(tested, (-turn) % 360) | |
| row = dict( | |
| file=filename, | |
| category=category, | |
| page_type=page_type, | |
| target_cw=turn, | |
| tilt_ccw=tilt, | |
| ) | |
| for mode in ("fast", "default"): | |
| start = perf_counter() | |
| pred = model.estimate(view, fast=(mode == "fast"), deskew=False) | |
| row[mode + "_ms"] = (perf_counter() - start) * 1000 | |
| row[mode + "_turn"] = pred["quarter_turn_cw"] | |
| row[mode + "_score"] = pred["agreement"] | |
| if mode == "fast": | |
| row["fast_path"] = pred["fast_path"] | |
| results.append(row) | |
| args.output.write_text(json.dumps(results, indent=1) + "\n") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("cohort", choices=["historical", "contemporary"]) | |
| parser.add_argument("images", type=Path) | |
| parser.add_argument("output", type=Path) | |
| parser.add_argument("--model", type=Path, default=HERE.parent / "model.onnx") | |
| run(parser.parse_args()) | |