Thank you @HaraldLieder for the in depth explanation. That makes sense!
Putting that into practice, I tested a few different configurations (all with pymupdf/pymupdf_layout/pymupdf4llm==2.1 and tessdata set as an environment variable) and ran the code below:
from pymupdf4llm.helpers.document_layout import select_ocr_function
import pymupdf
ocr_function = select_ocr_function()
with pymupdf.open(input_pdf_path) as test_doc:
for pno in range(test_doc.page_count):
page = test_doc.load_page(pno)
ocr_function(page)
test_doc.save(output_pdf_path)
rapidocr-onnxruntime==1.4.4
onnxruntime==1.31.1
Result: Uses RapidOCR & Tesseract for OCR processing.
rapidocr==3.9.1
onnxruntime==1.31.1
Result: Uses RapidOCR for OCR processing.
rapidocr==3.10.0
onnxruntime==1.31.1
Result: Uses RapidOCR for OCR processing.
rapidocr==3.9.1
rapidocr-onnxruntime==1.4.4
onnxruntime==1.31.1
Result: Uses RapidOCR for OCR processing.
<No Additional Packages>
Result: Uses Tesseract for OCR processing.
This all aligns with what you described, barring:
If RO is available with version 3.9.2 or later (in particular not equal to rapidocr-onnxruntime) it will be used as the default choice.
Even if using version 3.9.1, it is still used as the default over the combo. (Though perhaps my interpretation is incorrect)
Regarding the second issue, I had tessdata set as an environment variable and
pymupdf==2.1
pymupdf_layout==2.1
pymupdf4llm==2.1
rapidocr==3.10.0
onnxruntime==1.31.1
This resulted in the following code failing:
from pymupdf4llm.ocr.rapidtess_api import exec_ocr
import pymupdf
with pymupdf.open(input_pdf_path) as test_doc:
for pno in range(test_doc.page_count):
page = test_doc.load_page(pno)
exec_ocr(page)
test_doc.save(output_pdf_path)
With the error:
Traceback (most recent call last):
File "DemoOCR.py", line 25, in
exec_ocr(page)
File "...\\pymupdf4llm\\ocr\\rapidtess_api.py", line 22, in exec_ocr
return exec_ocr_detection(
^^^^^^^^^^^^^^^^^^^
File "...\\pymupdf4llm\\ocr\\exec_ocr_interface.py", line 138, in exec_ocr_detection
result = det_only(img)
^^^^^^^^^^^^^
File "...\\pymupdf4llm\\ocr\\rapidocr_391_backend.py", line 72, in det_only
result = engine.text_det(img)
^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not callable
I see now that RapidOCR 3.9.1 is explicitly stated as being supported in the code and 3.10.0 is not. Testing the same code with 3.9.1 works as expected.
The issue arose because newer versions RapidOCR (in my case 3.10.0) switched to dynamic loading of models. As a result of this; text_det must be manually initialized before it can be used.
With all of that said, what is the current, recommended packages/versions for running OCR?
Thank you again for your help!