Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ tetrak-ocr backends # what is installed, and what each
tetrak-ocr ocr <path> --backend tesseract-auto
tetrak-ocr batch --backend auto-local # workspace/: scans/ → processed/, failures to triage/
tetrak-ocr evaluate --all --save # → evaluation/ocr/benchmark.{csv,md} + runs/
python -m evaluation.ocr.reading_order --save # → evaluation/ocr/reading_order.{csv,md}: column-aware order vs a positioned layer's

pytest -m "not slow" # the fast loop; what CI gates on
pytest # adds the corpus-backed threshold tests
Expand Down Expand Up @@ -122,6 +123,15 @@ uniformity is what lets `registry.py`, the batch pipeline and the harness treat
backends interchangeably; a backend that needs a different signature needs an
adapter, not a special case in the callers.

**A backend that computes boxes returns them inside that string.** `vision`,
`easyocr`, `easyocr-hy` and `paddle` return `layout.Transcript`, a `str`
subclass carrying one `TextSpan` per recognised region -- pixels from the
top-left corner, in the engine's own order; the others return a plain `str`.
Callers take the geometry with `layout.spans_of(text)`, which gives `None`
for a plain string, and `batch` and `cli` hand it to every output writer as
the `spans` and `page_spans` options. Take the spans off before any string
operation: `.strip()` and `join` return a plain `str` and lose them.

**Missing extras raise `MissingBackendError`, not `ImportError`.** Each backend
module sets `_IMPORT_OK` rather than exiting at import time, so the registry can
report which extra to install. Keep that pattern when adding a backend —
Expand Down
29 changes: 29 additions & 0 deletions docs/guide/python-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ ocr = build_ocr_fn("tesseract-auto", contrast=3.0) # auto PSM, your contrast
ocr = build_ocr_fn("auto-local", with_paddle_vl=True)
```

## Positioned spans

Four backends -- `vision`, `easyocr`, `easyocr-hy` and `paddle` -- know where
each recognised region sits on the page. They return the same string as
before, but as `tetrak_ocr.layout.Transcript`, a `str` subclass carrying one
`TextSpan` per region: the text, its box as `(left, top, right, bottom)` in
pixels from the top-left corner, and the engine's confidence on a 0-100
scale. Take them off with `spans_of`, which returns `None` for a backend that
has none:

```python
from pathlib import Path
from tetrak_ocr.layout import reading_order, spans_of
from tetrak_ocr.registry import get_backend

