refactor(load,tract):将 ModelMetadata JSON 加载逻辑解耦至 ddddocr-tract, 优化 Error 枚举结构与错误透传
- 在 load 模块中精简 Error 与 Result 别名定义 - 增加 ParseError 子类型区分路径与字节流加载失败 - 支持通过 #[from] 自动转换 Tract 引擎底层错误 - 移出 core 中的 serde 依赖,保持核心库纯洁 - 在 tract 中实现 TractModelMetadata 扩展 trait 加载解析配置
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
use anyhow::Context;
|
||||
use ddddocr_core::det::DetectionResult;
|
||||
use ddddocr_core::{DetBuilder, Detector, ModelMetadata, Ocr, Slider}; // 假设你的包名是这个
|
||||
use ddddocr_tract::{DetSession, OcrSession};
|
||||
use ddddocr_core::{Detector, ModelMetadata, Normalization, Ocr, Slider};
|
||||
// 假设你的包名是这个
|
||||
use ddddocr_tract::{DetSession, OcrSession,OcrBuilder};
|
||||
use image::{DynamicImage, ImageBuffer, Luma, Rgb};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use tract_onnx::prelude::{ShapeFact, TDim};
|
||||
use tract_onnx::model;
|
||||
|
||||
mod char_slice;
|
||||
use char_slice::CHARSET_BETA;
|
||||
use ddddocr_core::ocr::metadata::{Normalization, Resize};
|
||||
use ddddocr_core::ocr::Resize;
|
||||
|
||||
use ddddocr_tract::loader::ModelLoader;
|
||||
|
||||
fn load_image<P: AsRef<Path>>(path: P) -> anyhow::Result<image::DynamicImage> {
|
||||
@@ -103,25 +105,29 @@ fn save_rust_result(result: &ImageBuffer<Luma<f32>, Vec<f32>>, filename: &str) {
|
||||
}
|
||||
#[test]
|
||||
fn test_full_classification() {
|
||||
// 1. 初始化模型
|
||||
let ocr = OcrSession::new(
|
||||
let model = ModelLoader::model_for_path(
|
||||
"D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx",
|
||||
ModelMetadata::from_static_slice(
|
||||
CHARSET_BETA,
|
||||
false,
|
||||
Resize::DynamicWidth(64),
|
||||
1,
|
||||
Normalization::MinusOneToOne,
|
||||
),
|
||||
)
|
||||
.expect("模型加载失败");
|
||||
|
||||
let metadata = ModelMetadata::from_static_slice(
|
||||
CHARSET_BETA,
|
||||
false,
|
||||
Resize::DynamicWidth(64),
|
||||
1,
|
||||
Normalization::MinusOneToOne,
|
||||
);
|
||||
// 1. 初始化模型
|
||||
let ocr = OcrSession::new(model, metadata);
|
||||
// 2. 加载测试图片
|
||||
let img =
|
||||
image::open("D:/CNWei/CNW/Rust/ddddocr-rs/samples/code2.png").expect("测试图片不存在");
|
||||
|
||||
// 3. 执行识别
|
||||
let result = Ocr::new(&ocr)
|
||||
// let result = Ocr::new(&ocr)
|
||||
// .predict(&img)
|
||||
// .expect("识别过程出错")
|
||||
// .into_text();
|
||||
let result = OcrBuilder::new().build(&ocr)
|
||||
.predict(&img)
|
||||
.expect("识别过程出错")
|
||||
.into_text();
|
||||
@@ -131,7 +137,10 @@ fn test_full_classification() {
|
||||
}
|
||||
#[test]
|
||||
fn test_det_load() -> anyhow::Result<()> {
|
||||
let det = DetSession::new("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")?;
|
||||
let det_model =
|
||||
ModelLoader::model_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")
|
||||
.expect("模型加载失败");
|
||||
let det = DetSession::new(det_model);
|
||||
let image_path = "D:/CNWei/CNW/Rust/ddddocr-rs/samples/det1.png";
|
||||
let image_bytes =
|
||||
fs::read(image_path).map_err(|e| anyhow::anyhow!("无法读取图片 {}: {}", image_path, e))?;
|
||||
@@ -167,7 +176,7 @@ fn test_det_load() -> anyhow::Result<()> {
|
||||
|
||||
#[test]
|
||||
fn test_real_slide_match() {
|
||||
let engine = Slider::new().unwrap();
|
||||
let engine = Slider::new();
|
||||
|
||||
// 1. 加载你准备好的测试图
|
||||
// 假设图片放在项目根目录下的 assets 文件夹
|
||||
@@ -198,7 +207,7 @@ fn test_real_slide_match() {
|
||||
|
||||
#[test]
|
||||
fn test_real_slide_comparison() {
|
||||
let engine = Slider::new().unwrap();
|
||||
let engine = Slider::new();
|
||||
|
||||
// 1. 加载你准备好的测试图
|
||||
// 假设图片放在项目根目录下的 assets 文件夹
|
||||
@@ -236,6 +245,4 @@ fn test_resolve_shape_logic_direct() {
|
||||
"D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_huashi666_i64.onnx",
|
||||
)
|
||||
.expect("建立测试模型图失败");
|
||||
let md_info = &loader.model_info().context("信息");
|
||||
println!("md_info: {:?}", md_info);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user