必须先在本机装好 Tesseract 原生库,否则 cgo 编译失败;Linux 需额外装 tesseract-ocr-eng 等语言数据包才能识别对应语言;Windows 需 vcpkg 加 MinGW-w64 手工生成导入库并设置 TESSDATA_PREFIX 等环境变量。
gosseract
otiai10MIT更新放缓Go 语言封装的 Tesseract OCR 库,让 Go 程序直接调用 C++ 版 Tesseract 做图片文字识别,省去自己写 CGO 绑定。
Go package for OCR (Optical Character Recognition), by using Tesseract C++ library
brew install tesseract && go get -t github.com/otiai10/gosseract/v2(macOS);Linux 用 sudo apt-get install -y libtesseract-dev libleptonica-dev tesseract-ocr-engREADME 未说明 GPU 或显存要求;依赖系统安装的 Tesseract 与 Leptonica 原生库,CPU 即可运行。
Go 语言 OCR 封装库,通过 cgo 调用 Tesseract C++ 库识别图片文字
Go 语言封装的 Tesseract OCR 库,让 Go 程序直接调用 C++ 版 Tesseract 做图片文字识别,省去自己写 CGO 绑定。
核心能力
- 用 cgo 绑定 Tesseract C++ 库做文字识别
- NewClient 创建客户端后 SetImage 指定图片再取 Text
- 提供配套 OCR Server 示例项目可直接部署
- 官方 Docker 镜像 otiai10/gosseract 可免本地安装运行
容易踩的坑
- v2.4.1[v2.4.1] Fix linker error: `-lleptonica` instead of `-llept`最新正式版2023-09-06 · 2023-09-06 10:07 UTC
这个版本没有写更新说明。
- v2.3.1Enable FreeBSD test on CI workflow2021-01-04 · 2021-01-04 10:07 UTC
这个版本没有写更新说明。
- v2.2.42020-01-07 · 2020-01-07 06:40 UTC
这个版本没有写更新说明。
- v2.2.32020-01-06 · 2020-01-06 04:55 UTC
这个版本没有写更新说明。
- v2.2.22020-01-04 · 2020-01-04 03:47 UTC
这个版本没有写更新说明。
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
gosseract OCR
Golang OCR package, by using Tesseract C++ library.
OCR Server
If you need an OCR server or want to see a working example of this package, there is a ready-made server application, which is very easy to deploy!
👉 https://github.com/otiai10/ocrserver
Example
package main
import (
"fmt"
"github.com/otiai10/gosseract/v2"
)
func main() {
client := gosseract.NewClient()
defer client.Close()
client.SetImage("path/to/image.png")
text, _ := client.Text()
fmt.Println(text)
// Hello, World!
}
Installation
macOS
brew install tesseract
go get -t github.com/otiai10/gosseract/v2
Linux (Debian/Ubuntu)
sudo apt-get install -y libtesseract-dev libleptonica-dev tesseract-ocr-eng
go get -t github.com/otiai10/gosseract/v2
Please check this Dockerfile to get started.
Alternatively, you can deploy the pre-existing Docker image by invoking docker run -it --rm otiai10/gosseract.
Windows
Windows support requires vcpkg and MinGW-w64:
# Install Tesseract via vcpkg
vcpkg install tesseract:x64-windows
# Create MinGW import libraries from vcpkg DLLs
cd C:/vcpkg/installed/x64-windows/bin
gendef tesseract55.dll leptonica-1.87.0.dll
dlltool -d tesseract55.def -l libtesseract.a -D tesseract55.dll
dlltool -d leptonica-1.87.0.def -l libleptonica.a -D leptonica-1.87.0.dll
mv *.a ../lib/
# Download language data
mkdir C:/tessdata
curl -L -o C:/tessdata/eng.traineddata https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata
Set environment variables before building:
export CGO_ENABLED=1
export CC=C:/mingw64/bin/gcc.exe
export CGO_CFLAGS="-IC:/vcpkg/installed/x64-windows/include"
export CGO_LDFLAGS="-LC:/vcpkg/installed/x64-windows/lib"
export TESSDATA_PREFIX="C:/tessdata"
export PATH="/c/mingw64/bin:/c/vcpkg/installed/x64-windows/bin:$PATH"
For detailed troubleshooting, see knowledge/windows-support.md.
Test
In case you have tesseract-ocr installed on your local environment, you can run the tests with:
% go test .
If you DON'T want to install tesseract-ocr on your local environment, run ./test/runtime which utilises Docker and Vagrant to test the source code on some runtimes.
% ./test/runtime --engine docker
% ./test/runtime --engine vagrant
Check ./test/runtimes for more information about runtime tests.
Note: Clear Linux support was removed in January 2026 as Intel discontinued the distribution in July 2025.
Note: Arch Linux support was removed in January 2026 as the official image does not provide ARM64 architecture support.