|
1 | 1 | import logging |
2 | 2 | from collections.abc import Iterable |
3 | 3 | from pathlib import Path |
4 | | -from typing import Literal, Optional, Type, TypedDict |
| 4 | +from typing import Literal, Type, TypedDict |
5 | 5 |
|
6 | 6 | import numpy |
7 | 7 | from docling_core.types.doc import BoundingBox, CoordOrigin |
@@ -33,64 +33,57 @@ class _ModelPathDetail(TypedDict): |
33 | 33 | path: str |
34 | 34 |
|
35 | 35 |
|
| 36 | +_RAPIDOCR_MODELSCOPE_RELEASE = "v3.8.0" |
| 37 | +_RAPIDOCR_MODELSCOPE_BASE_URL = ( |
| 38 | + "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve" |
| 39 | +) |
| 40 | +_RAPIDOCR_DEFAULT_MODEL_PATHS: dict[_ModelPathEngines, dict[_ModelPathTypes, str]] = { |
| 41 | + "onnxruntime": { |
| 42 | + "det_model_path": "onnx/PP-OCRv4/det/ch_PP-OCRv4_det_mobile.onnx", |
| 43 | + "cls_model_path": "onnx/PP-OCRv4/cls/ch_ppocr_mobile_v2.0_cls_mobile.onnx", |
| 44 | + "rec_model_path": "onnx/PP-OCRv4/rec/ch_PP-OCRv4_rec_mobile.onnx", |
| 45 | + "rec_keys_path": "paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_mobile/ppocr_keys_v1.txt", |
| 46 | + "font_path": "resources/fonts/FZYTK.TTF", |
| 47 | + }, |
| 48 | + "torch": { |
| 49 | + "det_model_path": "torch/PP-OCRv4/det/ch_PP-OCRv4_det_mobile.pth", |
| 50 | + "cls_model_path": "torch/PP-OCRv4/cls/ch_ptocr_mobile_v2.0_cls_mobile.pth", |
| 51 | + "rec_model_path": "torch/PP-OCRv4/rec/ch_PP-OCRv4_rec_mobile.pth", |
| 52 | + "rec_keys_path": "paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_mobile/ppocr_keys_v1.txt", |
| 53 | + "font_path": "resources/fonts/FZYTK.TTF", |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +def _build_model_detail(path: str) -> _ModelPathDetail: |
| 59 | + return { |
| 60 | + "url": f"{_RAPIDOCR_MODELSCOPE_BASE_URL}/{_RAPIDOCR_MODELSCOPE_RELEASE}/{path}", |
| 61 | + "path": path, |
| 62 | + } |
| 63 | + |
| 64 | + |
36 | 65 | class RapidOcrModel(BaseOcrModel): |
37 | 66 | _model_repo_folder = "RapidOcr" |
38 | | - # from https://github.com/RapidAI/RapidOCR/blob/main/python/rapidocr/default_models.yaml |
39 | | - # matching the default config in https://github.com/RapidAI/RapidOCR/blob/main/python/rapidocr/config.yaml |
40 | | - # and naming f"{file_info.engine_type.value}.{file_info.ocr_version.value}.{file_info.task_type.value}" |
| 67 | + # Match the PP-OCRv4 mobile defaults used by RapidOCR 3.8+: |
| 68 | + # - default_models.yaml in RapidOCR 3.8.1 points at the v3.8.0 modelscope assets |
| 69 | + # - config.yaml defaults Det/Cls/Rec model_type to "mobile" |
41 | 70 | _default_models: dict[ |
42 | 71 | _ModelPathEngines, dict[_ModelPathTypes, _ModelPathDetail] |
43 | 72 | ] = { |
44 | 73 | "onnxruntime": { |
45 | | - "det_model_path": { |
46 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/onnx/PP-OCRv4/det/ch_PP-OCRv4_det_infer.onnx", |
47 | | - "path": "onnx/PP-OCRv4/det/ch_PP-OCRv4_det_infer.onnx", |
48 | | - }, |
49 | | - "cls_model_path": { |
50 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/onnx/PP-OCRv4/cls/ch_ppocr_mobile_v2.0_cls_infer.onnx", |
51 | | - "path": "onnx/PP-OCRv4/cls/ch_ppocr_mobile_v2.0_cls_infer.onnx", |
52 | | - }, |
53 | | - "rec_model_path": { |
54 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/onnx/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer.onnx", |
55 | | - "path": "onnx/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer.onnx", |
56 | | - }, |
57 | | - "rec_keys_path": { |
58 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer/ppocr_keys_v1.txt", |
59 | | - "path": "paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer/ppocr_keys_v1.txt", |
60 | | - }, |
61 | | - "font_path": { |
62 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/resources/fonts/FZYTK.TTF", |
63 | | - "path": "fonts/FZYTK.TTF", |
64 | | - }, |
| 74 | + key: _build_model_detail(path) |
| 75 | + for key, path in _RAPIDOCR_DEFAULT_MODEL_PATHS["onnxruntime"].items() |
65 | 76 | }, |
66 | 77 | "torch": { |
67 | | - "det_model_path": { |
68 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/torch/PP-OCRv4/det/ch_PP-OCRv4_det_infer.pth", |
69 | | - "path": "torch/PP-OCRv4/det/ch_PP-OCRv4_det_infer.pth", |
70 | | - }, |
71 | | - "cls_model_path": { |
72 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/torch/PP-OCRv4/cls/ch_ptocr_mobile_v2.0_cls_infer.pth", |
73 | | - "path": "torch/PP-OCRv4/cls/ch_ptocr_mobile_v2.0_cls_infer.pth", |
74 | | - }, |
75 | | - "rec_model_path": { |
76 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/torch/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer.pth", |
77 | | - "path": "torch/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer.pth", |
78 | | - }, |
79 | | - "rec_keys_path": { |
80 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer/ppocr_keys_v1.txt", |
81 | | - "path": "paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer/ppocr_keys_v1.txt", |
82 | | - }, |
83 | | - "font_path": { |
84 | | - "url": "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.5.0/resources/fonts/FZYTK.TTF", |
85 | | - "path": "fonts/FZYTK.TTF", |
86 | | - }, |
| 78 | + key: _build_model_detail(path) |
| 79 | + for key, path in _RAPIDOCR_DEFAULT_MODEL_PATHS["torch"].items() |
87 | 80 | }, |
88 | 81 | } |
89 | 82 |
|
90 | 83 | def __init__( |
91 | 84 | self, |
92 | 85 | enabled: bool, |
93 | | - artifacts_path: Optional[Path], |
| 86 | + artifacts_path: Path | None, |
94 | 87 | options: RapidOcrOptions, |
95 | 88 | accelerator_options: AcceleratorOptions, |
96 | 89 | ): |
@@ -167,10 +160,10 @@ def __init__( |
167 | 160 | ) |
168 | 161 |
|
169 | 162 | for model_path in ( |
| 163 | + det_model_path, |
170 | 164 | rec_keys_path, |
171 | 165 | cls_model_path, |
172 | 166 | rec_model_path, |
173 | | - rec_keys_path, |
174 | 167 | font_path, |
175 | 168 | ): |
176 | 169 | if model_path is None: |
@@ -224,7 +217,7 @@ def __init__( |
224 | 217 | @staticmethod |
225 | 218 | def download_models( |
226 | 219 | backend: _ModelPathEngines, |
227 | | - local_dir: Optional[Path] = None, |
| 220 | + local_dir: Path | None = None, |
228 | 221 | force: bool = False, |
229 | 222 | progress: bool = False, |
230 | 223 | ) -> Path: |
|
0 commit comments