Overview
PyMuPDF4LLM supports default OCR functions. They come in the form of plugins that are present in itsocr subpackage. They are based on currently 3 popular OCR engines, Tesseract OCR, RapidOCR and PaddleOCR. Some engines can be combined to make use of their strengths and mitigate their weaknesses. For example, Tesseract OCR is very good at recognizing text, while RapidOCR is better at detecting text bounding boxes in images with complex backgrounds. By combining the two engines, we can achieve better overall OCR results while at the same time also reducing the overall OCR processing time.
Here is an overview of the available default plugins:
| Plugin Name | Engines | Description |
|---|---|---|
rapidocr_api | RapidOCR | Uses RapidOCR for both text detection and text recognition |
paddleocr_api | PaddleOCR | Uses PaddleOCR for both text detection and text recognition |
tesseract_api | Tesseract OCR | Uses Tesseract OCR for both text detection and text recognition |
rapidtess_api | RapidOCR + Tesseract OCR | Uses RapidOCR for text detection and Tesseract OCR for text recognition |
paddletess_api | PaddleOCR + Tesseract OCR | Uses PaddleOCR for text detection and Tesseract OCR for text recognition |
ocr_function parameter, PyMuPDF4LLM will check the availability of the three OCR engines and pick one of the above plugins in the following order of preference:
rapidtess_api(if both RapidOCR and Tesseract OCR are available)paddletess_api(if both PaddleOCR and Tesseract OCR are available)rapidocr_api(if RapidOCR is available, but not Tesseract OCR)paddleocr_api(if PaddleOCR is available, but not Tesseract OCR)tesseract_api(if Tesseract OCR is available, but neither RapidOCR nor PaddleOCR are available)
force_ocr parameter is True, an error will be raised. Otherwise, the document will be processed without OCR and a warning will be displayed.
The chosen plugin is displayed as an information message.
How Default Plugins Work
The provided default plugins use the following “hybrid” OCR approach:- Each page is cleaned from any existing standard text content.
- The remaining page is rendered as an image and passed to the OCR engine for text detection and recognition.
- Only the detected text is inserted back into the original page as standard text content.
Forcing the Choice of a Default Plugin
The default plugins are designed to be used as is, without any need for configuration. However, if you want to use a specific plugin, you can do so by using the following approach (which enforces for instance using RapidOCR and skipping the above selection process). Please note that all plugins have a function namedexec_ocr that does the actual OCR.
RapidOCR
If RapidOCR and the RapidOCR ONNX Runtime are available, you can use a pre-made callable OCR function for it, which is provided in thepymupdf4llm.ocr module as rapidocr_api.exec_ocr.
RapidOCR & Tesseract Side-by-Side
If you want to use both OCR engines side-by-side, you can do so by implementing a custom OCR function which calls both OCR engines — one for bbox recognition (RapidOCR) and the other for text recognition (Tesseract) — and then combines their results. This pre-made callable OCR function can be found in thepymupdf4llm.ocr module as rapidtess_api.exec_ocr.
Example
| Adaptor | Engines | Notes |
|---|---|---|
rapidocr_api.exec_ocr | RapidOCR | Requires RapidOCR and ONNX Runtime |
rapidtess_api.exec_ocr | RapidOCR & Tesseract | Better accuracy for bounding box detection and text recognition |
Providing your Own Plugin
If you want to use your own OCR function, you can do so as follows:page parameter which is a PyMuPDF Page object. The other parameters are optional. The plugin must create (or extend) the text of the passed-in page object by simply inserting text (using any of PyMuPDF’s text insertion methods). No return values expected.
Be prepared to accept None or a PyMuPDF Pixmap object as the pixmap parameter, which is the rendered image of the page if provided. Parameters dpi and language are passed through from the respective function parameters.
Selecting Pages for OCR
Usually in document processing, the vast majority of pages contain extractable text and do not require OCR. PyMuPDF4LLM contains logic that analyzes the content based on a number of criteria including (but not restricted to) the following:- Presence of extractable and legible (!) text
- Presence of images that appear to contain text
- Presence of vector graphics that simulate text
- Presence of text generated by previous OCR activities
analysis is a dictionary with the following keys and values. The area-related float values are computed as fractions of the total covered area.
| Key | Type | Description |
|---|---|---|
covered | pymupdf.Rect | Page area covered by content |
img_joins | float | Fraction of area of the joined images |
img_area | float | Fraction of sum of image area sizes |
txt_joins | float | Fraction of area of the joined text spans |
txt_area | float | Fraction of sum of text span bbox area sizes |
vec_joins | float | Fraction of area of the joined vector characters |
vec_area | float | Fraction of sum of vector character area sizes |
chars_total | int | Count of visible characters |
chars_bad | int | Count of Replacement Unicode characters |
ocr_spans | int | Count of text spans with ignored text (render mode 3) |
img_var | float | Area-weighted image variance |
img_edges | float | Area-weighted image edge energy |
vec_suspicious | int | Minimum number of suspected vector-based glyphs |
reason | str | Reason for the OCR decision, else None |
needs_ocr | bool | OCR decision (recommendation) |
"chars_bad"— more than 10% of all characters are illegible (i.e. Replacement Unicode characters)"ocr_spans"— there exist text spans created from previous OCR executions (render mode 3)"vec_text"— there exist suspected vector-based glyphs"img_text"— there exist images which (probably) contain recognizable text
-
By setting
force_ocr=Truein the output functions (to_markdown,to_text,to_json). All pages will then be OCRed with the selected or provided OCR function regardless of their content. This will obviously have a massive impact on your execution time: expect several seconds duration per each page. - Do as before, but add your own selection logic to the OCR plugin: