Tesseract OCR Documentation

repository·main·Indexed 25 days ago

https://github.com/tesseract-ocr/tessdoc

Documentation for Tesseract, an open-source OCR engine using LSTM neural networks. Includes guides on using the CLI and C++ API, managing traineddata models (tessdata, tessdata_best, tessdata_fast), training LSTM models for version 5, and implementing user patterns for non-dictionary fields. Provides a comprehensive list of language wrappers (Python, Java, Go, Node.js, etc.), box file editors, and training automation tools.

Tokens
65.1K
Snippets
157
Records
336
Agent score
78%

What's inside Tesseract OCR

  1. Overview of Tesseract OCR

    main

    Tesseract is an open-source Optical Character Recognition (OCR) engine released under the Apache 2.0 license. It is available for use via a command-line interface or through a C++ API for programmatic text extraction from images.

    Key features include:

    • Support for a wide variety of languages and scripts.
    • An OCR engine based on LSTM (Long Short-Term Memory) neural networks (introduced in version 4.0).
    • Ability to be compiled for various targets, including Android and iOS.
    • Support for multiple engine modes (OEM), including legacy engines and LSTM-based engines.
  2. Overview of the LSTM training process

    main

    The LSTM training workflow consists of the following steps:

    1. Prepare training text.
    2. Render text to image + box file. (Or create manual box files for existing images).
    3. Make unicharset file. (Can be partially specified or created manually).
    4. Make a starter/proto traineddata from the unicharset and optional dictionary data using combine_lang_model.
    5. Generate training data set (.lstmf files) by running Tesseract on image + box file pairs.
    6. Run training on the .lstmf data set using lstmtraining.
    7. Combine output files into the final traineddata format.

    Key differences from Legacy (3.04) training:

    • Boxes only need to be at the textline level.
    • .tr files are replaced by .lstmf files.
    • Fonts can and should be mixed freely.
    • Clustering steps are replaced by a single lstmtraining step.
  3. Overview of the Tesseract 4.00 training process

    main

    The conceptual workflow for training Tesseract 4.00 involves these steps:

    1. Prepare training text.
    2. Render text to image + box file. (Boxes only need to be at the textline level, making it easier to use existing image data).
    3. Make unicharset file. (Can be created manually).
    4. Make a starter traineddata from the unicharset and optional dictionary data.
    5. Process image + box file using Tesseract to create a training data set (outputting .lstmf files instead of the old .tr files).
    6. Run training on the training data set using lstmtraining (this replaces the old mftraining, cntraining, and shapeclustering steps).
    7. Combine data files.

    Key differences from Tesseract 3.04:

    • Boxes are textline-level.
    • .tr files are replaced by .lstmf files.
    • Fonts can and should be mixed freely.
    • Clustering steps are replaced by a single lstmtraining step.
  4. Unicharset compatibility when adding LSTM to existing traineddata

    main
    If you are adding an LSTM model to an existing Tesseract .traineddata file, the lstm-unicharset does not need to match the primary Tesseract unicharset. However, you must ensure that the same unicharset used to train the LSTM is also used to build the lstm-*-dawgs files.
  5. Understand script training and multi-language support in tessdata_best

    main

    In the tessdata_best repository, most language and script models are trained on multiple languages to increase robustness.

    Key training characteristics:

    • Multi-language training: Most non-Latin scripts are trained with eng (English) included.
    • Latin scripts: Includes all Latin-based languages, with the exception of Vietnamese (vie).
    • Vietnamese (vie): Specifically trained for Latin-based Vietnamese and does not include eng.
    • Fraktur: A combination of Latin-based languages with 'old' variants (enm+frm+frk+ita_old+spa_old).
    • Devanagari: Trained for hin+mar+nep+san+eng.

    You can find the specific list of languages a script is trained on by checking the SCRIPT.langs.txt file in the langdata_lstm repository for that specific script (e.g., Devanagari.langs.txt).

  6. Understand the structure of tessdata_best LSTM network specifications

    main

    The tessdata_best repository contains high-accuracy traineddata files. Each language file includes a metadata header describing its LSTM (Long Short-Term Memory) network architecture. This specification includes the network layer configuration, the number of training iterations, and the learning rates used for different layers during training.

    Key components of the specification include:

    • Version: The version of the traineddata (e.g., 4.00.00alpha).
    • LSTM network: A serialized description of the neural network layers (e.g., [1,36,0,1Ct3,3,16Mp3,3Lfys64Lfx96Lrx96Lfx512O1c1]).
    • Iteration/Sample Iteration: The training progress metrics.
    • Null Char: The character code used for the null character.
    • Layer Learning Rates: Specific learning rates assigned to different functional layers (Input, Convolve, Maxpool, etc.).
  7. Understand the use cases for tessdata_best models

    main

    The tessdata_best model set is designed for users who prioritize higher OCR accuracy over processing speed. These models are slower to execute than other available data files but provide superior recognition results.

    Additionally, tessdata_best is the only set of files suitable for use as a start_model in advanced retraining scenarios.

  8. Format and Edit Box Files

    main

    Each line in a .box file represents one character and follows this format: [character] [left] [bottom] [right] [top] [page_number]

    Key Rules:

    • Coordinate System: The origin (0,0) is at the bottom-left.
    • Character Encoding: Use a UTF-8 compatible editor (e.g., Vim, Emacs, Notepad++, Sublime Text) to ensure special characters (like ü or ) are correctly encoded.
    • Merging Boxes: If a single character is split into multiple boxes (e.g., a low double quote appearing as two commas), you must merge them into a single line by calculating:
      • Left: Minimum of the two lines' first numbers.
      • Bottom: Minimum of the two lines' second numbers.
      • Right: Maximum of the two lines' third numbers.
      • Top: Maximum of the two lines' fourth numbers.
    • Character Limits: There is a limit of 24 bytes for the character description. This allows for approximately 6 to 24 Unicode characters to describe a single complex glyph.
    • Page Number: The last number is the 0-based page index in a multi-page TIFF.
  9. Understand Viewer limitations and behavior

    main

    The Viewer is a debug tool rather than a production UI. Users should be aware of the following behaviors and limitations:

    • Window Management: If a secondary window is closed, it cannot be reopened without restarting Tesseract.
    • Process Hanging: If Tesseract appears to hang instead of launching the viewer, a rogue Java process may be running in the background. You must manually kill the existing Java process.
    • Concurrency: You cannot run more than one instance of Tesseract using the new viewer simultaneously.
    • Fidelity: The demo modes in the viewer do not exactly replicate Tesseract's internal execution because the 2-pass nature and adaptive classifier are not reproduced.
  10. Optimize image polarity for Tesseract versions

    main

    The required text polarity depends on your Tesseract version:

    • Tesseract 4.x and newer: Use dark text on a light background.
    • Tesseract 3.05 and older: Can handle inverted images (dark background with light text) without problems.
  11. Format of traineddata files

    main

    Tesseract .traineddata files are archives containing various component files. The specific components included depend on the version and the model type (Legacy vs. LSTM):

    • Pre 4.0.0 format (Nov 2016): Contains both LSTM and Legacy models. Components typically include unicharset, inttemp, pffmtable, normproto, lstm, and various -dawg files (e.g., word-dawg, number-dawg).
    • 4.00.00alpha LSTM-only format: Contains only LSTM-related components such as lstm, lstm-unicharset, and lstm-recoder.

    You can use combine_tessdata -u <filename>.traineddata <prefix> to unpack these files.

  12. Handling animated WebP and GIF files

    main

    Tesseract has limited support for animated image formats:

    • Animated WebP: Not supported.
    • Animated GIF: Not supported for full sequences. If an animated GIF is provided, Tesseract will only read and process the first image in the sequence.