ocrx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

README

ocrx 模块概览

core/ocrx 提供新一代 OCR 管线的基础设施,目标是抽离拆页、缓存、并发、Provider 调用等通用能力,便于在同一框架内扩展常规 OCR、表格 OCR、印章检测与后续自定义策略。

目录结构

  • service.go:面向外部的统一入口,封装 OCR/Table/Stamp 三类服务接口。
  • types.go / model/:统一的数据结构定义,可映射至旧版 core/ocr 的返回值。
  • config/:配置结构体及校验逻辑,支持按管线拆分 Provider、Splitter、Cache、Worker、Retry 等段落。
  • splitter/:拆页策略,内置 Poppler 本地拆页与远程 HTTP splitter;支持分批拆页、并发控制与流式上传。
  • provider/:Provider 接口及工厂注册,默认包含 TextIn、火山引擎、百度 OCR。
  • pipeline/:调度管理器,串联拆页、缓存、Provider 调用、diff 与结果聚合。
  • cache/:缓存构建器,默认提供文件系统缓存。
  • worker/:简单的并发控制器。
  • util/:常用辅助方法(例如缓存 Key 计算)。
  • diff/:为差异化比对预留的占位包。

关键设计

  1. 弹性配置config.ServiceConfig 按 Operation(ocr/table/stamp)划分,每个 Operation 可绑定不同的 Provider/Splitter/Cache/Worker 策略。
  2. 拆页策略替换splitter.Factory 支持通过配置选择 Poppler、远程拆页或自定义实现;Poppler 默认使用 300 DPI,并支持 batch_pages / max_parallel 控制长 PDF 的分批拆页并发度。
  3. 可插拔 Providerprovider.FactoryDependencies 结合,可在构建 Service 时注入不同 Provider;默认实现包含 TextIn 与火山引擎 Volcengine,均封装了鉴权、超时与结构化日志。
  4. 更稳的缓存键cache.Builder 默认返回文件系统实现(BaseDir、TTL 可配置),当 enabled=false 时自动降级为 noop;本地文件缓存键优先使用内容指纹,避免仅靠 mtime/size 造成误命中。
  5. 并发与资源控制worker.Pool 对调用并发进行限流;pipeline.Manager 对多页任务采用 fail-fast cancel,任一页失败会尽早取消同批其它页;splitter.Result 暴露 Cleanup 回调,用于统一清理拆页产生的临时目录。
  6. 可配置重试pipeline.Manager 结合 github.com/cenkalti/backoff/v5 提供指数退避重试策略,通过 config.Retry 控制初始/最大间隔及最大尝试次数,并对每次重试输出详细日志。
  7. 指标采集internal/metrics 集成 Prometheus Counter/Histogram,统计调用次数、重试次数、耗时及错误分类,帮助观察运行状况。
  8. Diff/Fuzzy 能力diff 包提供对 TextIn OCR/Table 原始 JSON 的差异比对,并通过 FuzzyLocateText 调用 internal/fuzzy 的快速模糊匹配,用于定位识别文本中的目标片段。多页 OCR/Table 场景会先合并原始响应再做 diff。
  9. 结构化输出:TextIn OCR 结果同时保留 RecognizedText、更适合规则/审阅的 NormalizedText,以及带 ul/table/td 结构的 HTML,便于 LLM 消费原始文档内容。
  10. 配置加载config.Load 支持从 YAML 文件读取配置;config.DefaultTextInConfig 可基于 TextIn 鉴权信息快速组装默认三条管线。
内置 Provider
kind 适用场景 配置要点
textin 通用 OCR / 表格 / 印章识别 auth.app_idauth.secret_code 必填,可复用 config.DefaultTextInConfig
volcengine 火山引擎通用文字识别(OCRNormal) auth.access_key_idauth.secret_access_key 必填;additional.action/version/region/service 如不设置将分别默认为 OCRNormal2020-08-26cn-north-1cv;支持 options.approximate_pixel/mode/filter_thresh/half_to_full 透传,并内置文本 diff 与模糊定位能力。
baidu 百度 OCR(通用 + 表格) auth.access_tokenauth.api_key/auth.secret_key 二选一;additional.token_url 默认为 https://aip.baidubce.com/oauth/2.0/token;可透传 language_typerecognize_granularityreturn_excelcell_contents 等参数,OCR/表格均已接入并支持文本 diff。

