Tesseract4Android Documentation

repository·master·Indexed 21 days ago

https://github.com/adaptech-cz/tesseract4android

A CMake-based fork of tess-two providing a JNI wrapper for the Tesseract OCR engine on Android. It supports modern Android Studio, offers Standard and OpenMP variants via JitPack, and utilizes the TessBaseAPI for image recognition. The project includes integrated support for the Leptonica image processing library and provides instructions for building from source, configuring trained data files, and running unit tests.

Tokens
16.6K
Snippets
44
Records
81
Agent score
71%

What's inside Tesseract4Android

  1. Overview of Tesseract OCR capabilities

    master

    Tesseract is an OCR (Optical Character Recognition) engine that provides both a library (libtesseract) and a command-line program (tesseract).

    Key features include:

    • Neural Net Engine: Tesseract 4+ uses an LSTM-based engine focused on line recognition.
    • Legacy Support: Supports the Tesseract 3 engine (character pattern recognition) via the --oem 0 mode.
    • Language Support: Supports Unicode (UTF-8) and over 100 languages out of the box.
    • Image Formats: Supports various formats including PNG, JPEG, and TIFF.
    • Output Formats: Can output to plain text, hOCR (HTML), PDF, invisible-text-only PDF, TSV, ALTO, and PAGE.

    Note: To achieve better results, you may need to improve the quality of the input image before processing.

  2. Overview of Leptonica Library capabilities

    master

    Leptonica is an image processing library used for document and natural image analysis. It provides a wide range of fundamental operations including:

    • Image Processing: Rasterop (bitblt), affine transforms (scaling, translation, rotation, shear), projective and bilinear transforms, morphology (binary and grayscale), rank order filters, convolution, and seedfill/connected components.
    • Image Analysis: Pixelwise masking, blending, enhancement, and arithmetic operations.
    • I/O Support: Standard image formats including _jpg_, _png_, _tiff_, _webp_, _jp2_, _bmp_, _pnm_, _gif_, _ps_, and _pdf_.
    • Data Utilities: Handling arrays of image-related data types (e.g., _pixa_, _boxa_, _pta_) and general data structures like stacks, queues, heaps, lists, sets, and maps.
  3. Configure Tesseract trained data files

    master

    Tesseract requires v4.0.0 trained data files (*.traineddata) for the languages you want to recognize.

    Requirements:

    1. Directory Structure: Files must be placed in a directory named tessdata (or a subdirectory).
    2. Permissions: The path must be readable by your app.
    3. API 29+ Compatibility: When targeting Android API level 29 or higher, you must use the app's private directories, such as context.getFilesDir() or context.getExternalFilesDir(), to store these files.
  4. Configure Tesseract language data (TESSDATA_PREFIX)

    master

    Tesseract requires trained data files to function. At a minimum, you must install the English language and OSD (Orientation and Script Detection) traineddata files into the directory specified by the TESSDATA_PREFIX environment variable.

    You can download individual files using wget, curl, or a browser.

    Note for Packagers: While all language data files are available in the tessdata git repository, it is very large (>1.2 GB) and generally not recommended for standard users to clone the entire repository.

    git clone https://github.com/tesseract-ocr/tessdata.git tesseract-ocr.tessdata
  5. Leptonica implementation characteristics

    master

    When integrating Leptonica, keep the following implementation details in mind:

    • Efficiency: Image data is packed binary into 32-bit words, and operations prioritize 32-bit data processing.
    • Consistency: Data is allocated on the heap with simple ownership rules. Function names typically follow a pattern starting with the primary data structure (e.g., pix_).
    • Robustness: The library checks all pointer arguments and makes extensive use of accessors.
    • Thread-safety: It is thread-safe, utilizing atomic operations for reference counting.
    • Portability: It is endian-independent and builds on Linux, macOS, MinGW, Cygwin, and Windows.
  6. Use the PngMinus utilities (png2pnm and pnm2png)

    master

    PngMinus provides two lightweight utilities for converting between PNG and Netpbm formats (PGM and PPM). These utilities are designed to be simple and can be used as standalone programs or as part of a command-line pipe.

    • png2pnm: Converts PNG images to Netpbm formats.
    • pnm2png: Converts Netpbm formats to PNG images.

    Key Features & Limitations:

    • Alpha Channels: Supports alpha channels. When reading PNGs, the alpha channel can be written to a PGM file. When creating PNGs, you can combine a PPM file with a PGM file containing the alpha channel.
    • Gamma Correction: Does not perform gamma correction. To correct gamma when reading PNGs, pipe the output of png2pnm to pnmgamma.
    • Memory Limit: Due to its design, it cannot handle images requiring memory blocks larger than 64k (on DOS versions); larger images will trigger an out-of-memory error.
    • File Formats: Supports PGM and PPM. It does not handle B&W PBM files directly, but you can use PGM with a bit-depth of 1.
    • Input/Output: Files can be provided as command-line parameters or via standard input/output redirection.

    To see all available options, use the -h flag.

    # List options for png2pnm
    png2pnm -h
    
    # List options for pnm2png
    pnm2png -h
    
    # Example of piping for gamma correction
    png2pnm input.png | pnmgamma > output.pnm
  7. Build VisualPng using Microsoft Developer Studio

    master

    To build the VisualPng demo, you must use Microsoft Developer Studio and ensure the following dependencies are present in your environment:

    • libpng DLLs and LIBs: ..\..\projects\msvc\win32\libpng
    • zlib DLLs and LIBs: ..\..\projects\msvc\win32\zlib
    • libpng header files: ..\..\..\libpng
    • zlib header files: ..\..\..\zlib
    • pngsuite images: ..\pngsuite

    Build Steps:

    1. Go to Build | Set Active configuration and choose the configuration corresponding to the library you want to test. (Note: The library must have been previously built using the libpng MS project in the ..\..\mscv subdirectory).
    2. Select Build | Clean.
    3. Select Build | Rebuild All.
    4. Once compiled, VisualPng will launch automatically. Use Ctrl-N or Ctrl-V to view different images from the PngSuite directory.
  8. Build PngMinus from source

    master

    PngMinus can be built using a provided Makefile. The build process generates two versions of each utility: one linked with static libraries and one using shared libraries.

    Dependencies:

    • libpng
    • zlib

    Build Configuration: By default, the Makefile expects:

    • libpng libraries in ../..
    • libz in ../../../zlib

    You can modify these paths in the Makefile to match your environment (e.g., ../libpng and ../zlib).

    Testing: If you have a ../pngsuite directory containing basn####.png files from PngSuite, you can verify the build by running the pngminus.sh script.

    # To test the build if PngSuite files are present
    ./pngminus.sh
  9. Build libpng using Microsoft Visual Studio

    master

    This directory provides a Visual Studio solution for building libpng. While designed for Visual Studio 2019, it may work with earlier or later versions.

    Prerequisites: Before attempting to build, you must edit the zlib.props file with the required information. Failure to configure zlib.props first may prevent a successful build.

  10. Link libpng to your application

    master

    Depending on your project requirements, choose between the release or release library configurations. It is critical that your application's runtime library settings match the libpng configuration to avoid crashes.

    Option 1: Dynamic Linking (DLL)

    Use the release configuration. This builds libpng as a DLL using the MultiThreaded DLL runtime.

    • Requirement: Your application must be built with the /MD (MultiThreaded DLL) option.
    • Warning: If your application is not built with /MD, it will crash inside libpng16.dll when attempting to read from a file handle.

    Option 2: Static Linking

    Use the release library configuration. This builds a static library using the MultiThreaded runtime.

    • Requirement: Your application must be compiled with the /MT (MultiThreaded) option.
    • Warning: Using a different runtime option will cause build failures or runtime crashes.
  11. Install Leptonica using vcpkg

    master

    You can build and install Leptonica using the vcpkg dependency manager. Follow these steps to clone, bootstrap, and install the library:

    1. Clone the vcpkg repository.
    2. Run the bootstrap script (use .bat for PowerShell on Windows).
    3. Integrate vcpkg with your build system.
    4. Install the leptonica package.
    git clone https://github.com/Microsoft/vcpkg.git
    cd vcpkg
    ./bootstrap-vcpkg.sh # "./bootstrap-vcpkg.bat" for powershell
    ./vcpkg integrate install
    ./vcpkg install leptonica
  12. Manage JPEG comment (COM) blocks with rdjpgcom and wrjpgcom

    master

    JPEG files can contain COM blocks for text annotations. These utilities allow you to read and write them.

    Read comments with rdjpgcom: rdjpgcom [-raw] [-verbose] [inputfilename]

    • -raw: Output non-printable characters (normally escaped).
    • -verbose: Display JPEG image dimensions.

    Write comments with wrjpgcom: wrjpgcom [switches] [inputfilename] [outputfilename]

    • -replace: Delete any existing COM blocks.
    • -comment "text": Supply new comment text via command line.
    • -cfile name: Read comment text from a specified file.

    Caution: Do not attempt to overwrite the input file by redirecting wrjpgcom output back into it; this may destroy the file.

    wrjpgcom -replace -comment "My Annotation" input.jpg output.jpg