1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Allocation-backed detection batch conveniences.
use alloc::{vec, vec::Vec};
use crate::{CoreError, Detection, NmsConfig, nms_in_place};
/// A heap-backed batch of validated detections.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct OwnedDetectionBatch {
detections: Vec<Detection>,
}
impl OwnedDetectionBatch {
/// Creates an empty owned detection batch.
#[must_use]
pub const fn new() -> Self {
Self {
detections: Vec::new(),
}
}
/// Appends a detection, growing the backing allocation when necessary.
pub fn push(&mut self, detection: Detection) {
self.detections.push(detection);
}
/// Returns the detections currently in the batch.
#[must_use]
pub fn as_slice(&self) -> &[Detection] {
&self.detections
}
/// Applies non-maximum suppression and removes suppressed detections.
///
/// # Errors
///
/// Propagates errors from [`nms_in_place`] without modifying this batch.
pub fn nms(&mut self, config: NmsConfig) -> Result<usize, CoreError> {
let len = self.detections.len();
let mut order = vec![0; len];
let mut suppressed = vec![false; len];
let kept = nms_in_place(
self.detections.as_mut_slice(),
order.as_mut_slice(),
suppressed.as_mut_slice(),
config,
)?;
self.detections.truncate(kept);
Ok(kept)
}
}