use std::fmt;
use crate::bbox::Detection;
use crate::image::RgbImage;
pub trait ObjectDetector {
type Error: std::error::Error + Send + Sync + 'static;
fn detect(&mut self, image: &RgbImage) -> Result<Vec<Detection>, Self::Error>;
}
#[derive(Debug, Clone, Default)]
pub struct ReplayDetector {
frames: Vec<Vec<Detection>>,
next: usize,
}
impl ReplayDetector {
pub fn new(frames: Vec<Vec<Detection>>) -> Self {
Self { frames, next: 0 }
}
pub fn remaining(&self) -> usize {
self.frames.len() - self.next
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReplayExhausted {
pub frames: usize,
}
impl fmt::Display for ReplayExhausted {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "all {} replayed frames were used", self.frames)
}
}
impl std::error::Error for ReplayExhausted {}
impl ObjectDetector for ReplayDetector {
type Error = ReplayExhausted;
fn detect(&mut self, _image: &RgbImage) -> Result<Vec<Detection>, Self::Error> {
let frame = self.frames.get(self.next).ok_or(ReplayExhausted {
frames: self.frames.len(),
})?;
self.next += 1;
Ok(frame.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bbox::BBox;
#[test]
fn replays_frames_in_order_then_reports_exhaustion() {
let a = Detection::new(BBox::from_xywh(0.0, 0.0, 10.0, 20.0), 0.9, 0);
let mut detector = ReplayDetector::new(vec![vec![a], vec![]]);
let image = RgbImage::from_gray(1, 1, &[0]).unwrap();
assert_eq!(detector.detect(&image).unwrap(), vec![a]);
assert_eq!(detector.remaining(), 1);
assert!(detector.detect(&image).unwrap().is_empty());
assert_eq!(detector.detect(&image), Err(ReplayExhausted { frames: 2 }));
}
}