od-bridge 0.2.4

C ABI bridge for od_opencv, designed for Go CGO 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! od-bridge: C ABI bridge for od_opencv, designed for Go CGO integration.
//!
//! Provides opaque model handles and flat C structs for detection results.
//! Each model is independent: create one for plate detection, another for OCR.

use std::ffi::CStr;
use std::os::raw::c_char;
use std::ptr;
use std::slice;

use ndarray::Array3;
use od_opencv::model_factory::Model;
use od_opencv::model_trait::ObjectDetector;
use od_opencv::BBox;
use od_opencv::face_pipeline::FacePipeline;

/// Single detection result (flat, no pointers, safe for CGO memcpy).
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct OdDetection {
    /// Top-left corner X coordinate (pixels).
    pub bbox_x: i32,
    /// Top-left corner Y coordinate (pixels).
    pub bbox_y: i32,
    /// Bounding box width (pixels).
    pub bbox_w: i32,
    /// Bounding box height (pixels).
    pub bbox_h: i32,
    /// Predicted class index (zero-based).
    pub class_id: i32,
    /// Detection confidence in [0.0, 1.0].
    pub confidence: f32,
}

/// Detection results batch. Caller must free via `od_detections_free`.
#[repr(C)]
pub struct OdDetections {
    /// Pointer to the first element of the results array.
    pub data: *mut OdDetection,
    /// Number of detections in the array.
    pub len: i32,
}

/// Error code returned by all functions.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OdError {
    /// No error.
    Ok = 0,
    /// Null pointer or invalid dimension passed.
    InvalidArgument = 1,
    /// ONNX model file could not be loaded.
    ModelLoadFailed = 2,
    /// Inference failed at runtime.
    DetectionFailed = 3,
    /// RGB pixel buffer could not be converted to Array3.
    ImageConvertFailed = 4,
}

/// Backend-specific model variant.
enum ModelInner {
    /// ONNX Runtime (CPU, CUDA, or TensorRT execution provider).
    Ort(od_opencv::backend_ort::ModelUltralyticsOrt),
    /// Native TensorRT engine.
    #[cfg(feature = "trt")]
    Trt(od_opencv::backend_tensorrt::ModelUltralyticsRt),
    /// Rockchip RKNN NPU.
    #[cfg(feature = "rknn")]
    Rknn(od_opencv::backend_rknn::ModelUltralyticsRknn),
}

/// Opaque model handle. Created by any `od_model_create_*` function.
pub struct ModelHandle {
    inner: ModelInner,
}

impl ModelHandle {
    /// Run detection on an `ImageBuffer`, dispatching to the active backend.
    fn detect(
        &mut self,
        img: &od_opencv::ImageBuffer,
        conf: f32,
        nms: f32,
    ) -> Result<(Vec<BBox>, Vec<usize>, Vec<f32>), OdError> {
        match &mut self.inner {
            ModelInner::Ort(m) => m.detect(img, conf, nms).map_err(|e| {
                eprintln!("od_model_detect (ort): {e:?}");
                OdError::DetectionFailed
            }),
            #[cfg(feature = "trt")]
            ModelInner::Trt(m) => m.detect(img, conf, nms).map_err(|e| {
                eprintln!("od_model_detect (trt): {e:?}");
                OdError::DetectionFailed
            }),
            #[cfg(feature = "rknn")]
            ModelInner::Rknn(m) => m.detect(img, conf, nms).map_err(|e| {
                eprintln!("od_model_detect (rknn): {e:?}");
                OdError::DetectionFailed
            }),
        }
    }
}

/// Helper: parse a C string pointer into a Rust `&str`.
/// Returns `None` if the pointer is null or not valid UTF-8.
unsafe fn parse_cstr(p: *const c_char) -> Option<&'static str> {
    if p.is_null() {
        return None;
    }
    unsafe { CStr::from_ptr(p) }.to_str().ok()
}

/// Helper: allocate a `ModelHandle` on the heap and return a raw pointer.
fn into_handle(inner: ModelInner) -> *mut ModelHandle {
    Box::into_raw(Box::new(ModelHandle { inner }))
}

