use image::RgbImage;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub enum PredictionResult<'a, I = Arc<RgbImage>> {
Detection {
input_path: Vec<Cow<'a, str>>,
index: Vec<usize>,
input_img: Vec<I>,
dt_polys: Vec<Vec<crate::processors::BoundingBox>>,
dt_scores: Vec<Vec<f32>>,
},
Recognition {
input_path: Vec<Cow<'a, str>>,
index: Vec<usize>,
input_img: Vec<I>,
rec_text: Vec<Cow<'a, str>>,
rec_score: Vec<f32>,
},
Classification {
input_path: Vec<Cow<'a, str>>,
index: Vec<usize>,
input_img: Vec<I>,
class_ids: Vec<Vec<usize>>,
scores: Vec<Vec<f32>>,
label_names: Vec<Vec<Cow<'a, str>>>,
},
Rectification {
input_path: Vec<Cow<'a, str>>,
index: Vec<usize>,
input_img: Vec<I>,
rectified_img: Vec<I>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OwnedPredictionResult<I = Arc<RgbImage>> {
Detection {
input_path: Vec<String>,
index: Vec<usize>,
#[serde(skip)]
input_img: Vec<I>,
dt_polys: Vec<Vec<crate::processors::BoundingBox>>,
dt_scores: Vec<Vec<f32>>,
},
Recognition {
input_path: Vec<String>,
index: Vec<usize>,
#[serde(skip)]
input_img: Vec<I>,
rec_text: Vec<String>,
rec_score: Vec<f32>,
},
Classification {
input_path: Vec<String>,
index: Vec<usize>,
#[serde(skip)]
input_img: Vec<I>,
class_ids: Vec<Vec<usize>>,
scores: Vec<Vec<f32>>,
label_names: Vec<Vec<String>>,
},
Rectification {
input_path: Vec<String>,
index: Vec<usize>,
#[serde(skip)]
input_img: Vec<I>,
#[serde(skip)]
rectified_img: Vec<I>,
},
}
impl<'a, I> PredictionResult<'a, I> {
pub fn input_paths(&self) -> &[Cow<'a, str>] {
match self {
PredictionResult::Detection { input_path, .. } => input_path,
PredictionResult::Recognition { input_path, .. } => input_path,
PredictionResult::Classification { input_path, .. } => input_path,
PredictionResult::Rectification { input_path, .. } => input_path,
}
}
pub fn indices(&self) -> &[usize] {
match self {
PredictionResult::Detection { index, .. } => index,
PredictionResult::Recognition { index, .. } => index,
PredictionResult::Classification { index, .. } => index,
PredictionResult::Rectification { index, .. } => index,
}
}
pub fn input_images(&self) -> &[I] {
match self {
PredictionResult::Detection { input_img, .. } => input_img,
PredictionResult::Recognition { input_img, .. } => input_img,
PredictionResult::Classification { input_img, .. } => input_img,
PredictionResult::Rectification { input_img, .. } => input_img,
}
}
pub fn is_detection(&self) -> bool {
matches!(self, PredictionResult::Detection { .. })
}
pub fn is_recognition(&self) -> bool {
matches!(self, PredictionResult::Recognition { .. })
}
pub fn is_classification(&self) -> bool {
matches!(self, PredictionResult::Classification { .. })
}
pub fn is_rectification(&self) -> bool {
matches!(self, PredictionResult::Rectification { .. })
}
pub fn into_owned(self) -> OwnedPredictionResult<I> {
match self {
PredictionResult::Detection {
input_path,
index,
input_img,
dt_polys,
dt_scores,
} => OwnedPredictionResult::Detection {
input_path: input_path.into_iter().map(|cow| cow.into_owned()).collect(),
index,
input_img,
dt_polys,
dt_scores,
},
PredictionResult::Recognition {
input_path,
index,
input_img,
rec_text,
rec_score,
} => OwnedPredictionResult::Recognition {
input_path: input_path.into_iter().map(|cow| cow.into_owned()).collect(),
index,
input_img,
rec_text: rec_text.into_iter().map(|cow| cow.into_owned()).collect(),
rec_score,
},
PredictionResult::Classification {
input_path,
index,
input_img,
class_ids,
scores,
label_names,
} => OwnedPredictionResult::Classification {
input_path: input_path.into_iter().map(|cow| cow.into_owned()).collect(),
index,
input_img,
class_ids,
scores,
label_names: label_names
.into_iter()
.map(|vec| vec.into_iter().map(|cow| cow.into_owned()).collect())
.collect(),
},
PredictionResult::Rectification {
input_path,
index,
input_img,
rectified_img,
} => OwnedPredictionResult::Rectification {
input_path: input_path.into_iter().map(|cow| cow.into_owned()).collect(),
index,
input_img,
rectified_img,
},
}
}
}
impl<I> OwnedPredictionResult<I> {
pub fn input_paths(&self) -> &[String] {
match self {
OwnedPredictionResult::Detection { input_path, .. } => input_path,
OwnedPredictionResult::Recognition { input_path, .. } => input_path,
OwnedPredictionResult::Classification { input_path, .. } => input_path,
OwnedPredictionResult::Rectification { input_path, .. } => input_path,
}
}
pub fn indices(&self) -> &[usize] {
match self {
OwnedPredictionResult::Detection { index, .. } => index,
OwnedPredictionResult::Recognition { index, .. } => index,
OwnedPredictionResult::Classification { index, .. } => index,
OwnedPredictionResult::Rectification { index, .. } => index,
}
}
pub fn input_images(&self) -> &[I] {
match self {
OwnedPredictionResult::Detection { input_img, .. } => input_img,
OwnedPredictionResult::Recognition { input_img, .. } => input_img,
OwnedPredictionResult::Classification { input_img, .. } => input_img,
OwnedPredictionResult::Rectification { input_img, .. } => input_img,
}
}
pub fn is_detection(&self) -> bool {
matches!(self, OwnedPredictionResult::Detection { .. })
}
pub fn is_recognition(&self) -> bool {
matches!(self, OwnedPredictionResult::Recognition { .. })
}
pub fn is_classification(&self) -> bool {
matches!(self, OwnedPredictionResult::Classification { .. })
}
pub fn is_rectification(&self) -> bool {
matches!(self, OwnedPredictionResult::Rectification { .. })
}
pub fn as_prediction_result(&self) -> PredictionResult<'_, &I> {
match self {
OwnedPredictionResult::Detection {
input_path,
index,
input_img,
dt_polys,
dt_scores,
} => PredictionResult::Detection {
input_path: input_path
.iter()
.map(|s| Cow::Borrowed(s.as_str()))
.collect(),
index: index.clone(),
input_img: input_img.iter().collect(),
dt_polys: dt_polys.clone(),
dt_scores: dt_scores.clone(),
},
OwnedPredictionResult::Recognition {
input_path,
index,
input_img,
rec_text,
rec_score,
} => PredictionResult::Recognition {
input_path: input_path
.iter()
.map(|s| Cow::Borrowed(s.as_str()))
.collect(),
index: index.clone(),
input_img: input_img.iter().collect(),
rec_text: rec_text.iter().map(|s| Cow::Borrowed(s.as_str())).collect(),
rec_score: rec_score.clone(),
},
OwnedPredictionResult::Classification {
input_path,
index,
input_img,
class_ids,
scores,
label_names,
} => PredictionResult::Classification {
input_path: input_path
.iter()
.map(|s| Cow::Borrowed(s.as_str()))
.collect(),
index: index.clone(),
input_img: input_img.iter().collect(),
class_ids: class_ids.clone(),
scores: scores.clone(),
label_names: label_names
.iter()
.map(|vec| vec.iter().map(|s| Cow::Borrowed(s.as_str())).collect())
.collect(),
},
OwnedPredictionResult::Rectification {
input_path,
index,
input_img,
rectified_img,
} => PredictionResult::Rectification {
input_path: input_path
.iter()
.map(|s| Cow::Borrowed(s.as_str()))
.collect(),
index: index.clone(),
input_img: input_img.iter().collect(),
rectified_img: rectified_img.iter().collect(),
},
}
}
}
pub trait IntoPrediction {
type Out;
fn into_prediction(self) -> Self::Out;
}
pub trait IntoOwnedPrediction {
type Out;
fn into_owned_prediction(self) -> Self::Out;
}
impl<T> IntoOwnedPrediction for T
where
T: IntoPrediction,
T::Out: Into<OwnedPredictionResult>,
{
type Out = OwnedPredictionResult;
fn into_owned_prediction(self) -> Self::Out {
self.into_prediction().into()
}
}
impl<I> From<PredictionResult<'_, I>> for OwnedPredictionResult<I> {
fn from(result: PredictionResult<'_, I>) -> Self {
result.into_owned()
}
}