Skip to content

The reference extractor reads scanned pages and pictures with OCR, in process - #298

Merged
zlareb1 merged 31 commits into
mainfrom
dp-ocr
Nov 6, 2026
Merged

zlareb1 merged 31 commits into
mainfrom
dp-ocr

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Related: #289, #292

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

The reference extractor reads scanned PDF pages, and pictures that cover a fifth of a page or slide, with OCR: a scan's headings, tables and text are cited by page like any page's, and a diagram's labels are searchable. Without OCR (LOOM_EXTRACTOR_OCR=off, or an image built with OCR=off) such a page or slide is reported not complete instead of read as if it held nothing.

Why

A scanned PDF failed every page as needing OCR, and the text in a picture (a whiteboard photo on a slide, a screenshot in a runbook) was not read at all, while the slide or page reported itself complete: a silent loss. On the document-parsing bench (#289) the held-out split's scans scored 0 on everything, and slides and decks lost 5 to 6% of their words with the file complete.

Design

extractor/loom_extractor/ocr.py binds libtesseract.so.5 and libleptonica.so.6 through ctypes, in the extraction child's own process: the child may start no program (its seccomp filter refuses a new process), so no tesseract binary is run. One engine per child, loaded at the first picture to read (a file with none never loads the model); its threads are capped by OMP_THREAD_LIMIT=1, set with the child's other native limits.

  • What is read: every picture on a PDF page with no text layer; on a page with text, and on a slide, a picture covering a fifth of it or more, unless the page's own text already fills a tenth of it (a scan whose text layer is drawn over its image, which would otherwise be read twice). Smaller pictures are decoration and are not read. An image is read once a file, by its object, however many times pages or slides show it, and only an image the engine decodes counts against the bound (one past it is not decoded, and one in a format Leptonica does not know, such as a pasted vector drawing, costs nothing); a page reads at most 50 pictures that need OCR, past which it is partial, and a page of small icons needs none.
  • A scanned page is laid out as any page is: OCR's words, at Tesseract's point size (which it gives at the 300 pixels an inch it reads at, turned to the size shown: at 1,500 pixels an inch a 12-point reading is 2.4 points), land where the picture draws them (its placement matrix, its width taken along the image's own axis however it is turned), and go through the same lines, columns, tables and headings. Tesseract reads the image in gray and sets its own threshold; an image mostly dark (light text on a dark screen) is turned light first. A scanned table's grid is found with Leptonica morphology (horizontal and vertical opening of a black-and-white copy, outside any dark region thicker than a tenth of an inch both ways and a margin around it, so a dark band and the gaps between its light letters are no rule), painted out of the image before OCR so its strokes do not become characters, and handed to the ruled-table reader as the page's rules; a word touching such a rule loses a stroke left on its edge, a word of vertical bars alone is dropped (a grid line too light to find), and other words are kept as read (timeout = 30, __init__).
  • A picture on a page with text (a diagram) is read as text: each of its lines a paragraph block after the page's text, with attrs.ocr, never a table or a heading. A diagram's boxes and labels laid out in rows would otherwise read as a table, and its large labels as headings.
  • Images: DCT, JPX and CCITT data are passed as they are; raw samples (gray, RGB, CMYK by its black, indexed) become a PNM, converted with bytes.translate rather than a loop per pixel; JBIG2 is refused (it would need a program). Past ocr.MAX_PIXELS an image is refused, from its own header before it is decoded (a GIF's by walking its blocks for its screen and every frame, as Leptonica decodes a GIF whole to give its header and decodes each frame at its own size), the clean-up's morphology uses composite bricks with its sizes those of 600 pixels an inch at most (a 4000 by 5000 scan in a one-inch box reads in 0.56 s, not 4.3; a 2x screenshot shown at 720 pixels an inch is read at its own size, exactly), and a recognition past 20 seconds (Tesseract's monitor deadline) leaves its page partial. A page whose drawing is too large to collect every picture from (past 1,000 images or the operators' bound) is partial rather than complete.
  • Coverage: a page or slide whose picture is not read (no OCR, the bound reached, an image OCR refuses or fails on with any error) is partial, and the unit says why; an image-only page that OCR reads none of fails. A scanned page whose layout is not trusted keeps its OCR lines as its text. Blocks read by OCR carry attrs.ocr, the artifact attrs.ocr_pages, and parser_version names the Tesseract version.
  • PPTX pictures inflate within a bound of their own (Package.media), so reading them never costs the deck's text parts their room.
  • Service settings: LOOM_EXTRACTOR_OCR (on/off), LOOM_EXTRACTOR_OCR_LANGUAGES (eng; Tesseract model names joined by +; a language set whose model is not installed stops the service at start) and LOOM_EXTRACTOR_OCR_MAX_IMAGES (30 a file), passed to the child with its limits. An image without the libraries logs once at start that such pages are reported not complete.

Alternatives: running the tesseract program is refused by the child's sandbox; a model-based OCR or layout stack would add hundreds of megabytes, a GPU-shaped runtime and non-determinism to the reference extractor.

Scope and risk

extractor/loom_extractor/ (ocr.py; the PDF parser, PARSER_VERSION 3; the PPTX parser, PARSER_VERSION 3; ooxml.Package.media; the service and child limits), extractor/Dockerfile, extractor/README.md, docs, tests.

  • The image gains libtesseract5 and tesseract-ocr-eng, about 44 MB unpacked (12.5 MB compressed; 511 MB against 467 MB); docker build --build-arg OCR=off leaves them out, and that build reads no image and reports such pages not complete.
  • A PDF or PPTX ingested again makes a new artifact: OCR text blocks, and a page or slide with a large picture is complete only when the picture was read.
  • Bounded on hostile input: images a file (30, each once), pixels an image (40 M), 20 seconds of recognition an image, picture bytes a deck, the child's memory and time limits as before; Tesseract runs in the child, so a crash in native code fails that file only. A letter in type past about 36 points can lose its stem with the rules taken out of a scan.

Compatibility, rollout and rollback

  • Wire: none; the artifact keeps contract version 1 (attrs.ocr, attrs.ocr_pages are attrs).
  • Operator steps: none. A deployment that wants no OCR sets LOOM_EXTRACTOR_OCR=off or builds with OCR=off; existing documents keep their artifact until they are ingested or synced again.
  • Rollback: reverting returns scanned pages to failing as needing OCR on their next ingest; artifacts written meanwhile stay as written.

Tests

tests/unit/test_document_extraction_ocr.py, with a fake engine: an image-only page is read and cited by page; OCR words land where the picture is drawn; a large picture on a text page is read and a small one is not; a text layer drawn over a page's image is read once, not again with OCR; a diagram's labels in columns on a text page are paragraphs, not a table or a heading; an image drawn on many pages, or shown on several slides, is read and counted once; an image drawn again on its page is read once there, a page past its picture bound is partial, and a text page of many small icons is complete; an image past the file's bound is not decoded; an image-only page names why its image was not read (inline, not drawn, drawing unreadable); an image that fails to decode with any error leaves its page partial and its text read; a scan whose layout is not trusted keeps its OCR text, and an image-only page whose layout fails says so; a negative 8-bit image is turned back; without OCR a large picture leaves its page partial and an image-only page failed; past the image bound a page is partial; a JBIG2 image is refused and says so; raw samples become a PNM Leptonica reads and a JPEG is passed as it is; a stroke is taken off a word only beside a rule; a table's strokes are taken off OCR's words; a deck's picture reads leave its text parts their room; an inline scan in a form beside a spacer still fails its page, a text page's large inline picture leaves it partial unless the page's text fills it, an inline scan beside a readable logo is not complete, and an inline icon drawn many times takes no room from the pictures OCR reads; a GIF's pixels count its screen and every frame; a header past the pixel bound is refused before decoding (real engine); a slide's large linked picture leaves it partial; a slide whose relationships cannot be read keeps its text and fails once, and a slide whose master cannot be read keeps its title; a slide's large picture is read into a block of its own, a small one is not, without OCR a large one fails its slide, a slide with a chart beside a picture OCR read is not complete, whether or not OCR found text; a picture in a placeholder is sized by its layout's placeholder of the same index (else type), or, when that one is unsized, by the master's placeholder its type inherits from (content takes the body's), or as the slide when none states it, and each layout, its relationships and each master are read once a deck, however many layouts share it; a grouped picture is sized on its slide, and a deck with no slide size has the default; an image whose header Leptonica cannot read leaves the file's image bound, and one whose decode fails after its header was read uses it; the service refuses a missing model it was set to and warns without OCR, and loads the engine once. An image turned a quarter is measured along its own width; a spacer under a point wide is not read, alone on a page or beside a scan, and the page stays complete; sizes under 6 points compare at a tenth. With the libraries installed (the extractor image), the real engine reads a ruled image end to end through ctypes, finds no rule in a dark band or between the light letters a dark band holds (skipped without them; run in the image, the last fails on the previous head); in the image, light text on a dark band (Release checklist for the queue) reads exactly, where it read as noise before the band was kept. The PDF and PPTX tests run with OCR off.

Document-parsing bench, held-out split (27 files): this branch in its image (OCR on) against main without OCR (python -m bench.docparse run --out ... on each, then score main=... ocr=... --split heldout), paired difference with a 95% bootstrap interval:

Category (files) Text recall Heading F1 Table cell F1 Locators Silent loss
All (27) 0.876 → 0.999 0.923 → 0.962 0.907 → 0.991 0.841 → 0.991 5 → 2
Scanned PDFs (3) 0.000 → 0.997 0.000 → 0.505 0.000 → 0.928 0.000 → 0.983 0 → 1
Slide PDFs (3) 0.949 → 0.992 1.000 → 1.000 1.000 → 1.000 0.902 → 0.983 2 → 1
PPTX (3) 0.938 → 1.000 1.000 → 1.000 1.000 → 1.000 0.865 → 1.000 3 → 0
DOCX, PDF exports (8) unchanged unchanged unchanged unchanged 0 → 0

The two silent losses left are OCR misreads on pages that read complete: a YAML code block on a scanned page, and one label of a blurred whiteboard photo on a slide. Scanned headings stay weak: OCR's point size separates a title from the text but not one heading level from the next. The OCR choices (gray input, rule sizes, bands kept) were made on the dev split, which moved the same way (text 0.926 → 0.999, headings 0.926 → 0.996, tables 0.917 → 0.992, locators 0.917 → 0.998, silent loss 0 → 0). Time 0.14 → 0.45 s and peak memory 37 → 66 MB a file; a scanned page about 2 s.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. I tuned OCR on the dev split only, checked the sandboxed service container reads scans end to end, and checked the bounds against hostile images in the tests.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 29 commits November 6, 2026 22:28
… process

A page with no text, and a picture covering a fifth of a page or slide, are read
with Tesseract through ctypes (no program started), their words laid out like a
page's own; a scanned table's grid is found, taken out before OCR and read as
the page's rules. Without OCR such a page or slide is not complete.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…le or a heading

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r reads a page's text twice

A picture the page's own text fills is not read; an image's failure of any kind
leaves its page partial; a scan's OCR lines survive a layout fallback; strokes
come off only words beside a rule; raw images convert without per-pixel loops;
deck pictures inflate within their own bound; the engine loads at the first
picture; a missing model it was set to stops the service.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… picture once a page

Only thin strokes are rules; a page reads at most 50 pictures; an image counts
against the bound only when the engine reads it; a slide is complete without
text only when OCR read all its pictures and it has no chart; the reasons an
image-only page names say what happened.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…, and decodes nothing past its bound

A stroke in or beside a dark region is no rule; a page of small icons stays
complete; an image drawn again is collected once; a slide counts its pictures
and charts; the 1-bit image is computed once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…whose pictures are not all collected is partial

The dark-image mask is made from the turned gray image; a failed region mask
refuses the image rather than painting its letters out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…shown screenshot at its own size

A page with no image is never partial for its drawing's bound; the drawing's
own reason follows OCR's; a band mask is freed when the next one fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…d measures a turned image along its own axis

A picture shown under a point wide is refused; a page whose pictures are known
skips the image search; the drawing's bounds say which costs what.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… points compare at a tenth; turned images are tested

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…tep; a slide skips a sub-point picture

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…nts are one class; the slide filter that could not fire is gone

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…refused by its header's size, and a large linked picture leaves its slide partial

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e is read from its own first bytes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e inline scan is complete, and a GIF counts every frame

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r is no picture, and a text page whose drawing fails beside an image is partial

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… page whose drawing names one is partial

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…out-error paths and the dangling reasons are tested

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…s, and an image in the deepest form is read

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…t form's image and the nested reasons are tested

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e comment sits over its condition

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… when the page may hold an image

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…lide, and an undecodable image costs no OCR read

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… layouts are read once a deck, and a decode that read its header is charged

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ach layout's relationships are read once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…de's picture is its size or its placeholder

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ide that asks for it

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ide failures name an unreadable layout

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 2 commits November 6, 2026 22:29
… a slide that cannot read them keeps its text

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…says what its parts replace

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 31a58bf into main Nov 6, 2026
@zlareb1
zlareb1 deleted the dp-ocr branch November 6, 2026 22:53
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
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.

1 participant