feat(model): 新增 ModelLoader 链式构建 API 及 ORT GPU/Tract 多线程配置

- 在 ddddocr-core 中定义 ModelBuilder Trait 及其错误类型
- ddddocr-ort 支持 use_gpu、device_id 及 num_threads 链式配置与 CUDA 硬件加速
- ddddocr-tract 基于 multithread-mm 特性支持 CPU 线程数控制
- 支持基于 tract-linalg 配置推理线程数,显式引入 tract-linalg 的 multithread-mm 特性,控制 GEMM 算子并发
- 优化线程池加载策略,适配 Tokio 异步及 CLI 等多场景
This commit is contained in:
2026-07-27 20:22:24 +08:00
parent 44dae08221
commit 7d159c5702
28 changed files with 1583 additions and 70 deletions

View File

@@ -5,12 +5,12 @@ edition = { workspace = true }
license = { workspace = true }
[dependencies]
image = "0.25.10"
base64 = "0.22.1"
imageproc = { version = "0.26.2", default-features = true }
serde = { workspace = true }
serde_json = "1.0.150"
ndarray = { workspace = true } # 继承自工作空间
base64 = { workspace = true }
image = { workspace = true }
imageproc = { workspace = true }
thiserror = { workspace = true } # 刚好可以开始接入你需要的标准库错误处理
tracing={workspace = true}
#serde = { workspace = true, features = ["derive"] }
tracing={workspace = true}

View File

@@ -1,16 +1,16 @@
pub mod det;
mod det;
pub mod error;
pub mod ocr;
mod ocr;
mod slide;
pub mod utils;
use crate::error::{Result, TensorError};
use error::{Result, TensorError};
use std::path::Path;
pub use crate::slide::{SlideResult, Slider};
pub use crate::det::{DetBuilder, DetectionResult, Detector};
pub use crate::ocr::{Ocr, OcrBuilder, OcrResult};
pub use crate::ocr::{ModelMetadata,Normalization};
pub use ocr::Charset;
pub use crate::ocr::{Charset, ModelMetadata, Normalization, Ocr, OcrBuilder, OcrResult, Resize};
pub use crate::slide::{SlideResult, Slider};
// DetSession
pub enum OcrOutput {
@@ -27,7 +27,7 @@ pub enum DetOutput {
pub trait InferenceEngine {
/// 关联类型:具体的 Session 需要声明自己到底产出什么枚举
type Output;
fn inference(&self, input_array: ndarray::Array4<f32>) -> Result<Self::Output,TensorError>;
fn inference(&self, input_array: ndarray::Array4<f32>) -> Result<Self::Output, TensorError>;
}
pub trait OcrEngine: InferenceEngine<Output = OcrOutput> {
@@ -35,3 +35,11 @@ pub trait OcrEngine: InferenceEngine<Output = OcrOutput> {
}
pub trait DetEngine: InferenceEngine<Output = DetOutput> {}
pub trait ModelBuilder {
type Session;
type Error;
fn model_for_path<P: AsRef<Path>>(&self,model_path: P) -> Result<Self::Session, Self::Error>;
fn model_from_bytes(&self,model_bytes: &[u8]) -> Result<Self::Session, Self::Error>;
}

View File

@@ -5,7 +5,6 @@ use crate::ocr::color_filter::{HsvRange, apply_to_image};
use crate::utils::image_convert::png_rgba_white_preprocess;
use crate::utils::image_processor::{convert_to_grayscale, resize_image};
use image::DynamicImage;
use serde::Serialize;
use std::borrow::Cow;
use std::fmt;
// use tract_onnx::prelude::tract_ndarray::{ Ix2, s};
@@ -19,7 +18,7 @@ use ndarray::ArrayView2;
use crate::error::{ImagePreprocessError, Result, TensorError};
use crate::{OcrEngine, OcrOutput};
use tracing::{ warn};
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone)]
pub enum OcrResult {
/// 纯文本分支(对应 probability = false
Text(String),

View File

@@ -1,8 +1,8 @@
pub mod image_convert;
mod image_helper;
pub mod image_processor;
mod tensor_transform;
mod image_helper;
// 对外统一暴露干净的 API 语义层
pub use image_convert::ColorMode;
pub use tensor_transform::normalize_ocr_logits;
pub use image_convert::{ColorMode};