Files
ddddocr-rs/ddddocr-core/src/traits.rs

47 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 推理引擎统一抽象接口。
use crate::error::TensorError;
use crate::types::{ModelInfo, TensorInfo};
use crate::{DetOutput, ModelMetadata, OcrOutput};
use std::path::Path;
/// 查询模型输入/输出信息的接口。
pub trait Info {
fn input_info(&self) -> crate::error::Result<Vec<TensorInfo>>;
fn output_info(&self) -> crate::error::Result<Vec<TensorInfo>>;
fn model_info(&self) -> crate::error::Result<ModelInfo>;
}
/// 推理引擎统一抽象接口,由 ddddocr-tract、ddddocr-ort 等引擎 crate 实现。
pub trait InferenceEngine {
/// 引擎产出的输出枚举OCR 为 [`crate::OcrOutput`],检测为 [`crate::DetOutput`])。
type Output;
fn inference(
&self,
input_array: ndarray::Array4<f32>,
) -> crate::error::Result<Self::Output, TensorError>;
}
/// OCR 引擎接口:输出 [`crate::OcrOutput`],并提供模型元数据。
pub trait OcrEngine: InferenceEngine<Output = OcrOutput> + Info {
fn metadata(&self) -> &ModelMetadata;
}
/// 目标检测引擎接口:输出 [`crate::DetOutput`]。
pub trait DetEngine: InferenceEngine<Output = DetOutput> {}
/// 模型加载器:从本地路径或字节流构建引擎会话。
pub trait Loader {
type Session;
type Error;
fn build_for_path<P: AsRef<Path>>(
&self,
model_path: P,
) -> crate::error::Result<Self::Session, Self::Error>;
fn build_from_bytes(
&self,
model_bytes: &[u8],
) -> crate::error::Result<Self::Session, Self::Error>;
}