|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from contextlib import nullcontext |
| 4 | +from pathlib import Path |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from plugin.plugins.galgame_plugin import rapidocr_support |
| 10 | + |
| 11 | + |
| 12 | +pytestmark = pytest.mark.plugin_unit |
| 13 | + |
| 14 | + |
| 15 | +class _RapidOcrWithKwargs: |
| 16 | + captured_kwargs: dict[str, object] | None = None |
| 17 | + |
| 18 | + def __init__(self, config_path=None, **kwargs) -> None: |
| 19 | + del config_path |
| 20 | + type(self).captured_kwargs = dict(kwargs) |
| 21 | + |
| 22 | + |
| 23 | +def _touch(path: Path) -> Path: |
| 24 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 25 | + path.write_text("", encoding="utf-8") |
| 26 | + return path |
| 27 | + |
| 28 | + |
| 29 | +def test_rapidocr_kwargs_resolve_configured_model_paths(tmp_path: Path) -> None: |
| 30 | + model_cache_dir = tmp_path / "RapidOCR" / "models" |
| 31 | + package_models_dir = tmp_path / "package" / "models" |
| 32 | + det_path = _touch(package_models_dir / "ch_PP-OCRv4_det_infer.onnx") |
| 33 | + cls_path = _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") |
| 34 | + rec_path = _touch(package_models_dir / "ch_PP-OCRv4_rec_infer.onnx") |
| 35 | + |
| 36 | + kwargs = rapidocr_support._build_runtime_constructor_kwargs( |
| 37 | + _RapidOcrWithKwargs, |
| 38 | + engine_type="onnxruntime", |
| 39 | + lang_type="ch", |
| 40 | + model_type="mobile", |
| 41 | + ocr_version="PP-OCRv4", |
| 42 | + model_cache_dir=model_cache_dir, |
| 43 | + package_models_dir=package_models_dir, |
| 44 | + ) |
| 45 | + |
| 46 | + assert kwargs == { |
| 47 | + "det_model_path": str(det_path), |
| 48 | + "cls_model_path": str(cls_path), |
| 49 | + "rec_model_path": str(rec_path), |
| 50 | + "engine_type": "onnxruntime", |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +def test_rapidocr_kwargs_prefers_user_model_cache(tmp_path: Path) -> None: |
| 55 | + model_cache_dir = tmp_path / "RapidOCR" / "models" |
| 56 | + package_models_dir = tmp_path / "package" / "models" |
| 57 | + user_det_path = _touch(model_cache_dir / "japan_PP-OCRv4_det_infer.onnx") |
| 58 | + user_rec_path = _touch(model_cache_dir / "japan_PP-OCRv4_rec_infer.onnx") |
| 59 | + package_cls_path = _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") |
| 60 | + _touch(package_models_dir / "japan_PP-OCRv4_det_infer.onnx") |
| 61 | + _touch(package_models_dir / "japan_PP-OCRv4_rec_infer.onnx") |
| 62 | + |
| 63 | + kwargs = rapidocr_support._build_runtime_constructor_kwargs( |
| 64 | + _RapidOcrWithKwargs, |
| 65 | + engine_type="onnxruntime", |
| 66 | + lang_type="japan", |
| 67 | + model_type="mobile", |
| 68 | + ocr_version="PP-OCRv4", |
| 69 | + model_cache_dir=model_cache_dir, |
| 70 | + package_models_dir=package_models_dir, |
| 71 | + ) |
| 72 | + |
| 73 | + assert kwargs["det_model_path"] == str(user_det_path) |
| 74 | + assert kwargs["rec_model_path"] == str(user_rec_path) |
| 75 | + assert kwargs["cls_model_path"] == str(package_cls_path) |
| 76 | + |
| 77 | + |
| 78 | +def test_rapidocr_kwargs_resolves_server_variant_filenames(tmp_path: Path) -> None: |
| 79 | + model_cache_dir = tmp_path / "RapidOCR" / "models" |
| 80 | + package_models_dir = tmp_path / "package" / "models" |
| 81 | + server_det_path = _touch(model_cache_dir / "ch_PP-OCRv4_det_server_infer.onnx") |
| 82 | + server_rec_path = _touch(model_cache_dir / "ch_PP-OCRv4_rec_server_infer.onnx") |
| 83 | + cls_path = _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") |
| 84 | + # Mobile variants exist alongside server ones to ensure model_type drives selection. |
| 85 | + _touch(package_models_dir / "ch_PP-OCRv4_det_infer.onnx") |
| 86 | + _touch(package_models_dir / "ch_PP-OCRv4_rec_infer.onnx") |
| 87 | + |
| 88 | + kwargs = rapidocr_support._build_runtime_constructor_kwargs( |
| 89 | + _RapidOcrWithKwargs, |
| 90 | + engine_type="onnxruntime", |
| 91 | + lang_type="ch", |
| 92 | + model_type="server", |
| 93 | + ocr_version="PP-OCRv4", |
| 94 | + model_cache_dir=model_cache_dir, |
| 95 | + package_models_dir=package_models_dir, |
| 96 | + ) |
| 97 | + |
| 98 | + assert kwargs == { |
| 99 | + "det_model_path": str(server_det_path), |
| 100 | + "rec_model_path": str(server_rec_path), |
| 101 | + "cls_model_path": str(cls_path), |
| 102 | + "engine_type": "onnxruntime", |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +def test_rapidocr_kwargs_omits_model_paths_when_configured_model_is_missing(tmp_path: Path) -> None: |
| 107 | + model_cache_dir = tmp_path / "RapidOCR" / "models" |
| 108 | + package_models_dir = tmp_path / "package" / "models" |
| 109 | + _touch(package_models_dir / "ch_PP-OCRv4_det_infer.onnx") |
| 110 | + _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") |
| 111 | + _touch(package_models_dir / "ch_PP-OCRv4_rec_infer.onnx") |
| 112 | + |
| 113 | + kwargs = rapidocr_support._build_runtime_constructor_kwargs( |
| 114 | + _RapidOcrWithKwargs, |
| 115 | + engine_type="onnxruntime", |
| 116 | + lang_type="ch", |
| 117 | + model_type="mobile", |
| 118 | + ocr_version="PP-OCRv5", |
| 119 | + model_cache_dir=model_cache_dir, |
| 120 | + package_models_dir=package_models_dir, |
| 121 | + ) |
| 122 | + |
| 123 | + assert kwargs == {"engine_type": "onnxruntime"} |
| 124 | + |
| 125 | + |
| 126 | +def test_load_rapidocr_runtime_uses_imported_package_models_dir( |
| 127 | + tmp_path: Path, |
| 128 | + monkeypatch: pytest.MonkeyPatch, |
| 129 | +) -> None: |
| 130 | + install_target = tmp_path / "RapidOCR" |
| 131 | + bundled_package_dir = tmp_path / "bundled" / "rapidocr_onnxruntime" |
| 132 | + _touch(bundled_package_dir / "__init__.py") |
| 133 | + det_path = _touch(bundled_package_dir / "models" / "ch_PP-OCRv4_det_infer.onnx") |
| 134 | + cls_path = _touch(bundled_package_dir / "models" / "ch_ppocr_mobile_v2.0_cls_infer.onnx") |
| 135 | + rec_path = _touch(bundled_package_dir / "models" / "ch_PP-OCRv4_rec_infer.onnx") |
| 136 | + _RapidOcrWithKwargs.captured_kwargs = None |
| 137 | + |
| 138 | + monkeypatch.setattr( |
| 139 | + rapidocr_support.importlib, |
| 140 | + "import_module", |
| 141 | + lambda name: SimpleNamespace( |
| 142 | + RapidOCR=_RapidOcrWithKwargs, |
| 143 | + __file__=str(bundled_package_dir / "__init__.py"), |
| 144 | + ), |
| 145 | + ) |
| 146 | + monkeypatch.setattr(rapidocr_support, "_onnxruntime_intra_op_thread_cap", lambda _limit: nullcontext()) |
| 147 | + |
| 148 | + runtime, metadata = rapidocr_support.load_rapidocr_runtime( |
| 149 | + install_target_dir_raw=str(install_target), |
| 150 | + engine_type="onnxruntime", |
| 151 | + lang_type="ch", |
| 152 | + model_type="mobile", |
| 153 | + ocr_version="PP-OCRv4", |
| 154 | + ) |
| 155 | + |
| 156 | + assert isinstance(runtime, _RapidOcrWithKwargs) |
| 157 | + assert _RapidOcrWithKwargs.captured_kwargs == { |
| 158 | + "det_model_path": str(det_path), |
| 159 | + "cls_model_path": str(cls_path), |
| 160 | + "rec_model_path": str(rec_path), |
| 161 | + "engine_type": "onnxruntime", |
| 162 | + } |
| 163 | + assert metadata["detected_path"] == str(bundled_package_dir.resolve()) |
| 164 | + assert metadata["selected_model"] == "PP-OCRv4/ch/mobile" |
0 commit comments