Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified Download ocr-tech/tools/deva_rec_torch/paddle_predict.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tools/deva_rec_torch/paddle_predict.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech/tools/deva_rec_torch/paddle_predict.py
-
curl -L -o paddle_predict.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tools/deva_rec_torch/paddle_predict.py
1.35 kB
| """Run a PaddleX text recognizer over a label file's crops (run in the Paddle worker venv). | |
| Usage: paddle_predict.py <model_name> <model_dir> <crop_root> <labels> <out.tsv> | |
| Writes ``relpath<TAB>prediction<TAB>score`` with the same BGR input the k2 worker uses. | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| from pathlib import Path | |
| import cv2 | |
| from paddleocr._models.text_recognition import TextRecognition | |
| def main() -> int: | |
| model_name, model_dir, crop_root, labels, out = sys.argv[1], Path(sys.argv[2]), Path(sys.argv[3]), Path(sys.argv[4]), Path(sys.argv[5]) | |
| rec = TextRecognition(model_name=model_name, model_dir=str(model_dir), device="cpu") | |
| rows = [line.split("\t", 1) for line in labels.read_text(encoding="utf-8").splitlines() if "\t" in line] | |
| with out.open("w", encoding="utf-8") as handle: | |
| for rel, _ in rows: | |
| image = cv2.imread(str(crop_root / rel), cv2.IMREAD_COLOR) | |
| if image is None: | |
| raise FileNotFoundError(f"cannot decode {crop_root / rel}") | |
| result = list(rec.predict([image]))[0].json["res"] | |
| text = str(result.get("rec_text") or "").replace("\t", " ") | |
| handle.write(f"{rel}\t{text}\t{result.get('rec_score')}\n") | |
| print(f"wrote {len(rows)} predictions to {out}") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |