Documentation
¶
Overview ¶
Package ocr reads the text of subtitle pictures (a Blu-ray's PGS): a line of text at a time, by a small network trained here (a CRNN: convolutions over the line's picture, a bidirectional LSTM over its columns, CTC to the characters), its weights embedded.
Index ¶
Constants ¶
const (
Height = 32
)
The network: a line's picture, Height rows of any width (a multiple of 4), in; per 4 columns, how likely each class is, out.
conv 3x3 1->16, ReLU, max pool 2x2 32 x W -> 16 x W/2 conv 3x3 16->32, ReLU, max pool 2x2 16 x W/2 -> 8 x W/4 conv 3x3 32->64, ReLU, max pool 2x1 8 x W/4 -> 4 x W/4 conv 3x3 64->64, ReLU, max pool 2x1 4 x W/4 -> 2 x W/4 a column (64 x 2 = 128 values) a step: a bidirectional LSTM, 96 each way, then a linear layer to the classes
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
Encode turns a text into its classes, leaving out what the charset cannot say; ok is false when something was left out.
func Fix ¶
Fix corrects what the pictures cannot tell apart: a capital I from a small l in sans-serif faces. An l in a word of capitals, alone, or before an apostrophe at a word's start (l'm, l'll) is an I, and so is the l of a word that is none with it (lt's, lf).
func LineHeight ¶
LineHeight is a track's line height: the most usual of its lines' heights (a line of only small letters, or a dash, is shorter).
Types ¶
type Bitmap ¶
Bitmap is a subtitle picture: per pixel, its brightness and how opaque it is (0 to 1).
func NewBitmap ¶
NewBitmap is a subtitle picture from its pixels' luma (limited range, as PGS palettes give it) and alpha: the ink each pixel's brightness as much as it is opaque.
func (*Bitmap) Image ¶
Image is a line ready for the network: its ink scaled so that the track's line height is lineFill, centred, a margin each side, the width a multiple of 4.
type Line ¶
type Line struct {
// Top, Bottom, Left and Right are its bounds in the picture.
Top, Bottom, Left, Right int
}
Line is a line of a subtitle, cut from its picture.
type Model ¶
type Model struct {
// Conv[i] holds layer i's weights (out x in*9), then its biases.
Conv [][]float32
// LSTM[d] is a direction's (0 forward, 1 backward): the input weights
// (4*hidden x features), the recurrent ones (4*hidden x hidden), the
// biases (4*hidden); the gates in the order i, f, g, o.
LSTM [2][]float32
// Out is the linear layer: weights (classes x 2*hidden), then biases.
Out []float32
}
Model is the network's weights.
func NewModel ¶
NewModel is a model with random weights (He's for the convolutions, Glorot's for the rest; the LSTM's forget gates biased open).
func (*Model) Loss ¶
Loss is the loss of a sample, for measuring; ok is false when the text cannot fit.
func (*Model) ReadTrack ¶
ReadTrack reads a track's pictures: their text, lines apart by newlines, an italic picture's lines in SRT's <i> ("" for a picture with none). The track's line height comes from all its pictures; the lines are read on every CPU.
type Sample ¶
Sample is a line to learn from: its picture (Height x W, values 0 to 1, the text bright on dark) and its text.