prosperious's picture
Add 323 files (ocr-tech-mps-integration/tools/prepare_native_spotcheck_bundle.py ...)
0cfd653 verified
Raw History Blame Contribute Delete
3.76 kB
#!/usr/bin/env python3
"""Run the deployed deva-v2 recognizer and the official devanagari_PP-OCRv5_mobile_rec
over labelled line sets, with the same preprocessing as the k2 OCR worker.
Writes <out>/<model>/<set>.tsv rows: image_path, reference, prediction, score.
Scoring is done separately with tools/eval_metrics.py.
"""
from __future__ import annotations
import json
import sys
import time
from pathlib import Path
import numpy as np
from paddleocr._models.text_recognition import TextRecognition
from PIL import Image, ImageOps
HOME = Path.home()
MODELS = {
"deva-v2": ("PP-OCRv5_mobile_rec", HOME / "ampixa-demo/models/ocr/space/models/deva-v2/artifacts"),
"official": ("devanagari_PP-OCRv5_mobile_rec", HOME / ".paddlex/official_models/devanagari_PP-OCRv5_mobile_rec"),
}
BATCH_SIZE = 16
def load_set(root: Path, labels: Path) -> list[tuple[str, str]]:
rows: list[tuple[str, str]] = []
for line_no, line in enumerate(labels.read_text(encoding="utf-8").splitlines(), start=1):
if not line.strip():
continue
parts = line.split("\t", 1)
if len(parts) != 2:
raise ValueError(f"{labels}:{line_no}: expected '<path>\\t<text>'")
image_path = root / parts[0]
if not image_path.is_file():
raise FileNotFoundError(f"{labels}:{line_no}: missing image {image_path}")
rows.append((parts[0], parts[1]))
return rows
def to_bgr(path: Path) -> np.ndarray:
# Mirrors the worker: exif_transpose -> RGB -> BGR contiguous ndarray.
with Image.open(path) as probe:
image = ImageOps.exif_transpose(probe).convert("RGB")
return np.asarray(image)[:, :, ::-1].copy()
def main() -> int:
out = Path(sys.argv[1])
sets = {
name: (Path(root), Path(labels))
for name, root, labels in (arg.split(":", 2) for arg in sys.argv[2:])
}
timings: dict[str, dict[str, float]] = {}
for model, (model_name, model_dir) in MODELS.items():
for required in ("inference.json", "inference.pdiparams", "inference.yml"):
if not (model_dir / required).is_file():
raise FileNotFoundError(f"{model}: missing {model_dir / required}")
predictor = TextRecognition(model_name=model_name, model_dir=str(model_dir), device="cpu")
timings[model] = {}
for set_name, (root, labels) in sets.items():
rows = load_set(root, labels)
crops = [to_bgr(root / rel) for rel, _ in rows]
started = time.perf_counter()
results = list(predictor.predict(crops, batch_size=BATCH_SIZE))
elapsed = time.perf_counter() - started
if len(results) != len(rows):
raise RuntimeError(f"{model}/{set_name}: {len(results)} results for {len(rows)} crops")
(out / model).mkdir(parents=True, exist_ok=True)
with (out / model / f"{set_name}.tsv").open("w", encoding="utf-8") as handle:
for (rel, reference), result in zip(rows, results, strict=True):
payload = result.json.get("res", result.json) if hasattr(result, "json") else result
text = str(payload.get("rec_text") or "").strip()
score = payload.get("rec_score")
clean_ref = reference.replace("\t", " ")
handle.write(f"{rel}\t{clean_ref}\t{text.replace(chr(9), ' ')}\t{score}\n")
timings[model][set_name] = round(elapsed / len(rows) * 1000, 2)
print(f"{model} {set_name}: {len(rows)} lines, {timings[model][set_name]} ms/line", flush=True)
(out / "timings_ms_per_line.json").write_text(json.dumps(timings, indent=2), encoding="utf-8")
return 0
if __name__ == "__main__":
raise SystemExit(main())