RTesseract Ruby Library

repository·master·Indexed 21 days ago

https://github.com/dannnylo/rtesseract

A Ruby wrapper for the Tesseract OCR engine that allows developers to convert images into text, searchable PDFs, or TSV data. It provides methods for extracting text strings, retrieving word bounding box positions and confidence scores via RTesseract::Box, and configuring OCR languages and custom Tesseract configuration files.

Tokens
1.9K
Snippets
15
Records
15
Agent score
75%

What's inside RTesseract

  1. Convert images to text, PDF, or TSV

    master

    Use RTesseract.new to initialize an instance with an image path, then call the appropriate conversion method.

    • to_s: Returns the extracted text as a string.
    • to_pdf: Returns an open file object of a searchable PDF (preserves image colors, pictures, and structure).
    • to_tsv: Returns an open file object of the TSV data.
    image = RTesseract.new("my_image.jpg")
    
    # Get text string
    text = image.to_s
    
    # Get searchable PDF
    pdf = image.to_pdf
    
    # Get TSV file
    tsv = image.to_tsv
  2. Install RTesseract

    master

    RTesseract is a Ruby wrapper for Tesseract OCR. Before installing the gem, you must ensure the Tesseract OCR binaries are installed on your system.

    1. Verify Tesseract installation:

      $ tesseract --version
    2. Install Tesseract if missing:

      • Ubuntu/Debian: apt install tesseract-ocr
      • macOS (Homebrew): brew install tesseract
      • Heroku: Use the heroku-buildpack-tesseract buildpack.
    3. Install the Gem: Add to your Gemfile:

      gem 'rtesseract'

      Then run bundle, or install it directly via gem install rtesseract.

    gem 'rtesseract'
  3. Configure RTesseract global settings

    master

    You can configure global settings for the RTesseract gem using the RTesseract.configure method. This method yields a configuration object that allows you to modify default settings like the command path or the debug_file location. These settings will apply to all subsequent calls unless overridden locally.

    RTesseract.configure do |config|
      config.command = '/usr/local/bin/tesseract'
      config.debug_file = 'tesseract_debug.log'
    end
  4. Get words with bounding box positions

    master

    The to_box method returns an array of hashes containing the extracted words along with their confidence scores and pixel coordinates (x_start, y_start, x_end, y_end).

    RTesseract.new('test_words.png').to_box
    # => [
    #      { :word => 'If', :confidence=>89, :x_start=>52, :y_start=>13, :x_end=>63, :y_end=>27},
    #      ...
    #    ]
  5. Configure Tesseract with custom config files

    master

    Use the config_file option to pass specific Tesseract configuration settings. You can pass a symbol or a string containing multiple configuration parameters.

    # Using a symbol for a predefined config
    RTesseract.new('test.jpg', config_file: :digits)
    
    # Using a string for custom parameters
    RTesseract.new('test.jpg', config_file: 'digits quiet')
  6. Configure OCR language

    master

    You can specify the language for OCR by passing the lang option to RTesseract.new. Ensure the corresponding language pack is installed in your system's Tesseract installation.

    Supported examples:

    • eng (English)
    • deu (German)
    • deu-f (German fraktur)
    • fra (French)
    • ita (Italian)
    • nld (Dutch)
    • por (Portuguese)
    • spa (Spanish)
    • vie (Vietnamese)
    RTesseract.new('test.jpg', lang: 'deu')
  7. Get words and their positions with to_box

    master

    The to_box method returns detailed information about words and their bounding boxes. You can use the words helper method to quickly retrieve an array of just the extracted word strings.

    ocr = RTesseract.new('image.png')
    
    # Get detailed box information
    batches = ocr.to_box
    
    # Get just the array of words
    word_list = ocr.words
  8. Initialize RTesseract

    master

    To use RTesseract, instantiate the RTesseract class by providing the path to your image source and an optional hash of configuration options. The configuration is merged with the global RTesseract.config.

    # src is the path to the image file
    # options is a hash of configuration overrides
    ocr = RTesseract.new('path/to/image.png', lang: 'eng')
  9. Get word positions and confidence using RTesseract::Box

    master

    When you need to know the exact location of words within an image (bounding boxes) and their OCR confidence scores, you can use the RTesseract::Box module. This module parses HOCR output to provide structured data for each detected word.

    Each word entry in the returned array is a hash containing:

    • word: The recognized text string.
    • confidence: The OCR confidence score (integer).
    • x_start: The horizontal starting position.
    • y_start: The vertical starting position.
    • x_end: The horizontal ending position.
    • y_end: The vertical ending position
    # Note: This is a conceptual usage based on the Box module's internal logic.
    # Typically, you would trigger this via an RTesseract instance configured for HOCR.
    
    results = RTesseract::Box.run(source, errors, options)
    
    results.each do |info| 
      puts "Word: #{info[:word]} | Confidence: #{info[:confidence]} | Position: (#{info[:x_start]}, #{info[:y_start]}) to (#{info[:x_end]}, #{info[:y_end]})"
    end