Skip to content

fix: OCR embedded image regions selectively instead of full-page render - #221

Open
AdemBoukhris457 wants to merge 1 commit into
run-llama:mainfrom
AdemBoukhris457:fix/selective-image-region-ocr
Open

fix: OCR embedded image regions selectively instead of full-page render#221
AdemBoukhris457 wants to merge 1 commit into
run-llama:mainfrom
AdemBoukhris457:fix/selective-image-region-ocr

Conversation

@AdemBoukhris457

Copy link
Copy Markdown
Contributor

Closes #220

Summary

  • Fixes OCR on text-heavy pages with embedded images: each qualifying image is rendered and OCR'd via render_image_object() instead of rendering and OCRing the entire page
  • Sparse pages (< 100 chars native text) still use full-page OCR (scanned / image-only pages)
  • Maps OCR bboxes from image pixel space back to page viewport coordinates

Motivation

Previously, any page with an embedded image (logo, icon, figure) triggered full-page OCR even when the page already had plenty of native text. image_bounds() was only used as a boolean trigger; image boxes were never used for cropping or regional OCR.

This caused unnecessary render + OCR cost on decks and reports where every slide has a small logo.

Documentation alignment

Code now matches documented selective OCR behavior:

  • AGENTS.md: "OCR only runs on embedded images where text extraction failed, not the entire document."
  • LiteParseConfig: runs on text-sparse pages and embedded images — image OCR is now scoped to those regions, not the whole page

Behavior change

Page condition Before After
< 100 chars Full-page OCR Full-page OCR (unchanged)
≥ 100 chars + images Full-page OCR Per-image OCR only
≥ 100 chars, no images Skip Skip (unchanged)

Test plan

  • cargo test -p liteparse --no-default-features ocr_merge (8/8 pass, incl. bbox mapping tests)
  • Parse a text-heavy PDF with a small logo per page — confirm faster OCR vs previous full-page behavior
  • Parse a scanned/sparse PDF — confirm full-page OCR still runs
  • Parse same file with --no-ocr — unchanged

@logan-markewich

Copy link
Copy Markdown
Contributor

I don't disagree with this change, but some complex/annoying merge conflicts popped up based on past PRs

@AdemBoukhris457
AdemBoukhris457 force-pushed the fix/selective-image-region-ocr branch from 0f2a48d to 52d02ef Compare May 26, 2026 08:35
@AdemBoukhris457

Copy link
Copy Markdown
Contributor Author

I don't disagree with this change, but some complex/annoying merge conflicts popped up based on past PRs

@logan-markewich Just rebased on main and took care of the conflicts. Should be good to go now

On text-heavy pages with embedded images, OCR each image region via render_image_object() rather than rendering and OCRing the entire page. Sparse pages (<100 chars) still use full-page OCR.

Co-authored-by: Cursor <cursoragent@cursor.com>
@AdemBoukhris457
AdemBoukhris457 force-pushed the fix/selective-image-region-ocr branch from 52d02ef to fb7c951 Compare May 26, 2026 08:49
@adarshmadrecha

Copy link
Copy Markdown

Just noting a edge case scenario, Sometimes, the PDF creating software will split the image and them embed it into the PDF.

Check this PDF for instance
Uploading 02.pdf…

Interesting pages

  • 32 -- Here the Image is occupying almost half the page. In this case, I would suggest only picking up per image OCR
  • 39 -- Here the Image is occupying almost all the area of the page. Except for the Header which appears on all the pages, there is NO text. In this case, I would suggest Full-page OCR. Technically, there are 3 images on this page because it got split.

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.

[Bug] Any embedded image triggers full-page OCR instead of selective image-region OCR

3 participants