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::{DetectionResult, Ocr};
|
||||
use ddddocr_core::traits::Loader;
|
||||
use ddddocr_core::{Detector, ModelMetadata, Normalization, Slider};
|
||||
// 假设你的包名是这个
|
||||
use ddddocr_ort::{DetSession, OcrBuilder, OcrSession};
|
||||
use ddddocr_ort::{DetRuntime, OcrBuilder, 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_ort::loader::OrtModelLoader;
|
||||
use ddddocr_ort::loader::ModelLoader as OrtModelLoader;
|
||||
|
||||
fn load_image<P: AsRef<Path>>(path: P) -> anyhow::Result<image::DynamicImage> {
|
||||
// 1. 先将泛型转为具体的 &Path 引用
|
||||
@@ -104,8 +105,9 @@ fn save_rust_result(result: &ImageBuffer<Luma<f32>, Vec<f32>>, filename: &str) {
|
||||
}
|
||||
#[test]
|
||||
fn test_full_classification() {
|
||||
let model = OrtModelLoader::builder().use_gpu(true)
|
||||
.model_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx")
|
||||
let model = OrtModelLoader::default().use_gpu(false)
|
||||
.build_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_sml2h3_f32.onnx")
|
||||
// .build_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_old.onnx")
|
||||
.expect("模型加载失败");
|
||||
let metadata = ModelMetadata::from_static_slice(
|
||||
CHARSET_BETA,
|
||||
@@ -115,7 +117,7 @@ fn test_full_classification() {
|
||||
Normalization::MinusOneToOne,
|
||||
);
|
||||
// 1. 初始化模型
|
||||
let ocr = OcrSession::new(model, metadata);
|
||||
let ocr = OcrRuntime::new(model, metadata);
|
||||
// 2. 加载测试图片
|
||||
let img =
|
||||
image::open("D:/CNWei/CNW/Rust/ddddocr-rs/samples/code2.png").expect("测试图片不存在");
|
||||
@@ -125,21 +127,24 @@ fn test_full_classification() {
|
||||
// .predict(&img)
|
||||
// .expect("识别过程出错")
|
||||
// .into_text();
|
||||
let result = OcrBuilder::new()
|
||||
.build(&ocr)
|
||||
.predict(&img)
|
||||
.expect("识别过程出错")
|
||||
.into_text();
|
||||
// let result = OcrBuilder::new()
|
||||
// .build(&ocr)
|
||||
// .predict(&img)
|
||||
// .expect("识别过程出错")
|
||||
// .into_text();
|
||||
let res=Ocr::builder().runner(&ocr).predict(&img).expect("s").into_text();
|
||||
|
||||
println!("识别结果: {}", result);
|
||||
assert!(!result.is_empty());
|
||||
// println!("识别结果: {}", result);
|
||||
println!("识别结果: {}", res);
|
||||
// assert!(!result.is_empty());
|
||||
assert!(!res.is_empty());
|
||||
}
|
||||
#[test]
|
||||
fn test_det_load() -> anyhow::Result<()> {
|
||||
let det_model = OrtModelLoader::builder()
|
||||
.model_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")
|
||||
let det_model = OrtModelLoader::default()
|
||||
.build_for_path("D:\\CNWei\\CNW\\Rust\\ddddocr-rs\\models\\common_det.onnx")
|
||||
.expect("模型加载失败");
|
||||
let det = DetSession::new(det_model);
|
||||
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))?;
|
||||
@@ -239,8 +244,8 @@ fn test_real_slide_comparison() {
|
||||
#[test]
|
||||
fn test_resolve_shape_logic_direct() {
|
||||
// 创建一个哑 ModelLoader 实例(session 用不上,因为我们直接测私有方法)
|
||||
let loader = OrtModelLoader::builder()
|
||||
.model_for_path(
|
||||
let loader = OrtModelLoader::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",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user