ocr

package module
v0.0.0-...-15e83dd Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 14 Imported by: 0

README


license: mit pipeline_tag: image-to-text

logo

go-ocr forks go-ocr stars go-ocr pull-requests

go-ocr 是一款基于 Golang + ONNX 构建的 OCR 工具库,专注于为 Go 生态提供简单易用、可扩展的文字识别能力。 目前已完成与 PaddleOCR、DdddOCR 的对接,支持快速实现图像文字检测与识别。

安装

# 下载包
go get -u github.com/getcharzp/go-ocr

# 下载模型 + 动态链接库
git clone https://huggingface.co/getcharzp/go-ocr

快速开始

PaddleOCR

示例代码

通过 OCR 引擎的 RunOCR() 方法能直接进行完整的检测与识别,也可以通过 RunDetect()RunRecognize() 分别进行检测与识别。

package main

import (
	ocr "github.com/getcharzp/go-ocr"
	"github.com/up-zero/gotool/imageutil"
	"log"
)

func main() {
	// 按实际情况配置下述路径
	config := ocr.Config{
		OnnxRuntimeLibPath: "./lib/onnxruntime_amd64.so",
		DetModelPath:       "./paddle_weights/det.onnx",
		RecModelPath:       "./paddle_weights/rec.onnx",
		DictPath:           "./paddle_weights/dict.txt",
	}

	// 初始化引擎
	var engine ocr.Engine
	engine, err := ocr.NewPaddleOcrEngine(config)
	if err != nil {
		log.Fatalf("创建 OCR 引擎失败: %v\n", err)
	}
	defer engine.Destroy()

	// 打开图像
	imagePath := "./test.jpg"
	img, err := imageutil.Open(imagePath)
	if err != nil {
		log.Fatalf("加载图像失败: %v\n", err)
	}

	// OCR识别
	results, err := engine.RunOCR(img)
	if err != nil {
		log.Fatalf("运行 OCR 失败: %v\n", err)
	}
	for _, result := range results {
		log.Printf("识别结果: %v\n", result)
	}
}

示例效果

原图 检测结果

DdddOCR

示例代码

package main

import (
	"github.com/getcharzp/go-ocr/ddddocr"
	"github.com/up-zero/gotool/imageutil"
	"image"
	"image/color"
	"image/draw"
	"log"
)

func main() {
	config := ddddocr.Config{
		OnnxRuntimeLibPath: "../lib/onnxruntime.dll",
		DetModelPath:       "../ddddocr_weights/common_det.onnx",
	}

	engine, err := ddddocr.NewEngine(config)
	if err != nil {
		log.Fatalf("创建 OCR 引擎失败: %v\n", err)
	}
	defer engine.Destroy()

	imagePath := "./captcha_det.png"
	img, err := imageutil.Open(imagePath)
	if err != nil {
		log.Fatalf("加载图像失败: %v\n", err)
	}

	boxes, err := engine.Detect(img)
	if err != nil {
		log.Fatalf("运行检测失败: %v\n", err)
	}

	tagImg := image.NewRGBA(img.Bounds())
	draw.Draw(tagImg, img.Bounds(), img, image.Point{}, draw.Src)

	for _, box := range boxes {
		imageutil.DrawThickRectOutline(tagImg, image.Rectangle{Min: image.Point{X: box.Box[0], Y: box.Box[1]},
			Max: image.Point{X: box.Box[2], Y: box.Box[3]}}, color.Black, 2)
	}
	imageutil.Save("captcha_det_result.png", tagImg, 100)
}

示例效果

原图 检测结果

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLibraryPath

func DefaultLibraryPath() string

DefaultLibraryPath 根据运行时环境判断加载哪个库文件

func DrawBoxes

func DrawBoxes(img image.Image, boxes [][4]int) image.Image

DrawBoxes 在图像上绘制检测区域

Types

type Config

type Config struct {
	// 必填参数
	OnnxRuntimeLibPath string // onnxruntime.dll (或 .so, .dylib) 的路径
	DetModelPath       string // det.onnx (检测模型) 的路径
	RecModelPath       string // rec.onnx (识别模型) 的路径
	DictPath           string // dict.txt (字典) 的路径

	// 可选参数
	UseCuda             bool    // (可选) 是否启用 CUDA
	NumThreads          int     // (可选) ONNX 线程数, 默认由CPU核心数决定
	DetMaxSideLen       int     // (可选) 检测模型预处理的最长边, 默认 960
	DetOutsideExpandPix int     // (可选) 检测框外扩像素, 默认 10
	RecHeight           int     // (可选) 识别模型预处理的高度, 默认 48
	RecModelNumClasses  int64   // (可选) 识别模型类别数, 默认 18385
	HeatmapThreshold    float32 // (可选) 检测热力图阈值, 默认 0.3
}

Config OCR 引擎的开放配置

type Engine

type Engine interface {
	// RunDetect 图像文字区域检测
	RunDetect(img image.Image) ([][4]int, error)

	// RunRecognize 识别图像中指定区域的文字
	RunRecognize(img image.Image, box [4]int) (RecResult, error)

	// RunOCR 对图像执行检测和识别
	RunOCR(img image.Image) ([]RecResult, error)

	// Destroy 释放所有引擎相关的资源
	Destroy()
}

Engine 定义了 OCR 引擎必须实现的通用接口

type PaddleOcrEngine

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

PaddleOcrEngine 是 PaddleOCR 引擎的主结构体

func NewPaddleOcrEngine

func NewPaddleOcrEngine(cfg Config) (*PaddleOcrEngine, error)

NewPaddleOcrEngine 用于初始化 ONNX Runtime、加载模型和字典。

func (*PaddleOcrEngine) Destroy

func (e *PaddleOcrEngine) Destroy()

Destroy 释放所有 C++ ONNX 资源

func (*PaddleOcrEngine) RunDetect

func (e *PaddleOcrEngine) RunDetect(img image.Image) ([][4]int, error)

RunDetect 图像文字区域检测

func (*PaddleOcrEngine) RunOCR

func (e *PaddleOcrEngine) RunOCR(img image.Image) ([]RecResult, error)

RunOCR 对图像执行检测和识别 核心优化:并发执行识别

func (*PaddleOcrEngine) RunRecognize

func (e *PaddleOcrEngine) RunRecognize(img image.Image, box [4]int) (RecResult, error)

RunRecognize 识别图像中指定区域的文字

type RecResult

type RecResult struct {
	Box   [4]int  // [x1, y1, x2, y2]
	Text  string  // 识别的文本
	Score float32 // 平均置信度
}

RecResult OCR 识别结果结构体

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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