Add private local OCR comparison package
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""PP-OCRv5 Latin inference using preloaded local model directories only."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
|
||||
ROOT = Path(os.environ.get("OCR_MODEL_HOME", Path(__file__).resolve().parents[1] / ".cache")).expanduser().resolve()
|
||||
MODELS = ROOT / "official_models"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) != 3:
|
||||
raise SystemExit(2)
|
||||
image, output = map(Path, sys.argv[1:])
|
||||
detector = MODELS / "PP-OCRv5_mobile_det"
|
||||
recognizer = MODELS / "latin_PP-OCRv5_mobile_rec"
|
||||
if not (detector / "inference.pdiparams").is_file() or not (recognizer / "inference.pdiparams").is_file():
|
||||
raise SystemExit(3)
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
engine = PaddleOCR(
|
||||
text_detection_model_name="PP-OCRv5_mobile_det",
|
||||
text_detection_model_dir=str(detector),
|
||||
text_recognition_model_name="latin_PP-OCRv5_mobile_rec",
|
||||
text_recognition_model_dir=str(recognizer),
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=False,
|
||||
device="cpu",
|
||||
)
|
||||
results = list(engine.predict(str(image)))
|
||||
if len(results) != 1:
|
||||
raise RuntimeError("expected_one_page")
|
||||
raw = results[0].json["res"]
|
||||
lines = []
|
||||
for text, polygon in zip(raw["rec_texts"], raw["rec_polys"]):
|
||||
points = [[int(x), int(y)] for x, y in polygon]
|
||||
xs, ys = zip(*points)
|
||||
lines.append({"text": str(text), "bbox": [min(xs), min(ys), max(xs), max(ys)]})
|
||||
output.write_text(json.dumps({"lines": lines}, ensure_ascii=False))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user