Skip to content

Commit 0e95171

Browse files
authored
feat(RapidOcr): Support generic extra arguments for RapidOcr (#2266)
* feat: add support for additional parameters in RapidOcrOptions and fix RapidOcr font_path * DCO Remediation Commit for David Morady <[email protected]> I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 133d989 Signed-off-by: David Morady <[email protected]> * fix: RapidOcr ensure backwards compatibility and add deprecation note * add warning log for rec_font_path * DCO Remediation Commit for David Morady <[email protected]> I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 133d989 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 0a65eed I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: ac96f14 Signed-off-by: David Morady <[email protected]> * add tests for code coverage for rapidocr * DCO Remediation Commit for David Morady <[email protected]> I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 133d989 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 0a65eed I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: ac96f14 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: af5df4b Signed-off-by: David Morady <[email protected]> * add small comment for test * DCO Remediation Commit for David Morady <[email protected]> I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 133d989 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 0a65eed I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: ac96f14 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: af5df4b I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: ab893b6 Signed-off-by: David Morady <[email protected]> * fix test comment * DCO Remediation Commit for David Morady <[email protected]> I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 133d989 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 0a65eed I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: ac96f14 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: af5df4b I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: ab893b6 I, David Morady <[email protected]>, hereby add my Signed-off-by to this commit: 028e332 Signed-off-by: David Morady <[email protected]> --------- Signed-off-by: David Morady <[email protected]>
1 parent ad2f738 commit 0e95171

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

docling/datamodel/pipeline_options.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ class RapidOcrOptions(OcrOptions):
114114
cls_model_path: Optional[str] = None # same default as rapidocr
115115
rec_model_path: Optional[str] = None # same default as rapidocr
116116
rec_keys_path: Optional[str] = None # same default as rapidocr
117-
rec_font_path: Optional[str] = None # same default as rapidocr
117+
rec_font_path: Optional[str] = None # Deprecated, please use font_path instead
118+
font_path: Optional[str] = None # same default as rapidocr
119+
120+
# Dictionary to overwrite or pass-through additional parameters
121+
rapidocr_params: Dict[str, Any] = Field(default_factory=dict)
118122

119123
model_config = ConfigDict(
120124
extra="forbid",

docling/models/rapid_ocr_model.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,44 @@ def __init__(
6262
}
6363
backend_enum = _ALIASES.get(self.options.backend, EngineType.ONNXRUNTIME)
6464

65+
params = {
66+
# Global settings (these are still correct)
67+
"Global.text_score": self.options.text_score,
68+
"Global.font_path": self.options.font_path,
69+
# "Global.verbose": self.options.print_verbose,
70+
# Detection model settings
71+
"Det.model_path": self.options.det_model_path,
72+
"Det.use_cuda": use_cuda,
73+
"Det.use_dml": use_dml,
74+
"Det.intra_op_num_threads": intra_op_num_threads,
75+
# Classification model settings
76+
"Cls.model_path": self.options.cls_model_path,
77+
"Cls.use_cuda": use_cuda,
78+
"Cls.use_dml": use_dml,
79+
"Cls.intra_op_num_threads": intra_op_num_threads,
80+
# Recognition model settings
81+
"Rec.model_path": self.options.rec_model_path,
82+
"Rec.font_path": self.options.rec_font_path,
83+
"Rec.keys_path": self.options.rec_keys_path,
84+
"Rec.use_cuda": use_cuda,
85+
"Rec.use_dml": use_dml,
86+
"Rec.intra_op_num_threads": intra_op_num_threads,
87+
"Det.engine_type": backend_enum,
88+
"Cls.engine_type": backend_enum,
89+
"Rec.engine_type": backend_enum,
90+
}
91+
92+
if self.options.rec_font_path is not None:
93+
_log.warning(
94+
"The 'rec_font_path' option for RapidOCR is deprecated. Please use 'font_path' instead."
95+
)
96+
user_params = self.options.rapidocr_params
97+
if user_params:
98+
_log.debug("Overwriting RapidOCR params with user-provided values.")
99+
params.update(user_params)
100+
65101
self.reader = RapidOCR(
66-
params={
67-
# Global settings (these are still correct)
68-
"Global.text_score": self.options.text_score,
69-
# "Global.verbose": self.options.print_verbose,
70-
# Detection model settings
71-
"Det.model_path": self.options.det_model_path,
72-
"Det.use_cuda": use_cuda,
73-
"Det.use_dml": use_dml,
74-
"Det.intra_op_num_threads": intra_op_num_threads,
75-
# Classification model settings
76-
"Cls.model_path": self.options.cls_model_path,
77-
"Cls.use_cuda": use_cuda,
78-
"Cls.use_dml": use_dml,
79-
"Cls.intra_op_num_threads": intra_op_num_threads,
80-
# Recognition model settings
81-
"Rec.model_path": self.options.rec_model_path,
82-
"Rec.font_path": self.options.rec_font_path,
83-
"Rec.keys_path": self.options.rec_keys_path,
84-
"Rec.use_cuda": use_cuda,
85-
"Rec.use_dml": use_dml,
86-
"Rec.intra_op_num_threads": intra_op_num_threads,
87-
"Det.engine_type": backend_enum,
88-
"Cls.engine_type": backend_enum,
89-
"Rec.engine_type": backend_enum,
90-
}
102+
params=params,
91103
)
92104

93105
def __call__(

tests/test_e2e_ocr_conversion.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ def test_e2e_conversions():
7474
if sys.version_info < (3, 13):
7575
engines.append((RapidOcrOptions(), False))
7676
engines.append((RapidOcrOptions(force_full_page_ocr=True), False))
77+
engines.append(
78+
(
79+
RapidOcrOptions(
80+
force_full_page_ocr=True,
81+
rec_font_path="test",
82+
rapidocr_params={"Rec.font_path": None}, # overwrites rec_font_path
83+
),
84+
False,
85+
)
86+
)
7787

7888
# only works on mac
7989
if "darwin" == sys.platform:

0 commit comments

Comments
 (0)