/// Create a model from an ONNX file (ORT backend, CPU).
///
/// # Parameters
/// - `model_path`: null-terminated path to `.onnx` file
/// - `input_w`, `input_h`: model input dimensions (e.g. 416, 416)
///
/// # Returns
/// Opaque pointer, or null on error.
///
/// # Safety
/// `model_path` must be a valid null-terminated C string.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_create(
    model_path: *const c_char,
    input_w: u32,
    input_h: u32,
) -> *mut ModelHandle {
    let Some(path) = (unsafe { parse_cstr(model_path) }) else {
        return ptr::null_mut();
    };
    match Model::ort(path, (input_w, input_h)) {
        Ok(model) => into_handle(ModelInner::Ort(model)),
        Err(e) => {
            eprintln!("od_model_create: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Create a model from an ONNX file with CUDA execution provider.
///
/// # Safety
/// `model_path` must be a valid null-terminated C string.
#[cfg(feature = "cuda")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_create_cuda(
    model_path: *const c_char,
    input_w: u32,
    input_h: u32,
) -> *mut ModelHandle {
    let Some(path) = (unsafe { parse_cstr(model_path) }) else {
        return ptr::null_mut();
    };
    match Model::ort_cuda(path, (input_w, input_h)) {
        Ok(model) => into_handle(ModelInner::Ort(model)),
        Err(e) => {
            eprintln!("od_model_create_cuda: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Create a model from an ONNX file with TensorRT execution provider (via ORT).
///
/// # Safety
/// `model_path` must be a valid null-terminated C string.
#[cfg(feature = "tensorrt")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_create_tensorrt(
    model_path: *const c_char,
    input_w: u32,
    input_h: u32,
) -> *mut ModelHandle {
    let Some(path) = (unsafe { parse_cstr(model_path) }) else {
        return ptr::null_mut();
    };
    match Model::ort_tensorrt(path, (input_w, input_h)) {
        Ok(model) => into_handle(ModelInner::Ort(model)),
        Err(e) => {
            eprintln!("od_model_create_tensorrt: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Create a model from a serialized TensorRT engine file (native TensorRT, no ORT).
///
/// # Safety
/// `engine_path` must be a valid null-terminated C string.
#[cfg(feature = "trt")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_create_trt(
    engine_path: *const c_char,
) -> *mut ModelHandle {
    let Some(path) = (unsafe { parse_cstr(engine_path) }) else {
        return ptr::null_mut();
    };
    match Model::tensorrt(path) {
        Ok(model) => into_handle(ModelInner::Trt(model)),
        Err(e) => {
            eprintln!("od_model_create_trt: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Create a model from an RKNN model file (Rockchip NPU).
///
/// # Parameters
/// - `model_path`: null-terminated path to `.rknn` file
/// - `num_classes`: number of classes the model was trained on
///
/// # Safety
/// `model_path` must be a valid null-terminated C string.
#[cfg(feature = "rknn")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_create_rknn(
    model_path: *const c_char,
    num_classes: u32,
) -> *mut ModelHandle {
    let Some(path) = (unsafe { parse_cstr(model_path) }) else {
        return ptr::null_mut();
    };
    match Model::rknn(path, num_classes as usize) {
        Ok(model) => into_handle(ModelInner::Rknn(model)),
        Err(e) => {
            eprintln!("od_model_create_rknn: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Free a model handle.
///
/// # Safety
/// `handle` must have been returned by `od_model_create*` and not yet freed.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_free(handle: *mut ModelHandle) {
    if !handle.is_null() {
        drop(unsafe { Box::from_raw(handle) });
    }
}

/// Run detection on an RGB image.
///
/// Works with any backend: the handle dispatches to the correct runtime internally.
///
/// # Parameters
/// - `handle`: model handle from any `od_model_create_*` function
/// - `pixels_rgb`: pointer to `width * height * 3` bytes (RGB, row-major, HWC)
/// - `img_w`, `img_h`: image dimensions in pixels
/// - `conf_threshold`: confidence threshold (e.g. 0.3)
/// - `nms_threshold`: NMS IoU threshold (e.g. 0.4)
/// - `out`: pointer to `OdDetections` struct, filled on success
///
/// # Returns
/// `OdError::Ok` on success. On error, `out` is zeroed.
///
/// # Safety
/// - `handle` must be valid.
/// - `pixels_rgb` must point to at least `img_w * img_h * 3` bytes.
/// - `out` must be a valid pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_model_detect(
    handle: *mut ModelHandle,
    pixels_rgb: *const u8,
    img_w: i32,
    img_h: i32,
    conf_threshold: f32,
    nms_threshold: f32,
    out: *mut OdDetections,
) -> OdError {
    if handle.is_null() || pixels_rgb.is_null() || out.is_null() {
        return OdError::InvalidArgument;
    }
    if img_w <= 0 || img_h <= 0 {
        return OdError::InvalidArgument;
    }

    let model = unsafe { &mut *handle };
    let h = img_h as usize;
    let w = img_w as usize;
    let n_bytes = h * w * 3;

    let rgb_slice = unsafe { slice::from_raw_parts(pixels_rgb, n_bytes) };
    let arr = match Array3::from_shape_vec((h, w, 3), rgb_slice.to_vec()) {
        Ok(a) => a,
        Err(_) => {
            unsafe {
                (*out).data = ptr::null_mut();
                (*out).len = 0;
            }
            return OdError::ImageConvertFailed;
        }
    };

    let img_buf = od_opencv::ImageBuffer::from_rgb(arr);

    let (bboxes, class_ids, confidences) = match model.detect(&img_buf, conf_threshold, nms_threshold) {
        Ok(result) => result,
        Err(e) => {
            unsafe {
                (*out).data = ptr::null_mut();
                (*out).len = 0;
            }
            return e;
        }
    };

    let count = bboxes.len();
    if count == 0 {
        unsafe {
            (*out).data = ptr::null_mut();
            (*out).len = 0;
        }
        return OdError::Ok;
    }

    let mut results: Vec<OdDetection> = Vec::with_capacity(count);
    for i in 0..count {
        results.push(OdDetection {
            bbox_x: bboxes[i].x,
            bbox_y: bboxes[i].y,
            bbox_w: bboxes[i].width,
            bbox_h: bboxes[i].height,
            class_id: class_ids[i] as i32,
            confidence: confidences[i],
        });
    }

    let mut results = results.into_boxed_slice();
    unsafe {
        (*out).data = results.as_mut_ptr();
        (*out).len = count as i32;
    }
    std::mem::forget(results);

    OdError::Ok
}

/// Free detection results.
///
/// # Safety
/// `detections` must point to a valid `OdDetections` returned by `od_model_detect`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn od_detections_free(detections: *mut OdDetections) {
    if detections.is_null() {
        return;
    }
    let d = unsafe { &mut *detections };
    if !d.data.is_null() && d.len > 0 {
        let _ = unsafe {
            Vec::from_raw_parts(d.data, d.len as usize, d.len as usize)
        };
        d.data = ptr::null_mut();
        d.len = 0;
    }
}

/// Single face detection + recognition result (flat, safe for CGO memcpy).
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct FaceDetectionResult {
    /// Bounding box top-left X (pixels).
    pub bbox_x: f32,
    /// Bounding box top-left Y (pixels).
    pub bbox_y: f32,
    /// Bounding box width (pixels).
    pub bbox_w: f32,
    /// Bounding box height (pixels).
    pub bbox_h: f32,
    /// Detection confidence in [0.0, 1.0].
    pub confidence: f32,
    /// 5 facial landmarks: [x0,y0, x1,y1, ..., x4,y4] (10 floats).
    pub landmarks: [f32; 10],
    /// 512-dimensional L2-normalized embedding.
    pub embedding: [f32; 512],
}

/// Face detection results batch. Caller must free via `face_pipeline_results_free`.
#[repr(C)]
pub struct FaceDetectionResults {
    /// Pointer to the first element of the results array.
    pub data: *mut FaceDetectionResult,
    /// Number of face detections in the array.
    pub len: i32,
}

/// Opaque face pipeline handle. Created by `face_pipeline_create*`.
pub struct FacePipelineHandle {
    inner: FacePipeline,
}

/// Create a face pipeline (YuNet detector + ArcFace recognizer, ORT CPU).
///
/// # Parameters
/// - `detector_path`: null-terminated path to YuNet `.onnx` file
/// - `recognizer_path`: null-terminated path to ArcFace `.onnx` file (e.g. `w600k_mbf.onnx`)
///
/// # Returns
/// Opaque pointer, or null on error.
///
/// # Safety
/// Both paths must be valid null-terminated C strings.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_create(
    detector_path: *const c_char,
    recognizer_path: *const c_char,
) -> *mut FacePipelineHandle {
    let Some(det_path) = (unsafe { parse_cstr(detector_path) }) else {
        return ptr::null_mut();
    };
    let Some(rec_path) = (unsafe { parse_cstr(recognizer_path) }) else {
        return ptr::null_mut();
    };
    match FacePipeline::new(det_path, rec_path) {
        Ok(pipeline) => Box::into_raw(Box::new(FacePipelineHandle { inner: pipeline })),
        Err(e) => {
            eprintln!("face_pipeline_create: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Create a face pipeline with CUDA acceleration.
///
/// # Safety
/// Both paths must be valid null-terminated C strings.
#[cfg(feature = "cuda")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_create_cuda(
    detector_path: *const c_char,
    recognizer_path: *const c_char,
) -> *mut FacePipelineHandle {
    let Some(det_path) = (unsafe { parse_cstr(detector_path) }) else {
        return ptr::null_mut();
    };
    let Some(rec_path) = (unsafe { parse_cstr(recognizer_path) }) else {
        return ptr::null_mut();
    };
    match FacePipeline::new_cuda(det_path, rec_path) {
        Ok(pipeline) => Box::into_raw(Box::new(FacePipelineHandle { inner: pipeline })),
        Err(e) => {
            eprintln!("face_pipeline_create_cuda: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Create a face pipeline with TensorRT acceleration (via ORT).
///
/// # Safety
/// Both paths must be valid null-terminated C strings.
#[cfg(feature = "tensorrt")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_create_tensorrt(
    detector_path: *const c_char,
    recognizer_path: *const c_char,
) -> *mut FacePipelineHandle {
    let Some(det_path) = (unsafe { parse_cstr(detector_path) }) else {
        return ptr::null_mut();
    };
    let Some(rec_path) = (unsafe { parse_cstr(recognizer_path) }) else {
        return ptr::null_mut();
    };
    match FacePipeline::new_tensorrt(det_path, rec_path) {
        Ok(pipeline) => Box::into_raw(Box::new(FacePipelineHandle { inner: pipeline })),
        Err(e) => {
            eprintln!("face_pipeline_create_tensorrt: {e:?}");
            ptr::null_mut()
        }
    }
}

/// Returns the expected aligned face size (square side, read from the ONNX model).
///
/// E.g. 112 for MobileFaceNet (w600k_mbf.onnx).
/// Go-side should call this instead of hardcoding a constant.
///
/// # Safety
/// `handle` must be a valid pointer returned by `face_pipeline_create*`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_aligned_size(
    handle: *const FacePipelineHandle,
) -> u32 {
    if handle.is_null() {
        return 0;
    }
    unsafe { &*handle }.inner.aligned_size()
}

/// Run face detection + recognition on an RGB image.
///
/// # Parameters
/// - `handle`: face pipeline handle
/// - `pixels_rgb`: pointer to `width * height * 3` bytes (RGB, row-major, HWC)
/// - `img_w`, `img_h`: image dimensions in pixels
/// - `conf_threshold`: detection confidence threshold (e.g. 0.7)
/// - `nms_threshold`: NMS IoU threshold (e.g. 0.3)
/// - `out`: pointer to `FaceDetectionResults` struct, filled on success
///
/// # Returns
/// `OdError::Ok` on success. On error, `out` is zeroed.
///
/// # Safety
/// - `handle` must be valid.
/// - `pixels_rgb` must point to at least `img_w * img_h * 3` bytes.
/// - `out` must be a valid pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_process(
    handle: *mut FacePipelineHandle,
    pixels_rgb: *const u8,
    img_w: i32,
    img_h: i32,
    conf_threshold: f32,
    nms_threshold: f32,
    out: *mut FaceDetectionResults,
) -> OdError {
    if handle.is_null() || pixels_rgb.is_null() || out.is_null() {
        return OdError::InvalidArgument;
    }
    if img_w <= 0 || img_h <= 0 {
        return OdError::InvalidArgument;
    }

    let pipeline = unsafe { &mut *handle };
    let h = img_h as usize;
    let w = img_w as usize;
    let n_bytes = h * w * 3;

    let rgb_slice = unsafe { slice::from_raw_parts(pixels_rgb, n_bytes) };
    let arr = match Array3::from_shape_vec((h, w, 3), rgb_slice.to_vec()) {
        Ok(a) => a,
        Err(_) => {
            unsafe {
                (*out).data = ptr::null_mut();
                (*out).len = 0;
            }
            return OdError::ImageConvertFailed;
        }
    };

    let img_buf = od_opencv::ImageBuffer::from_rgb(arr);

    let faces = match pipeline.inner.process(&img_buf, conf_threshold, nms_threshold) {
        Ok(f) => f,
        Err(e) => {
            eprintln!("face_pipeline_process: {e:?}");
            unsafe {
                (*out).data = ptr::null_mut();
                (*out).len = 0;
            }
            return OdError::DetectionFailed;
        }
    };

    let count = faces.len();
    if count == 0 {
        unsafe {
            (*out).data = ptr::null_mut();
            (*out).len = 0;
        }
        return OdError::Ok;
    }

    let mut results: Vec<FaceDetectionResult> = Vec::with_capacity(count);
    for face in &faces {
        let mut landmarks = [0.0f32; 10];
        for k in 0..5 {
            landmarks[k * 2] = face.landmarks[k][0];
            landmarks[k * 2 + 1] = face.landmarks[k][1];
        }
        results.push(FaceDetectionResult {
            bbox_x: face.x,
            bbox_y: face.y,
            bbox_w: face.width,
            bbox_h: face.height,
            confidence: face.confidence,
            landmarks,
            embedding: face.embedding,
        });
    }

    let mut results = results.into_boxed_slice();
    unsafe {
        (*out).data = results.as_mut_ptr();
        (*out).len = count as i32;
    }
    std::mem::forget(results);

    OdError::Ok
}

/// Extract embedding from a pre-aligned face image.
///
/// The image must be aligned to the size returned by `face_pipeline_aligned_size()`
/// (typically 112x112).
///
/// # Parameters
/// - `handle`: face pipeline handle
/// - `pixels_rgb`: pointer to aligned face RGB data (size x size x 3 bytes)
/// - `size`: aligned face size (e.g. 112)
/// - `out_embedding`: pointer to caller-allocated `[f32; 512]` buffer
///
/// # Returns
/// `OdError::Ok` on success.
///
/// # Safety
/// - `handle` must be valid.
/// - `pixels_rgb` must point to at least `size * size * 3` bytes.
/// - `out_embedding` must point to at least 512 f32 elements.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_embed(
    handle: *mut FacePipelineHandle,
    pixels_rgb: *const u8,
    size: i32,
    out_embedding: *mut f32,
) -> OdError {
    if handle.is_null() || pixels_rgb.is_null() || out_embedding.is_null() {
        return OdError::InvalidArgument;
    }
    if size <= 0 {
        return OdError::InvalidArgument;
    }

    let pipeline = unsafe { &mut *handle };
    let s = size as usize;
    let n_bytes = s * s * 3;

    let rgb_slice = unsafe { slice::from_raw_parts(pixels_rgb, n_bytes) };
    let arr = match Array3::from_shape_vec((s, s, 3), rgb_slice.to_vec()) {
        Ok(a) => a,
        Err(_) => return OdError::ImageConvertFailed,
    };

    let img_buf = od_opencv::ImageBuffer::from_rgb(arr);

    match pipeline.inner.embed(&img_buf) {
        Ok(embedding) => {
            unsafe {
                ptr::copy_nonoverlapping(embedding.as_ptr(), out_embedding, 512);
            }
            OdError::Ok
        }
        Err(e) => {
            eprintln!("face_pipeline_embed: {e:?}");
            OdError::DetectionFailed
        }
    }
}

/// Free a face pipeline handle.
///
/// # Safety
/// `handle` must have been returned by `face_pipeline_create*` and not yet freed.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_destroy(handle: *mut FacePipelineHandle) {
    if !handle.is_null() {
        drop(unsafe { Box::from_raw(handle) });
    }
}

/// Free face detection results.
///
/// # Safety
/// `results` must point to a valid `FaceDetectionResults` returned by `face_pipeline_process`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn face_pipeline_results_free(results: *mut FaceDetectionResults) {
    if results.is_null() {
        return;
    }
    let r = unsafe { &mut *results };
    if !r.data.is_null() && r.len > 0 {
        let _ = unsafe {
            Vec::from_raw_parts(r.data, r.len as usize, r.len as usize)
        };
        r.data = ptr::null_mut();
        r.len = 0;
    }
}