Skip to content

Pass ocr_language to the recognition engine in the RapidOCR plugins - #507

Merged
veget-able merged 2 commits into
mainfrom
rapidocr-language
Nov 16, 2026
Merged

veget-able merged 2 commits into
mainfrom
rapidocr-language

Conversation

@veget-able

@veget-able veget-able commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

ocr_language is not currently used by the recognition engine in two
RapidOCR-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 it
to get_text(), so Tesseract uses the default language, eng. This PR
passes 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 for
Korean, 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_onnxruntime backend, det_only(), paddleocr_api, and the
exec_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 in
the relevant script found in the Markdown output, alongside the number on
the source page. These counts are not character accuracy scores.

Page ocr_language Before (output / source) After (output / source)
Korean, 3 lines + 1 English line (RapidOCR 3.9.2 and 3.10.0) kor, eng+kor 0 / 32 22 / 32
Russian, 2 lines + 1 English line (RapidOCR 3.10.0) rus, eng+rus 0 / 42 42 / 42
Greek, 2 lines + 1 English line (RapidOCR 3.10.0) ell 26 / 36 36 / 36

The 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 and
after the change.

Limitations

  • A RapidOCR engine uses one recognition model. When multiple language codes
    are supplied, the first code present in the mapping selects the model.
    For example, eng+kor selects the Korean model.
  • RapidOCR downloads the selected recognition model on first use, if it is
    not already available. If loading fails, full_ocr() logs the error and
    returns no OCR text for the page. This PR does not fall back to the default
    model, which may not support the requested script well.
  • Thai, Indic scripts and Arabic are not included. The font used to insert
    recognized text lacks glyphs for these scripts, and the Arabic model also
    needs the optional python-bidi package.
  • The Tesseract recognition change is covered by a test that replaces
    get_text(). I could not test it against a real Tesseract installation.

The current documentation describes ocr_language as the language for
Tesseract. If this approach is welcome, I can follow up with an update to the
OCR documentation.

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

Copy link
Copy Markdown
Collaborator

Thanks a lot @veget-able !
Please merge and reflect the introduced changes in the docs!

@veget-able
veget-able merged commit 486dcc5 into main Nov 16, 2026
8 of 12 checks passed
@veget-able

Copy link
Copy Markdown
Contributor Author

Merged, thanks for the quick review! The docs update is in pymupdf/PyMuPDF#5283.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants