refactor(errors): 重构错误处理,支持强类型匹配并剥离 base64 依赖
- 新增 Other变体以及构造函数new - 剥离图像预处理中的 Base64 相关错误至业务层处理 - 引入强类型 `LogitsDimensionMismatch` 替代不便匹配的字符串错误 - 优化 `normalize_ocr_logits` 的转换流程,兼顾零拷贝性能与精细化报错 - 优化 全库错误处理
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::loader::ModelLoader;
|
||||
use anyhow::Context;
|
||||
use ddddocr_core::error::{DdddError, Result};
|
||||
use ddddocr_core::error::{DdddError, Result, TensorErrorReason};
|
||||
use ddddocr_core::{DetEngine, DetOutput, InferenceEngine};
|
||||
use ndarray::Ix3;
|
||||
use std::path::Path;
|
||||
@@ -42,26 +42,28 @@ impl InferenceEngine for DetSession {
|
||||
// let result = self.ocr.run(tvec!(tensor.into()))?;
|
||||
let tensor = Tensor::from(input_array);
|
||||
|
||||
let mut result = self
|
||||
.session
|
||||
.run(tvec!(tensor.into()))
|
||||
.context("执行模型推理失败")?;
|
||||
let mut result = self.session.run(tvec!(tensor.into())).map_err(|_| {
|
||||
DdddError::Inference(TensorErrorReason::EngineError(
|
||||
"执行模型推理失败".to_string(),
|
||||
))
|
||||
})?;
|
||||
println!("模型输出原始数据: {:?}", result);
|
||||
// Ok(result.swap_remove(0).into_tensor())
|
||||
let raw_tensor = result.swap_remove(0).into_tensor();
|
||||
let array_d = raw_tensor
|
||||
.into_array::<f32>()
|
||||
.context("Tract 实体张量无法转换为 ndarray::ArrayD")?;
|
||||
let array_d = raw_tensor.into_array::<f32>().map_err(|_| {
|
||||
DdddError::Inference(TensorErrorReason::EngineError(
|
||||
"Tract 实体张量无法转换为 ndarray::ArrayD".to_string(),
|
||||
))
|
||||
})?;
|
||||
// 提前利用克隆(Clone)备份好当前未转维度前的真实 shape (Vec<usize>)
|
||||
let actual_shape = array_d.shape().to_vec();
|
||||
|
||||
let array3 =
|
||||
array_d
|
||||
.into_dimensionality::<Ix3>()
|
||||
.map_err(|_| DdddError::DimensionMismatch {
|
||||
expected: "3D 检测矩阵 [Batch, Box_Count, Box_Attributes]".to_string(),
|
||||
actual: actual_shape, // 优雅降维失败时动态捕获
|
||||
})?;
|
||||
let array3 = array_d.into_dimensionality::<Ix3>().map_err(|_| {
|
||||
DdddError::Inference(TensorErrorReason::TensorDimensionMismatch {
|
||||
expected: "3D 检测矩阵 [Batch, Box_Count, Box_Attributes]".to_string(),
|
||||
actual: actual_shape, // 优雅降维失败时动态捕获
|
||||
})
|
||||
})?;
|
||||
Ok(DetOutput::Detection(array3))
|
||||
|
||||
// 在引擎内部消化掉 DatumType 强耦合
|
||||
|
||||
Reference in New Issue
Block a user