feat(ocr,det,slide): 重构配置解析流程,移除非必要的生命周期方法

- 优化 规范化模型目录
- 重构 Ocr,Detector配置解析流程
This commit is contained in:
2026-07-08 15:48:56 +08:00
parent 31271e80db
commit 0cf3d5fefb
19 changed files with 306 additions and 214 deletions

View File

@@ -1,4 +1,4 @@
use ddddocr_rs::{Ocr, Slider, Detector, ModelMetadata}; // 假设你的包名是这个
use ddddocr_rs::{Ocr, Slider, Detector, ModelMetadata, OcrSession, DetBuilder, DetSession}; // 假设你的包名是这个
use image::{DynamicImage, Rgb};
use std::fs;
use std::path::Path;
@@ -64,20 +64,20 @@ fn save_debug_image(
#[test]
fn test_full_classification() {
// 1. 初始化模型
let ocr = Ocr::new("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx",ModelMetadata::from_builtin_beta()).expect("模型加载失败");
let ocr = OcrSession::new("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx",ModelMetadata::from_builtin_beta()).expect("模型加载失败");
// 2. 加载测试图片
let img = image::open("samples/code2.png").expect("测试图片不存在");
// 3. 执行识别
let result = ocr.predictor().predict(&img).expect("识别过程出错").into_text();
let result = Ocr::new(&ocr).predict(&img).expect("识别过程出错").into_text();
println!("识别结果: {}", result);
assert!(!result.is_empty());
}
#[test]
fn test_det_load() -> anyhow::Result<()> {
let det = Detector::new("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")?;
let det = DetSession::new("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")?;
let image_path = "samples/det1.png";
let image_bytes =
fs::read(image_path).map_err(|e| anyhow::anyhow!("无法读取图片 {}: {}", image_path, e))?;
@@ -89,8 +89,8 @@ fn test_det_load() -> anyhow::Result<()> {
.map_err(|e| anyhow::anyhow!("图片解码失败: {}", e))?;
// 【修改点 2】传入统一的 &DynamicImage 引用
let bboxes = det.predict(&img)?;
println!("{:?}", det);
let bboxes = Detector::new(&det).predict(&img)?;
// println!("{:?}", det);
println!("检测到的目标数量: {}", bboxes.len());
if bboxes.is_empty() {