Repository navigation
Pass ocr_language to the recognition engine in the RapidOCR plugins - #507
Merged
Merged
Conversation
Plugin rapidtess_api (RapidOCR text detection, Tesseract recognition) is selected when both Tesseract language data and RapidOCR are installed. exec_ocr_detection() receives the language, but calls get_text(pix, irect) without it. So Tesseract always recognizes with "eng", whatever ocr_language says. Pass the language on to get_text(). Nothing changes for the default "eng". The test replaces get_text(), so it needs no Tesseract installation.
Plugin rapidocr_api (RapidOCR detection and recognition) ignores the language: exec_ocr_full() calls full_ocr(img), and the backend always uses RapidOCR's default recognition model. That model (PP-OCRv6 "small" in rapidocr 3.9.2 and 3.10.0) reads Chinese, Japanese, English and the Latin script languages, but no Hangul and no Cyrillic, and Greek only in part. rapidocr_universal_backend.full_ocr() now accepts the language, a Tesseract language string like the other plugins use. A small table, REC_LANGUAGES, maps the codes for Korean, Greek and six Cyrillic script languages to a RapidOCR recognition language. For these, an engine with the PP-OCRv5 "mobile" recognition model is created on first use and cached. RapidOCR's default recognition version has no models for these scripts and RapidOCR does not choose another one itself, so the version is named explicitly. reset_engines() also clears these engines. rapidocr_api.exec_ocr() binds the language to full_ocr() if the rapidocr 3.x backend is in use. exec_ocr_full() and its engine callback contract are unchanged, and so are the rapidocr_onnxruntime backend, det_only() and paddleocr_api. Nothing changes for the default "eng" or any language outside the table: they use the same default engine with the same parameters as before, and no additional model is downloaded. Results for image-only pages through to_markdown(force_ocr=True), as characters of the script found in the Markdown out of those on the page: - Korean, 3 lines plus 1 English line, rapidocr 3.9.2 and 3.10.0: 0 of 32 before, 22 of 32 with "kor" or "eng+kor". The missing line is turned upside down by RapidOCR's text direction classifier, which is not changed here. - Russian, 2 lines plus 1 English line, rapidocr 3.10.0: 0 of 42 before, 42 of 42 with "rus" or "eng+rus". - Greek, same layout, rapidocr 3.10.0: 26 of 36 before, 36 of 36 with "ell". The English line is read in all cases, and the output for "eng" is identical to before. Limitations: - An engine has one recognition model. For "a+b", the first code found in the table selects it. - RapidOCR downloads a recognition model (8 to 14 MB) on first use. If that fails, full_ocr() logs the error and returns no text for the page, like it does for any other engine error. There is no fallback to the default model, because it cannot read the requested script. - Thai, the Indic scripts and Arabic are not in the table. Their PP-OCRv5 models exist, but the font which exec_ocr_full() uses to insert the recognized text has no glyphs for them, and the Arabic model needs the optional python-bidi package. One of the tests downloads the Korean model. It only runs if PYMUPDF4LLM_TEST_RAPIDOCR_DOWNLOAD=1 is set.
JorjMcKie
approved these changes
Nov 16, 2026
Collaborator
|
Thanks a lot @veget-able ! |
Contributor
Author
|
Merged, thanks for the quick review! The docs update is in pymupdf/PyMuPDF#5283. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ocr_languageis not currently used by the recognition engine in twoRapidOCR-based OCR paths. This PR addresses both cases.
Changes
RapidOCR detection with Tesseract recognition (
rapidtess_api)exec_ocr_detection()receives the requested language but does not pass itto
get_text(), so Tesseract uses the default language,eng. This PRpasses the requested language through to Tesseract.
Since #502, this plugin is selected when both Tesseract language data and
RapidOCR are available.
RapidOCR detection and recognition (
rapidocr_api)The RapidOCR 3.x backend currently uses its default recognition model
regardless of
ocr_language. This PR maps Tesseract language codes forKorean, Greek and six Cyrillic-script languages to the corresponding
RapidOCR recognition models.
The selected PP-OCRv5 mobile model is loaded on first use and its engine is
cached. If no language code in the mapping is requested, the backend continues
to use the existing default engine and parameters.
The
rapidocr_onnxruntimebackend,det_only(),paddleocr_api, and theexec_ocr_full()callback contract are unchanged.Example results
The following results were obtained from image-only test pages using
to_markdown(force_ocr=True). Each value shows the number of characters inthe relevant script found in the Markdown output, alongside the number on
the source page. These counts are not character accuracy scores.
ocr_languagekor,eng+korrus,eng+rusellThe English line was recognized in all cases. On the Korean test page,
RapidOCR's text direction classifier turned the remaining Korean line upside
down. This PR does not change the classifier.
For
eng, the output on the Korean test page was byte-identical before andafter the change.
Limitations
are supplied, the first code present in the mapping selects the model.
For example,
eng+korselects the Korean model.not already available. If loading fails,
full_ocr()logs the error andreturns no OCR text for the page. This PR does not fall back to the default
model, which may not support the requested script well.
recognized text lacks glyphs for these scripts, and the Arabic model also
needs the optional
python-bidipackage.get_text(). I could not test it against a real Tesseract installation.The current documentation describes
ocr_languageas the language forTesseract. If this approach is welcome, I can follow up with an update to the
OCR documentation.