gosseract

repository·main·Indexed 25 days ago

https://github.com/otiai10/gosseract

A 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.

Tokens
4.3K
Snippets
15
Records
32
Agent score
74%

What's inside gosseract

  1. Create MinGW import libraries from MSVC DLLs

    main

    Because 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:

    1. Generate a .def file from the Tesseract DLL.
    2. Create the .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.dll
  2. Install gosseract on Linux (Debian/Ubuntu)

    main

    To 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/v2
  3. Set up gosseract on Windows

    main

    To build and run gosseract on Windows, you must satisfy dependencies for both the Go compiler (CGO) and the Tesseract OCR engine.

    Prerequisites

    1. MinGW-w64: Required for the GCC compiler used by CGO.
    2. vcpkg: Used to install Tesseract and its dependencies.
    3. Tesseract Installation: Install Tesseract via vcpkg using vcpkg install tesseract:x64-windows.
    4. Language Data: Download eng.traineddata and place it in a directory (e.g., C:/tessdata).

    Environment Variables

    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"
  4. Install gosseract on Windows

    main

    Windows support requires vcpkg and MinGW-w64.

    1. Install Tesseract via vcpkg

    vcpkg install tesseract:x64-windows

    2. Create MinGW import libraries from vcpkg DLLs

    Navigate 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/

    3. Download language data

    mkdir C:/tessdata
    curl -L -o C:/tessdata/eng.traineddata https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata

    4. Set environment variables

    Set 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"
  5. Fix 'exit status 0xc0000135' (DLL Not Found) on Windows

    main

    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"
  6. Fix 'Error opening data file ./eng.traineddata'

    main

    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:

    1. Download the language data (e.g., English):
      curl -L -o /c/tessdata/eng.traineddata https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata
    2. Set the TESSDATA_PREFIX environment variable to that directory.
    export TESSDATA_PREFIX=C:/tessdata
  7. Fix 'undefined: Version, NewClient, etc.' errors on Windows

    main

    If 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:

    1. Ensure CGO_ENABLED=1 is set.
    2. Verify GCC is installed and present in your PATH.
    3. Ensure the 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 gcc
  8. Fix 'ln: 'libtesseract.a' and 'libtesseract.a' are the same file'

    main

    This 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
    done
  9. Fix 'undefined reference to `TessBaseAPICreate`' on Windows

    main

    This error occurs when the linker cannot find the Tesseract library.

    Solutions:

    1. Verify that import libraries (e.g., libtesseract.a) exist in your library directory.
    2. Ensure CGO_LDFLAGS includes the correct library path.
    3. Verify that preprocessflags_windows.go contains the correct LDFLAGS.
    export CGO_LDFLAGS="-LC:/vcpkg/installed/x64-windows/lib"
  10. Fix 'cannot find -ltesseract' on Windows

    main

    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:

    1. Generate a definition file and create the .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/
    2. Create a symlink with the standard name libtesseract.a in the library directory.
    cd /c/vcpkg/installed/x64-windows/lib
    ln -sf libtesseract55.a libtesseract.a