Expose hybrid OCR in the Python API + fix RapidOCR runner crash#44
Open
sh3raawii wants to merge 2 commits into
Open
Expose hybrid OCR in the Python API + fix RapidOCR runner crash#44sh3raawii wants to merge 2 commits into
sh3raawii wants to merge 2 commits into
Conversation
The embedded RapidOCR driver read its results with `getattr(result, 'boxes', []) or []`. Current rapidocr returns NumPy arrays, and `ndarray or []` raises "The truth value of an array with more than one element is ambiguous". The runner subprocess then exits non-zero, run_rapidocr_words returns None, and hybrid OCR silently recovers nothing whenever the RapidOCR engine is selected. Normalize each field explicitly (None -> [], otherwise list(...)).
The CLI supports --hybrid docling-fast, but the Python bindings built a
ProcessingConfig that always left hybrid = Off, so the OCR-recovery path
was unreachable from Python.
Add `hybrid` ("off" | "docling-fast") and `hybrid_mode` ("auto" |
"full") keyword arguments to convert() and convert_file() in the pyo3
binding and the edgeparse Python wrapper, mapping identically to the CLI.
Defaults are unchanged, so this is backward compatible.
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.
Summary
Two related changes that make hybrid OCR (
--hybrid docling-fast) usable from the Python API:fix: RapidOCR runner crashes on NumPy result fields. The embedded RapidOCR driver read its results with
getattr(result, 'boxes', []) or []. Currentrapidocrreturns NumPy arrays, andndarray or []raisesValueError: The truth value of an array with more than one element is ambiguous. The runner subprocess therefore exits non-zero,run_rapidocr_wordsreturnsNone, and hybrid OCR silently recovers nothing wheneverEDGEPARSE_OCR_ENGINE=rapidocris selected. Fixed by normalizing each field explicitly (None→[], otherwiselist(...)).feat(python): expose
hybrid/hybrid_modeinconvert()andconvert_file(). The CLI supports--hybrid docling-fast, but the Python bindings built aProcessingConfigthat always lefthybrid = Off, so the OCR-recovery path was unreachable from Python. This adds the two keyword arguments (mapping identically to the CLI) to both the pyo3 binding and theedgeparsePython wrapper.Motivation
import edgeparsehad no way to turn on hybrid OCR — the only door to it was the CLI. And even via the CLI, the higher-accuracy RapidOCR engine was broken by the crash above. Together these meant hybrid OCR was effectively unavailable to Python users. After this change:hybridaccepts"off"(default) or"docling-fast";hybrid_modeaccepts"auto"(default) or"full". Defaults are unchanged, so this is backward compatible.As with the CLI,
docling-fastshells out topdftoppmandtesseract(andrapidocrwhenEDGEPARSE_OCR_ENGINE=rapidocrand it is importable), so those need to be present on the host for OCR recovery to run.Testing
Built the wheel with maturin and verified against an image-only PDF (dense prose rendered as a picture, no text layer) with
poppler-utils,tesseract-ocr, andrapidocrinstalled:edgeparse.convert(pdf, hybrid="off")→ 0 words (born-digital parser sees no text layer)edgeparse.convert(pdf, hybrid="docling-fast", hybrid_mode="full")→ full text recovered via RapidOCRBorn-digital PDFs are unaffected.
Changes
crates/edgeparse-core/src/pdf/raster_table_ocr.rs— NumPy-safe result parsing in the RapidOCR runner.crates/edgeparse-python/src/lib.rs—hybrid/hybrid_modeargs onconvert()/convert_file().sdks/python/edgeparse/__init__.py— same args on the Python wrapper, with docs.sdks/python/edgeparse/_types.py—HYBRID_BACKENDS/HYBRID_MODESconstants.