refactor(core): 提炼公共类型
- 将 AxisDim、TensorInfo 等公共类型下沉至 ddddocr_core::types - 项目结构优化
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use anyhow::Context;
|
||||
use ddddocr_core::{DetectionResult, ModelBuilder};
|
||||
use ddddocr_core::{Detector, ModelMetadata, Normalization, Slider};
|
||||
use ddddocr_core::traits::Loader;
|
||||
use ddddocr_tract::{DetectionResult, Ocr};
|
||||
use ddddocr_tract::{Detector, ModelMetadata, Normalization, Slider};
|
||||
// 假设你的包名是这个
|
||||
use ddddocr_tract::{DetSession, OcrBuilder, OcrSession};
|
||||
use ddddocr_tract::{DetRuntime, OcrRuntime};
|
||||
use image::{DynamicImage, ImageBuffer, Luma, Rgb};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
@@ -11,7 +12,7 @@ mod char_slice;
|
||||
use char_slice::CHARSET_BETA;
|
||||
use ddddocr_core::Resize;
|
||||
|
||||
use ddddocr_tract::loader::{ TractModelLoader};
|
||||
use ddddocr_tract::loader::ModelLoader as TractModelLoader;
|
||||
|
||||
fn load_image<P: AsRef<Path>>(path: P) -> anyhow::Result<image::DynamicImage> {
|
||||
// 1. 先将泛型转为具体的 &Path 引用
|
||||
@@ -104,9 +105,10 @@ fn save_rust_result(result: &ImageBuffer<Luma<f32>, Vec<f32>>, filename: &str) {
|
||||
}
|
||||
#[test]
|
||||
fn test_full_classification() {
|
||||
let model = TractModelLoader::builder()
|
||||
.model_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx")
|
||||
let session = TractModelLoader::default()
|
||||
.build_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx")
|
||||
.expect("模型加载失败");
|
||||
|
||||
let metadata = ModelMetadata::from_static_slice(
|
||||
CHARSET_BETA,
|
||||
false,
|
||||
@@ -115,18 +117,18 @@ fn test_full_classification() {
|
||||
Normalization::MinusOneToOne,
|
||||
);
|
||||
// 1. 初始化模型
|
||||
let ocr = OcrSession::new(model, metadata);
|
||||
let ocr_runtime = OcrRuntime::new(session, 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_runtime)
|
||||
// .predict(&img)
|
||||
// .expect("识别过程出错")
|
||||
// .into_text();
|
||||
let result = OcrBuilder::new()
|
||||
.build(&ocr)
|
||||
let result = Ocr::builder()
|
||||
.runner(&ocr_runtime)
|
||||
.predict(&img)
|
||||
.expect("识别过程出错")
|
||||
.into_text();
|
||||
@@ -136,11 +138,10 @@ fn test_full_classification() {
|
||||
}
|
||||
#[test]
|
||||
fn test_det_load() -> anyhow::Result<()> {
|
||||
let det_model =
|
||||
TractModelLoader::builder()
|
||||
.model_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")
|
||||
.expect("模型加载失败");
|
||||
let det = DetSession::new(det_model);
|
||||
let det_model = TractModelLoader::default()
|
||||
.build_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")
|
||||
.expect("模型加载失败");
|
||||
let det = DetRuntime::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))?;
|
||||
@@ -240,11 +241,11 @@ fn test_real_slide_comparison() {
|
||||
#[test]
|
||||
fn test_resolve_shape_logic_direct() {
|
||||
// 创建一个哑 ModelLoader 实例(session 用不上,因为我们直接测私有方法)
|
||||
let loader = TractModelLoader::builder()
|
||||
.model_for_path(
|
||||
// "D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx",
|
||||
"D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_huashi666_i64.onnx",
|
||||
)
|
||||
.expect("建立测试模型图失败");
|
||||
let loader = TractModelLoader::default()
|
||||
.build_for_path(
|
||||
// "D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx",
|
||||
"D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_huashi666_i64.onnx",
|
||||
)
|
||||
.expect("建立测试模型图失败");
|
||||
println!("{:?}", loader.model().inputs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user