rusty_scrfd 1.3.1

A high-performance face detection library using SCRFD model with OpenCV integration
Documentation
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use ndarray::{s, Array2, Array3, ArrayD, ArrayViewD, Axis};
use opencv::core::Mat;
use opencv::prelude::MatTraitConst;
use ort::{
    session::{RunOptions, Session},
    value::Value,
};
use std::{collections::HashMap, error::Error};

use super::helpers::{
    opencv_helper::OpenCVHelper, relative_conversion::RelativeConversion,
    scrfd_helper::ScrfdHelpers,
};

/// A face detection model using SCRFD (Selective Convolutional Response Face Detector) architecture.
///
/// This struct provides asynchronous face detection capabilities with configurable parameters
/// for input size, confidence threshold, and IoU threshold.
///
/// # Example
/// ```no_run
/// use ort::session::Session;
/// use rusty_scrfd::SCRFDAsync;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let session = Session::builder()?
///     .commit_from_file("path/to/model.onnx")?;
///
/// let detector = SCRFDAsync::new(
///     session,
///     (640, 640),  // input size
///     0.5,         // confidence threshold
///     0.45,        // IoU threshold
///     true         // relative output
/// )?;
/// # Ok(())
/// # }
/// ```
pub struct SCRFDAsync {
    input_size: (i32, i32),
    conf_thres: f32,
    iou_thres: f32,
    _fmc: usize,
    feat_stride_fpn: Vec<i32>,
    num_anchors: usize,
    use_kps: bool,
    opencv_helper: OpenCVHelper,
    center_cache: HashMap<(i32, i32, i32), Array2<f32>>,
    session: Session,
    _output_names: Vec<String>,
    input_names: Vec<String>,
    relative_output: bool,
}

impl SCRFDAsync {
    /// Creates a new SCRFD face detector instance.
    ///
    /// # Arguments
    /// * `session` - An ONNX Runtime session containing the loaded SCRFD model
    /// * `input_size` - Tuple of (width, height) for the input image dimensions
    /// * `conf_thres` - Confidence threshold for face detection (0.0 to 1.0)
    /// * `iou_thres` - IoU threshold for non-maximum suppression (0.0 to 1.0)
    /// * `relative_output` - Whether to return coordinates relative to image dimensions
    ///
    /// # Returns
    /// * `Result<Self, Box<dyn Error>>` - A new SCRFDAsync instance or an error
    ///
    /// # Example
    /// ```no_run
    /// use ort::session::Session;
    /// use rusty_scrfd::SCRFDAsync;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let session = Session::builder()?.commit_from_file("model.onnx")?;
    /// let detector = SCRFDAsync::new(
    ///     session,
    ///     (640, 640),
    ///     0.5,
    ///     0.45,
    ///     true
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(
        session: Session,
        input_size: (i32, i32),
        conf_thres: f32,
        iou_thres: f32,
        relative_output: bool,
    ) -> Result<Self, Box<dyn Error>> {
        // SCRFD model parameters
        let fmc = 3;
        let feat_stride_fpn = vec![8, 16, 32];
        let num_anchors = 2;
        let use_kps = true;

        let mean = 127.5;
        let std = 128.0;

        let center_cache = HashMap::new();

        // Get model input and output names
        let output_names = session
            .outputs()
            .iter()
            .map(|o| o.name().to_string())
            .collect();
        let input_names = session
            .inputs()
            .iter()
            .map(|i| i.name().to_string())
            .collect();

        Ok(Self {
            input_size,
            conf_thres,
            iou_thres,
            _fmc: fmc,
            feat_stride_fpn,
            num_anchors,
            use_kps,
            opencv_helper: OpenCVHelper::new(mean, std),
            center_cache,
            session,
            _output_names: output_names,
            input_names,
            relative_output,
        })
    }

