engine

package
v0.0.0-...-d0a3c32 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 25 Imported by: ? 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 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

View Source
const ManifestFile = "manifest.json"

ManifestFile is the manifest's file name inside a model directory.

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

func Load(dir string, backend compute.Backend) (*Engine, error)

Load reads the manifest in dir, its character list and its models, and prepares them for backend.

func (*Engine) Builds

func (e *Engine) Builds() int64

Builds counts graph compilations so far, one per model and input shape.

func (*Engine) Close

func (e *Engine) Close()

Close releases the compiled models.

func (*Engine) Denormals

func (e *Engine) Denormals() int

Denormals counts subnormal weights left in the loaded models (0 after the load-time flush).

func (*Engine) FellBack

func (e *Engine) FellBack() []string

FellBack lists the models that moved to the pure-Go backend because the selected backend cannot build their graphs.

func (*Engine) Flushed

func (e *Engine) Flushed() int

Flushed counts the subnormal weights zeroed at load.

func (*Engine) Recognize

func (e *Engine) Recognize(img image.Image) ([]Line, error)

Recognize finds and reads the text lines of img, in reading order.

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 Line

type Line struct {
	Text       string
	X0, Y0     float64
	X1, Y1     float64
	Confidence float64
}

Line is one recognized text line; Box is in image pixels (top-left origin, y down).

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

func LoadManifest(dir string) (*Manifest, error)

LoadManifest reads and validates dir/manifest.json.

func (*Manifest) Files

func (m *Manifest) Files() []string

Files lists the files the manifest refers to, relative to the model directory.

func (*Manifest) Validate

func (m *Manifest) Validate() error

Validate checks the fields the engine relies on.

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.

Jump to

Keyboard shortcuts

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