use core::fmt;
use smol_bytes::Utf8Bytes;
use crate::{
BoundingBox, BoundingBoxFilter, BoundingBoxLeaf, ColumnEq, ColumnKind, ColumnName, ColumnShape,
CompositeFilterMarker, CompositeMarker, ConfidenceFilter, DefaultMarker, DefaultVecMarker,
FieldName, OrderLevel, PayloadFilter, QuantifiedMarker, Score, TextFilter, shape,
};
use super::{detection::CONFIDENCE, located::BBOX};
#[cfg(test)]
mod tests;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct TextDetection<T> {
pub(super) text: Utf8Bytes,
pub(super) confidence: Score,
pub(super) bbox: BoundingBox<T>,
}
impl<T> TextDetection<T> {
#[inline]
#[must_use]
pub fn new(text: impl Into<Utf8Bytes>, confidence: Score, bbox: BoundingBox<T>) -> Self {
Self {
text: text.into(),
confidence,
bbox,
}
}
#[inline]
#[must_use]
pub fn text(&self) -> &str {
self.text.as_str()
}
#[inline]
#[must_use]
pub const fn text_bytes(&self) -> &Utf8Bytes {
&self.text
}
#[inline]
#[must_use]
pub const fn confidence(&self) -> Score {
self.confidence
}
#[inline]
#[must_use]
pub const fn bbox(&self) -> &BoundingBox<T> {
&self.bbox
}
}
impl<T: fmt::Debug> fmt::Debug for TextDetection<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TextDetection")
.field("text", &self.text.as_str())
.field("confidence", &self.confidence.get())
.field("bbox", &self.bbox)
.finish()
}
}
pub(super) const TEXT: &FieldName = FieldName::new("text");
impl<T: ColumnKind> ColumnKind for TextDetection<T> {
const SEGMENTS: &'static [ColumnShape] = &[
ColumnShape::Plain(ColumnName::of(TEXT)),
ColumnShape::Plain(ColumnName::of(CONFIDENCE)),
shape::<BoundingBox<T>>(ColumnName::of(BBOX)),
];
const ORDER: &'static [OrderLevel] = ORDER;
}
impl<T: ColumnEq> ColumnEq for TextDetection<T> {
#[inline]
fn column_eq(&self, other: &Self) -> bool {
self.text_bytes().column_eq(other.text_bytes())
&& self.confidence().column_eq(&other.confidence())
&& self.bbox().column_eq(other.bbox())
}
}
pub(super) const ORDER: &[OrderLevel] = &[
OrderLevel::new(0),
OrderLevel::new(1),
OrderLevel::new(2),
OrderLevel::new(3),
OrderLevel::new(4),
OrderLevel::new(5),
];
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TextDetectionLeaf<T> {
Text(Utf8Bytes),
Confidence(Score),
Bbox(BoundingBoxLeaf<T>),
}
impl<T: ColumnEq> ColumnEq for TextDetectionLeaf<T> {
fn column_eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Text(this), Self::Text(that)) => this.column_eq(that),
(Self::Confidence(this), Self::Confidence(that)) => this.column_eq(that),
(Self::Bbox(this), Self::Bbox(that)) => this.column_eq(that),
_ => false,
}
}
}
impl<T: ColumnKind + Copy> crate::OrderLeaves for TextDetection<T> {
type Leaf = TextDetectionLeaf<T>;
fn leaf(&self, at: usize) -> Option<TextDetectionLeaf<T>> {
match at {
0 => Some(TextDetectionLeaf::Text(self.text.clone())),
1 => Some(TextDetectionLeaf::Confidence(self.confidence)),
2..=6 => crate::OrderLeaves::leaf(&self.bbox, at - 2).map(TextDetectionLeaf::Bbox),
_ => None,
}
}
}
impl<T: crate::CursorValue> crate::CursorValue for TextDetectionLeaf<T> {
fn write_cursor(&self, out: &mut Vec<u8>) {
match self {
Self::Text(text) => {
out.push(0);
crate::CursorValue::write_cursor(text, out);
}
Self::Confidence(confidence) => {
out.push(1);
crate::CursorValue::write_cursor(confidence, out);
}
Self::Bbox(leaf) => {
out.push(2);
crate::CursorValue::write_cursor(leaf, out);
}
}
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let (tag, value) = bytes.split_first()?;
match tag {
0 => <Utf8Bytes as crate::CursorValue>::read_cursor(value).map(Self::Text),
1 => <Score as crate::CursorValue>::read_cursor(value).map(Self::Confidence),
2 => <BoundingBoxLeaf<T> as crate::CursorValue>::read_cursor(value).map(Self::Bbox),
_ => None,
}
}
}
#[cfg(feature = "sqlx")]
impl<Db, T> crate::Carrier<Db> for TextDetectionLeaf<T>
where
Db: sqlx::Database,
Utf8Bytes: crate::Carrier<Db>,
Score: crate::Carrier<Db>,
BoundingBoxLeaf<T>: crate::Carrier<Db>,
{
fn push_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
match self {
Self::Text(text) => <Utf8Bytes as crate::Carrier<Db>>::push_bind(text, qb),
Self::Confidence(confidence) => <Score as crate::Carrier<Db>>::push_bind(confidence, qb),
Self::Bbox(leaf) => <BoundingBoxLeaf<T> as crate::Carrier<Db>>::push_bind(leaf, qb),
}
}
}
impl<Db, T> crate::StoredAs<Db> for TextDetection<T>
where
Utf8Bytes: crate::StoredAs<Db>,
Score: crate::StoredAs<Db>,
BoundingBox<T>: crate::StoredAs<Db>,
{
const WORDS: &'static [&'static str] = &[
<Utf8Bytes as crate::StoredAs<Db>>::WORDS[0],
<Score as crate::StoredAs<Db>>::WORDS[0],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[0],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[1],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[2],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[3],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[4],
];
}
impl<T: crate::CollectionElement> crate::CollectionElement for TextDetection<T> {}
#[cfg(feature = "sqlx")]
impl<Db, T> crate::Carrier<Db> for TextDetection<T>
where
Db: sqlx::Database,
Utf8Bytes: crate::Carrier<Db>,
Score: crate::Carrier<Db>,
BoundingBox<T>: crate::Carrier<Db>,
{
fn push_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
<Utf8Bytes as crate::Carrier<Db>>::push_bind(&self.text, qb);
qb.push(", ");
<Score as crate::Carrier<Db>>::push_bind(&self.confidence, qb);
qb.push(", ");
<BoundingBox<T> as crate::Carrier<Db>>::push_bind(&self.bbox, qb);
}
fn push_value_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
<Utf8Bytes as crate::Carrier<Db>>::push_bind(&self.text, qb);
qb.push(", ");
<Score as crate::Carrier<Db>>::push_bind(&self.confidence, qb);
qb.push(", ");
<BoundingBox<T> as crate::Carrier<Db>>::push_value_bind(&self.bbox, qb);
}
}
#[cfg(feature = "sqlx")]
impl<'a, R, T> crate::Assemble<'a, R> for TextDetection<T>
where
R: sqlx::Row,
Utf8Bytes: crate::Assemble<'a, R>,
Score: crate::Assemble<'a, R>,
BoundingBox<T>: crate::Assemble<'a, R>,
{
fn assemble(row: &'a R, under: Option<&crate::Path<'_>>) -> sqlx::Result<Self> {
let text: Utf8Bytes = crate::assembled(row, TEXT, under)?;
let confidence: Score = crate::assembled(row, CONFIDENCE, under)?;
let bbox: BoundingBox<T> = crate::assembled(row, BBOX, under)?;
Ok(Self::new(text, confidence, bbox))
}
}
impl<T: crate::CursorValue> crate::CursorValue for TextDetection<T> {
fn write_cursor(&self, out: &mut Vec<u8>) {
crate::cursor::write_member(&self.text, out);
crate::cursor::write_member(&self.confidence, out);
crate::cursor::write_member(&self.bbox, out);
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let mut rest = bytes;
let text = crate::cursor::read_member::<Utf8Bytes>(&mut rest)?;
let confidence = crate::cursor::read_member::<Score>(&mut rest)?;
let bbox = crate::cursor::read_member::<BoundingBox<T>>(&mut rest)?;
rest.is_empty().then(|| Self::new(text, confidence, bbox))
}
}
impl<T> CompositeFilterMarker for TextDetection<T>
where
T: DefaultMarker,
{
type Filter = TextDetectionFilter<PayloadFilter<T>>;
type ColumnFilter = TextDetectionFilter<PayloadFilter<T>>;
}
impl<T: DefaultMarker> DefaultMarker for TextDetection<T> {
type Marker = CompositeMarker<TextDetection<T>>;
}
impl<T: DefaultMarker> DefaultVecMarker for TextDetection<T> {
type Marker = QuantifiedMarker<Vec<TextDetection<T>>>;
}
pub struct TextDetectionFilter<CF> {
pub(super) text: Option<TextFilter<Utf8Bytes>>,
pub(super) confidence: Option<ConfidenceFilter>,
pub(super) bbox: Option<BoundingBoxFilter<CF>>,
}
impl<CF> TextDetectionFilter<CF> {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
text: None,
confidence: None,
bbox: None,
}
}
crate::graphql::filter::opt_accessor!(
text: TextFilter<Utf8Bytes> => with_text, set_text, maybe_text, update_text
);
crate::graphql::filter::opt_accessor!(
confidence: ConfidenceFilter => with_confidence, set_confidence, maybe_confidence, update_confidence
);
crate::graphql::filter::opt_accessor!(
bbox: BoundingBoxFilter<CF> => with_bbox, set_bbox, maybe_bbox, update_bbox
);
}
impl<CF: crate::GraphqlFilter> TextDetectionFilter<CF> {
pub fn is_empty(&self) -> bool {
self
.text
.as_ref()
.is_none_or(crate::GraphqlFilter::is_empty)
&& self
.confidence
.as_ref()
.is_none_or(ConfidenceFilter::is_empty)
&& self
.bbox
.as_ref()
.is_none_or(crate::GraphqlFilter::is_empty)
}
}
impl<CF: crate::GraphqlFilter> crate::GraphqlFilter for TextDetectionFilter<CF> {
#[inline]
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
}
impl<CF> Default for TextDetectionFilter<CF> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<CF: fmt::Debug> fmt::Debug for TextDetectionFilter<CF> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TextDetectionFilter")
.field("text", &self.text)
.field("confidence", &self.confidence)
.field("bbox", &self.bbox)
.finish()
}
}
impl<CF: Clone> Clone for TextDetectionFilter<CF> {
fn clone(&self) -> Self {
Self {
text: self.text.clone(),
confidence: self.confidence.clone(),
bbox: self.bbox.clone(),
}
}
}
impl<CF: PartialEq> PartialEq for TextDetectionFilter<CF> {
fn eq(&self, other: &Self) -> bool {
self.text == other.text && self.confidence == other.confidence && self.bbox == other.bbox
}
}