pico_detect/detect/
mod.rs

1mod detection;
2mod detector;
3mod padding;
4
5pub mod clusterize;
6pub mod multiscale;
7
8use derive_builder::Builder;
9use image::Luma;
10use pixelutil_image::ExtendedImageView;
11
12use crate::geometry::Target;
13
14use clusterize::Clusterizer;
15use multiscale::Multiscaler;
16
17pub use detection::Detection;
18pub use detector::Detector;
19pub use padding::Padding;
20
21/// Utility for running multiscale detection with clustering and padding
22/// using [`Detector`].
23#[derive(Debug, Clone, Copy, Builder)]
24#[builder]
25pub struct DetectMultiscale {
26    /// Multiscale detection parameters.
27    pub multiscaler: Multiscaler,
28    /// Clustering parameters.
29    #[builder(default)]
30    pub clusterizer: Clusterizer,
31    /// Padding parameters.
32    #[builder(default)]
33    pub padding: Padding,
34}
35
36impl DetectMultiscale {
37    /// Create default builder struct.
38    #[inline]
39    pub fn builder() -> DetectMultiscaleBuilder {
40        Default::default()
41    }
42
43    /// Run multiscale detection with clustering and padding on the specified image.
44    #[inline]
45    pub fn run<I>(&self, detector: &Detector, image: &I) -> Vec<Detection<Target>>
46    where
47        I: ExtendedImageView<Pixel = Luma<u8>>,
48    {
49        let mut detections = Vec::new();
50
51        self.multiscaler
52            .run(self.padding.image_rect(image), |region| {
53                if let Some(detection) = detector.detect(image, region) {
54                    detections.push(detection);
55                }
56            });
57
58        let mut clusters = Vec::new();
59
60        self.clusterizer.clusterize(&mut detections, &mut clusters);
61
62        clusters
63    }
64}