Row grouping interleaves the two halves of an open-book photo: 6 of 7 spread
pages flipped sides 32-69 times per page, now 1. Cut at the sparsest vertical
slice near the middle, not at a gap -- one box across the gutter hid the gap
on ht_3. Only cut within 0.08 of centre, or the index page vandailoaingu/495
splits between its name and page-number fields (31.24% -> 74.17% CER).
det: pick the facing-page filter on geometry ratios, not box counts
The first version keyed on how many boxes a cluster held and how much its
line angles spread. It worked on my machine and never fired in prod: on
the same photo detection yields 43 boxes locally and 26 on the Space, and
both of those quantities move with it - the minority share goes 0.30 to
0.73 and the main cluster's angle spread 2.2 to 10.2 degrees, so each
gate fails there.
Two ratios do not move: the facing page is squeezed to about a fifth of
the main page's text width (0.205 local, 0.211 Space) and its boxes are
about one and a half times taller because the lines run at a slant (1.52
and 1.49). Both describe the geometry of a page seen edge-on rather than
how many boxes detection happened to find.
A third condition, at least four boxes in the cluster, came from a
measured regression: without it the index page vandailoaingu/page_495
lost the single-box cluster holding its page number 501, which is narrow
and tall enough to pass the other two gates, and its CER went 10.17% to
10.…
det: drop boxes belonging to the facing page of an open book
A phone photo of an open book usually catches the other page edge-on.
Detection boxes it like any other ink, the recogniser returns fragments,
and worse: reading order groups by row, so the two pages interleave. On
the reported photo the output began with a scrap of the facing page and
had 6 backward jumps in y across 42 pairs.
The two pages separate on two signals, both required. They do not
overlap horizontally - a sweep of the gap threshold from 0 to 0.016 over
42 test images splits exactly one image, the open-book photo, and leaves
the other 41 as a single cluster. And a page seen edge-on gives each line
a different slope: 15.8 degrees of spread against 2.2 for the flat page.
The second signal is what keeps two-column documents safe, since both
columns of a flat page share one angle.
Where it runs matters more than the thresholds. Called straight after
detection it never fired: there are 58 boxes at that point and one of
them spans x[0.005-0.999] across both pages, so no horizont…
docs: bring the pipeline overview back in line with the code
Section 1 still showed the flow as it was before orientation handling,
polarity inversion, beam search and fix_ortho went in, and it listed
space_fix and BARTpho as part of the chain although both are off. The
diagram now follows the actual call order, including that page
preprocessing runs in api.py outside the pipeline.
Adds the current numbers and a list of what is deliberately switched off
with the measurement that settled each one, so the next person does not
re-enable them blind.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
_apply_prep runs in api.py before the pipeline, while the light-on-dark
check lives inside run(), so a slide was eroded while still a negative.
Erosion shrinks the bright region: on a normal page that thickens the ink
(7836 -> 9858 ink pixels on a sample) but on a negative it thins the text
(7836 -> 5862). Exactly backwards for the images the inversion support was
added for.
Both the API path and the bench's prep_path now invert first, which also
keeps bench and prod doing the same thing. run()'s own check then finds
the image already upright and does nothing.
Also carries the bench's VOCR_BENCH_CTX knob from the corrector work.
15-image regression: CER 4.03%, every text identical character for
character.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
post: repair words that break Vietnamese spelling rules
Vietnamese has hard orthographic constraints, and breaking one means the
word is certainly wrong: q only ever occurs in qu, gh/ngh only before
e/ê/i/y and g/ng never there, k only before e/ê/i/y and c never there,
and a final consonant must be one of c ch m n ng nh p t.
The dictionary-derived rime inventory already in _impossible() cannot
express the co-occurrence half of this: "nge" splits into ng + e and both
pieces exist, yet Vietnamese writes "nghe". So _ortho_violation() states
those laws directly.
The laws also generate the candidates, and that is where the win is.
Generic Lev-1 offers 62 candidates for "ce", 50 for "ka", 34 for "nge"
and 21 for "qa", then asks the language model to pick one. Each law
admits only a few legal repairs: 1, 1, 1 and 4.
Context gate: a first version broke a real line, turning "Qách mần nhiên
linh mến" into "Quách" where the label reads "Cách mầu nhiệm kính mến".
The right candidate was in the list but lost, 19.73 to 18.73 - the whole
line was misre…
det: invert light-on-dark images before detection
DBNet det and the recogniser are both trained on dark text on light
paper. On a slide or a negative scan det finds nothing at all: dep
inverted gives 0 lines and 100% CER against 17 lines normally. Reported
from the UI as "1 line, 56% confidence" on a 5-line slide.
_is_light_on_dark() runs before everything else, deskew included, since
deskew also thresholds on the assumption that ink is the dark class.
Otsu split, majority class is the background, and the background must be
dark in two regions: the central 60% block and an 8% border ring. Each
one alone has a case that fools it - without the centre a page shot on a
dark table flips, without the ring a single-line crop flips because
strokes dominate the middle of a text strip.
Measured on 1500 real images from the corpus: centre only gives 43 false
positives, all under crop_img/; adding the ring gives 0, while still
catching 15/15 inverted bench images. End to end the inverted image now
matches the normal one line for line on dep, p8…
orient: generalise rotation to any angle, keep 4 directions as default
run_oriented() now takes tiers instead of a fixed angle list, and
_rotate_any() rotates by an arbitrary angle with canvas expansion and a
paper-coloured border (multiples of 90 still go through cv2.rotate, so
the default path is unchanged).
Measured before choosing the default. Deskew does not leave a +-45
residual as assumed: it lands the page on a multiple of 90, because the
score picks the angle that makes text lines horizontal and "horizontal"
is only defined mod 90. Across 4 images x 24 angles, 92/96 landed within
3 degrees of a multiple of 90, the other 4 within 5. So deskew plus the
three 90-multiples already covers the full circle: a 24-angle OCR sweep
reads 0.20-0.98% CER at every angle against 0.39% upright, and 12
diagonal cases score identically with and without a diagonal tier (no
diagonal rotation file was ever written).
Diagonal tier stays available behind VOCR_ORIENT_DIAG=1, off by default
so a junk image costs 4 det+rec passes rather than 8.
Regression, 15 hand-labe…
rec: turn beam search on for v11, K=10 at alpha 0.30
Measured against the rule chain settled earlier today rather than the ten-rule
chain the benchmarks had been running by mistake. Over 20 pages accuracy goes
0.5090 to 0.5254 and CER 3.98 to 3.89, 34 lines better against 12 worse,
p=0.002; over the 15 labelled images CER goes 4.35 to 4.03 with 8 better and 1
worse. It costs 17% more wall time, about 1.1s a page; VOCR_BEAM=0 turns it off.
The earlier call to leave it off was made on that contaminated baseline, where
the ten-rule chain was itself breaking 45 lines and hiding what beam gained.
Beam and tone correction do not overlap even though both arbitrate tone marks
with the same language model: together they score above beam alone, because
tone correction now only adds a mark where the syllable has none, which is
where CTC saw no stroke and every beam hypothesis is bare too.
alpha stays at 0.30. The 0.10 that wins on pre-cut crops is a wash end to end
(52 better / 46 worse against the current chain), since crops contain none of
the…
docs: separate the compound-word idea from the PMI way of measuring it
PMI computed from this corpus fails, but that does not retire the idea. The
corpus is built from this archive's own labels, so it does not know tue cong or
dinh Quang Ngai are collocations; recomputing statistics over it will not teach
it. The idea needs a compound or gazetteer list from outside.
post: let tone correction only restore a missing mark, never swap one
Over 20 pages the free version fixed 22 lines and broke 8. Every one of the 8
was a swap between two real words: cong to cong with a different mark, hai to
hai, diem to diem, da to da, Thuy to Thuy. Restoring a mark on a syllable that
carries none is a different kind of claim: faint print drops the mark, mostly
the dot below, and more than half the good fixes are dam to dam, huyen to
huyen, truyen to truyen, ruong to ruong.
Restricting to that direction keeps the same accuracy, lowers CER from 3.99 to
3.98, and goes from 22/8 to 14 better and none worse, p 0.016 to 0.0001. Other
restrictions were weaker: allowing the dot below as well gives 14/3, forbidding
only removals 17/5, the dot below alone 13/3.
Collocation PMI as a veto was re-tested at page level and still fails: the two
highest values sit among the lines the rule fixed, not the ones it broke.
docs: record the tone-placement lock and the rejected capitalisation veto
The overview line and the production summary both still listed it, and the
section that used it as the example of re-checking old numbers now carries the
full arc: rejected on v11, adopted on v6 crops, dropped end to end.
post: drop the d-stroke rule, keep only tone correction for v11 rec
The rule was adopted on a line-level number (8 better / 3 worse on pre-cut
crops). End to end it loses: 0 better / 4 worse over 20 pages and 1/1 over the
15 labelled images, and chaining it after tone correction scores below tone
alone. All four lines it broke are the same failure: dinh -> dinh with a stroke,
da -> da with a stroke, trading administrative Sino-Vietnamese words for common
ones because the n-gram always bets on the frequent form, and two of the four
were already correct. All four add a stroke, while the model's own bias is to
add one 72 times against dropping one 34 times.
The benchmarks were also running the wrong chain: they built the pipeline with
textfix=True but never installed fix_text_v11 the way api.py does, so they
measured the full ten-rule chain. VOCR_BENCH_RULES now selects which rules run.
prep: measure whether thickening helps detection or recognition
Thickening the whole page before the pipeline was justified by "most of the gain
comes from detection", which turned out to be only part of the story. Thickening
just the crops, with detection on the untouched page, wins 144 lines to 46 on
its own, so recognition really does read bolder strokes better. It still only
recovers about 38% of what whole-page thickening gets, and eroding twice
overshoots, so the default is unchanged.
The old line-level number said the opposite (239 better / 370 worse). That set's
crops have a median height of 33px against 60px for real pipeline lines, so the
same 2px erosion bites twice as hard there. Scale-sensitive operations measured
on it do not carry over.
Adds env switches for the detection post-processing steps so they can be ablated
one at a time, and a box dedup pass left off: the whole 20-page set contains one
overlapping pair, too rare to validate a rule on.
bench: match predictions to labels by polygon, in deskewed coordinates
run_pages.py matched bounding boxes against labels in the original frame, but
run() deskews before detecting, so boxes came back rotated ~63px off, and a
skewed label's axis-aligned box is three line-heights tall and swallows its
neighbour. One page read nearly right and scored 80% CER. It now rebuilds the
deskew matrix, warps the labels, and intersects polygons.
That flipped the beam verdict at page level: 26 lines better / 13 worse and 12
pages better / 3 worse at alpha 0.30, not the wash the earlier crude protocol
reported. Also recorded that alpha tuned on cropped lines (0.10) is a wash
end-to-end, since cropped lines contain no detection damage.
rec: add beam search + LM rerank for v6, measured, left off
rec_v6 now exposes topk() built from the same probability matrix greedy reads,
so both decoders see one image; ctc_beam_rec.BeamRecognizer cannot be reused
because it pads to the v11 width. The pipeline uses the loaded RecV6Predictor
as its n-best source rather than loading the model twice.
On 3000 test crops this wins clearly (acc 0.6123 -> 0.6367, 275 better / 136
worse) but end-to-end on 20 scanned pages it moves CER by 0.01 point for +20%
time, and it hurts phone photos. Left off; numbers and the alpha/gate sweep are
in docs/PIPELINE.md 4.4. Adds run_pages.py, a 20-page end-to-end benchmark.
det: fix the orientation scorer so rotated pages actually get flipped
The score used the space-fixer vocab, which accepts junk syllables, so rec v11
garbage matched at rate 1.000 and the score collapsed to text length; a
180-flipped read is only ~17% shorter and never cleared the 1.25x margin.
Score with ViLM.lm_score instead (syllable bigrams put correct reads at -1.4
and flipped ones at -4.5), and let the fast-path gate measure the same joined
string it scores. 15/15 rotated cases now read like upright ones (90 deg:
91-100% -> 2.4-5.6% CER); 0 of 15 upright images change. On by default.
prep: thicken strokes (erode 2x2) by default for ppv6_3
First pixel-level preprocessing that wins here: labelled bench 4.58% -> 3.75%
(11 better / 3 worse), 20 held-out book pages 3.55% -> 2.46% (17 / 3). Most of
it is det no longer splitting faint lines. prep=auto picks it only for the v11
rec; ppv6_1 keeps none. The whiten options lose on v11 and leave the UI.
prod: switch default rec to ppv6_3 (v11), keep ppv6_1 as secondary
v11 fixes CTC training on PP-OCRv6: PPLCNetV4 pooled to a fixed 40 steps in
train mode, so no line over ~40 chars ever got CTC loss. Labelled bench:
6.81% (ppv6_1 + rules + BARTpho) -> 4.58%, 9 images better / 5 worse.
The full rule chain and BARTpho lose on v11 output, so ppv6_3 runs only
fix_tone (margin 6) + fix_d_stroke via fix_text_v11; numbers in its docstring.
det: fall back to detection when the small-image shortcut reads nothing
An image 1170x117 came back with no lines at all. run() has a shortcut that
treats a small image as an already-cropped line and hands it straight to
rec, skipping detection — and every piece of thin-strip handling with it.
Here rec read the whole strip as empty, the confidence filter dropped the
empty line, and the user got nothing, while the ordinary detection path
reads the line fine.
Try the shortcut first, and when it returns empty or low confidence, fall
through to detection. That image goes from 0 lines to 0.00% CER. Costs one
wasted rec call, about 35ms.
Also moves the labelled benchmark into the repo at tests/bench/ — it had
been living in a scratch directory and was lost when that was cleaned. 15
images now, each aimed at a different failure: thin strips, faded ink,
show-through, indented verse, a typed letter and a street sign that are both
out of domain. run_bench.py prints per-image wins and losses next to the
combined CER and refuses a change where losses outnumber wins,…
ui: say when an image falls outside what the model was trained on
A typewritten letter reads at 15.79% CER and a street sign at 70.79% — the
sign returns 'DÍCHLCSỬĂHAN' for 'DI TÍCH LỊCH SỬ VĂN HÓA CẤP TỈNH'. Both sit
outside the 995k lines of old printed books the recogniser was tuned on, and
the pipeline's own domain check already spots it (8 of 17 lines on the
letter, 2 of 4 on the sign). That warning only reached the log, so users saw
inexplicable garbage instead of a reason.
The batch response now carries the out-of-domain line count and the page
says so plainly when most of an image is out of domain.
Also checked whether the sign fails because its text is white on red, the
opposite polarity to the training data: inverting the crops changes nothing,
so it is the letterforms, not the polarity. Recorded in docs along with what
would actually fix it.
prod: one model, batch upload with PDF page ranges, and a written record
Three changes asked for once ppv6_1 with BARTpho settled as the version to
keep.
Only ppv6_1 loads now. v4, v11, v11.2 and ppv6_2 came off the Space — they
were there to compare against and the measurements are done, while each one
cost a rec model, a ViLM (70s of startup) and RAM on cpu-basic. Their code
is untouched; adding a repo back to MODEL_REPOS brings one back. The
corrector lives in the v11 repo, so it is now fetched on its own when
VOCR_CONTEXT_FIX is set.
/ocr/batch takes several files at once, images and PDFs mixed. A PDF reads
its pages from ?pages=1-5,8 or 180- and renders them at 200 DPI; empty means
from page one. Twenty pages a call, since each costs 5-15s on CPU, and a
request that asks for more says how many it dropped rather than quietly
truncating. The page picker and multi-file input are wired into the UI; the
version and BARTpho selects are gone, image preprocessing stays because it
is the one option that still changes the answer usefully.
docs/PIPELINE.md rec…