rusty-tesseract 1.1.1

A Rust wrapper for Google Tesseract
Documentation

rusty-tesseract

A Rust wrapper for Google Tesseract

GitHub last commit GitHub Github All Releases

Installation

Add the following line to your Cargo.toml file:

rusty-tesseract = "1.1.1"

Description

  • Brings all relevant command-line tesseract functionality to Rust
  • Based on the Python wrapper for tesseract (i.e. https://github.com/madmaze/pytesseract)
  • Enables testing a pre-trained tesseract model and outputting the results in different formats such as strings, bounding boxes, dicts, or dataframes.

Dependencies

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

Usage

1. Read Image

Create an Image object by specifying a path or alternatively a DynamicImage from the image crate https://docs.rs/image/latest/image/

// you can use the from_path function
let _ = Image::from_path("img/string.png");


// or instantiate Image from a DynamicImage
let dynamic_image = ImageReader::open("img/string.png")
    .unwrap()
    .decode()
    .unwrap();
let img = Image::from_dynamic_image(&dynamic_image).unwrap();

2. Set tesseract parameters

Set tesseract parameters using the Args struct.

let default_args = Args::default();

// the default parameters are
/*
Args {
    lang: "eng",
    dpi: 150,
    psm: 3,
    oem: 3,
}
*/

// fill your own argument struct if needed
let mut my_args = Args {
    lang: "eng",    // model language (tesseract default = 'eng')
    // use config_variables: "="     if no config variables are required
    config_variables: "'tessedit_char_whitelist=abcdefghijklmnopqrstuvwABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'", // -c VAR=VALUE Set value for config variables. This example shows a whitelist for the normal alphabet.Multiple -c arguments are allowed. Allowed commands can be found by running 'tesseract --print-parameters'
    dpi: 150,       // specify DPI for input image
    psm: 3,         // define page segmentation mode 6 (i.e. "Assume a single uniform block of text")
    oem: 3,         // define optical character recognition mode 3 (i.e. "Default, based on what is available")
};

3. Get the tesseract model output

Choose either string, bounding box or data output:

// define parameters
let mut my_args = Args {
    lang: "eng",
    config_variables: "'tessedit_char_whitelist=abcdefghijklmnopqrstuvwABCDEFGHIJKLMNOPQRSTUVWXYZ'",
    dpi: 150,
    psm: 6,
    oem: 3
};

// string output
let output = rusty_tesseract::image_to_string(&img, &my_args).unwrap();
    println!("The String output is: {:?}", output);



// image_to_boxes creates a BoxOutput containing the parsed output from Tesseract when using the "makebox" Parameter
let box_output = rusty_tesseract::image_to_boxes(&img, &my_args).unwrap();
println!(
    "The first boxfile symbol is: {}",
    box_output.boxes[0].symbol
);
println!("The full boxfile output is:\n{}", box_output.output);

// image_to_data creates a DataOutput containing the parsed output from Tesseract when using the "TSV" Parameter
let data_output = rusty_tesseract::image_to_data(&img, &my_args).unwrap();
let first_text_line = &data_output.data[4];
println!(
    "The first text is '{}' with confidence {}",
    first_text_line.text, first_text_line.conf
);
println!("The full data output is:\n{}", data_output.output);

Get tesseract version

let tesseract_version = rusty_tesseract::get_tesseract_version().unwrap();
println!("The tesseract version is: {:?}", tesseract_version);

Contributing

  1. Fork the repository
  2. Create a new feature branch (git checkout -b my-feature-branch-name)
  3. Commit your new changes (git commit -m 'commit message' <changed-file>)
  4. Push changes to the branch (git push origin my-feature-branch-name)
  5. Create a Pull Request