"""Predict eval sets through the PaddleX serving stack (run in the worker-equivalent venv). Usage: serve_predict.py name:root:labels [...] ``engine`` is ``paddle_static`` (official Paddle export) or ``onnxruntime`` (a directory with ``inference.onnx`` + ``inference.yml`` from export_onnx.py). Images are fed one at a time as BGR arrays, exactly as the k2 worker's line path does, so every model is scored on the identical preprocessing and decoding code. """ from __future__ import annotations import sys from pathlib import Path import cv2 from paddlex import create_model MODEL_NAME = "devanagari_PP-OCRv5_mobile_rec" def main() -> int: model_dir, engine, out_dir = Path(sys.argv[1]), sys.argv[2], Path(sys.argv[3]) out_dir.mkdir(parents=True, exist_ok=True) model = create_model(model_name=MODEL_NAME, model_dir=str(model_dir), engine=engine, device="cpu") for spec in sys.argv[4:]: name, root, labels = spec.split(":") rows = [line.split("\t", 1)[0] for line in Path(labels).read_text(encoding="utf-8").splitlines() if "\t" in line] tmp = out_dir / f"{name}.tsv.tmp" with tmp.open("w", encoding="utf-8") as handle: for rel in rows: image = cv2.imread(str(Path(root) / rel), cv2.IMREAD_COLOR) if image is None: raise FileNotFoundError(f"cannot decode {Path(root) / rel}") res = list(model.predict([image]))[0].json["res"] text = str(res.get("rec_text") or "").replace("\t", " ") handle.write(f"{rel}\t{text}\t{res.get('rec_score')}\n") tmp.rename(out_dir / f"{name}.tsv") print(f"{name}: {len(rows)} predictions", flush=True) return 0 if __name__ == "__main__": sys.exit(main())