File size: 1,353 Bytes
d82db76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | """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())
|