use crate::processors::BoundingBox;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextRegion {
pub bounding_box: BoundingBox,
#[serde(default)]
pub dt_poly: Option<BoundingBox>,
#[serde(default)]
pub rec_poly: Option<BoundingBox>,
pub text: Option<Arc<str>>,
pub confidence: Option<f32>,
pub orientation_angle: Option<f32>,
pub word_boxes: Option<Vec<BoundingBox>>,
#[serde(default)]
pub label: Option<Arc<str>>,
}
impl TextRegion {
pub fn new(bounding_box: BoundingBox) -> Self {
Self {
bounding_box,
dt_poly: None,
rec_poly: None,
text: None,
confidence: None,
orientation_angle: None,
word_boxes: None,
label: None,
}
}
pub fn with_recognition(
bounding_box: BoundingBox,
text: Option<Arc<str>>,
confidence: Option<f32>,
) -> Self {
Self {
bounding_box,
dt_poly: None,
rec_poly: None,
text,
confidence,
orientation_angle: None,
word_boxes: None,
label: None,
}
}
pub fn with_all(
bounding_box: BoundingBox,
text: Option<Arc<str>>,
confidence: Option<f32>,
orientation_angle: Option<f32>,
) -> Self {
Self {
bounding_box,
dt_poly: None,
rec_poly: None,
text,
confidence,
orientation_angle,
word_boxes: None,
label: None,
}
}
pub fn has_text(&self) -> bool {
self.text.is_some()
}
pub fn has_confidence(&self) -> bool {
self.confidence.is_some()
}
pub fn has_orientation(&self) -> bool {
self.orientation_angle.is_some()
}
pub fn has_word_boxes(&self) -> bool {
self.word_boxes.is_some()
}
pub fn text_with_confidence(&self) -> Option<(&str, f32)> {
match (&self.text, self.confidence) {
(Some(text), Some(confidence)) => Some((text, confidence)),
_ => None,
}
}
pub fn has_label(&self) -> bool {
self.label.is_some()
}
pub fn is_formula(&self) -> bool {
self.label.as_deref() == Some("formula")
}
pub fn with_label(mut self, label: Option<&str>) -> Self {
self.label = label.map(|s| s.into());
self
}
}