Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified Download ocr-tech/tools/deva_rec_torch/serve_predict.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tools/deva_rec_torch/serve_predict.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech/tools/deva_rec_torch/serve_predict.py
-
curl -L -o serve_predict.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tools/deva_rec_torch/serve_predict.py
1.81 kB
| """Predict eval sets through the PaddleX serving stack (run in the worker-equivalent venv). | |
| Usage: serve_predict.py <model_dir> <engine> <out_dir> 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()) | |