File size: 2,776 Bytes
7c02b2e
 
 
 
5d4ecd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aff6029
5d4ecd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
license: mit
pipeline_tag: image-to-text
---
<h1 align="center">
    <img width="100%" src="./assets/logo.png" alt="">
</h1>

<p align="center">
   <a href="https://github.com/getcharzp/go-ocr/fork" target="blank">
      <img src="https://img.shields.io/github/forks/getcharzp/go-ocr?style=for-the-badge" alt="go-ocr forks"/>
   </a>
   <a href="https://github.com/getcharzp/go-ocr/stargazers" target="blank">
      <img src="https://img.shields.io/github/stars/getcharzp/go-ocr?style=for-the-badge" alt="go-ocr stars"/>
   </a>
   <a href="https://github.com/getcharzp/go-ocr/pulls" target="blank">
      <img src="https://img.shields.io/github/issues-pr/getcharzp/go-ocr?style=for-the-badge" alt="go-ocr pull-requests"/>
   </a>
   <a href='https://github.com/getcharzp/go-ocr/releases'>
      <img src='https://img.shields.io/github/release/getcharzp/go-ocr?&label=Latest&style=for-the-badge'>
   </a>
</p>

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

## 安装

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

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

## 快速开始

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

```go
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)
	}
}
```

## 示例效果

| 原图                                                  | 检测结果                                               |
|-----------------------------------------------------|----------------------------------------------------|
| <img width="100%" src="./examples/test.jpg" alt=""> | <img width="100%" src="./examples/det.jpg" alt=""> |