当前仓库只提供 TextIn 的 config/example.yaml。其中凭据均为明显不可用的占位值;请把配置复制到仓库外的受控位置后再填入真实凭据,不要把真实凭据提交到源码仓库。Volcengine、Baidu 暂无随包示例文件,应由 host 根据上表字段显式提供配置。

HTTP 鉴权与重定向边界
  • TextIn 的自定义 headers 不得覆盖 x-ti-app-idx-ti-secret-code;字段名按大小写不敏感处理,发现冲突时配置校验会直接拒绝,鉴权值只由 auth.app_idauth.secret_code 提供。
  • 内置 TextIn、Baidu、Volcengine provider 的默认带凭据 HTTP client 最多跟随 5 次重定向,并要求每一跳与初始请求保持相同 scheme、hostname 和 effective port;未显式填写端口时,HTTP/HTTPS 分别按 80/443 比较。
  • 这项约束只限制默认 provider client 的凭据随重定向跨 origin 传播,不等价于 SSRF 防护、egress allowlist、DNS 解析或重绑定防护、TLS 策略、代理策略、响应体大小或读取预算。Host 自行实现 provider 时,其 HTTP client 必须负责这些策略以及对应验证。

当前建议

  • TextIn 主产物建议优先使用 HTML:对表格、列表和层次化内容保留标签,通常比纯文本更适合后续 LLM 解析。
  • 规则抽取、日志审阅和 diff baseline 建议使用 NormalizedText / NormalizedPageTexts,可读性比原始拼接文本更好。
  • 长 PDF 可以打开 splitter.batch_pages,并配合 splitter.max_parallel 控制拆页吞吐;若环境缺少 pdfinfo,会自动退回单次拆分。
  • 如果接远程 splitter,可通过 splitter.options.timeout 调整 HTTP 超时;上传默认是流式 multipart,不会先把整份文件拼成内存字符串。

命令行示例

命令 说明
go run ./core/ocrx/cmd/ocrxtool -config /absolute/path/ocrx.yaml -mode ocr path/to/file.pdf 通用命令行工具。-config 必须是绝对路径;也可以省略该参数并设置绝对 OCRX_CONFIG_PATH,或使用 TEXTIN_APP_IDTEXTIN_SECRET_CODE 让 OCRX owner 在内存中构造 TextIn 配置
go run ./core/ocrx/cmd/demo -config /absolute/path/ocrx.yaml -provider textin Demo 脚本要求显式绝对 -config 或绝对 OCRX_CONFIG_PATH,读取 core/ocrx/cmd/demo/data 下的样例,结果写入 core/ocrx/cmd/demo/output

ocrxtool 的输出目录当前包含两层产物:

  • *.json:snake_case 的稳定结果快照,适合脚本和回归对比。
  • *_artifacts/raw_files 保留 provider 原始返回,derived_files 则按模式写出 recognized.txtnormalized.txtcombined.html、分页文本/HTML 或印章摘要,便于人工排查。

后续可考虑补充的命令:serve(提供 HTTP 接口)、diff(对比两份识别结果)、watch(监听目录自动识别)等。

快速调用示例

client, err := ocrx.NewClientFromConfigOrEnv("/absolute/path/ocrx.yaml")
if err != nil {
    log.Fatal(err)
}

textRes, _ := client.RecognizeText("foo.pdf")
htmlRes, _ := client.RecognizeHTML("foo.pdf")
tableRes, _ := client.RecognizeTable("bar.pdf")
stampRes, _ := client.RecognizeStamp("stamp.png")

ocrx.WriteText("out/foo.txt", textRes.Text)
ocrx.WriteText("out/foo.normalized.txt", textRes.NormalizedText)
ocrx.WriteText("out/foo.html", htmlRes.HTML)
ocrx.WriteJSONResult("out/table.json", tableRes)
ocrx.WriteJSONResult("out/stamp.json", stampRes)

传入空路径时,NewClientFromConfigOrEnv 只读取 OCRX owner 管理的 OCRX_CONFIG_PATH 或 TextIn 环境凭据;它不会从当前工作目录、最近的 go.mod 或源码仓库中猜测配置。文件路径必须为绝对路径。

