- 公开颜色过滤与字符集限制扩展 API,修复宏路径 - 库内打印替换为 tracing 日志,清理遗留废弃代码 - 补充核心逻辑单元测试与 crate 元数据 - 开启 missing_docs 并统一 rustfmt/clippy 格式
64 lines
1.8 KiB
Rust
64 lines
1.8 KiB
Rust
//! 快速开始示例:演示 ddddocr-core 与引擎 crate 的解耦用法。
|
|
//!
|
|
//! 运行:`cargo run -p ddddocr-core --example quick_start`
|
|
|
|
use ddddocr_core::error::{Result, TensorError};
|
|
use ddddocr_core::traits::{InferenceEngine, Info, OcrEngine};
|
|
use ddddocr_core::types::{ModelInfo, TensorInfo};
|
|
use ddddocr_core::{ModelMetadata, Normalization, OcrBuilder, OcrOutput, Resize};
|
|
|
|
/// 演示引擎:只实现接口,不接入真实 ONNX 运行时。
|
|
struct DemoEngine {
|
|
meta: ModelMetadata,
|
|
}
|
|
|
|
impl Info for DemoEngine {
|
|
fn input_info(&self) -> Result<Vec<TensorInfo>> {
|
|
Ok(vec![])
|
|
}
|
|
fn output_info(&self) -> Result<Vec<TensorInfo>> {
|
|
Ok(vec![])
|
|
}
|
|
fn model_info(&self) -> Result<ModelInfo> {
|
|
Ok(ModelInfo {
|
|
inputs: vec![],
|
|
outputs: vec![],
|
|
providers: None,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl InferenceEngine for DemoEngine {
|
|
type Output = OcrOutput;
|
|
|
|
fn inference(&self, input: ndarray::Array4<f32>) -> Result<Self::Output, TensorError> {
|
|
// 用全零 logits 模拟推理输出:[Steps, Classes]
|
|
let steps = input.shape()[2];
|
|
let classes = self.meta.charset.size();
|
|
Ok(OcrOutput::Logits(ndarray::Array2::zeros((steps, classes))))
|
|
}
|
|
}
|
|
|
|
impl OcrEngine for DemoEngine {
|
|
fn metadata(&self) -> &ModelMetadata {
|
|
&self.meta
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let engine = DemoEngine {
|
|
meta: ModelMetadata::from_static_slice(
|
|
&["", "a", "b"],
|
|
false,
|
|
Resize::Fixed(64, 64),
|
|
1,
|
|
Normalization::ZeroToOne,
|
|
),
|
|
};
|
|
|
|
let ocr = OcrBuilder::new().probability(true).build_with(&engine);
|
|
let image = image::DynamicImage::new_luma8(64, 64);
|
|
let result = ocr.predict(&image).expect("识别失败");
|
|
println!("识别结果: {result}");
|
|
}
|