tetrak_ocr.backends.tuning¶
Auto-configuration bands for the Tesseract backend.
analyse_image() picks a contrast factor and a page segmentation mode per image from two cheap pixel statistics. This module holds the thresholds that mapping uses, as data rather than as a chain of if/elif, so that they can be fitted, published and tested without importing the backend.
Standard library only, deliberately. Nothing here imports Pillow, pytesseract or tesseract.py. That is what lets the fast test suite, the band fitter under evaluation/ocr/calibration/ and the API documentation generator all import it with no OCR stack installed.
The bands are a fit, not a set of opinions, and FIT_PROVENANCE records which corpus produced them and how well it generalised.
The three blocks below are generated. They come from python -m evaluation.ocr.calibration.fit –write, which chooses the edges by minimising mean regret over the committed configuration sweep. Editing the numbers by hand makes the module disagree with the sweep it claims to come from, and fit.py –check will say so. Re-fit instead — see brief 007 (the calibration toolkit).
Reading the bands¶
Each band is an upper bound and the value to use below it. Bounds are exclusive, matching the < comparisons these replaced, and the last band in each family is terminated by math.inf so every input lands somewhere. Given Band(30.0, 3.5) followed by Band(55.0, 3.0):
stddev 29.9 -> contrast 3.5 (below the first bound, 30) stddev 30.0 -> contrast 3.0 (30 is not < 30)
Those two bounds are an illustration. The fitted ones are below, and they move.
Functions
|
Return the bands as rows, for documentation and diagnostics. |
|
Return the value of the first band whose upper bound value is below. |
|
Return the (contrast, psm) pair for a pair of measured image statistics. |
Classes
|
One threshold in a band family: use value while below upper. |