OAR-OCR 为 Rust 库 oar-ocr 提供基于 Paddle 的 OCR 及文档解析模型资源。本 ModelScope 仓库托管运行时自动下载功能所使用的 ONNX 模型、词典、分词器和结构分析文件。
添加 Rust crate:
cargo add oar-ocr --features auto-download当启用 auto-download 时,已注册的裸文件名会从 ModelScope 上的 greatv/oar-ocr 获取到 $OAR_HOME(默认路径为 ~/.oar),并在使用前进行验证。
use oar_ocr::prelude::*;
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let ocr = OAROCRBuilder::new(
"pp-ocrv5_mobile_det.onnx",
"pp-ocrv5_mobile_rec.onnx",
"ppocrv5_dict.txt",
)
.build()?;
let image = load_image(Path::new("document.jpg"))?;
let results = ocr.predict(vec![image])?;
for region in &results[0].text_regions {
if let Some((text, score)) = region.text_with_confidence() {
println!("{text} ({score:.2})");
}
}
Ok(())
}该结构处理流程整合了 Paddle 布局检测、OCR、表格识别、公式识别及印章检测功能,以生成类 Markdown 格式的文档输出。
use oar_ocr::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let structure = OARStructureBuilder::new("pp-doclayout_plus-l.onnx")
.with_ocr(
"pp-ocrv5_mobile_det.onnx",
"pp-ocrv5_mobile_rec.onnx",
"ppocrv5_dict.txt",
)
.with_table_classification("pp-lcnet_x1_0_table_cls.onnx")
.with_table_structure_recognition("slanet_plus.onnx", "wireless")
.table_structure_dict_path("table_structure_dict_ch.txt")
.build()?;
let result = structure.predict("document.jpg")?;
println!("{}", result.to_markdown());
Ok(())
}本仓库中的模型文件作为 Paddle 框架资源提供,并由 Rust 实现通过 ONNX Runtime 进行使用。有关详细的模型列表、哈希值以及高级用法,请参阅 GitHub 仓库中的项目文档。