yscv-detect 0.1.8

Object detection pipeline with YOLOv8, NMS, and heatmap decoding
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
416
417
418
419
420
421
422
use yscv_video::Frame;

use crate::heatmap::{HeatmapDetectScratch, detect_from_heatmap_data_with_scratch, map_shape};
use crate::nms::validate_nms_args;
use crate::{CLASS_ID_FACE, DetectError, Detection, non_max_suppression};

/// Reusable scratch storage for RGB8 people detection.
///
/// This allows callers with stable frame dimensions (for example camera loops)
/// to avoid reallocating grayscale heatmap buffers on each frame.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Rgb8PeopleDetectScratch {
    grayscale_heatmap: Vec<f32>,
    heatmap: HeatmapDetectScratch,
}

/// Reusable scratch storage for RGB8 face detection.
///
/// This allows callers with stable frame dimensions (for example camera loops)
/// to avoid reallocating skin-probability heatmap buffers on each frame.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Rgb8FaceDetectScratch {
    skin_heatmap: Vec<f32>,
    heatmap: HeatmapDetectScratch,
}

/// Reusable scratch storage for frame-based people detection.
///
/// This avoids per-frame grayscale heatmap and traversal-buffer allocations
/// when detection runs on [`Frame`] inputs.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct FramePeopleDetectScratch {
    grayscale_heatmap: Vec<f32>,
    heatmap: HeatmapDetectScratch,
}

/// Reusable scratch storage for frame-based face detection.
///
/// This avoids per-frame skin heatmap and traversal-buffer allocations
/// when detection runs on [`Frame`] inputs.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct FrameFaceDetectScratch {
    skin_heatmap: Vec<f32>,
    heatmap: HeatmapDetectScratch,
}

/// Convenience adapter from frame to heatmap-based detection.
pub fn detect_people_from_frame(
    frame: &Frame,
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
) -> Result<Vec<Detection>, DetectError> {
    let mut scratch = FramePeopleDetectScratch::default();
    detect_people_from_frame_with_scratch(
        frame,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch,
    )
}

/// Convenience adapter from frame to heatmap-based detection with reusable scratch storage.
pub fn detect_people_from_frame_with_scratch(
    frame: &Frame,
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
    scratch: &mut FramePeopleDetectScratch,
) -> Result<Vec<Detection>, DetectError> {
    let image = frame.image();
    let (h, w, c) = map_shape(image)?;
    match c {
        1 => detect_from_heatmap_data_with_scratch(
            (h, w),
            image.data(),
            score_threshold,
            min_area,
            iou_threshold,
            max_detections,
            &mut scratch.heatmap,
        ),
        3 => {
            fill_frame_rgb_grayscale_heatmap((h, w), image.data(), &mut scratch.grayscale_heatmap);
            detect_from_heatmap_data_with_scratch(
                (h, w),
                &scratch.grayscale_heatmap,
                score_threshold,
                min_area,
                iou_threshold,
                max_detections,
                &mut scratch.heatmap,
            )
        }
        other => Err(DetectError::InvalidChannelCount {
            expected: 1,
            got: other,
        }),
    }
}

/// People detector over raw RGB8 bytes.
///
/// This bypasses frame-tensor conversion and is useful for camera paths
/// where RGB8 bytes are available directly from the capture backend.
pub fn detect_people_from_rgb8(
    width: usize,
    height: usize,
    rgb8: &[u8],
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
) -> Result<Vec<Detection>, DetectError> {
    let mut scratch = Rgb8PeopleDetectScratch::default();
    detect_people_from_rgb8_with_scratch(
        (width, height),
        rgb8,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch,
    )
}

/// People detector over raw RGB8 bytes with reusable scratch storage.
pub fn detect_people_from_rgb8_with_scratch(
    shape: (usize, usize),
    rgb8: &[u8],
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
    scratch: &mut Rgb8PeopleDetectScratch,
) -> Result<Vec<Detection>, DetectError> {
    let (width, height) = shape;
    fill_rgb8_grayscale_heatmap(width, height, rgb8, &mut scratch.grayscale_heatmap)?;
    detect_from_heatmap_data_with_scratch(
        (height, width),
        &scratch.grayscale_heatmap,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch.heatmap,
    )
}

