ocr

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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 Decode

func Decode(labels []int) string

Decode turns classes back into text.

func Encode

func Encode(text string) (labels []int, ok bool)

Encode turns a text into its classes, leaving out what the charset cannot say; ok is false when something was left out.

func Fix

func Fix(line string) string

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

func LineHeight(heights []int) float64

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

type Bitmap struct {
	W, H         int
	Ink, Opacity []float32
}

Bitmap is a subtitle picture: per pixel, its brightness and how opaque it is (0 to 1).

func NewBitmap

func NewBitmap(w, h int, luma, alpha []byte) *Bitmap

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

func (b *Bitmap) Image(l Line, lineHeight float64) ([]float32, int)

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.

func (*Bitmap) Italic

func (b *Bitmap) Italic(lines []Line) bool

Italic reports whether a picture's lines lean as italics do.

func (*Bitmap) Lines

func (b *Bitmap) Lines(lineHeight float64) []Line

Lines finds a picture's lines. With the track's line height (0 when it is not known yet), a band much taller than a line is two lines touching, cut apart.

func (*Bitmap) Slant

func (b *Bitmap) Slant(l Line) float64

Slant is how far a line leans, as the shear (columns per row, up to the right) that makes its strokes most upright: near 0 for upright faces, about 0.2 for italics.

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.

func (Line) Height

func (l Line) Height() int

Height of a line, in the picture's pixels.

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 Default

func Default() (*Model, error)

Default is the model trained here, embedded.

func Load

func Load(r io.Reader) (*Model, error)

Load reads weights Save wrote.

func NewModel

func NewModel(seed uint64) *Model

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

func (m *Model) Loss(s Sample) (float64, bool)

Loss is the loss of a sample, for measuring; ok is false when the text cannot fit.

func (*Model) ReadTrack

func (m *Model) ReadTrack(pics []*Bitmap) []string

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.

func (*Model) Recognise

func (m *Model) Recognise(img []float32, w int) string

Recognise reads a line's picture.

func (*Model) Save

func (m *Model) Save(w io.Writer) error

Save writes the weights as half-precision floats.

type Sample

type Sample struct {
	Img  []float32
	W    int
	Text string
}

Sample is a line to learn from: its picture (Height x W, values 0 to 1, the text bright on dark) and its text.

type Trainer

type Trainer struct {
	M  *Model
	LR float64

	ClipNorm float64 // the gradient's largest norm, 0 for none
	// contains filtered or unexported fields
}

Trainer fits a model to samples with Adam.

func NewTrainer

func NewTrainer(m *Model, lr float64) *Trainer

NewTrainer starts training m.

func (*Trainer) Step

func (tr *Trainer) Step(batch []Sample) (float64, int)

Step learns from a batch, returning its mean loss and how many samples counted (a text too long for its picture does not).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL