prosperious's picture
Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified
Raw History Blame Contribute Delete
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())