/// Heuristic face detector over RGB frames using a skin-probability heatmap.
///
/// This is a classical CV baseline that does not require a trained model and is
/// intended for camera demos where low-latency face regions are needed.
pub fn detect_faces_from_frame(
    frame: &Frame,
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
) -> Result<Vec<Detection>, DetectError> {
    let mut scratch = FrameFaceDetectScratch::default();
    detect_faces_from_frame_with_scratch(
        frame,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch,
    )
}

/// Heuristic face detector over RGB frames with reusable scratch storage.
pub fn detect_faces_from_frame_with_scratch(
    frame: &Frame,
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
    scratch: &mut FrameFaceDetectScratch,
) -> Result<Vec<Detection>, DetectError> {
    validate_nms_args(iou_threshold, max_detections)?;
    let image = frame.image();
    let (h, w, c) = map_shape(image)?;
    if c != 3 {
        return Err(DetectError::InvalidChannelCount {
            expected: 3,
            got: c,
        });
    }

    fill_frame_rgb_skin_heatmap((h, w), image.data(), &mut scratch.skin_heatmap);
    detect_faces_from_skin_heatmap_data_with_scratch(
        (h, w),
        &scratch.skin_heatmap,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch.heatmap,
    )
}

/// Heuristic face detector over raw RGB8 bytes.
///
/// This bypasses temporary tensor conversion and is useful for camera paths
/// where frame bytes are available directly from the capture backend.
pub fn detect_faces_from_rgb8(
    width: usize,
    height: usize,
    rgb8: &[u8],
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
) -> Result<Vec<Detection>, DetectError> {
    let mut scratch = Rgb8FaceDetectScratch::default();
    detect_faces_from_rgb8_with_scratch(
        (width, height),
        rgb8,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch,
    )
}

/// Heuristic face detector over raw RGB8 bytes with reusable scratch storage.
pub fn detect_faces_from_rgb8_with_scratch(
    shape: (usize, usize),
    rgb8: &[u8],
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
    scratch: &mut Rgb8FaceDetectScratch,
) -> Result<Vec<Detection>, DetectError> {
    let (width, height) = shape;
    validate_nms_args(iou_threshold, max_detections)?;
    fill_rgb8_skin_heatmap(width, height, rgb8, &mut scratch.skin_heatmap)?;
    detect_faces_from_skin_heatmap_data_with_scratch(
        (height, width),
        &scratch.skin_heatmap,
        score_threshold,
        min_area,
        iou_threshold,
        max_detections,
        &mut scratch.heatmap,
    )
}

fn detect_faces_from_skin_heatmap_data_with_scratch(
    shape: (usize, usize),
    skin_heatmap_data: &[f32],
    score_threshold: f32,
    min_area: usize,
    iou_threshold: f32,
    max_detections: usize,
    heatmap_scratch: &mut HeatmapDetectScratch,
) -> Result<Vec<Detection>, DetectError> {
    let candidate_limit = max_detections.saturating_mul(4).max(max_detections);
    let candidates = detect_from_heatmap_data_with_scratch(
        shape,
        skin_heatmap_data,
        score_threshold,
        min_area,
        iou_threshold,
        candidate_limit,
        heatmap_scratch,
    )?;

    let mut faces = Vec::with_capacity(candidates.len());
    for candidate in candidates {
        let height = candidate.bbox.height();
        if height <= 1.0e-6 {
            continue;
        }
        let aspect_ratio = candidate.bbox.width() / height;
        if !(0.65..=1.8).contains(&aspect_ratio) {
            continue;
        }

        let shape_score = triangular_score(aspect_ratio, 0.65, 1.8, 1.0);
        let score = clamp01(0.75 * candidate.score + 0.25 * shape_score);
        faces.push(Detection {
            bbox: candidate.bbox,
            score,
            class_id: CLASS_ID_FACE,
        });
    }

    Ok(non_max_suppression(&faces, iou_threshold, max_detections))
}

fn fill_frame_rgb_grayscale_heatmap(shape: (usize, usize), rgb: &[f32], out: &mut Vec<f32>) {
    let pixel_count = shape.0 * shape.1;
    if out.len() != pixel_count {
        out.resize(pixel_count, 0.0);
    }

    for (rgb, value) in rgb.chunks_exact(3).zip(out.iter_mut()) {
        *value = (rgb[0] + rgb[1] + rgb[2]) / 3.0;
    }
}