text = get_backend("easyocr")(Path("scan.jpg"))
spans = spans_of(text) # a list of TextSpan, or None for marker, claude and tesseract
if spans:
for span in reading_order(spans):
print(span.bbox, span.text)
```

The spans are in the engine's own order; `reading_order` arranges them as a
person reads the page, column by column. Any string operation on the
transcript returns a plain `str`, so take the spans off before stripping or
joining it. `auto-local` returns the winning backend's transcript whole, spans
included. The output writers receive them as the `spans` and `page_spans`
options; the text and PDF formats ignore them.

## Fan-out and the quality gate

`auto-local` runs every eligible local engine on a file, scores each
Expand Down
4 changes: 4 additions & 0 deletions evaluation/ocr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
`harness.py` scores every installed backend over `corpus/images/` against the
reference transcripts in `corpus/expected/`, and writes `benchmark.csv`, which
the documentation site reads at build time.

`reading_order.py` scores the column-aware ordering of each span-carrying
backend's regions against the line order a positioned layer reads back as,
on the same fixtures, and writes `reading_order.csv` for the site likewise.
"""
55 changes: 55 additions & 0 deletions evaluation/ocr/reading_order.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
fixture,backend,layout,engine_chr,engine_wrd,linesort_chr,linesort_wrd,column_chr,column_wrd,transcript_chr,transcript_wrd
inside-facts-1930-cover,vision,multi-column,0.5810,0.8331,0.4570,0.8331,0.4570,0.8331,0.4354,0.8219
inside-facts-1930-page-six,vision,multi-column,0.3065,0.8431,0.3089,0.8431,0.6647,0.8431,0.6016,0.8396
kinema-theater-ad-1920,vision,multi-column,0.9519,0.8856,0.4191,0.8856,0.8799,0.8856,0.8386,0.8686
mean,vision,multi-column,0.6131,0.8540,0.3950,0.8540,0.6672,0.8540,0.6252,0.8434
bancroft-magician-poster,vision,single-column,0.7442,0.5000,0.6744,0.5000,0.6744,0.5000,0.6744,0.5000
carthay-circle-postcard-back,vision,single-column,0.8976,0.8333,0.9606,0.8333,0.6850,0.8333,0.6850,0.8333
carthay-circle-premiere,vision,single-column,0.8715,0.8571,0.8715,0.8571,0.8715,0.8571,0.8715,0.8571
chinese-theatre-triptych-postcard,vision,single-column,0.8936,1.0000,0.7064,1.0000,0.6298,1.0000,0.6298,1.0000
graumans-chinese-theatre,vision,single-column,0.7342,0.3750,0.7342,0.3750,0.7342,0.3750,0.7342,0.3750
greek-theatre-night,vision,single-column,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000
hollywood-boulevard-east,vision,single-column,0.7232,0.3889,0.7232,0.3889,0.7232,0.3889,0.7232,0.3889
hollywood-boulevard-west,vision,single-column,0.5981,0.4333,0.5981,0.4333,0.5483,0.4333,0.5483,0.4333
hollywood-music-box-playbill-1926,vision,single-column,0.7219,0.7655,0.7400,0.7655,0.7400,0.7655,0.7077,0.7655
kar-mi-troupe-poster,vision,single-column,0.3082,0.3961,0.2949,0.3961,0.2829,0.3961,0.2802,0.3961
over-the-fence-poster,vision,single-column,0.4725,0.3182,0.4725,0.3182,0.4725,0.3182,0.4725,0.3182
parlor-match-poster,vision,single-column,0.7139,0.7188,0.8215,0.7188,0.8215,0.7188,0.8215,0.7188
thurston-magician-poster,vision,single-column,0.7402,0.6296,0.6762,0.6296,0.6263,0.6296,0.6263,0.6296
mean,vision,single-column,0.6476,0.5551,0.6364,0.5551,0.6007,0.5551,0.5981,0.5551
inside-facts-1930-cover,easyocr,multi-column,0.4130,0.7080,0.4111,0.7080,0.7762,0.7080,0.4130,0.7080
inside-facts-1930-page-six,easyocr,multi-column,0.3013,0.7778,0.2978,0.7778,0.7026,0.7778,0.3013,0.7778
kinema-theater-ad-1920,easyocr,multi-column,0.4175,0.6653,0.4342,0.6653,0.7902,0.6653,0.4175,0.6653
mean,easyocr,multi-column,0.3773,0.7170,0.3810,0.7170,0.7563,0.7170,0.3773,0.7170
bancroft-magician-poster,easyocr,single-column,0.3970,0.1364,0.5094,0.1364,0.5393,0.1364,0.3970,0.1364
carthay-circle-postcard-back,easyocr,single-column,0.7860,0.6250,0.7773,0.6250,0.7686,0.6250,0.7860,0.6250
carthay-circle-premiere,easyocr,single-column,0.8701,0.7143,0.8701,0.7143,0.8701,0.7143,0.8701,0.7143
chinese-theatre-triptych-postcard,easyocr,single-column,0.5044,0.6667,0.7080,0.6667,0.5310,0.6667,0.5044,0.6667
graumans-chinese-theatre,easyocr,single-column,0.7168,0.5000,0.8439,0.5000,0.8439,0.5000,0.7168,0.5000
greek-theatre-night,easyocr,single-column,0.9486,0.7500,0.8800,0.7500,0.9486,0.7500,0.9486,0.7500
hollywood-boulevard-east,easyocr,single-column,0.7240,0.4444,0.7240,0.4444,0.7240,0.4444,0.7240,0.4444
hollywood-boulevard-west,easyocr,single-column,0.5660,0.3000,0.5660,0.3000,0.4654,0.3000,0.5660,0.3000
hollywood-music-box-playbill-1926,easyocr,single-column,0.5032,0.3948,0.4974,0.3948,0.4974,0.3948,0.5032,0.3948
kar-mi-troupe-poster,easyocr,single-column,0.1239,0.3913,0.2529,0.3913,0.2401,0.3913,0.1239,0.3913
over-the-fence-poster,easyocr,single-column,0.4896,0.2273,0.4896,0.2273,0.4896,0.2273,0.4896,0.2273
parlor-match-poster,easyocr,single-column,0.6687,0.4375,0.6505,0.4375,0.4802,0.4375,0.6687,0.4375
thurston-magician-poster,easyocr,single-column,0.7353,0.5185,0.6691,0.5185,0.6691,0.5185,0.7353,0.5185
mean,easyocr,single-column,0.6180,0.4697,0.6491,0.4697,0.6206,0.4697,0.6180,0.4697
inside-facts-1930-cover,paddle,multi-column,0.5009,0.9414,0.4849,0.9414,0.8435,0.9414,0.5009,0.9414
inside-facts-1930-page-six,paddle,multi-column,0.2953,0.9693,0.2751,0.9693,0.8063,0.9693,0.2953,0.9693
kinema-theater-ad-1920,paddle,multi-column,0.4907,0.9280,0.4509,0.9280,0.7336,0.9280,0.4907,0.9280
mean,paddle,multi-column,0.4290,0.9462,0.4036,0.9462,0.7945,0.9462,0.4290,0.9462
bancroft-magician-poster,paddle,single-column,0.5467,0.3182,0.5467,0.3182,0.6574,0.3182,0.5467,0.3182
carthay-circle-postcard-back,paddle,single-column,0.2844,0.6250,0.2844,0.6250,0.2844,0.6250,0.2844,0.6250
carthay-circle-premiere,paddle,single-column,0.0885,0.0000,0.0885,0.0000,0.0885,0.0000,0.0885,0.0000
chinese-theatre-triptych-postcard,paddle,single-column,0.7850,0.7333,0.7196,0.7333,0.5421,0.7333,0.7850,0.7333
graumans-chinese-theatre,paddle,single-column,0.7386,0.1875,0.6477,0.1875,0.7273,0.1875,0.7386,0.1875
greek-theatre-night,paddle,single-column,0.9114,0.5000,0.9114,0.5000,0.9114,0.5000,0.9114,0.5000
hollywood-boulevard-east,paddle,single-column,0.6352,0.4444,0.6352,0.4444,0.6524,0.4444,0.6352,0.4444
hollywood-boulevard-west,paddle,single-column,0.5759,0.4333,0.5882,0.4333,0.5882,0.4333,0.5759,0.4333
hollywood-music-box-playbill-1926,paddle,single-column,0.7777,0.9086,0.9003,0.9086,0.9003,0.9086,0.7777,0.9086
kar-mi-troupe-poster,paddle,single-column,0.2741,0.3816,0.2927,0.3816,0.2582,0.3816,0.2741,0.3816
over-the-fence-poster,paddle,single-column,0.6355,0.4545,0.6075,0.4545,0.5047,0.4545,0.6355,0.4545
parlor-match-poster,paddle,single-column,0.6806,0.3125,0.7582,0.3125,0.7582,0.3125,0.6806,0.3125
thurston-magician-poster,paddle,single-column,0.7372,0.4444,0.6788,0.4444,0.6715,0.4444,0.7372,0.4444
mean,paddle,single-column,0.5901,0.4418,0.5892,0.4418,0.5803,0.4418,0.5901,0.4418
87 changes: 87 additions & 0 deletions evaluation/ocr/reading_order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Reading order of positioned output

Generated by `python -m evaluation.ocr.reading_order --save`. Do not edit.

The same spans serialised four ways and scored against the ground
truth: character similarity / word recall. `engine` is the order the
engine reported, `linesort` is lines top to bottom with no column
awareness (what a word-positioned layer reads back as), `column` is
`layout.reading_order`, and `transcript` is what `ocr_image` returns.
Word recall must not move between the first three.

## Multi-column fixtures, mean per backend

| Backend | engine | linesort | column | transcript |
|---|---:|---:|---:|---:|
| `vision` | 0.613 / 0.854 | 0.395 / 0.854 | 0.667 / 0.854 | 0.625 / 0.843 |
| `easyocr` | 0.377 / 0.717 | 0.381 / 0.717 | 0.756 / 0.717 | 0.377 / 0.717 |
| `paddle` | 0.429 / 0.946 | 0.404 / 0.946 | 0.794 / 0.946 | 0.429 / 0.946 |

## vision

| Fixture | engine | linesort | column | transcript |
|---|---:|---:|---:|---:|
| `inside-facts-1930-cover` | 0.581 / 0.833 | 0.457 / 0.833 | 0.457 / 0.833 | 0.435 / 0.822 |
| `inside-facts-1930-page-six` | 0.306 / 0.843 | 0.309 / 0.843 | 0.665 / 0.843 | 0.602 / 0.840 |
| `kinema-theater-ad-1920` | 0.952 / 0.886 | 0.419 / 0.886 | 0.880 / 0.886 | 0.839 / 0.869 |
| *mean, multi-column* | 0.613 / 0.854 | 0.395 / 0.854 | 0.667 / 0.854 | 0.625 / 0.843 |
| `bancroft-magician-poster` | 0.744 / 0.500 | 0.674 / 0.500 | 0.674 / 0.500 | 0.674 / 0.500 |
| `carthay-circle-postcard-back` | 0.898 / 0.833 | 0.961 / 0.833 | 0.685 / 0.833 | 0.685 / 0.833 |
| `carthay-circle-premiere` | 0.872 / 0.857 | 0.872 / 0.857 | 0.872 / 0.857 | 0.872 / 0.857 |
| `chinese-theatre-triptych-postcard` | 0.894 / 1.000 | 0.706 / 1.000 | 0.630 / 1.000 | 0.630 / 1.000 |
| `graumans-chinese-theatre` | 0.734 / 0.375 | 0.734 / 0.375 | 0.734 / 0.375 | 0.734 / 0.375 |
| `greek-theatre-night` | 0.000 / 0.000 | 0.000 / 0.000 | 0.000 / 0.000 | 0.000 / 0.000 |
| `hollywood-boulevard-east` | 0.723 / 0.389 | 0.723 / 0.389 | 0.723 / 0.389 | 0.723 / 0.389 |
| `hollywood-boulevard-west` | 0.598 / 0.433 | 0.598 / 0.433 | 0.548 / 0.433 | 0.548 / 0.433 |
| `hollywood-music-box-playbill-1926` | 0.722 / 0.766 | 0.740 / 0.766 | 0.740 / 0.766 | 0.708 / 0.766 |
| `kar-mi-troupe-poster` | 0.308 / 0.396 | 0.295 / 0.396 | 0.283 / 0.396 | 0.280 / 0.396 |
| `over-the-fence-poster` | 0.473 / 0.318 | 0.473 / 0.318 | 0.473 / 0.318 | 0.473 / 0.318 |
| `parlor-match-poster` | 0.714 / 0.719 | 0.822 / 0.719 | 0.822 / 0.719 | 0.822 / 0.719 |
| `thurston-magician-poster` | 0.740 / 0.630 | 0.676 / 0.630 | 0.626 / 0.630 | 0.626 / 0.630 |
| *mean, single-column* | 0.648 / 0.555 | 0.636 / 0.555 | 0.601 / 0.555 | 0.598 / 0.555 |

## easyocr

| Fixture | engine | linesort | column | transcript |
|---|---:|---:|---:|---:|
| `inside-facts-1930-cover` | 0.413 / 0.708 | 0.411 / 0.708 | 0.776 / 0.708 | 0.413 / 0.708 |
| `inside-facts-1930-page-six` | 0.301 / 0.778 | 0.298 / 0.778 | 0.703 / 0.778 | 0.301 / 0.778 |
| `kinema-theater-ad-1920` | 0.418 / 0.665 | 0.434 / 0.665 | 0.790 / 0.665 | 0.418 / 0.665 |
| *mean, multi-column* | 0.377 / 0.717 | 0.381 / 0.717 | 0.756 / 0.717 | 0.377 / 0.717 |
| `bancroft-magician-poster` | 0.397 / 0.136 | 0.509 / 0.136 | 0.539 / 0.136 | 0.397 / 0.136 |
| `carthay-circle-postcard-back` | 0.786 / 0.625 | 0.777 / 0.625 | 0.769 / 0.625 | 0.786 / 0.625 |
| `carthay-circle-premiere` | 0.870 / 0.714 | 0.870 / 0.714 | 0.870 / 0.714 | 0.870 / 0.714 |
| `chinese-theatre-triptych-postcard` | 0.504 / 0.667 | 0.708 / 0.667 | 0.531 / 0.667 | 0.504 / 0.667 |
| `graumans-chinese-theatre` | 0.717 / 0.500 | 0.844 / 0.500 | 0.844 / 0.500 | 0.717 / 0.500 |
| `greek-theatre-night` | 0.949 / 0.750 | 0.880 / 0.750 | 0.949 / 0.750 | 0.949 / 0.750 |
| `hollywood-boulevard-east` | 0.724 / 0.444 | 0.724 / 0.444 | 0.724 / 0.444 | 0.724 / 0.444 |
| `hollywood-boulevard-west` | 0.566 / 0.300 | 0.566 / 0.300 | 0.465 / 0.300 | 0.566 / 0.300 |
| `hollywood-music-box-playbill-1926` | 0.503 / 0.395 | 0.497 / 0.395 | 0.497 / 0.395 | 0.503 / 0.395 |
| `kar-mi-troupe-poster` | 0.124 / 0.391 | 0.253 / 0.391 | 0.240 / 0.391 | 0.124 / 0.391 |
| `over-the-fence-poster` | 0.490 / 0.227 | 0.490 / 0.227 | 0.490 / 0.227 | 0.490 / 0.227 |
| `parlor-match-poster` | 0.669 / 0.438 | 0.650 / 0.438 | 0.480 / 0.438 | 0.669 / 0.438 |
| `thurston-magician-poster` | 0.735 / 0.519 | 0.669 / 0.519 | 0.669 / 0.519 | 0.735 / 0.519 |
| *mean, single-column* | 0.618 / 0.470 | 0.649 / 0.470 | 0.621 / 0.470 | 0.618 / 0.470 |

## paddle

| Fixture | engine | linesort | column | transcript |
|---|---:|---:|---:|---:|
| `inside-facts-1930-cover` | 0.501 / 0.941 | 0.485 / 0.941 | 0.844 / 0.941 | 0.501 / 0.941 |
| `inside-facts-1930-page-six` | 0.295 / 0.969 | 0.275 / 0.969 | 0.806 / 0.969 | 0.295 / 0.969 |
| `kinema-theater-ad-1920` | 0.491 / 0.928 | 0.451 / 0.928 | 0.734 / 0.928 | 0.491 / 0.928 |
| *mean, multi-column* | 0.429 / 0.946 | 0.404 / 0.946 | 0.794 / 0.946 | 0.429 / 0.946 |
| `bancroft-magician-poster` | 0.547 / 0.318 | 0.547 / 0.318 | 0.657 / 0.318 | 0.547 / 0.318 |
| `carthay-circle-postcard-back` | 0.284 / 0.625 | 0.284 / 0.625 | 0.284 / 0.625 | 0.284 / 0.625 |
| `carthay-circle-premiere` | 0.088 / 0.000 | 0.088 / 0.000 | 0.088 / 0.000 | 0.088 / 0.000 |
| `chinese-theatre-triptych-postcard` | 0.785 / 0.733 | 0.720 / 0.733 | 0.542 / 0.733 | 0.785 / 0.733 |
| `graumans-chinese-theatre` | 0.739 / 0.188 | 0.648 / 0.188 | 0.727 / 0.188 | 0.739 / 0.188 |
| `greek-theatre-night` | 0.911 / 0.500 | 0.911 / 0.500 | 0.911 / 0.500 | 0.911 / 0.500 |
| `hollywood-boulevard-east` | 0.635 / 0.444 | 0.635 / 0.444 | 0.652 / 0.444 | 0.635 / 0.444 |
| `hollywood-boulevard-west` | 0.576 / 0.433 | 0.588 / 0.433 | 0.588 / 0.433 | 0.576 / 0.433 |
| `hollywood-music-box-playbill-1926` | 0.778 / 0.909 | 0.900 / 0.909 | 0.900 / 0.909 | 0.778 / 0.909 |
| `kar-mi-troupe-poster` | 0.274 / 0.382 | 0.293 / 0.382 | 0.258 / 0.382 | 0.274 / 0.382 |
| `over-the-fence-poster` | 0.636 / 0.455 | 0.607 / 0.455 | 0.505 / 0.455 | 0.636 / 0.455 |
| `parlor-match-poster` | 0.681 / 0.312 | 0.758 / 0.312 | 0.758 / 0.312 | 0.681 / 0.312 |
| `thurston-magician-poster` | 0.737 / 0.444 | 0.679 / 0.444 | 0.672 / 0.444 | 0.737 / 0.444 |
| *mean, single-column* | 0.590 / 0.442 | 0.589 / 0.442 | 0.580 / 0.442 | 0.590 / 0.442 |
Loading
Loading