### Build and install Tesseract Source: https://tesseract-ocr.github.io/tessdoc/Compiling-%E2%80%93-GitInstallation.html Standard sequence of commands to configure, build, and install Tesseract from source. ```bash cd tesseract ./autogen.sh ./configure make sudo make install sudo ldconfig ``` -------------------------------- ### Install Tesseract Developer Tools Source: https://tesseract-ocr.github.io/tessdoc/Compiling Command to install development headers required for training tools. ```bash sudo apt install libtesseract-dev ``` -------------------------------- ### Install Pango for training tools Source: https://tesseract-ocr.github.io/tessdoc/Compiling-%E2%80%93-GitInstallation.html Install Pango development headers required if building Tesseract training tools. ```bash apt-get install libpango1.0-dev ``` -------------------------------- ### Build and install Tesseract with training tools Source: https://tesseract-ocr.github.io/tessdoc/Compiling-%E2%80%93-GitInstallation.html Sequence of commands to build and install Tesseract including the training tools. ```bash cd tesseract ./autogen.sh ./configure make sudo make install sudo ldconfig make training sudo make training-install ``` -------------------------------- ### Build and install Tesseract from command line Source: https://tesseract-ocr.github.io/tessdoc/Compiling Command to build the Release configuration and install Tesseract to the default location. ```bash cmake --build . --config Release --target install ``` -------------------------------- ### Install Tesseract via Snap Source: https://tesseract-ocr.github.io/tessdoc/Installation.html Install the Tesseract binary using snapd on supported distributions. ```bash sudo snap install --channel=edge tesseract ``` -------------------------------- ### Install Tesseract on Ubuntu Source: https://tesseract-ocr.github.io/tessdoc/Installation.html Use apt to install Tesseract and its developer tools on Ubuntu systems. ```bash sudo apt install tesseract-ocr sudo apt install libtesseract-dev ``` -------------------------------- ### Build and install training tools Source: https://tesseract-ocr.github.io/tessdoc/tess5/TrainingTesseract-5.html Commands to build and install the training tools after configuring Tesseract. Requires optional dependencies to be available. ```bash make make training sudo make training-install ``` -------------------------------- ### Install Tesseract on CentOS_8_Stream Source: https://tesseract-ocr.github.io/tessdoc/InstallationOpenSuse.html Adds the openSUSE repository, imports the GPG key, and installs Tesseract with all language packs. Run as root. ```bash sudo dnf config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_8_Stream/ sudo rpm --import https://build.opensuse.org/projects/home:Alexander_Pozdnyakov/signing_keys/download?kind=gpg sudo dnf install tesseract sudo dnf install tesseract-langpack-* ``` -------------------------------- ### Install optional manpage dependencies Source: https://tesseract-ocr.github.io/tessdoc/Compiling-%E2%80%93-GitInstallation.html Install asciidoc and related tools to build optional manpages. ```bash apt-get install --no-install-recommends asciidoc docbook-xsl xsltproc ``` -------------------------------- ### Example build error output Source: https://tesseract-ocr.github.io/tessdoc/Compiling-%E2%80%93-GitInstallation.html Example of a common build error encountered during the make process. ```text make all-recursive Making all in ccstruct /bin/sh ../libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. - I.. -I../ccutil -I../cutil -I../image -I../viewer -I/opt/local/ include -I/usr/local/include/leptonica -g -O2 -MT blobbox.lo -MD -MP - MF .deps/blobbox.Tpo -c -o blobbox.lo blobbox.cpp mv -f .deps/blobbox.Tpo .deps/blobbox.Plo mv: rename .deps/blobbox.Tpo to .deps/blobbox.Plo: No such file or directory make[3]: *** [blobbox.lo] Error 1 make[2]: *** [all-recursive] Error 1 make[1]: *** [all-recursive] Error 1 make: *** [all] Error 2 ``` -------------------------------- ### Build and install Tesseract with make Source: https://tesseract-ocr.github.io/tessdoc/ReleaseNotes.html Standard build and install commands to move data files to the standard location. Required for non-DevStudio users. ```bash ./configure make make install ``` -------------------------------- ### Install Tesseract to a custom directory Source: https://tesseract-ocr.github.io/tessdoc/Compiling Steps to configure and install Tesseract into a local directory without root access. ```bash ./autogen.sh ./configure --prefix=$HOME/local/ make make install ``` -------------------------------- ### Install Tesseract on Fedora 31 Source: https://tesseract-ocr.github.io/tessdoc/InstallationOpenSuse.html Adds the openSUSE repository and installs Tesseract with the German language pack. Run 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 ``` -------------------------------- ### Install Tesseract on Fedora 32 Source: https://tesseract-ocr.github.io/tessdoc/InstallationOpenSuse.html Adds the openSUSE repository and installs Tesseract with the German language pack. 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 ``` -------------------------------- ### Manually build and install OpenMP Source: https://tesseract-ocr.github.io/tessdoc/Compiling Alternative method to get, compile, and install OpenMP manually using cmake and git. This method should no longer be needed if libomp is available. ```bash # Install cmake if it is not available. sudo port install cmake git clone https://github.com/llvm-mirror/openmp.git cd openmp mkdir build cd build cmake .. make sudo make install ``` -------------------------------- ### GetComponentImages Example Source: https://tesseract-ocr.github.io/tessdoc/Examples_C%2B%2B.html Gets textline components from the image, iterates over each box, and prints OCR text and confidence for each. ```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); } Pix *image = pixRead("phototest.tif"); 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); } // Destroy used object and release memory api->End(); delete api; delete [] outText; pixDestroy(&image); return 0; } ``` -------------------------------- ### Configure Ubuntu sources.list Source: https://tesseract-ocr.github.io/tessdoc/Installation.html If apt cannot find the package, add the universe repository to your sources.list file. ```bash sudo vi /etc/apt/sources.list deb http://archive.ubuntu.com/ubuntu bionic universe ``` -------------------------------- ### Get LSTM alternative symbol choices and confidence Source: https://tesseract-ocr.github.io/tessdoc/APIExample.html This example initializes Tesseract with the English language, sets the 'lstm_choice_mode' variable to 2 to enable alternative symbol choices, and iterates through the result iterator to print symbol confidence and bounding box data. ```cpp #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; } ``` -------------------------------- ### Find fonts with text2image --find_fonts Source: https://tesseract-ocr.github.io/tessdoc/Fonts.html Use this command to find fonts installed on your system that render a given training text. Change the language code and directory locations to match your setup; fontslist.txt will provide text usable in training/language-specific.sh. It will not work for Fraktur fonts and will identify all Latin fonts, so review the generated images and choose appropriate fonts. ```bash text2image --find_fonts \ --fonts_dir /usr/share/fonts \ --text ./langdata/eng/eng.training_text \ --min_coverage .9 \ --outputbase ./langdata/eng/eng \ |& grep raw \ | sed -e 's/ :.*/@ \\/g' \ | sed -e "s/^/ '/" \ | sed -e "s/@/'/g" >./langdata/eng/fontslist.txt ``` -------------------------------- ### Set PKG_CONFIG_PATH for custom Leptonica installation Source: https://tesseract-ocr.github.io/tessdoc/Compiling Command to set PKG_CONFIG_PATH to /usr/local/lib/pkgconfig when pkg-config is not looking at the custom install folder. Use this after installing Leptonica in /usr/local. ```bash PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ``` -------------------------------- ### Specify custom install path for Tesseract Source: https://tesseract-ocr.github.io/tessdoc/Compiling CMake configuration command to set a custom install prefix, avoiding the need for admin rights to install to Program Files. ```bash cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=inst ``` -------------------------------- ### Install fonts on Debian with apt-get Source: https://tesseract-ocr.github.io/tessdoc/Fonts.html Install required fonts on Debian GNU Linux and similar distributions (Linux Mint, Ubuntu, etc.) using apt-get. Many font groups are marked as todo and are not yet installed; only DEVANAGARI_FONTS, JPN_FONTS, LATIN_FONTS, and NEOLATIN_FONTS have actual install commands. ```bash # AMHARIC_FONTS (todo) # ANCIENT_GREEK_FONTS (todo) # ARABIC_FONTS (todo) # ARMENIAN_FONTS (todo) # BENGALI_FONTS (todo) # BURMESE_FONTS (todo) # CHI_SIM_FONTS (todo) # CHI_TRA_FONTS (todo) # DEVANAGARI_FONTS (see also external links below) apt-get install fonts-deva # EARLY_LATIN_FONTS (todo) # FRAKTUR_FONTS (todo) # GEORGIAN_FONTS (todo) # GREEK_FONTS (todo) # GUJARATI_FONTS (todo) # HEBREW_FONTS (todo) # JPN_FONTS (todo) apt-get install fonts-noto-cjk fonts-japanese-mincho.ttf fonts-takao-gothic fonts-vlgothic # KANNADA_FONTS (todo) # KHMER_FONTS (todo) # KOREAN_FONTS (todo) # KURDISH_FONTS (todo) # KYRGYZ_FONTS (todo) # LAOTHIAN_FONTS (todo) # LATIN_FONTS apt-get install fonts-dejavu gsfonts ttf-mscorefonts-installer # MALAYALAM_FONTS (todo) # NEOLATIN_FONTS (still incomplete) apt-get install fonts-ebgaramond fonts-gfs-didot fonts-gfs-didot-classic fonts-junicode # NORTH_AMERICAN_ABORIGINAL_FONTS (todo) # OLD_GEORGIAN_FONTS (todo) # ORIYA_FONTS (todo) # PERSIAN_FONTS (todo) # PUNJABI_FONTS (todo) # RUSSIAN_FONTS (todo) # SINHALA_FONTS (todo) # SYRIAC_FONTS (todo) # TAMIL_FONTS (todo) # TELUGU_FONTS (todo) # THAANA_FONTS (todo) # THAI_FONTS (todo) # TIBETAN_FONTS (todo) # VERTICAL_FONTS (todo) # VIETNAMESE_FONTS (todo) ``` -------------------------------- ### Install Tesseract with Homebrew Source: https://tesseract-ocr.github.io/tessdoc/Installation.html Install Tesseract on macOS using the Homebrew package manager. ```bash brew install tesseract ``` -------------------------------- ### Configure with custom options Source: https://tesseract-ocr.github.io/tessdoc/Compiling-%E2%80%93-GitInstallation.html Example of running configure with specific flags to disable features and set compiler flags. ```bash ./configure --disable-openmp --disable-debug --disable-opencl --disable-graphics --disable-shared 'CXXFLAGS=-g -O2 -Wall -Wextra -Wpedantic' ``` -------------------------------- ### Configure output showing optional dependencies Source: https://tesseract-ocr.github.io/tessdoc/tess5/TrainingTesseract-5.html Expected output from ./configure when all optional dependencies for training tools are available. Look for "yes" for each dependency; version numbers may change over time. ```text checking for pkg-config... [some valid path] checking for lept >= 1.74... yes checking for libarchive... yes checking for icu-uc >= 52.1... yes checking for icu-i18n >= 52.1... yes checking for pango >= 1.22.0... yes checking for cairo... yes [...] ``` -------------------------------- ### Build training tools with Software Network Source: https://tesseract-ocr.github.io/tessdoc/Compiling Command to build training tools using the Software Network client, which creates a solution link in the root directory. ```bash sw build ``` -------------------------------- ### C++ API Example for user patterns Source: https://tesseract-ocr.github.io/tessdoc/APIExample-user_patterns.html A C++ program demonstrating how to initialize Tesseract with a config file containing user patterns. ```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 required libraries for training tools Source: https://tesseract-ocr.github.io/tessdoc/tess5/TrainingTesseract-5.html Command to install the necessary dependencies for building Tesseract training tools on Debian-based Linux systems. ```bash sudo apt-get install libicu-dev libpango1.0-dev libcairo2-dev ``` -------------------------------- ### Install Leptonica development package Source: https://tesseract-ocr.github.io/tessdoc/Compiling Command to install the Leptonica development headers from the distribution package manager. ```bash sudo apt-get install libleptonica-dev ``` -------------------------------- ### Install Tesseract with MacPorts Source: https://tesseract-ocr.github.io/tessdoc/Installation.html Commands to install the Tesseract base package and additional language data via MacPorts. ```bash sudo port install tesseract ``` ```bash sudo port install tesseract- ``` -------------------------------- ### Example of a config file Source: https://tesseract-ocr.github.io/tessdoc/APIExample-user_patterns.html A configuration file that specifies the path to the user patterns file using the user_patterns_file key. ```text user_patterns_file path/to/my.patterns ``` -------------------------------- ### Install Tesseract on openSUSE Leap 15.0 Source: https://tesseract-ocr.github.io/tessdoc/InstallationOpenSuse.html Adds the openSUSE repository, refreshes the package list, and installs Tesseract OCR with the German traineddata. Run 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 ```