Repository navigation
fix(ocr): restore OCR of scanned pages only with page_bitmap_area_threshold - #4999
yashikam19 wants to merge 4 commits into
Conversation
…eshold Since docling-project#3710, `bitmap_area_threshold` raises a ValidationError, and no OcrMode can skip OCR on pages that carry only small bitmaps. Add `OcrOptions.page_bitmap_area_threshold`: a page is OCRed only when bitmaps cover at least that fraction of it. The pages below it keep their native text, also in FULL_PAGE mode. Accept `bitmap_area_threshold` as a deprecated alias, and pass the field on from OcrAutoOptions. Resolves docling-project#4834 Signed-off-by: Yashika Malhotra <106444881+yashikam19@users.noreply.github.com>
|
✅ DCO Check Passed Thanks @yashikam19, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
AI triage (advisory)Checked commit This pull request adds CI safety: no concern found. A maintainer must still approve CI. 2 related pull request(s) in the same code
The |
There was a problem hiding this comment.
AI first review (advisory): blocker found
This review covers the full diff at cb2853c283. It read the code and did not run it.
The PR adds page_bitmap_area_threshold to skip OCR on pages with low bitmap coverage. When mode=OcrMode.FULL_PAGE is used, post_process_cells deletes native word_cells and char_cells on pages that skip OCR.
A maintainer makes the merge decision. This review never approves the pull request.
…eshold In FULL_PAGE mode, post_process_cells removed every word and char cell that is not from OCR, also on a page that page_bitmap_area_threshold kept from OCR. Filter them only when full-page OCR ran on the page. Test with the default docling-parse backend, which gives word cells. Signed-off-by: Yashika Malhotra <106444881+yashikam19@users.noreply.github.com>
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the changes since cb2853c283 at 07260b0df9. It read the code and did not run it.
I found no blocking defect in the update. The earlier blocker is fixed: post_process_cells now filters word and char cells only when OCR ran on the page. I could not check the real-PDF coverage values that the tests rely on (full coverage for ocr_test.pdf, about 15% for picture_classification.pdf).
Earlier findings
- fixed (blocker): FULL_PAGE mode clears native word and character cells on skipped pages
A maintainer makes the merge decision. This review never approves the pull request.
…ning Code written before OcrMode also set bitmap_area_threshold on a built options object, which still raised a ValidationError. Add a deprecated property that sets page_bitmap_area_threshold. It stays out of dumps. Point the deprecation warning at the caller. Python hides a DeprecationWarning that points to library code, so users did not see it. Signed-off-by: Yashika Malhotra <106444881+yashikam19@users.noreply.github.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
The page_bitmap_area_threshold key widens the Key column, which cannot wrap, so at 80 columns the Type column cut enum choices such as [tiny|small|medium]. Fold long values instead, and give the test a wide console: the rich console reads its width at import, so terminal_width did not apply. Signed-off-by: Yashika Malhotra <106444881+yashikam19@users.noreply.github.com>
Summary
Since #3710 (2.116.0),
bitmap_area_thresholdraises aValidationError, also withdo_ocr=False. NoOcrModecan replace it:FULL_PAGEOCRs every page, andDEFAULTOCRs every cluster that touches a bitmap, so pages with only logos or photos still get OCR.This PR adds
OcrOptions.page_bitmap_area_threshold(off by default). A page is OCRed only when bitmaps cover at least that fraction of it. Other pages skip OCR and keep their native text, whatever themode.The change:
pipeline_options.py: the new field (float | None, 0.0-1.0). The oldbitmap_area_thresholdworks again, in the constructor and on assignment, with aDeprecationWarning, asforce_full_page_ocrdoes.base_ocr_model.py:get_ocr_rects()returns no rects for a page below the threshold, so every engine supports it. InFULL_PAGEmode, a skipped page keeps its PDF line, word and char cells.page_preprocessing_model.py: inFULL_PAGEmode, native text is still extracted when the threshold is set.auto_ocr_model.py:OcrAutoModelpasses the field to the engine it selects.docs/concepts/OCR.mdand an example in the usage skill.Issue resolved by this Pull Request:
Resolves #4834
Checklist:
Review evidence:
Before and after, on a 3-page PDF made from test files, with Tesseract:
main,FULL_PAGEmain,DEFAULTFULL_PAGE+0.972206.01062.pdfp1, programmaticpicture_classification.pdfp1, picturesocr_test.pdfp1, scanThe old
bitmap_area_threshold=0.97gives the last column, with the warning.Reference data: no change. The option is off by default.
Tests:
tests/test_ocr_page_bitmap_area_threshold.py(13 tests, real PDFs, default backend) covers the deprecated field (constructor, assignment, warning shown at the caller's line), pages above and below the threshold, and native lines, words and chars kept inFULL_PAGEmode.Commands:
uv run pyteston the new tests and the OCR option, rect and cell tests: 207 passed.make validate: passed.Scope:
mode. To OCR scans in full, as beforeOcrMode, also setmode=OcrMode.FULL_PAGE.bitmap_area_threshold: 0.05. They now skip OCR on pages with less than 5% bitmaps, as they did then.OcrAutoModelchange, because it would load a real OCR engine.