11 个稳定版本
1.1.10 | 2024 年 3 月 25 日 |
---|---|
1.1.9 | 2023 年 10 月 11 日 |
1.1.7 | 2023 年 7 月 26 日 |
1.1.5 | 2023 年 6 月 20 日 |
1.0.0 | 2022 年 2 月 24 日 |
在 机器学习 中排名第 24
每月下载量 1,333 次
51KB
731 行
rusty-tesseract
Google Tesseract 的 Rust 封装
安装
将以下行添加到您的 Cargo.toml 文件中
rusty-tesseract = "1.1.10"
描述
- 将所有相关的 tesseract 命令行功能带到 Rust
- 部分基于 tesseract 的 Python 封装(即 https://github.com/madmaze/pytesseract)
- 允许测试预训练的 tesseract 模型,并以不同的格式(如字符串、边界框、字典或数据框)输出结果。
依赖项
Tesseract: https://github.com/tesseract-ocr/tesseract
用法
1. 读取图像
通过指定路径或使用来自 image crate 的 DynamicImage 创建一个 Image 对象 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. 设置 tesseract 参数
使用 Args 结构体设置 tesseract 参数。
let default_args = Args::default();
// the default parameters are
/*
Args {
lang: "eng",
dpi: Some(150),
psm: Some(3),
oem: Some(3),
}
*/
// fill your own argument struct if needed
// Optional arguments are ignored if set to `None`
let mut my_args = Args {
//model language (tesseract default = 'eng')
//available languages can be found by running 'rusty_tesseract::get_tesseract_langs()'
lang: "eng",
//map of config variables
//this example shows a whitelist for the normal alphabet. Multiple arguments are allowed.
//available arguments can be found by running 'rusty_tesseract::get_tesseract_config_parameters()'
config_variables: HashMap::from([(
"tessedit_char_whitelist".into(),
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".into(),
)]),
dpi: Some(150), // specify DPI for input image
psm: Some(6), // define page segmentation mode 6 (i.e. "Assume a single uniform block of text")
oem: Some(3), // define optical character recognition mode 3 (i.e. "Default, based on what is available")
};
3. 获取 tesseract 模型输出
选择字符串、边界框或数据输出
// define parameters
let mut my_args = Args {
lang: "eng",
config_variables: HashMap::from([(
"tessedit_char_whitelist".into(),
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".into(),
)]),
dpi: Some(150),
psm: Some(6),
oem: Some(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);
获取有关 tesseract 的信息
//tesseract version
let tesseract_version = rusty_tesseract::get_tesseract_version().unwrap();
println!("The tesseract version is: {:?}", tesseract_version);
//available languages
let tesseract_langs = rusty_tesseract::get_tesseract_langs().unwrap();
println!("The available languages are: {:?}", tesseract_langs);
//available config parameters
let parameters = rusty_tesseract::get_tesseract_config_parameters().unwrap();
println!("Example config parameter: {}", parameters.config_parameters.first().unwrap());
贡献
- 分支仓库
- 创建一个新的功能分支(
git checkout -b my-feature-branch-name
) - 提交您的最新更改 (
git commit -m '提交信息' <更改的文件>
) - 将更改推送到分支 (
git push origin my-feature-branch-name
) - 创建 Pull Request
依赖项
~4–14MB
~185K SLoC