tetrak_ocr.layout¶
Positioned text: the vocabulary shared by the backends and the output writers.
An OCR engine knows more than the string it returns: four of the backends
here – vision, easyocr, paddle and easyocr-hy – compute a box per
recognised region. The registry contract is -> str and stays so: a backend
that has boxes returns a Transcript, a string that carries its
TextSpan list alongside the text, and a backend that has none
returns a plain string. Every caller that wants the text is unchanged; a
caller that wants the geometry asks spans_of(). This module is where
that information has a shape, so the backends that keep it agree on what it
means.
Two backends order their text through it: the vision backend, whose
reading-order logic this originally was, lifted out and tested, and
easyocr-hy, which needs the column handling because the Armenian pages it
exists for are set in two columns. The ordering is deliberately not wired
into the searchable PDF writer: a positioned text layer measured worse on
reading order than the plain transcript, badly so on multi-column pages, and
that is what a screen reader announces. See brief 008 (searchable PDF). The
boxes become load-bearing at ALTO/hOCR, where coordinates are the whole
point, which is why the spans now travel with the transcript rather than
being thrown away at the backend’s boundary. Whether reading_order()
is good enough to carry a positioned output is a measured question:
evaluation/ocr/reading_order.py scores it against the line order a
positioned layer reads back as, per backend and fixture.
Standard library only, so the fast tests can use it with no OCR stack installed.
This module has a twin. tetrak_hy_trainer.align (in the public tetrak-hy-trainer repository) carries its own copy of the gutter detection, the baseline line-grouping and the three straddle constants below, because that repository is public and this one is not, so they cannot share code. The duplication is deliberate; the drift is not. A fix to the column logic here is worth porting there, and vice versa – the two solve the same problem on the same two-column pages, and diverge only in what they do with a span that crosses the gutter: this one must place it (dropping text is not an option when the output is the text), the harvester drops it (the transcripts omit running heads, so it has no truth token to pair with).
Coordinates¶
TextSpan.bbox is pixels with a top-left origin – the Pillow convention. That choice is deliberate, because three conventions are in play:
Pillow, Tesseract, EasyOCR, Paddle pixels, top-left origin Apple Vision normalised, bottom-left origin PDF points, bottom-left origin
Four of the five sources already speak the Pillow convention, so it wins. Vision converts at its own boundary and the writer converts once on the way out. Anything in between is unambiguous.
Functions
|
Rejoin words that a line break split in two. |
|
The x of the column gutter, or |
|
Bucket spans into visual lines, top to bottom, each ordered left to right. |
|
The axis-aligned |
|
Return the spans in the order a person would read them. |
|
The spans carried by text, or None when the backend returned a plain string. |
|
Join spans into a transcript: one line per visual line, in reading order. |
|
Visual lines across the page, ordered by recursive XY-cut. |
Classes
|
One recognised piece of text, and where it sits on the page. |
|
A backend's text, carrying the positioned spans it was built from. |