### C++ API Example with User Patterns Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample-user_patterns.md This C++ code demonstrates how to initialize Tesseract with a user patterns config file and process an image. Ensure Tesseract and Leptonica are correctly installed and linked. ```cpp #include #include int main() { Pix *image; char *outText; char *configs[]={\"path/to/my.patterns.config\"}; int configs_size = 1; tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); if (api->Init(NULL, \"eng\", tesseract::OEM_LSTM_ONLY, configs, configs_size, NULL, NULL, false)) { fprintf(stderr, \"Could not initialize tesseract.\\n\"); exit(1); } image = pixRead(\"Arial.png\"); api->SetImage(image); outText = api->GetUTF8Text(); printf(outText); api->End(); delete api; delete [] outText; pixDestroy(&image); return 0; } ``` -------------------------------- ### Install Training Dependencies (Ubuntu/Debian) Source: https://github.com/tesseract-ocr/tessdoc/blob/main/tess5/TrainingTesseract-5.md Installs essential libraries required for building Tesseract training tools. Ensure these are installed before compiling. ```bash sudo apt-get install libicu-dev libpango1.0-dev libcairo2-dev ``` -------------------------------- ### Get Tesseract Command Line Help Source: https://github.com/tesseract-ocr/tessdoc/blob/main/FAQ.md Run `tesseract --help` to display the most recent command-line usage information for your installed version. ```bash tesseract --help ``` -------------------------------- ### Build and Install Tesseract with Training Tools Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Compiles and installs Tesseract along with its training tools from source. ```bash git clone https://github.com/tesseract-ocr/tesseract/ cd tesseract ./autogen.sh ./configure make training sudo make install training-install ``` -------------------------------- ### Install Tesseract on Fedora 31 Source: https://github.com/tesseract-ocr/tessdoc/blob/main/InstallationOpenSuse.md Install Tesseract and the German language pack on Fedora 31. Execute as root. ```bash dnf config-manager --add-repo https://download.opensuse.org/repositories/home:Alexander_Pozdnyakov/Fedora_31/home:Alexander_Pozdnyakov.repo dnf install tesseract dnf install tesseract-langpack-deu ``` -------------------------------- ### Configure Tesseract Installation to a Custom Prefix Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Configures Tesseract to install into a specified directory, useful for installing without root privileges. This example installs to `$HOME/local/`. ```bash ./autogen.sh ./configure --prefix=$HOME/local/ make make install ``` -------------------------------- ### Build and Install Tesseract Training Tools Source: https://github.com/tesseract-ocr/tessdoc/blob/main/tess5/TrainingTesseract-5.md Commands to build and install the Tesseract training tools after configuring the build. This is necessary for training new models. ```bash make make training sudo make training-install ``` -------------------------------- ### Install Tesseract on RHEL 7 Source: https://github.com/tesseract-ocr/tessdoc/blob/main/InstallationOpenSuse.md Install Tesseract and the German language pack on RHEL 7. Execute as root. ```bash yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/RHEL_7/ yum update yum install tesseract yum install tesseract-langpack-deu ``` -------------------------------- ### Install Tesseract on openSUSE Leap 15.0 Source: https://github.com/tesseract-ocr/tessdoc/blob/main/InstallationOpenSuse.md Install Tesseract OCR and the German trained data on openSUSE Leap 15.0. Execute as root. ```bash zypper addrepo https://download.opensuse.org/repositories/home:Alexander_Pozdnyakov/openSUSE_Leap_15.0/home:Alexander_Pozdnyakov.repo zypper refresh zypper install tesseract-ocr zypper install tesseract-ocr-traineddata-german ``` -------------------------------- ### Install Cygwin Packages for Tesseract Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md List of Tesseract-specific packages to install for building on Cygwin. Ensure these are installed before proceeding with compilation. ```bash Tesseract specific packages to be installed: tesseract-ocr 3.04.01-1 tesseract-ocr-eng 3.04-1 tesseract-training-core 3.04-1 tesseract-training-eng 3.04-1 tesseract-training-util 3.04.01-1 ``` -------------------------------- ### Extract Orientation and Script with cffi in Python Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample.md Python example using cffi to interact with Tesseract's C-API for detecting page orientation and script. Requires 'pip install cffi'. ```python # /usr/bin/env python3 # coding: utf-8 PATH_TO_LIBTESS = '/path/to/development/libtesseract.so' import cffi # requires "pip install cffi" ffi = cffi.FFI() ffi.cdef("" struct Pix; typedef struct Pix PIX; PIX * pixRead ( const char *filename ); char * getLeptonicaVersion ( ); typedef struct TessBaseAPI TessBaseAPI; typedef int BOOL; const char* TessVersion(); TessBaseAPI* TessBaseAPICreate(); int TessBaseAPIInit3(TessBaseAPI* handle, const char* datapath, const char* language); void TessBaseAPISetImage2(TessBaseAPI* handle, struct Pix* pix); BOOL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, char** best_script_name, int* best_orientation_deg, float* script_confidence, float* orientation_confidence); """) libtess = ffi.dlopen(PATH_TO_LIBTESS) from ctypes.util import find_library liblept = ffi.dlopen(find_library('lept')) ffi.string(libtess.TessVersion()) ffi.string(liblept.getLeptonicaVersion()) api = libtess.TessBaseAPICreate() libtess.TessBaseAPIInit3(api, ffi.NULL, ffi.NULL) pix = liblept.pixRead('mono.png'.encode()) libtess.TessBaseAPISetImage2(api, pix) script_name = ffi.new('char **') orient_deg = ffi.new('int *') script_conf = ffi.new('float *') orient_conf = ffi.new('float *') libtess.TessBaseAPIDetectOrientationScript(api, script_name, orient_deg, script_conf, orient_conf) print(ffi.string(script_name).decode('utf-8')) print(orient_deg[0]) print(script_conf[0]) print(orient_conf[0]) ``` -------------------------------- ### Extracting Textline Components with Bounding Boxes Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample.md This example demonstrates how to get bounding boxes for text lines in an image and then perform OCR on each identified component. It iterates through components, extracts text, and prints confidence. ```c++ Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif"); tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); api->Init(NULL, "eng"); api->SetImage(image); Boxa* boxes = api->GetComponentImages(tesseract::RIL_TEXTLINE, true, NULL, NULL); printf("Found %d textline image components.\n", boxes->n); for (int i = 0; i < boxes->n; i++) { BOX* box = boxaGetBox(boxes, i, L_CLONE); api->SetRectangle(box->x, box->y, box->w, box->h); char* ocrResult = api->GetUTF8Text(); int conf = api->MeanTextConf(); fprintf(stdout, "Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s", i, box->x, box->y, box->w, box->h, conf, ocrResult); boxDestroy(&box); } delete api; ``` -------------------------------- ### Tesseract Training: Install Dependencies and Build Source: https://context7.com/tesseract-ocr/tessdoc/llms.txt Steps to install necessary dependencies and build Tesseract with training tools. This is a prerequisite for LSTM fine-tuning. ```bash # Install dependencies sudo apt-get install libicu-dev libpango1.0-dev libcairo2-dev ``` ```bash # Build Tesseract with training tools ./configure make && make training && sudo make training-install ``` ```bash # Clone the tesstrain repo for Python training scripts git clone https://github.com/tesseract-ocr/tesstrain cd tesstrain ``` -------------------------------- ### Install Core Dependencies for Tesseract on Ubuntu Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs essential development libraries for compiling Tesseract on Ubuntu 16.04/14.04. Ensure these are installed before building Tesseract from source. ```bash sudo apt-get install g++ # or clang++ (presumably) sudo apt-get install autoconf automake libtool sudo apt-get install pkg-config sudo apt-get install libpng-dev sudo apt-get install libjpeg8-dev sudo apt-get install libtiff5-dev sudo apt-get install zlib1g-dev sudo apt-get install libwebpdemux2 libwebp-dev sudo apt-get install libopenjp2-7-dev sudo apt-get install libgif-dev sudo apt-get install libarchive-dev libcurl4-openssl-dev ``` -------------------------------- ### Install Tesseract with CMake Build Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Build and install Tesseract from the command line using CMake. This command performs a Release build and installs the necessary files. ```sh cmake --build . --config Release --target install ``` -------------------------------- ### Build Tesseract from Source Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling-–-GitInstallation.md Compiles and installs Tesseract after cloning the repository. This sequence includes autogen, configure, make, and install steps. ```bash cd tesseract ./autogen.sh ./configure make sudo make install sudo ldconfig ``` -------------------------------- ### Install Tesseract on Scientific Linux 7 Source: https://github.com/tesseract-ocr/tessdoc/blob/main/InstallationOpenSuse.md Install Tesseract and the German language pack on Scientific Linux 7. Execute as root. ```bash yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/ScientificLinux_7/ yum update yum install tesseract yum install tesseract-langpack-deu ``` -------------------------------- ### Install Training Dependencies for Tesseract on Ubuntu Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs additional libraries required for Tesseract training tools on Ubuntu. Include these if you are building the training components. ```bash sudo apt-get install libicu-dev sudo apt-get install libpango1.0-dev sudo apt-get install libcairo2-dev ``` -------------------------------- ### Install Packages for Tesseract Training Tools Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs additional packages required for building Tesseract's training tools using MacPorts. ```bash sudo port install cairo pango sudo port install icu +devel ``` -------------------------------- ### Install Tesseract via Snap Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Installation.md Use this command to install the Tesseract snap package on systems with snapd. The --channel=edge flag installs the latest development version. ```bash sudo snap install --channel=edge tesseract ``` -------------------------------- ### Install Optional Manpage Dependencies Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling-–-GitInstallation.md Installs packages required for building manpages using asciidoc. Use this if you need documentation generated from source. ```bash apt-get install --no-install-recommends asciidoc docbook-xsl xsltproc ``` -------------------------------- ### Install Tesseract OCR Developer Tools on Ubuntu Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs the Tesseract OCR developer tools, which are necessary for training Tesseract. Use this if you plan to train Tesseract. ```bash sudo apt install libtesseract-dev ``` -------------------------------- ### Install Debian/Ubuntu Dependencies Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling-–-GitInstallation.md Installs essential packages for building Tesseract on Debian/Ubuntu systems. Includes automake, git, and development libraries. ```bash apt-get install automake ca-certificates g++ git libtool libleptonica-dev make pkg-config ``` -------------------------------- ### Install Tesseract on Fedora 32 Source: https://github.com/tesseract-ocr/tessdoc/blob/main/InstallationOpenSuse.md Commands to install Tesseract and the German language pack on Fedora 32. Run as root. ```bash dnf config-manager --add-repo https://download.opensuse.org/repositories/home:Alexander_Pozdnyakov/Fedora_32/home:Alexander_Pozdnyakov.repo dnf install tesseract dnf install tesseract-langpack-deu ``` -------------------------------- ### Install Tesseract on openSUSE Tumbleweed Source: https://github.com/tesseract-ocr/tessdoc/blob/main/InstallationOpenSuse.md Commands to install Tesseract OCR and the German trained data on openSUSE Tumbleweed. Run as root. ```bash zypper addrepo https://download.opensuse.org/repositories/home:Alexander_Pozdnyakov/openSUSE_Tumbleweed/home:Alexander_Pozdnyakov.repo zypper refresh zypper install tesseract-ocr zypper install tesseract-ocr-traineddata-german ``` -------------------------------- ### Install Mingw-w64 Cross-Compilation Tools on Debian/Ubuntu Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Install the necessary Mingw-w64 cross-compilation tools for targeting 32-bit and 64-bit Windows executables on Debian-based systems. ```bash # Development environment targeting 32- and 64-bit Windows (required) apt-get install mingw-w64 # Development tools for 32- and 64-bit Windows (optional) apt-get install mingw-w64-tools ``` -------------------------------- ### Iterating Through Words with Confidence and Bounding Boxes Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample.md This example shows how to use a ResultIterator to get the text, confidence value, and bounding box for each word in the OCR result. It supports various iterator levels like block, line, and word. ```c++ Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif"); tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); api->Init(NULL, "eng"); api->SetImage(image); api->Recognize(0); tesseract::ResultIterator* ri = api->GetIterator(); tesseract::PageIteratorLevel level = tesseract::RIL_WORD; if (ri != 0) { do { const char* word = ri->GetUTF8Text(level); float conf = ri->Confidence(level); int x1, y1, x2, y2; ri->BoundingBox(level, &x1, &y1, &x2, &y2); printf("word: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n", word, conf, x1, y1, x2, y2); delete[] word; } while (ri->Next(level)); } delete api; ``` -------------------------------- ### Install Tesseract OCR and Dev Tools on Ubuntu Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Installation.md Use apt to install the Tesseract OCR engine and its development libraries. Ensure the 'universe' repository is enabled if packages are not found. ```bash sudo apt install tesseract-ocr sudo apt install libtesseract-dev ``` ```bash deb http://archive.ubuntu.com/ubuntu bionic universe ``` -------------------------------- ### Build Tesseract with Training Tools Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling-–-GitInstallation.md Compiles and installs Tesseract along with its training tools. This requires additional dependencies and a separate 'make training' step. ```bash cd tesseract ./autogen.sh ./configure make sudo make install sudo ldconfig make training sudo make training-install ``` -------------------------------- ### Get Confidence for Alternative LSTM Symbol Choices Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample.md This C++ example shows how to retrieve confidence levels for alternative symbol choices per character using the LSTM engine. It involves setting 'lstm_choice_mode' to '2' and iterating through the results obtained from GetBestLSTMSymbolChoices. ```C++ #include #include int main() { tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); // Initialize tesseract-ocr with English, without specifying tessdata path if (api->Init(NULL, "eng")) { fprintf(stderr, "Could not initialize tesseract.\n"); exit(1); } // Open input image with leptonica library Pix *image = pixRead("/home/ubuntu/tesseract/test/testing/trainingital.tif"); api->SetImage(image); // Set lstm_choice_mode to alternative symbol choices per character, bbox is at word level. api->SetVariable("lstm_choice_mode", "2"); api->Recognize(0); tesseract::PageIteratorLevel level = tesseract::RIL_WORD; tesseract::ResultIterator* res_it = api->GetIterator(); // Get confidence level for alternative symbol choices. Code is based on // https://github.com/tesseract-ocr/tesseract/blob/a7a729f6c315e751764b72ea945da961638effc5/src/api/hocrrenderer.cpp#L325-L344 std::vector>>* choiceMap = nullptr; if (res_it != 0) { do { const char* word; float conf; int x1, y1, x2, y2, tcnt = 1, gcnt = 1, wcnt = 0; res_it->BoundingBox(level, &x1, &y1, &x2, &y2); choiceMap = res_it->GetBestLSTMSymbolChoices(); for (auto timestep : *choiceMap) { if (timestep.size() > 0) { for (auto & j : timestep) { conf = int(j.second * 100); word = j.first; printf("%d symbol: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n", wcnt, word, conf, x1, y1, x2, y2); gcnt++; } tcnt++; } wcnt++; printf("\n"); } } while (res_it->Next(level)); } // Destroy used object and release memory api->End(); delete api; pixDestroy(&image); return 0; } ``` -------------------------------- ### Get HOCR Output with Alternative LSTM Symbol Choices Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample.md This C++ example demonstrates how to obtain HOCR output that includes alternative symbol choices per character from the LSTM engine. It is equivalent to running Tesseract from the command line with '-c lstm_choice_mode=2 hocr'. ```C++ #include #include int main() { char *outText; tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); // Initialize tesseract-ocr with English, without specifying tessdata path if (api->Init(NULL, "eng")) { fprintf(stderr, "Could not initialize tesseract.\n"); exit(1); } // Open input image with leptonica library Pix *image = pixRead("/tesseract/test/testing/trainingital.tif"); api->SetImage(image); api->SetVariable("lstm_choice_mode", "2"); // Get HOCR result outText = api->GetHOCRText(0); printf("HOCR alternative symbol choices per character :\n%s", outText); // Destroy used object and release memory api->End(); delete api; delete [] outText; pixDestroy(&image); return 0; } ``` -------------------------------- ### Build and Run Script for C++ API Example Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample-user_patterns.md This script sets environment variables for include and library paths, compiles the C++ code, and runs the executable. It also includes a diff command to compare the output with a ground truth file. ```sh #!/bin/bash export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/include export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib g++ -std=c++17 -o my.patterns.api my.patterns.api.cpp -llept -ltesseract export TESSDATA_PREFIX=~/tessdata_best ./my.patterns.api > Arial-patterns.txt diff -u Arial-patterns.txt Arial-gt.txt ``` -------------------------------- ### Install Tesseract via MacPorts Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Installation.md Installs Tesseract using the MacPorts package manager. Ensure MacPorts is installed and configured on your macOS system. ```bash sudo port install tesseract ``` -------------------------------- ### Install Training Tools Dependencies Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling-–-GitInstallation.md Installs additional libraries needed to build Tesseract's training tools. This includes ICU, Pango, and Cairo development packages. ```bash apt-get install libicu-dev apt-get install libpango1.0-dev apt-get install libcairo2-dev ``` -------------------------------- ### Install Devanagari Fonts on Debian Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Fonts.md Installs Devanagari fonts on Debian-based systems using apt-get. ```bash apt-get install fonts-deva ``` -------------------------------- ### CLI Example for User Patterns Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample-user_patterns.md User patterns can be invoked directly from the command line using the --user-patterns option. ```sh tesseract input.tif output --user-patterns path/to/my.patterns ``` -------------------------------- ### Install Required Packages with MacPorts Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs essential development packages for Tesseract compilation using MacPorts. ```bash sudo port install autoconf \ automake \ libtool \ pkgconfig \ leptonica ``` -------------------------------- ### Install Neo-Latin Fonts on Debian Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Fonts.md Installs specific Neo-Latin fonts on Debian-based systems using apt-get. ```bash apt-get install fonts-ebgaramond fonts-gfs-didot fonts-gfs-didot-classic fonts-junicode ``` -------------------------------- ### Install Japanese Fonts on Debian Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Fonts.md Installs various Japanese fonts on Debian-based systems using apt-get. ```bash apt-get install fonts-noto-cjk fonts-japanese-mincho.ttf fonts-takao-gothic fonts-vlgothic ``` -------------------------------- ### Fix autoconf syntax error Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md This error typically occurs when `autoconf-archive` is not installed. Ensure it is installed and then rerun `./autogen.sh`. ```bash ./configure: line 4237: syntax error near unexpected token `-mavx,' ./configure: line 4237: `AX_CHECK_COMPILE_FLAG(-mavx, avx=1, avx=0)' ``` -------------------------------- ### Config File Example for User Patterns Source: https://github.com/tesseract-ocr/tessdoc/blob/main/APIExample-user_patterns.md The config file specifies the path to the user patterns file. Use UNIX line endings and a blank line at the end. ```plaintext user_patterns_file path/to/my.patterns ``` -------------------------------- ### Install Tesseract Dependencies with Homebrew Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs core and optional dependencies for Tesseract compilation using Homebrew on macOS. ```bash # Packages which are always needed. brew install automake autoconf libtool brew install pkgconfig brew install icu4c brew install leptonica # Packages required for training tools. brew install pango # Optional packages for extra features. brew install libarchive # Optional package for builds using g++. brew install gcc ``` -------------------------------- ### Make AppImage Executable and Run Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Installation.md After downloading an AppImage, make it executable using chmod and then run it to perform OCR. This example demonstrates running OCR on 'page.tif' and saving output to 'page.txt' using the English language model. ```bash $ chmod a+x tesseract*.AppImage ``` ```bash ./tesseract*.AppImage -l eng page.tif page.txt ``` -------------------------------- ### Install OpenMP with MacPorts Source: https://github.com/tesseract-ocr/tessdoc/blob/main/Compiling.md Installs OpenMP using the MacPorts package manager. This is an optional step for enabling multithreading. ```bash sudo port install libomp ```