ocr

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package ocr 提供 OCR 引擎的统一接口与结果标准化。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinWords

func JoinWords(words []string) string

JoinWords 合并同一行的 OCR 单词。汉字相邻 token 不插空格,拉丁字母/数字间插空格。

Types

type Engine

type Engine interface {
	Recognize(ctx context.Context, image image.Image) ([]TextBlock, error)
}

Engine 是 OCR 后端接口。实现必须尊重 ctx 的取消信号。

type HTTPClient

type HTTPClient struct {
	Endpoint         string
	APIKey           string
	Language         string
	Client           *http.Client
	MaxResponseBytes int64
}

HTTPClient 把图片发送到实现 OFD 通用 OCR HTTP 协议的服务。 请求体是 PNG,X-OCR-Language 指定语言;响应 JSON 的 blocks 使用左上角原点像素坐标。

func NewHTTPClient

func NewHTTPClient(endpoint, apiKey, language string) *HTTPClient

NewHTTPClient 创建远程 OCR 引擎。

func (*HTTPClient) Recognize

func (h *HTTPClient) Recognize(ctx context.Context, input image.Image) ([]TextBlock, error)

Recognize 将 PNG 图片发送至远程服务并解析文字块结果。

type Tesseract

type Tesseract struct {
	Path     string
	Language string
	PSM      int
	TempDir  string
}

Tesseract 调用系统中的 tesseract 命令行程序。

func NewTesseract

func NewTesseract(path, language string) *Tesseract

NewTesseract 创建 Tesseract OCR 引擎。

func (*Tesseract) Recognize

func (t *Tesseract) Recognize(ctx context.Context, input image.Image) ([]TextBlock, error)

Recognize 执行 OCR,并把 TSV 单词结果聚合为文字行。

type TextBlock

type TextBlock struct {
	Text       string
	Bounds     image.Rectangle
	Confidence float64
}

TextBlock 是 OCR 返回的一行文字。Bounds 使用图片像素坐标,原点在左上角。

func Normalize

func Normalize(blocks []TextBlock, bounds image.Rectangle) []TextBlock

Normalize 清理 OCR 结果并按从上到下、从左到右稳定排序。

func ParseTSV

func ParseTSV(input io.Reader) ([]TextBlock, error)

ParseTSV 解析 Tesseract TSV,仅保留 word 层并按 page/block/paragraph/line 聚合。

Jump to

Keyboard shortcuts

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