通过 TextIn 环境凭据在内存中构造的默认配置不会启用文件缓存,避免把敏感派生数据写入隐式的工作目录。需要缓存时,应由 host 在显式配置文件中选择缓存类型和受控绝对目录。

Documentation

Index

Constants

View Source
const (
	OperationKindAny   = model.OperationKindAny
	OperationKindOCR   = model.OperationKindOCR
	OperationKindTable = model.OperationKindTable
	OperationKindStamp = model.OperationKindStamp
)

Variables

This section is empty.

Functions

func PreviewText

func PreviewText(text string, limit int) string

PreviewText 返回文本预览。

func WriteJSONResult

func WriteJSONResult(path string, v interface{}) error

WriteJSONResult 将结果以 JSON 形式写入文件。

func WriteText

func WriteText(path, content string) error

WriteText 将文本写入文件。

Types

type BaseResult

type BaseResult struct {
	Source string
	Mode   Mode
	Meta   Meta
	Diff   *DiffSummary
}

BaseResult 为快捷结果的通用部分。

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 封装 Service,提供按需的便捷接口。

func NewClientFromConfig

func NewClientFromConfig(path string, opts ...ClientOption) (*Client, error)

NewClientFromConfig 从 YAML 配置构建 Client。

func NewClientFromService

func NewClientFromService(svc *Service, opts ...ClientOption) *Client

NewClientFromService 从现有 Service 构建 Client。

func (*Client) RecognizeDetailed

func (c *Client) RecognizeDetailed(path string, mode Mode, opts ...Option) (BaseResult, interface{}, error)

RecognizeDetailed 返回 BaseResult 和原始 Payload。

func (*Client) RecognizeDirectory

func (c *Client) RecognizeDirectory(root string, mode Mode, opts ...Option) ([]FileResult, error)

RecognizeDirectory 会递归识别目录。

func (*Client) RecognizeFiles

func (c *Client) RecognizeFiles(paths []string, mode Mode, opts ...Option) []FileResult

RecognizeFiles 批量处理文件。

func (*Client) RecognizeHTML

func (c *Client) RecognizeHTML(path string, opts ...Option) (HTMLResult, error)

RecognizeHTML 返回整体 HTML 与按页 HTML。

func (*Client) RecognizeStamp

func (c *Client) RecognizeStamp(path string, opts ...Option) (StampResult, error)

RecognizeStamp 返回印章识别结果。

func (*Client) RecognizeTable

func (c *Client) RecognizeTable(path string, opts ...Option) (TableResult, error)

RecognizeTable 返回表格识别结果。

func (*Client) RecognizeText

func (c *Client) RecognizeText(path string, opts ...Option) (TextResult, error)

RecognizeText 返回拼接文本。

func (*Client) RecognizeTextPages

func (c *Client) RecognizeTextPages(path string, opts ...Option) (PageTextResult, error)

RecognizeTextPages 返回按页文本。

type ClientOption

type ClientOption func(*Client)

ClientOption 配置 Client 的默认行为。

func WithDefaultContext

func WithDefaultContext(ctx context.Context) ClientOption

WithDefaultContext 配置默认 Context。

func WithDefaultDiff

func WithDefaultDiff(enabled bool, baseline string) ClientOption

WithDefaultDiff 配置默认 diff 行为。

func WithDefaultDiffPreview

func WithDefaultDiffPreview(preview int) ClientOption

WithDefaultDiffPreview 配置默认 diff 预览数量。

func WithDefaultMaxPages

func WithDefaultMaxPages(maxPages int) ClientOption

WithDefaultMaxPages sets a default per-request page cap for PDF inputs.

func WithDefaultNeedCharacter

func WithDefaultNeedCharacter(on bool) ClientOption

WithDefaultNeedCharacter 设置默认 NeedCharacter。

type Coordinate

type Coordinate = model.Coordinate

type Dependencies

type Dependencies struct {
	ProviderFactories        map[string]provider.Factory
	SplitterFactories        map[string]splitter.Factory
	CacheBuilder             cache.Builder
	ProcessorFactories       processor.Registry
	ProviderConfigValidators map[string]provider.ConfigValidator
}

Dependencies groups optional factories used to build the Service instance.

type DiffResultSummary