    /// Performs the forward pass of the SCRFD model.
    ///
    /// # Arguments
    /// * `input_tensor` - Input tensor of shape [1, 3, height, width]
    /// * `center_cache` - Mutable reference to the center cache for anchor points
    ///
    /// # Returns
    /// * `Result<(Vec<Array2<f32>>, Vec<Array2<f32>>, Vec<Array3<f32>>), Box<dyn Error>>` - Tuple containing:
    ///   - Vector of score arrays
    ///   - Vector of bounding box arrays
    ///   - Vector of keypoint arrays (if enabled)
    ///
    /// # Example
    /// ```no_run
    /// use ort::session::Session;
    /// use rusty_scrfd::SCRFDAsync;
    /// use ndarray::ArrayD;
    /// use std::collections::HashMap;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let session = Session::builder()?.commit_from_file("model.onnx")?;
    /// let mut detector = SCRFDAsync::new(session, (640, 640), 0.5, 0.45, true)?;
    ///
    /// // Create dummy input tensor (1, 3, 640, 640)
    /// let input_tensor = ArrayD::<f32>::zeros(vec![1, 3, 640, 640]);
    /// let mut center_cache = HashMap::new();
    ///
    /// let (scores, bboxes, kpss) = detector.forward(&input_tensor, &mut center_cache).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn forward(
        &mut self,
        input_tensor: &ArrayD<f32>,
        center_cache: &mut HashMap<(i32, i32, i32), Array2<f32>>,
    ) -> Result<(Vec<Array2<f32>>, Vec<Array2<f32>>, Vec<Array3<f32>>), Box<dyn Error>> {
        let mut scores_list = Vec::new();
        let mut bboxes_list = Vec::new();
        let mut kpss_list = Vec::new();
        let input_height = input_tensor.shape()[2];
        let input_width = input_tensor.shape()[3];
        let input_value = Value::from_array(input_tensor.to_owned())?;
        let input_name = self.input_names[0].clone();
        let input = ort::inputs![input_name => input_value];

        let run_options = match RunOptions::new() {
            Ok(options) => options,
            Err(e) => return Err(Box::new(e)),
        };

        // Run the model
        let session_output = match self.session.run_async(input, &run_options) {
            Ok(output) => output,
            Err(e) => return Err(Box::new(e)),
        };

        let session_output = match session_output.await {
            Ok(output) => output,
            Err(e) => return Err(Box::new(e)),
        };

        let mut outputs = vec![];
        for (_, output) in session_output.iter().enumerate() {
            let f32_array: ArrayViewD<f32> = match output.1.try_extract_array() {
                Ok(array) => array,
                Err(e) => return Err(Box::new(e)),
            };
            outputs.push(f32_array.to_owned());
        }

        drop(session_output);

        // reverse the order of outputs
        let fmc = self._fmc;
        for (idx, &stride) in self.feat_stride_fpn.iter().enumerate() {
            let scores = outputs[idx].clone();
            let bbox_preds = outputs[idx + fmc].to_shape((outputs[idx + fmc].len() / 4, 4))?;
            let bbox_preds = bbox_preds * stride as f32;
            let kps_preds = outputs[idx + fmc * 2]
                .to_shape((outputs[idx + fmc * 2].len() / 10, 10))?
                * stride as f32;

            // Determine feature map dimensions
            let height = input_height / stride as usize;
            let width = input_width / stride as usize;

            // Generate anchor centers
            let key = (height as i32, width as i32, stride);
            let anchor_centers = if let Some(centers) = self.center_cache.get(&key) {
                centers.clone()
            } else {
                let centers = ScrfdHelpers::generate_anchor_centers(
                    self.num_anchors,
                    height,
                    width,
                    stride as f32,
                );
                if center_cache.len() < 100 {
                    center_cache.insert(key, centers.clone());
                }
                centers
            };

            // Filter scores by threshold
            let pos_inds: Vec<usize> = scores
                .iter()
                .enumerate()
                .filter(|(_, &s)| s > self.conf_thres)
                .map(|(i, _)| i)
                .collect();

            if pos_inds.is_empty() {
                continue;
            }

            let pos_scores = scores.select(Axis(0), &pos_inds);
            let bboxes = ScrfdHelpers::distance2bbox(&anchor_centers, &bbox_preds.to_owned(), None);
            let pos_bboxes = bboxes.select(Axis(0), &pos_inds);

            scores_list.push(pos_scores.to_shape((pos_scores.len(), 1))?.to_owned());
            bboxes_list.push(pos_bboxes);

            if self.use_kps {
                let kpss = ScrfdHelpers::distance2kps(&anchor_centers, &kps_preds.to_owned(), None);
                let kpss = kpss.to_shape((kpss.shape()[0], kpss.shape()[1] / 2, 2))?;
                let pos_kpss = kpss.select(Axis(0), &pos_inds);
                kpss_list.push(pos_kpss);
            }
        }

        Ok((scores_list, bboxes_list, kpss_list))
    }

    /// Detects faces in an input image.
    ///
    /// # Arguments
    /// * `image` - Input image as OpenCV Mat
    /// * `max_num` - Maximum number of faces to detect (0 for unlimited)
    /// * `metric` - Metric for selecting faces when max_num is exceeded ("max" for largest area)
    /// * `center_cache` - Mutable reference to the center cache for anchor points
    ///
    /// # Returns
    /// * `Result<(Array2<f32>, Option<Array3<f32>>), Box<dyn Error>>` - Tuple containing:
    ///   - Array of bounding boxes [x1, y1, x2, y2, score]
    ///   - Optional array of keypoints [x1, y1, x2, y2, ..., x5, y5]
    ///
    /// # Example
    /// ```no_run
    /// use ort::session::Session;
    /// use rusty_scrfd::SCRFDAsync;
    /// use opencv::prelude::*;
    /// use opencv::core::Mat;
    /// use std::collections::HashMap;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let session = Session::builder()?.commit_from_file("model.onnx")?;
    /// let mut detector = SCRFDAsync::new(session, (640, 640), 0.5, 0.45, true)?;
    ///
    /// // Create dummy image
    /// let image = Mat::default();
    /// let mut center_cache = HashMap::new();
    ///
    /// let (bboxes, keypoints) = detector.detect(&image, 10, "max", &mut center_cache).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn detect(
        &mut self,
        image: &Mat,
        max_num: usize,
        metric: &str,
        center_cache: &mut HashMap<(i32, i32, i32), Array2<f32>>,
    ) -> Result<(Array2<f32>, Option<Array3<f32>>), Box<dyn Error>> {
        let orig_width = image.cols() as f32;
        let orig_height = image.rows() as f32;

        let (det_image, det_scale) = self
            .opencv_helper
            .resize_with_aspect_ratio(image, self.input_size)?;
        let input_tensor = self
            .opencv_helper
            .prepare_input_tensor(&det_image, self.input_size)?;
        let (scores_list, bboxes_list, kpss_list) =
            match self.forward(&input_tensor.into_dyn(), center_cache).await {
                Ok(result) => result,
                Err(e) => return Err(e),
            };

        if scores_list.is_empty() {
            return Err("No faces detected".into());
        }

        // Concatenate scores and bboxes
        let scores = ScrfdHelpers::concatenate_array2(&scores_list)?;
        let bboxes = ScrfdHelpers::concatenate_array2(&bboxes_list)?;
        let bboxes = &bboxes / det_scale;

        let mut kpss = if self.use_kps {
            let kpss = ScrfdHelpers::concatenate_array3(&kpss_list)?;
            Some(&kpss / det_scale)
        } else {
            None
        };

        let scores_ravel = scores.iter().collect::<Vec<_>>();
        let mut order = (0..scores_ravel.len()).collect::<Vec<usize>>();
        order.sort_unstable_by(|&i, &j| scores_ravel[j].partial_cmp(&scores_ravel[i]).unwrap());

        // Prepare pre_detections
        let mut pre_det = ndarray::concatenate(Axis(1), &[bboxes.view(), scores.view()])?;
        pre_det = pre_det.select(Axis(0), &order);

        let keep = ScrfdHelpers::nms(&pre_det, self.iou_thres);
        let det = pre_det.select(Axis(0), &keep);

        if self.use_kps {
            if let Some(ref mut kpss_array) = kpss {
                *kpss_array = kpss_array.select(Axis(0), &order);
                *kpss_array = kpss_array.select(Axis(0), &keep);
            }
        }

        let det = if max_num > 0 && max_num < det.shape()[0] {
            let area = (&det.slice(s![.., 2]) - &det.slice(s![.., 0]))
                * (&det.slice(s![.., 3]) - &det.slice(s![.., 1]));
            let image_center = (
                self.input_size.0 as f32 / 2.0,
                self.input_size.1 as f32 / 2.0,
            );
            let offsets = ndarray::stack![
                Axis(0),
                (&det.slice(s![.., 0]) + &det.slice(s![.., 2])) / 2.0 - image_center.1 as f32,
                (&det.slice(s![.., 1]) + &det.slice(s![.., 3])) / 2.0 - image_center.0 as f32,
            ];
            let offset_dist_squared = offsets.mapv(|x| x * x).sum_axis(Axis(0));
            let values = if metric == "max" {
                area.to_owned()
            } else {
                &area - &(offset_dist_squared * 2.0)
            };
            let mut bindex = (0..values.len()).collect::<Vec<usize>>();
            bindex.sort_unstable_by(|&i, &j| values[j].partial_cmp(&values[i]).unwrap());
            bindex.truncate(max_num);
            let det = det.select(Axis(0), &bindex);
            if self.use_kps {
                if let Some(ref mut kpss_array) = kpss {
                    *kpss_array = kpss_array.select(Axis(0), &bindex);
                }
            }
            det
        } else {
            det
        };

        let bounding_boxes = if self.relative_output {
            RelativeConversion::absolute_to_relative_bboxes(
                &det,
                orig_width as u32,
                orig_height as u32,
            )
        } else {
            det
        };

        let keypoints = if let Some(kpss) = kpss {
            if self.relative_output {
                Some(RelativeConversion::absolute_to_relative_keypoints(
                    &kpss,
                    orig_width as u32,
                    orig_height as u32,
                ))
            } else {
                Some(kpss)
            }
        } else {
            None
        };

        Ok((bounding_boxes, keypoints))
    }
}