ocr

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 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 recognises text in images on the device: no network, no account. It's what a scanner needs for searchable PDFs, copying text off a page and searching inside documents.

res, err := ocr.Recognize(ctx, page, ocr.Options{})
fmt.Println(res.Text)
for _, l := range res.Lines {
	for _, w := range l.Words {
		pdf.InvisibleText(w.Text, w.Box) // a searchable layer
	}
}

iOS and macOS use Apple's Vision framework; Android uses Google's ML Kit text recognition, bundled into the app (about 4 MB per CPU architecture), so it works offline and without Google Play services. ML Kit's bundled model reads Latin scripts; Vision also reads Chinese, Japanese, Korean, Cyrillic and more (see Options.Languages). Windows uses Windows.Media.Ocr, in the languages whose OCR packs are installed; Linux uses Tesseract, loaded at run time with the user's installed languages (the tesseract-ocr packages), Supported reporting whether it's there. Elsewhere Recognize returns giode.ErrNotSupported.

Android builds need the giode CLI, which adds ML Kit to the app from this package's android_dependencies.txt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Supported

func Supported() bool

Supported reports whether this platform can recognise text.

Types

type Line

type Line struct {
	Text string
	// Box is the line's bounds in the image's pixels, top left origin.
	Box image.Rectangle
	// Confidence is from 0 to 1; 0 when the platform doesn't say.
	Confidence float32
	Words      []Word
}

Line is a line of text.

type Options

type Options struct {
	// Languages are BCP 47 tags to read, most likely first: "en-US",
	// "de-DE", "zh-Hans". Empty reads the platform's default (English,
	// and other Latin-script languages). Vision only: ML Kit's bundled
	// model reads Latin scripts whatever is asked.
	Languages []string
	// Fast trades accuracy for speed (Vision's fast level), for live
	// recognition on camera frames.
	Fast bool
}

Options configure recognition.

type Result

type Result struct {
	// Text is every line, in reading order, separated by newlines.
	Text  string
	Lines []Line
}

Result is the text found in an image.

func Recognize

func Recognize(ctx context.Context, img image.Image, opts Options) (Result, error)

Recognize finds the text in img.

func RecognizeData

func RecognizeData(ctx context.Context, data []byte, opts Options) (Result, error)

RecognizeData finds the text in an encoded image: JPEG or PNG. Boxes are in its pixels as stored, ignoring any EXIF orientation.

type Word

type Word struct {
	Text string
	Box  image.Rectangle
}

Word is a word of a line.

Jump to

Keyboard shortcuts

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