Skip to content

feat(orientation): turn pages upright before OCR with --orient - #83

Merged
stephen-masters merged 1 commit into
mainfrom
feat/page-orientation
Oct 31, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
feat/page-orientation

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

Builds stages 1 and 2 of brief 020: turning pages upright before OCR.

What changed

  • --orient latin|armenian on tetrak-ocr ocr and tetrak-ocr batch. It reads which way up a page is from its ink and turns the page before the engine sees it.
  • New module tetrak_ocr.orientation. It asks two questions, in order:
    • Do the lines run across or down? This is script-agnostic: the line-direction vote from the Empire on Trial reading site, at 2000 px.
    • With the lines across, which way is up? This depends on the script, so the caller names it. For Latin, it uses the share of a line's outer ink above the line, with the plateau merge and the lines-found guard. For Armenian, it uses which way the open sides of the letters face, after Aradhye (2005). Both run at up to 4000 px. Design research 002 found the Latin measure fails on Armenian; 003 found the concavity measure works on Armenian and not on Latin.
  • Unsure means untouched. A blank leaf, a dark frame, unclear line direction, or a share within the margin of a half is read as it is and named in the output. The margins (0.05 for Latin, 0.02 for Armenian) come from the studies' saved per-page readings.
  • The scan is never changed. The engine reads a turned PNG copy with the same stem, kept in a temporary directory. The searchable PDF embeds that copy so its text layer lines up with the page, and the original moves to processed/ byte for byte. The turn is recorded in the manifest's note column and as an orientation event in the run log.
  • Single-page files only. PDFs and multi-page TIFFs are read as they are, and the output says so.
  • New orientation extra (NumPy, OpenCV), needed only for --orient armenian. Both packages are already transitive through EasyOCR. A missing extra stops batch before any file is touched. dev now installs the extra, so its tests run in CI; that adds OpenCV, about 40 MB, to the CI install.
  • Docs. The command-line guide has a "Turning pages upright" section; the installation guide's extras table has the new row; the manifest's note column is documented.
  • ocr's body after argument handling moved into _transcribe, so it can run inside the upright block. process_file became a thin wrapper around _process_file for the same reason. There are no behaviour changes without --orient.

Checks

  • pytest -m "not slow": 492 passed, 40 of them new, on synthetic pages drawn in the tests, so no image needs a licence.
  • The tests fail if the plateau merge is disabled or the clockwise convention is swapped.
  • ruff check and ruff format --check: clean.
  • sphinx-build -W: clean, with the new module in the API reference.
  • Spot check on real upright pages, each tried in all four turns:
    • Armenian, one page per held-out register: 16 registers right every time. One page was left alone as unsure, at the 0.511 reading design research 003 recorded for it. The bilingual Armenian–English dictionary was wrong every time, as 003 predicted; the guide warns about bilingual pages.
    • Latin corpus: none wrong. Most posters and postcards were left alone because their lines are not clear enough to call.

Not in this PR

  • Stage 3 of the brief: re-running the shipped module over the studies' full page sets.
  • Stage 4: a real batch with hand-turned scans.

🤖 Generated with Claude Code

Scans come in on their side or upside down, and an engine reading a page
that is not upright returns nonsense. `--orient latin|armenian` on `ocr`
and `batch` reads which way up a page is from its ink and turns it
before the engine sees it (brief 020).

Two questions, in order. Whether the lines run across or down is
script-agnostic: the line-direction vote from the Empire on Trial
reading site. Which way is up depends on the script, so the caller names
it: for Latin, the share of a line's outer ink above it (with the
plateau merge and the lines-found guard); for Armenian, which way the
open sides of the letters face, after Aradhye (2005). Design research
002 found the Latin measure fails on Armenian, and 003 that the
concavity measure works on it and not on Latin.

Unsure means untouched: a blank leaf, a dark frame or a share within the
margin of a half is read as it is and named. The margins come from the
studies' saved per-page readings. The scan is never changed; the engine
reads a turned PNG copy, the searchable PDF embeds that copy so its text
layer lines up, and the manifest note and an `orientation` run-log event
record the turn. Single-page files only.

The Armenian measure needs NumPy and OpenCV, in a new `orientation`
extra (already transitive through EasyOCR), which `dev` installs so the
tests run in CI. The tests draw their pages, so no image needs a
licence.

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying tetrak with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3befe10
Status: ✅  Deploy successful!
Preview URL: https://f677eb33.tetrak.pages.dev
Branch Preview URL: https://feat-page-orientation.tetrak.pages.dev

View logs

@stephen-masters
stephen-masters merged commit 35abe78 into main Oct 31, 2026
8 checks passed
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