Tesseract OCR Documentation

repository·main·Indexed 24 days ago

https://github.com/ub-mannheim/tesseract

An open-source OCR engine providing a command-line tool and the libtesseract library for extracting text from images. It supports over 100 languages and features both a modern LSTM-based neural net engine and a legacy engine. Documentation covers installation via binary packages or source, CLI usage, C/C++ API integration (capi.h and baseapi.h), supported image formats (PNG, JPEG, TIFF), and output formats including PDF, hOCR, and ALTO.

Tokens
8.1K
Snippets
9
Records
31
Agent score
89%

What's inside Tesseract

  1. Install required fonts for unit testing

    main

    The unit tests require specific font files to function correctly. Ensure the following fonts are installed in the test environment:

    • Microsoft fonts: arialbi.ttf, times.ttf, verdana.ttf
    • Arabic: ae_Arab.ttf
    • DejaVu fonts: DejaVuSans-ExtraLight.ttf
    • Hindi: Lohit-Hindi.ttf
    • Korean: UnBatang.ttf
  2. Build ScrollView.jar

    main

    To compile ScrollView.jar, you must have an internet connection and curl installed. The build process will automatically download the following dependencies and place them in tesseract/java:

    • piccolo2d-core-3.0.1.jar
    • piccolo2d-extras-3.0.1.jar
    • jaxb-api-2.3.1.jar
    make ScrollView.jar
  3. Install Tesseract OCR

    main

    You can install Tesseract using one of two methods:

    1. Pre-built binary packages: Use your operating system's package manager to install a ready-to-use version.
    2. Build from source: Download the source code and compile it manually. If building from source, ensure your system has a supported compiler installed.
  4. Run Tesseract unit tests

    main

    To execute the unit tests for Tesseract, you must first prepare the environment by regenerating build files, initializing submodules, and fetching the required test data and fonts. You must also set the TESSDATA_PREFIX environment variable to point to your tessdata directory before running the check command.

    autoreconf -fiv
    git submodule update --init
    git clone https://github.com/egorpugin/tessdata tessdata_unittest --depth 1
    cp tessdata_unittest/fonts/* test/testing/
    mv tessdata_unittest/* ../
    export TESSDATA_PREFIX=/prefix/to/path/to/tessdata
    make check
  5. Configure Tesseract language data (traineddata)

    main

    Tesseract requires traineddata files (at minimum English and OSD) to be installed in the directory specified by the TESSDATA_PREFIX environment variable.

    You can download individual files using wget, curl, or a browser. To clone the entire language repository (note: this is >1.2 GB), use:

    git clone https://github.com/tesseract-ocr/tessdata.git tesseract-ocr.tessdata
  6. Build Tesseract from source using autotools (Linux/Unix/msys)

    main

    If you have cloned Tesseract from GitHub, you must generate the configure script using autogen.sh.

    Prerequisites:

    • Remove any existing Tesseract 4.0x installations before building.
    • Leptonica: Version 1.74.2 or higher is required.
    • Training Tool Dependencies:
      • Compiler with C++17 support
      • automake
      • pkg-config
      • pango-devel
      • cairo-devel
      • icu-devel

    Build Steps:

    1. Generate configuration: ./autogen.sh
    2. Configure the build: ./configure
    3. Compile: make
    4. Install: sudo make install and sudo ldconfig
    5. Build training tools: make training
    6. Install training tools: sudo make training-install
    ./autogen.sh
    ./configure
    make
    sudo make install
    sudo ldconfig
    make training
    sudo make training-install
  7. Use libtesseract C/C++ APIs

    main

    Developers can integrate Tesseract into their own applications using the provided C or C++ APIs.

    • C API: Available via capi.h.
    • C++ API: Available via baseapi.h.

    For other programming languages, check the Tesseract Wrappers documentation in the AddOns section.

  8. Configure Tesseract OCR engine parameters

    main

    Tesseract uses a wide range of configuration variables (often prefixed with tessedit_, thresholding_, quality_, etc.) to control OCR behavior. These can be set via the API or CLI. Key categories include:

    • OCR Engine & Mode: tessedit_ocr_engine_mode, tessedit_pageseg_mode.
    • Character Filtering: tessedit_char_blacklist, tessedit_char_whitelist.
    • Output Formats: tessedit_create_pdf, tessedit_create_hocr, tessedit_create_tsv, tessedit_create_page_xml, tessedit_create_alto.
    • Thresholding & Image Processing: thresholding_method, thresholding_window_size, invert_threshold, enable_noise_removal.
    • Rejection & Quality: tessedit_reject_mode, quality_char_pc, quality_blob_pc, tessedit_reject_block_percent.
    • LSTM Settings: lstm_use_matrix, lstm_choice_mode, lstm_choice_iterations.
    • Language Specifics: pageseg_devanagari_split_strategy, ocr_devanagari_split_strategy.
  9. Configure Classify adaptive learning parameters

    main

    The tesseract::Classify class provides several configuration variables (control knobs) to tune the behavior of the adaptive matcher and normalization processes. These are typically set during training or via configuration files.

    Adaptive Matcher Settings

    • classify_enable_adaptive_matcher (BOOL): Enables/disables the adaptive matcher.
    • classify_use_pre_adapted_templates (BOOL): Use templates that were previously adapted.
    • classify_save_adapted_templates (BOOL): Save templates after adaptation.
    • matcher_good_threshold (double): Threshold for a good match.
    • matcher_perfect_threshold (double): Threshold for a perfect match.
    • matcher_rating_margin (double): Margin for ratings.

    Normalization Settings

    • classify_norm_method (INT): Method used for outline normalization.
    • classify_char_norm_range (double): Range for character normalization.
    • classify_nonlinear_norm (BOOL): Enables non-linear normalization to distribute edges.

    Learning & Debugging

    • classify_enable_learning (BOOL): Enables/disables the learning mechanism.
    • classify_debug_level (INT): Sets the level of debug output for classification.
  10. Run Tesseract via Command Line

    main

    Use the tesseract command to perform OCR on images. The basic syntax requires an input image name and a base name for the output file.

    Syntax: tesseract imagename outputbase [-l lang] [--oem ocrenginemode] [--psm pagesegmode] [configfiles...]

    Key Options:

    • -l lang: Specify the language (e.g., eng).
    • --oem ocrenginemode: Select the OCR engine mode. Use --oem 0 to enable the Legacy OCR Engine mode (Tesseract 3 compatibility).
    • --psm pagesegmode: Set the page segmentation mode.

    For a full list of options, run tesseract --help or man tesseract.

    tesseract imagename outputbase [-l lang] [--oem ocrenginemode] [--psm pagesegmode] [configfiles...]