-
Notifications
You must be signed in to change notification settings - Fork 159
fix(galgame): 让 RapidOCR 模型配置实际生效 #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wehos
merged 4 commits into
Project-N-E-K-O:main
from
MomiJiSan:fix/galgame-rapidocr-config-not-applied
May 6, 2026
+245
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e14d2a9
修复 Galgame 插件中 RapidOCR 的模型配置未生效问题。
MomiJiSan 5fd9684
fix(galgame): honor rapidocr_model_type when resolving ONNX filenames
432318d
fix(galgame): correct rapidocr server filename infix + harden empty-m…
81c8062
Merge remote-tracking branch 'origin/main' into fix/galgame-rapidocr-…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
plugin/tests/unit/plugins/test_galgame_rapidocr_support.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from contextlib import nullcontext | ||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from plugin.plugins.galgame_plugin import rapidocr_support | ||
|
|
||
|
|
||
| pytestmark = pytest.mark.plugin_unit | ||
|
|
||
|
|
||
| class _RapidOcrWithKwargs: | ||
| captured_kwargs: dict[str, object] | None = None | ||
|
|
||
| def __init__(self, config_path=None, **kwargs) -> None: | ||
| del config_path | ||
| type(self).captured_kwargs = dict(kwargs) | ||
|
|
||
|
|
||
| def _touch(path: Path) -> Path: | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| path.write_text("", encoding="utf-8") | ||
| return path | ||
|
|
||
|
|
||
| def test_rapidocr_kwargs_resolve_configured_model_paths(tmp_path: Path) -> None: | ||
| model_cache_dir = tmp_path / "RapidOCR" / "models" | ||
| package_models_dir = tmp_path / "package" / "models" | ||
| det_path = _touch(package_models_dir / "ch_PP-OCRv4_det_infer.onnx") | ||
| cls_path = _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") | ||
| rec_path = _touch(package_models_dir / "ch_PP-OCRv4_rec_infer.onnx") | ||
|
|
||
| kwargs = rapidocr_support._build_runtime_constructor_kwargs( | ||
| _RapidOcrWithKwargs, | ||
| engine_type="onnxruntime", | ||
| lang_type="ch", | ||
| model_type="mobile", | ||
| ocr_version="PP-OCRv4", | ||
| model_cache_dir=model_cache_dir, | ||
| package_models_dir=package_models_dir, | ||
| ) | ||
|
|
||
| assert kwargs == { | ||
| "det_model_path": str(det_path), | ||
| "cls_model_path": str(cls_path), | ||
| "rec_model_path": str(rec_path), | ||
| "engine_type": "onnxruntime", | ||
| } | ||
|
|
||
|
|
||
| def test_rapidocr_kwargs_prefers_user_model_cache(tmp_path: Path) -> None: | ||
| model_cache_dir = tmp_path / "RapidOCR" / "models" | ||
| package_models_dir = tmp_path / "package" / "models" | ||
| user_det_path = _touch(model_cache_dir / "japan_PP-OCRv4_det_infer.onnx") | ||
| user_rec_path = _touch(model_cache_dir / "japan_PP-OCRv4_rec_infer.onnx") | ||
| package_cls_path = _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") | ||
| _touch(package_models_dir / "japan_PP-OCRv4_det_infer.onnx") | ||
| _touch(package_models_dir / "japan_PP-OCRv4_rec_infer.onnx") | ||
|
|
||
| kwargs = rapidocr_support._build_runtime_constructor_kwargs( | ||
| _RapidOcrWithKwargs, | ||
| engine_type="onnxruntime", | ||
| lang_type="japan", | ||
| model_type="mobile", | ||
| ocr_version="PP-OCRv4", | ||
| model_cache_dir=model_cache_dir, | ||
| package_models_dir=package_models_dir, | ||
| ) | ||
|
|
||
| assert kwargs["det_model_path"] == str(user_det_path) | ||
| assert kwargs["rec_model_path"] == str(user_rec_path) | ||
| assert kwargs["cls_model_path"] == str(package_cls_path) | ||
|
|
||
|
|
||
| def test_rapidocr_kwargs_resolves_server_variant_filenames(tmp_path: Path) -> None: | ||
| model_cache_dir = tmp_path / "RapidOCR" / "models" | ||
| package_models_dir = tmp_path / "package" / "models" | ||
| server_det_path = _touch(model_cache_dir / "ch_PP-OCRv4_det_server_infer.onnx") | ||
| server_rec_path = _touch(model_cache_dir / "ch_PP-OCRv4_rec_server_infer.onnx") | ||
| cls_path = _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") | ||
| # Mobile variants exist alongside server ones to ensure model_type drives selection. | ||
| _touch(package_models_dir / "ch_PP-OCRv4_det_infer.onnx") | ||
| _touch(package_models_dir / "ch_PP-OCRv4_rec_infer.onnx") | ||
|
|
||
| kwargs = rapidocr_support._build_runtime_constructor_kwargs( | ||
| _RapidOcrWithKwargs, | ||
| engine_type="onnxruntime", | ||
| lang_type="ch", | ||
| model_type="server", | ||
| ocr_version="PP-OCRv4", | ||
| model_cache_dir=model_cache_dir, | ||
| package_models_dir=package_models_dir, | ||
| ) | ||
|
|
||
| assert kwargs == { | ||
| "det_model_path": str(server_det_path), | ||
| "rec_model_path": str(server_rec_path), | ||
| "cls_model_path": str(cls_path), | ||
| "engine_type": "onnxruntime", | ||
| } | ||
|
|
||
|
|
||
| def test_rapidocr_kwargs_omits_model_paths_when_configured_model_is_missing(tmp_path: Path) -> None: | ||
| model_cache_dir = tmp_path / "RapidOCR" / "models" | ||
| package_models_dir = tmp_path / "package" / "models" | ||
| _touch(package_models_dir / "ch_PP-OCRv4_det_infer.onnx") | ||
| _touch(package_models_dir / "ch_ppocr_mobile_v2.0_cls_infer.onnx") | ||
| _touch(package_models_dir / "ch_PP-OCRv4_rec_infer.onnx") | ||
|
|
||
| kwargs = rapidocr_support._build_runtime_constructor_kwargs( | ||
| _RapidOcrWithKwargs, | ||
| engine_type="onnxruntime", | ||
| lang_type="ch", | ||
| model_type="mobile", | ||
| ocr_version="PP-OCRv5", | ||
| model_cache_dir=model_cache_dir, | ||
| package_models_dir=package_models_dir, | ||
| ) | ||
|
|
||
| assert kwargs == {"engine_type": "onnxruntime"} | ||
|
|
||
|
|
||
| def test_load_rapidocr_runtime_uses_imported_package_models_dir( | ||
| tmp_path: Path, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| install_target = tmp_path / "RapidOCR" | ||
| bundled_package_dir = tmp_path / "bundled" / "rapidocr_onnxruntime" | ||
| _touch(bundled_package_dir / "__init__.py") | ||
| det_path = _touch(bundled_package_dir / "models" / "ch_PP-OCRv4_det_infer.onnx") | ||
| cls_path = _touch(bundled_package_dir / "models" / "ch_ppocr_mobile_v2.0_cls_infer.onnx") | ||
| rec_path = _touch(bundled_package_dir / "models" / "ch_PP-OCRv4_rec_infer.onnx") | ||
| _RapidOcrWithKwargs.captured_kwargs = None | ||
|
|
||
| monkeypatch.setattr( | ||
| rapidocr_support.importlib, | ||
| "import_module", | ||
| lambda name: SimpleNamespace( | ||
| RapidOCR=_RapidOcrWithKwargs, | ||
| __file__=str(bundled_package_dir / "__init__.py"), | ||
| ), | ||
| ) | ||
| monkeypatch.setattr(rapidocr_support, "_onnxruntime_intra_op_thread_cap", lambda _limit: nullcontext()) | ||
|
|
||
| runtime, metadata = rapidocr_support.load_rapidocr_runtime( | ||
| install_target_dir_raw=str(install_target), | ||
| engine_type="onnxruntime", | ||
| lang_type="ch", | ||
| model_type="mobile", | ||
| ocr_version="PP-OCRv4", | ||
| ) | ||
|
|
||
| assert isinstance(runtime, _RapidOcrWithKwargs) | ||
| assert _RapidOcrWithKwargs.captured_kwargs == { | ||
| "det_model_path": str(det_path), | ||
| "cls_model_path": str(cls_path), | ||
| "rec_model_path": str(rec_path), | ||
| "engine_type": "onnxruntime", | ||
| } | ||
| assert metadata["detected_path"] == str(bundled_package_dir.resolve()) | ||
| assert metadata["selected_model"] == "PP-OCRv4/ch/mobile" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.