Expand description
ONNX-free half of the PP-OCRv3 recognition pipeline: everything before
and after the session.run call — line segmentation, crop preparation,
width-batching, CTC decoding, dictionary handling.
Split out of ocr.rs (which keeps the ort session) so the browser build
can reuse it (issue #79 phase 2): docling-wasm runs these exact
functions and delegates only the inference call to ONNX Runtime Web on
the JS side. Keeping one implementation is what makes the wasm output
byte-comparable to the native CPU path — any drift then comes from the
runtime, not from pre/post-processing.
Structs§
- Prep
Line - A text-line crop prepared for recognition: resized to the fixed model
height, normalised to
[-1, 1], laid out CHW.
Constants§
- REC_
BATCH - Cap on lines per recognition run: bounds peak input-tensor memory (16 × 3 × 48 × 2400 px ≈ 22 MB f32) without costing measurable batching benefit — same-width groups are rarely larger.
- REC_
HEIGHT - PP-OCRv3’s fixed input height.
Functions§
- batch_
input - Pack one width-batch into the model’s
(N, 3, H, W)input buffer. - decode_
row - Greedy CTC decode of one row’s
(T, C)probabilities. - dict_
chars - The CTC class table for a recognition dictionary file: index 0 = blank, then one class per dictionary line, then the space class.
- is_
text_ label - Layout labels whose content is recognised as running text.
- normalize_
polarity - Normalize an image to the scan polarity every stage assumes — dark ink on light paper (the segmentation threshold and the recognition model’s training data both bake it in): a predominantly dark page (mean luma below mid-gray — a dark-mode screenshot, an inverted scan) is inverted. Browser-path helper; the native pipeline never calls it (its input is scanned paper, and the conformance baseline stays untouched).
- prep_
line - Prepare one line crop, or
Nonefor a degenerate (zero-sized) crop. - prep_
page_ lines - Whole-image line preparation for the browser OCR path (no layout model:
the page itself is the single text region). Returns page-order prepared
lines; callers that need geometry use
segment_linesdirectly. - prep_
region_ lines - Gather every text-region line crop on a page, in page order: crop each
text region (page points ×
scale→ image px), split it into lines, prep each line, and keep the line’s page-point bbox. The exact gathering the nativeocr_pagedoes — shared so the browser path produces the same cells given the same probabilities. - prep_
table_ words - Gather word crops from a page’s table regions (browser table path, #157
stage 3): crop each table region, split it into lines, split each line into
words (
segment_words), prep each word for recognition, and keep the word’s page-point bbox. Recognizing table interiors is what gives the cell matcher the word boxes it needs — the native pipeline reads those from pdfium’s text layer, which the browser doesn’t have. NOT used by native. - segment_
lines - Split a region crop into text lines via a horizontal ink-projection profile.
Returns tight
(l, t, r, b)boxes in crop pixels. - segment_
words - Split a text line into word tokens by a vertical ink-projection profile:
runs of ink columns separated by whitespace wider than ~0.6× the line
height (an inter-word/-column gap, not an inter-character one). Returns tight
(l, 0, r, h)boxes in line-crop pixels. The browser table path recognizes these individually so each word carries its own box for column matching — the native pipeline gets word boxes from the pdfium text layer instead. - width_
batches - Deterministic recognition batching: page-order line indices grouped by
exact width (equal widths share a run — bit-identical to one-at-a-time
recognition, see
ocr.rs), each group split intoREC_BATCHchunks.
Type Aliases§
- LineBox
- A line’s page-point bounding box,
(l, t, r, b).