use std::collections::HashMap;
use image::io::Reader as ImageReader;
use rusty_tesseract::{Args, Image};
fn main() {
let test_image = Image::from_path("img/string.png").unwrap();
println!("test image is stored at: {}", test_image.get_image_path().unwrap());
let dynamic_image = ImageReader::open("img/string.png")
.unwrap()
.decode()
.unwrap();
let img = Image::from_dynamic_image(&dynamic_image).unwrap();
println!("temp image is stored at: {}", img.get_image_path().unwrap());
let default_args = Args::default();
let tesseract_version = rusty_tesseract::get_tesseract_version().unwrap();
println!("The tesseract version is: {}", tesseract_version);
let tesseract_langs = rusty_tesseract::get_tesseract_langs().unwrap();
println!("The available languages are: {:?}", tesseract_langs);
let image_to_string_args = Args {
lang: "eng".into(),
config_variables: HashMap::from([(
"tessedit_char_whitelist".into(),
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".into(),
)]),
dpi: Some(150),
psm: Some(6),
oem: Some(3),
};
let parameters = rusty_tesseract::get_tesseract_config_parameters().unwrap();
println!(
"Example config variable: {}",
parameters.config_parameters.first().unwrap(),
);
let output = rusty_tesseract::image_to_string(&img, &image_to_string_args).unwrap();
println!("\nThe String output is: {}", output);
let box_output = rusty_tesseract::image_to_boxes(&img, &default_args).unwrap();
println!(
"The first boxfile symbol is: {}",
box_output.boxes[0].symbol
);
println!("The full boxfile output is:\n{}", box_output.output);
let data_output = rusty_tesseract::image_to_data(&img, &default_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);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn vertical_text() {
let img = Image::from_path("img/vertical_text.png").unwrap();
let image_to_string_args = Args {
psm: Some(6),
..Default::default()
};
let output = rusty_tesseract::image_to_string(&img, &image_to_string_args).unwrap();
assert_eq!(
output.lines().collect::<Vec<&str>>(),
vec!["D", "O", "L", "O", "R", "S", "I", "", "T"]
);
}
#[test]
fn horizontal_text() {
let img = Image::from_path("img/horizontal_text.png").unwrap();
let default_args = Args::default();
let output = rusty_tesseract::image_to_string(&img, &default_args).unwrap();
assert_eq!(output.trim(), "Lorem ipsum dolor sit amet");
}
#[test]
fn image_to_string() {
let img = Image::from_path("img/string.png").unwrap();
let default_args = Args::default();
let output = rusty_tesseract::image_to_string(&img, &default_args).unwrap();
assert_eq!(output.trim(), "LOREM IPSUM DOLOR SIT AMET");
}
#[test]
fn command_without_options() {
let img = Image::from_path("img/string.png").unwrap();
let args = Args {
dpi: None,
psm: None,
oem: None,
..Args::default()
};
let output = rusty_tesseract::image_to_string(&img, &args).unwrap();
assert_eq!(output.trim(), "LOREM IPSUM DOLOR SIT AMET");
}
#[test]
fn command_with_partial_options() {
let img = Image::from_path("img/string.png").unwrap();
let args = Args {
dpi: Some(300),
psm: None,
oem: None,
..Args::default()
};
let output = rusty_tesseract::image_to_string(&img, &args).unwrap();
assert_eq!(output.trim(), "LOREM IPSUM DOLOR SIT AMET");
}
}