π Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
fix(rec): honour model_root_dir for the rec dict and str paths in torch engine (#752)
Rett Wilson332b4fcc235b
diff --git a/python/rapidocr/ch_ppocr_rec/main.py b/python/rapidocr/ch_ppocr_rec/main.pyindex b22f81a..11ee322 100644--- a/python/rapidocr/ch_ppocr_rec/main.py+++ b/python/rapidocr/ch_ppocr_rec/main.py@@ -76,7 +76,11 @@ class TextRecognizer:dict_download_url = (dict_download_url if dict_download_url is not None else DEFAULT_DICT_URL)- dict_path = DEFAULT_MODEL_PATH / Path(dict_download_url).name+ model_root_dir = cfg.get("model_root_dir", None)+ dict_dir = (+ DEFAULT_MODEL_PATH if model_root_dir is None else Path(model_root_dir)+ )+ dict_path = dict_dir / Path(dict_download_url).nameif not Path(dict_path).exists():DownloadFile.run(DownloadFileInput(diff --git a/python/rapidocr/inference_engine/pytorch/networks/main.py b/python/rapidocr/inference_engine/pytorch/networks/main.pyindex 86b8295..6820fa4 100644--- a/python/rapidocr/inference_engine/pytorch/networks/main.py+++ b/python/rapidocr/inference_engine/pytorch/networks/main.py@@ -37,7 +37,7 @@ class ModelLoader:))default_model_url = model_info["model_dir"]- model_path = cfg.model_root_dir / Path(default_model_url).name+ model_path = Path(cfg.model_root_dir) / Path(default_model_url).nameDownloadFile.run(DownloadFileInput(file_url=default_model_url,diff --git a/python/tests/test_model_root_dir.py b/python/tests/test_model_root_dir.pynew file mode 100644index 0000000..6753002--- /dev/null+++ b/python/tests/test_model_root_dir.py@@ -0,0 +1,105 @@+# -*- encoding: utf-8 -*-+from pathlib import Path++import pytest+from omegaconf import OmegaConf++from rapidocr.ch_ppocr_rec import TextRecognizer+from rapidocr.ch_ppocr_rec import main as rec_main+from rapidocr.utils.typings import EngineType, ModelType, OCRVersion, TaskType++DICT_URL = "https://example.com/models/ppocrv5_dict.txt"+++class StubSession:+ """Session without an embedded character list, like the torch/paddle engines."""++ def have_key(self, key: str = "character") -> bool:+ return False++ @classmethod+ def get_dict_key_url(cls, file_info) -> str:+ return DICT_URL+++def make_rec_cfg(model_root_dir):+ return OmegaConf.create(+ {+ "engine_type": EngineType.TORCH,+ "ocr_version": OCRVersion.PPOCRV5,+ "task_type": TaskType.REC,+ "lang_type": "ch",+ "model_type": ModelType.MOBILE,+ "rec_keys_path": None,+ "model_root_dir": model_root_dir,+ }+ )+++def downloads(monkeypatch):+ saved = []+ monkeypatch.setattr(+ rec_main.DownloadFile, "run", lambda params: saved.append(params.save_path)+ )+ return saved+++def get_character_dict(cfg):+ recognizer = TextRecognizer.__new__(TextRecognizer)+ recognizer.session = StubSession()+ return recognizer.get_character_dict(cfg)++[email protected]("as_type", [Path, str])+def test_rec_dict_is_downloaded_into_model_root_dir(tmp_path, downloads, as_type):+ _, dict_path = get_character_dict(make_rec_cfg(as_type(tmp_path)))++ expected = tmp_path / "ppocrv5_dict.txt"+ assert Path(dict_path) == expected+ assert downloads == [expected]+++def test_rec_dict_falls_back_to_package_models_dir(downloads):+ _, dict_path = get_character_dict(make_rec_cfg(None))++ assert Path(dict_path) == rec_main.DEFAULT_MODEL_PATH / "ppocrv5_dict.txt"+++def test_torch_model_path_accepts_str_model_root_dir(tmp_path, monkeypatch):+ pytest.importorskip("torch")+ from rapidocr.inference_engine.pytorch.networks import main as torch_networks++ saved = []++ def fake_download(params):+ saved.append(params.save_path)+ Path(params.save_path).touch()++ monkeypatch.setattr(torch_networks.DownloadFile, "run", fake_download)+ monkeypatch.setattr(+ torch_networks.InferSession,+ "get_model_url",+ classmethod(+ lambda cls, file_info: {+ "model_dir": "https://example.com/models/det.pth",+ "SHA256": None,+ }+ ),+ )+ cfg = OmegaConf.create(+ {+ "model_path": None,+ "model_root_dir": str(tmp_path),+ "engine_type": EngineType.TORCH,+ "ocr_version": OCRVersion.PPOCRV5,+ "task_type": TaskType.DET,+ "lang_type": "ch",+ "model_type": ModelType.MOBILE,+ }+ )++ model_path = torch_networks.ModelLoader._init_model_path(None, cfg)++ assert Path(model_path) == tmp_path / "det.pth"+ assert saved == [tmp_path / "det.pth"]