type DiffResultSummary = model.DiffResultSummary

type DiffSummary

type DiffSummary = model.DiffSummary

type FileResult

type FileResult struct {
	BaseResult
	Output interface{}
	Err    error
}

FileResult 用于批量场景。

type HTMLResult

type HTMLResult struct {
	BaseResult
	HTML  string
	Pages []string
}

HTMLResult 返回整体 HTML 及按页 HTML。

type Meta

type Meta = model.Meta

type Mode

type Mode = OperationKind

Mode 是快捷接口使用的识别类型。

const (
	ModeOCR   Mode = OperationKindOCR
	ModeTable Mode = OperationKindTable
	ModeStamp Mode = OperationKindStamp
)

type OCRPayload

type OCRPayload = model.OCRPayload

type OperationKind

type OperationKind = model.OperationKind

type Option

type Option func(*RequestOptions)

Option 覆盖单次请求的设置。

func WithContext

func WithContext(ctx context.Context) Option

func WithDiffEnabled

func WithDiffEnabled(on bool, baseline string) Option

func WithDiffPreview

func WithDiffPreview(preview int) Option

WithDiffPreview 覆盖本次请求的 diff 预览限制。

func WithMaxPages

func WithMaxPages(maxPages int) Option

WithMaxPages limits how many pages a recognition request may process. Values <= 0 leave the service/pipeline default unchanged.

func WithNeedCharacter

func WithNeedCharacter(on bool) Option

Option helpers.

type PageText

type PageText struct {
	Index          int
	Text           string
	NormalizedText string
}

type PageTextResult

type PageTextResult struct {
	BaseResult
	Pages []PageText
}

PageTextResult 返回按页文本。

type Request

type Request = model.Request

type RequestOptions

type RequestOptions struct {
	NeedCharacter *bool
	DiffEnabled   *bool
	DiffBaseline  *string
	DiffPreview   *int
	MaxPages      *int
	Context       context.Context
}

RequestOptions 控制单次请求。

type Response

type Response = model.Response

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service exposes the main OCR, table, and stamp recognition entry points. It acts as a façade coordinating the lower-level pipelines.

func NewService

func NewService(cfg config.ServiceConfig, deps Dependencies) (*Service, error)

NewService constructs a Service from the provided configuration bundle.

func (*Service) RecognizeOCR

func (s *Service) RecognizeOCR(ctx context.Context, req model.Request) (model.Response, error)

RecognizeOCR runs the standard OCR pipeline.

func (*Service) RecognizeStamp

func (s *Service) RecognizeStamp(ctx context.Context, req model.Request) (model.StampResponse, error)

RecognizeStamp runs the stamp detection pipeline.

func (*Service) RecognizeTable

func (s *Service) RecognizeTable(ctx context.Context, req model.Request) (model.TableResponse, error)

RecognizeTable runs the table OCR pipeline.

type StampDetail

type StampDetail = model.StampDetail

type StampPage

type StampPage = model.StampPage

type StampPageResult

type StampPageResult struct {
	Index  int
	Stamps []StampDetail
}

type StampPayload

type StampPayload = model.StampPayload

type StampResponse

type StampResponse = model.StampResponse

type StampResult

type StampResult struct {
	BaseResult
	Pages []StampPageResult
}

StampResult 返回印章识别结果。

type TablePage

type TablePage = model.TablePage

type TablePageResult

type TablePageResult struct {
	Index          int
	Text           string
	NormalizedText string
	HTML           string
	Coordinates    []Coordinate
}

type TablePayload

type TablePayload = model.TablePayload

type TableResponse

type TableResponse = model.TableResponse

type TableResult

type TableResult struct {
	BaseResult
	Pages                  []TablePageResult
	CombinedText           string
	NormalizedCombinedText string
}

TableResult 返回表格识别结果。

type TextBox

type TextBox = model.TextBox

type TextResult

type TextResult struct {
	BaseResult
	Text           string
	NormalizedText string
}

TextResult 返回拼接后的纯文本。

Directories

Path Synopsis
internal
ratio
Package rapidfuzz owns the pure-Go similarity primitive used by OCR diff.
Package rapidfuzz owns the pure-Go similarity primitive used by OCR diff.

Jump to

Keyboard shortcuts

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