Documentation
¶
Overview ¶
Package engine runs text detection, line orientation and text recognition models described by a manifest, on a GoMLX backend.
A manifest composes reusable building blocks (image preprocessing, a detector post-processor, a decoder) around a model family's ONNX files, so a new family needs a manifest, not Go code.
Index ¶
Constants ¶
const ManifestFile = "manifest.json"
ManifestFile is the manifest's file name inside a model directory.
const ManifestSchema = 1
ManifestSchema is the manifest format version this package reads.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Charset ¶
type Charset struct {
// YAML reads a list under Key from a YAML file (for example PaddleOCR's
// inference.yml, key "character_dict").
YAML string `json:"yaml,omitempty"`
Key string `json:"key,omitempty"`
// File reads one character per line.
File string `json:"file,omitempty"`
// JSON reads a string under Key from a JSON object (for example docTR's
// config.json, key "vocab"); each character is one class.
JSON string `json:"json,omitempty"`
}
Charset is the recognizer's character list, one class per character.
type Classifier ¶
type Classifier struct {
Model string `json:"model"`
Input Input `json:"input"`
// Height and Width are the fixed input size.
Height int `json:"height"`
Width int `json:"width"`
// Angles lists the rotation, in degrees, each class stands for.
Angles []int `json:"angles"`
Threshold float64 `json:"threshold"`
// Batches are batch-size buckets; lines are grouped greedily into the
// largest bucket that fits (default [1]).
Batches []int `json:"batches,omitempty"`
}
Classifier decides whether a text line is upside down.
type Decoder ¶
type Decoder struct {
// Type is "ctc" (per-step classes, repeats merged, blanks dropped) or
// "attention" (one class per output position until the end token).
Type string `json:"type"`
// Blank is the CTC blank class index; characters take the other
// indices in order. -1 means the class after the characters.
Blank int `json:"blank"`
// EOS is the attention decoder's end-of-sequence class; -1 means the
// class after the characters.
EOS int `json:"eos,omitempty"`
// Softmax turns raw logits into probabilities before decoding.
Softmax bool `json:"softmax,omitempty"`
// Confidence is the mean ("mean", default) or the minimum ("min") of
// the kept characters' probabilities.
Confidence string `json:"confidence,omitempty"`
// AppendSpace adds a space class after the character list.
AppendSpace bool `json:"append_space,omitempty"`
}
Decoder turns recognizer outputs into text.
type Detector ¶
type Detector struct {
Model string `json:"model"`
Input Input `json:"input"`
// LimitSide bounds the input: its longer side (limit_type "max") or its
// shorter side ("min") is scaled to LimitSide; "fit" scales the image,
// up or down, to fit a LimitSide square keeping its aspect ratio.
LimitSide int `json:"limit_side"`
LimitType string `json:"limit_type,omitempty"`
// Pad places the image at the top-left of its bucket ("end", default)
// or centres it ("center"); PadValue is the padding gray level (default
// 255, white).
Pad string `json:"pad,omitempty"`
PadValue *int `json:"pad_value,omitempty"`
// Multiple rounds input sides (32 for DB-style detectors).
Multiple int `json:"multiple,omitempty"`
// Buckets are input sizes [height, width] compiled once per session;
// inputs are padded to the smallest bucket that holds them.
Buckets [][2]int `json:"buckets,omitempty"`
PostProcess PostProcess `json:"postprocess"`
}
Detector finds text regions.
type Engine ¶
type Engine struct {
Manifest *Manifest
// contains filtered or unexported fields
}
Engine runs one model. It is not safe for concurrent use.
func Load ¶
Load reads the manifest in dir, its character list and its models, and prepares them for backend.
func (*Engine) Denormals ¶
Denormals counts subnormal weights left in the loaded models (0 after the load-time flush).
func (*Engine) FellBack ¶
FellBack lists the models that moved to the pure-Go backend because the selected backend cannot build their graphs.
type Input ¶
type Input struct {
// Color is "bgr" (default) or "rgb".
Color string `json:"color,omitempty"`
Scale float64 `json:"scale"`
Mean []float64 `json:"mean"`
Std []float64 `json:"std"`
}
Input is how an image becomes a model input: channel order, then value*scale, then (value-mean)/std per channel, laid out NCHW.
type Manifest ¶
type Manifest struct {
Schema int `json:"schema"`
ID string `json:"id"`
Family string `json:"family"`
Languages []string `json:"languages"`
License string `json:"license"`
// OutputOrder is "logical" (default) when the recognizer emits text in
// reading order, "visual" when it emits right-to-left scripts in display
// order and needs bidi reordering.
OutputOrder string `json:"output_order,omitempty"`
Detector Detector `json:"detector"`
Classifier *Classifier `json:"classifier,omitempty"`
Recognizer Recognizer `json:"recognizer"`
}
Manifest describes one OCR model: a detector, an optional line orientation classifier and a recognizer. Paths are relative to the model directory.
func LoadManifest ¶
LoadManifest reads and validates dir/manifest.json.
type PostProcess ¶
type PostProcess struct {
// Type is "db" (differentiable binarization probability map).
Type string `json:"type"`
Thresh float64 `json:"thresh"`
BoxThresh float64 `json:"box_thresh"`
UnclipRatio float64 `json:"unclip_ratio"`
MaxCandidates int `json:"max_candidates,omitempty"`
MinSize float64 `json:"min_size,omitempty"`
// Activation is applied to the raw output first: "" (already a
// probability map) or "sigmoid" (logits).
Activation string `json:"activation,omitempty"`
// Boxes are "rotated" (minimum-area rectangles, default) or "straight"
// (axis-aligned).
Boxes string `json:"boxes,omitempty"`
}
PostProcess turns a detector output into text boxes.
type Recognizer ¶
type Recognizer struct {
Model string `json:"model"`
Input Input `json:"input"`
// Height is the fixed input height; widths follow the aspect ratio
// unless Width fixes them.
Height int `json:"height"`
// Width, when set, fixes the input width: lines are resized keeping
// their aspect ratio (squeezed if wider) and padded at the end.
Width int `json:"width,omitempty"`
// PadValue is the gray level of padding in pixel space; unset pads with
// 0 after normalization.
PadValue *int `json:"pad_value,omitempty"`
MinWidth int `json:"min_width,omitempty"`
MaxWidth int `json:"max_width,omitempty"`
// Widths are ascending width buckets compiled once per session; wider
// lines use the next multiple of the first bucket.
Widths []int `json:"widths,omitempty"`
// Batches are batch-size buckets; lines of similar width are grouped
// greedily into the largest bucket that fits (default [1]).
Batches []int `json:"batches,omitempty"`
Charset Charset `json:"charset"`
Decoder Decoder `json:"decoder"`
}
Recognizer reads the text of one line image.