fn fill_frame_rgb_skin_heatmap(shape: (usize, usize), rgb: &[f32], out: &mut Vec<f32>) {
    let pixel_count = shape.0 * shape.1;
    if out.len() != pixel_count {
        out.resize(pixel_count, 0.0);
    }

    let max_value = rgb.iter().copied().fold(0.0f32, f32::max);
    let scale = if max_value > 1.5 { 1.0 / 255.0 } else { 1.0 };
    for (rgb, value) in rgb.chunks_exact(3).zip(out.iter_mut()) {
        let r = clamp01(rgb[0] * scale);
        let g = clamp01(rgb[1] * scale);
        let b = clamp01(rgb[2] * scale);
        *value = skin_probability(r, g, b);
    }
}

fn fill_rgb8_skin_heatmap(
    width: usize,
    height: usize,
    rgb8: &[u8],
    out: &mut Vec<f32>,
) -> Result<(), DetectError> {
    validate_rgb8_buffer_size(width, height, rgb8)?;
    let pixel_count = width
        .checked_mul(height)
        .ok_or(DetectError::Rgb8DimensionsOverflow { width, height })?;
    if out.len() != pixel_count {
        out.resize(pixel_count, 0.0);
    }

    const SCALE: f32 = 1.0 / 255.0;
    for (rgb, value) in rgb8.chunks_exact(3).zip(out.iter_mut()) {
        let r = rgb[0] as f32 * SCALE;
        let g = rgb[1] as f32 * SCALE;
        let b = rgb[2] as f32 * SCALE;
        *value = skin_probability(r, g, b);
    }
    Ok(())
}

fn fill_rgb8_grayscale_heatmap(
    width: usize,
    height: usize,
    rgb8: &[u8],
    out: &mut Vec<f32>,
) -> Result<(), DetectError> {
    validate_rgb8_buffer_size(width, height, rgb8)?;
    let pixel_count = width
        .checked_mul(height)
        .ok_or(DetectError::Rgb8DimensionsOverflow { width, height })?;
    if out.len() != pixel_count {
        out.resize(pixel_count, 0.0);
    }

    const SCALE: f32 = 1.0 / 255.0;
    for (rgb, value) in rgb8.chunks_exact(3).zip(out.iter_mut()) {
        *value = (rgb[0] as f32 + rgb[1] as f32 + rgb[2] as f32) * (SCALE / 3.0);
    }
    Ok(())
}

fn validate_rgb8_buffer_size(width: usize, height: usize, rgb8: &[u8]) -> Result<(), DetectError> {
    let expected = width
        .checked_mul(height)
        .and_then(|pixels| pixels.checked_mul(3))
        .ok_or(DetectError::Rgb8DimensionsOverflow { width, height })?;
    if rgb8.len() != expected {
        return Err(DetectError::InvalidRgb8BufferSize {
            expected,
            got: rgb8.len(),
        });
    }
    Ok(())
}

fn skin_probability(r: f32, g: f32, b: f32) -> f32 {
    let y = 0.299 * r + 0.587 * g + 0.114 * b;
    let cb = 0.5 + 0.564 * (b - y);
    let cr = 0.5 + 0.713 * (r - y);

    let cb_score = triangular_score(cb, 0.28, 0.57, 0.43);
    let cr_score = triangular_score(cr, 0.36, 0.76, 0.56);
    let luminance_score = triangular_score(y, 0.08, 0.95, 0.55);

    let rg_bias = clamp01((r - g + 0.15) / 0.35);
    let gb_bias = clamp01((g - b + 0.10) / 0.35);
    let chroma = ((r - g).abs() + (g - b).abs() + (r - b).abs()) / 3.0;
    let saturation_score = clamp01(chroma / 0.45);

    let score = 0.32 * cb_score
        + 0.32 * cr_score
        + 0.16 * luminance_score
        + 0.10 * rg_bias
        + 0.10 * gb_bias;
    clamp01(score * saturation_score.max(0.3))
}

fn triangular_score(value: f32, min: f32, max: f32, center: f32) -> f32 {
    if value < min || value > max {
        return 0.0;
    }
    if (value - center).abs() <= f32::EPSILON {
        return 1.0;
    }
    if value < center {
        return (value - min) / (center - min);
    }
    (max - value) / (max - center)
}

fn clamp01(value: f32) -> f32 {
    value.clamp(0.0, 1.0)
}