Install gosseract on macOS
mainTo use gosseract on macOS, install the Tesseract dependency via Homebrew and then fetch the Go package.
brew install tesseract
go get -t github.com/otiai10/gosseract/v2repository·main·Indexed 25 days ago
https://github.com/otiai10/gosseractA Golang OCR package that provides a wrapper around the Tesseract C++ library. It allows developers to perform Optical Character Recognition tasks, including text recognition via Text(), hOCR extraction via HOCRText(), and retrieving bounding boxes for recognized words. The package supports configuring OCR languages, Page Segmentation Modes (PSM), and Tesseract API variables.
To use gosseract on macOS, install the Tesseract dependency via Homebrew and then fetch the Go package.
brew install tesseract
go get -t github.com/otiai10/gosseract/v2Because vcpkg builds Tesseract with MSVC, it provides .dll and .lib files. However, Go's CGO on Windows requires MinGW .a import libraries. You can generate these using gendef and dlltool (included with MinGW) by following these steps:
.def file from the Tesseract DLL..a import library using the .def file.Note: Replace tesseract55 with the specific version number of your installed DLL.
# Generate .def file from DLL
gendef tesseract55.dll
# Create MinGW import library
dlltool -d tesseract55.def -l libtesseract55.a -D tesseract55.dllTo use gosseract on Debian or Ubuntu, install the required Tesseract and Leptonica development libraries, then fetch the Go package.
sudo apt-get install -y libtesseract-dev libleptonica-dev tesseract-ocr-eng
go get -t github.com/otiai10/gosseract/v2To build and run gosseract on Windows, you must satisfy dependencies for both the Go compiler (CGO) and the Tesseract OCR engine.
vcpkg install tesseract:x64-windows.eng.traineddata and place it in a directory (e.g., C:/tessdata).Set the following environment variables to ensure CGO can find the Tesseract headers, libraries, and the Tesseract data:
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"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"Windows support requires vcpkg and MinGW-w64.
vcpkg install tesseract:x64-windowsNavigate to your vcpkg installation bin directory and run:
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/mkdir C:/tessdata
curl -L -o C:/tessdata/eng.traineddata https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddataSet the following variables before building your project:
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"The error exit status 0xc0000135 indicates STATUS_DLL_NOT_FOUND. The binary compiled successfully, but the required Tesseract/Leptonica DLLs are not in the system path at runtime.
Solution:
Add the directory containing your Tesseract DLLs (e.g., the vcpkg bin directory) to your PATH before running tests.
export PATH="/c/vcpkg/installed/x64-windows/bin:$PATH"Tesseract cannot locate its language data files. You must download the required .traineddata files and set the TESSDATA_PREFIX environment variable to the directory containing them.
Steps:
curl -L -o /c/tessdata/eng.traineddata https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddataTESSDATA_PREFIX environment variable to that directory.export TESSDATA_PREFIX=C:/tessdataIf you encounter undefined errors for core functions like Version or NewClient, CGO is likely disabled or failing. This causes Go to skip files containing import "C".
Solutions:
CGO_ENABLED=1 is set.PATH.CC environment variable points to a valid compiler.Debug commands:
go env CGO_ENABLED # Should be 1
go env CC # Should show the path to gccThis error occurs because a glob pattern like libtesseract*.a matches the symlink itself during creation.
Solution: Use a more specific glob pattern to target the actual library files instead of the symlink.
for f in libtesseract[0-9]*.a; do
ln -sf "$f" libtesseract.a
doneThis error occurs when the linker cannot find the Tesseract library.
Solutions:
libtesseract.a) exist in your library directory.CGO_LDFLAGS includes the correct library path.preprocessflags_windows.go contains the correct LDFLAGS.export CGO_LDFLAGS="-LC:/vcpkg/installed/x64-windows/lib"This error indicates the MinGW import library is missing or incorrectly named. You may need to manually create the import library from the DLL.
Steps:
.a library from the DLL:cd /c/vcpkg/installed/x64-windows/bin
gendef tesseract55.dll
dlltool -d tesseract55.def -l libtesseract55.a -D tesseract55.dll
mv libtesseract55.a ../lib/libtesseract.a in the library directory.cd /c/vcpkg/installed/x64-windows/lib
ln -sf libtesseract55.a libtesseract.aIf Go cannot find the GCC compiler (even if it works in a bash shell), you must explicitly set the CC environment variable to the full path of the executable.
Solution:
Set CC to the absolute path of your gcc.exe.
export CC=C:/mingw64/bin/gcc.exe