Elamur OCR0.1 preview

How hanocr reads a page

hanocr reads a page in a few plain steps, each measured on its own: it finds the columns and cuts out the characters, compares each character's lines with exemplars from the same book, lets a model of the language choose among close shapes, says how sure it is, and learns again from what it is sure of. No neural network is involved; every step below is plain code, measured on pages it never learned from.

The steps, in order
  1. frame and columns
  2. characters cut
  3. 100 candidates by stroke direction
  4. elastic matching
  5. language model
  6. calibrated confidence
  7. self-training

Finding the characters

A page of this book is a frame with ten columns between ruled lines. hanocr first finds the frame, then the columns as an even grid: rules and the gaps between columns repeat at one step, so the grid is fitted to the whole page and each rule is then refined in place, within a sixth of a step. The grid is scored two ways, by the ink on the rules and by the gaps, where the text in the middle of a column is dark and the rule's neighbourhood is light; this cut the false grids from 34 to 15 of the 190 pages.

Inside a column, characters are cut where the ink is thinnest, between one character and the next. What is not a character is taken away first: the ends of the rules, the dashes of a rule that broke in the block, the inner line of the frame, and specks of ink at the ends of a box. Commentary is printed in two small columns inside a column; hanocr reads those as notes, right before left.

A piece of page 100: the first characters of its three right-hand columns, each in the box hanocr cut, coloured by how sure it is (as on every page of the book).

The column at the edge

Many scans cut the outermost column at the edge of the image: the rule beyond it is missing, so the grid saw nine columns. hanocr now accepts a column the edge has cut, when the grid's step says one more column fits. Pages where all ten columns were found rose from 62 to 161 of 190, and the characters that get a box of their own from 86.9 % to 94.9 %.

edge of the scanthe tenth column, by the step
The scan cut the outermost column and the rule beyond it: the grid's even step says one more column fits, and hanocr reads it.

Exemplars from the book itself

The proofread pages on Wikisource are hanocr's teacher. Their text is laid onto the boxes cut from the same page by dynamic programming, in several rounds, each with the exemplars of the round before. Every matched box becomes an exemplar: this character, as this carver cut it.

Each box is reduced to the directions of its strokes in a 6 × 6 grid of cells with eight directions each, and a coarse map of its ink. Those features pick the 100 most likely characters for every box.

Elastic matching

Two prints of one character are never quite alike: the carver's knife wandered, the ink spread, the paper moved. So each of the 100 candidates is compared again, elastically: every point of a line may move up to 2 pixels (of 32) to find the nearest point of the exemplar with a similar neighbourhood of stroke directions, both ways. A stroke a little lower or a little longer stays the same stroke; a missing stroke still costs. This step alone raised the test score from 69.6 % to 74.7 %.

exemplarthe box readwithin 2 px
Every point of a line may move a little, up to 2 pixels of 32, to meet a point of the exemplar whose strokes run the same way: the bar a little lower is still the same bar.

The language model

Shapes alone cannot tell some characters apart, and the text can: a model of how characters follow each other, built from other proofread volumes of the same edition and from the 資治通鑑, chooses among the candidates of a whole page at once (the Viterbi algorithm). The test pages are left out of the model: the first, higher score was found to have leaked them, since the series the model came from includes this very volume. Trigrams and Kneser–Ney smoothing changed nothing, because the model's texts are far from this book; bigrams do the work.

Confidence, calibrated

For every character, hanocr gives the probability that it is right, the posterior of the reading. On its own that probability is too sure of itself, so it is calibrated on the dev pages 60–69, which take no part in training. On the test pages the mean confidence was 78.0 % with 80.2 % right: slightly too modest. The characters read with a confidence of 0.8 or more, two thirds of all, were right 96.8 % of the time.

under 0.513.1 %
0.5 to 0.85.7 %
0.8 to 0.94.0 %
0.9 to 0.9914.6 %
0.99 or more62.6 %
How sure hanocr is of the 17 015 characters it read on pages 89–196: the share of them in each band of confidence. Under 0.8 they are marked on every page.

Self-training

Once the proofread pages are learnt, hanocr reads the 108 pages nobody has proofread. Every character read there with a confidence of 0.9 or more becomes an exemplar too, and the pages are read again; two rounds. The last reading added 10,482 such exemplars to the book's own, and the test score rose from 80.5 % to 81.5 %.

Characters built from parts

About 4,100 characters the language model knows never appear on the proofread pages, so hanocr has no exemplar of them. Many can be built from the book's own parts: the Ideographic Description Sequences (IDS) say how a character is composed — left and right, top and bottom, or three in a row either way — and the parts can be cut from characters the book has. 攻 is 工 on the left and 攵 on the right: hanocr takes the left part of the book's 功 and the right part of its 故, each from a character where it stands in the same place, cuts where the ink is thinnest, and leaves both where they are, so the carver's proportions stay. Among the characters with no exemplar, the right ones rose from 2.9 % to 11.2 % on the test pages.

功, page 179: 工 on the left
故, page 194: 攵 on the right
攻, built from the two
攻 as cut, page 188
Parts cut from the book's own characters and set side by side. hanocr cuts where the ink is thinnest; here, for the picture, at the middle of the box.

The IDS data is cjkvi-ids (from the CHISE project), under the GPLv2; hanocr uses it as data and keeps it outside its code.

What we learned

More exemplars help most. With the old segmentation, training on pages 7–24 gave 60.8 %, 7–39 gave 67.7 %, 7–54 gave 72.6 % and 7–69 gave 75.3 %. Every proofread page someone adds on Wikisource makes the next reading better.

The share right counts characters that got a box. Of all characters in the text, before self-training, the test pages went from 64.2 % before the columns were fixed to 71.5 % and then 76.8 %. What is left: 4.2 % of the characters are lost by the cutting, 2.1 % sit in a box of odd height (mostly two characters in one, such as 一 with the next), and 16.9 % are misread in an ordinary box.

Of the 562 misread characters on the test pages (without self-training), 36 % have no exemplar in the book, 17 % differ from the right one in a single part of the same composition, and most of the rest are near-twins that differ by a dot or a short stroke: 玉 read as 王, 入 as 人, 日 as 自, 民 as 氏, 矢 as 夫, 戚 as 成. None were variant forms. The book shows 王 149 times but 玉 once, and 矢 and 戚 never: a rule learnt from the book has nothing to learn these from.

Some steps did not help, and they are kept in the results for that reason: filtering candidates by stroke count, a mask for rules inside a column, a finer elastic grid, fonts thickened to the book's stroke, and a second stage that compared near-twins by the ends, dots and junctions of their skeletons. That last one fixed some pairs (玉 and 王) and broke others: the skeleton of a woodblock print grows spurs a dot cannot be told from.

This page in Markdown https://md.ocr.elamur.ai/free/en/method/