od_opencv 0.10.1

Object detection utilities in Rust programming language for YOLO-based neural networks in OpenCV ecosystem
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
//! Model factory for creating object detection models.
//!
//! This module provides a unified `Model` factory with static methods
//! for creating models with different backends. Each method returns
//! the concrete model type (zero-cost, no dynamic dispatch).
//!
//! # Example
//!
//! ```ignore
//! use od_opencv::{Model, DnnBackend, DnnTarget};
//!
//! // ORT backend (CPU)
//! let model = Model::ort("yolov8n.onnx", (640, 640))?;
//!
//! // ORT backend with CUDA
//! let model = Model::ort_cuda("yolov8n.onnx", (640, 640))?;
//!
//! // OpenCV backend for Ultralytics models (CUDA)
//! let model = Model::opencv("yolov8n.onnx", (640, 640), DnnBackend::Cuda, DnnTarget::Cuda)?;
//!
//! // OpenCV backend for Darknet models (CUDA)
//! let model = Model::darknet("yolov4.cfg", "yolov4.weights", (416, 416), DnnBackend::Cuda, DnnTarget::Cuda)?;
//! ```

/// Factory for creating object detection models.
///
/// This is a zero-sized type that serves as a namespace for model constructors.
/// Each method returns the concrete model type, enabling full compiler optimization.
pub struct Model;

// ============================================================================
// ORT Backend
// ============================================================================

#[cfg(feature = "ort-backend")]
impl Model {
    /// Creates a new Ultralytics YOLO model (v8/v9/v11) using ONNX Runtime (CPU).
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::ort("yolov8n.onnx", (640, 640))?;
    /// ```
    pub fn ort(
        model_path: &str,
        input_size: (u32, u32),
    ) -> Result<crate::backend_ort::ModelUltralyticsOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelUltralyticsOrt::new_from_file(model_path, input_size, vec![])
    }

    /// Creates a new Ultralytics YOLO model with class filtering using ONNX Runtime (CPU).
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `class_filters` - List of class indices to detect (empty for all classes)
    pub fn ort_filtered(
        model_path: &str,
        input_size: (u32, u32),
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_ort::ModelUltralyticsOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelUltralyticsOrt::new_from_file(model_path, input_size, class_filters)
    }

    /// Creates a new YOLOv5 model using ONNX Runtime (CPU).
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::yolov5_ort("yolov5s.onnx", (640, 640))?;
    /// ```
    pub fn yolov5_ort(
        model_path: &str,
        input_size: (u32, u32),
    ) -> Result<crate::backend_ort::ModelYOLOv5Ort, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYOLOv5Ort::new_from_file(model_path, input_size, vec![])
    }

    /// Creates a new YOLOv5 model with class filtering using ONNX Runtime (CPU).
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `class_filters` - List of class indices to detect (empty for all classes)
    pub fn yolov5_ort_filtered(
        model_path: &str,
        input_size: (u32, u32),
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_ort::ModelYOLOv5Ort, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYOLOv5Ort::new_from_file(model_path, input_size, class_filters)
    }
}

// YuNet face detection via ORT (CPU)
#[cfg(feature = "ort-backend")]
impl Model {
    /// Creates a new YuNet face detection model using ONNX Runtime (CPU).
    ///
    /// Input dimensions are read from the ONNX metadata automatically.
    ///
    /// # Arguments
    /// * `model_path` - Path to the YuNet ONNX model file
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::yunet_ort("face_detection_yunet_2023mar.onnx")?;
    /// ```
    pub fn yunet_ort(
        model_path: &str,
    ) -> Result<crate::backend_ort::ModelYuNetOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYuNetOrt::new_from_file(model_path)
    }
}

// ArcFace face recognition via ORT (CPU)
#[cfg(feature = "ort-backend")]
impl Model {
    /// Creates a new ArcFace model using ONNX Runtime (CPU).
    ///
    /// Uses `MobileFaceNet` normalization ([-1, 1]) by default.
    /// For ResNet50 models, use [`arcface_ort_with_norm`].
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::arcface_ort("w600k_mbf.onnx")?;
    /// ```
    pub fn arcface_ort(
        model_path: &str,
    ) -> Result<crate::backend_ort::ModelArcFaceOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelArcFaceOrt::new_from_file(model_path)
    }

    /// Creates a new ArcFace model using ONNX Runtime (CPU) with explicit normalization.
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::ArcFaceNorm;
    /// let mut model = Model::arcface_ort_with_norm("w600k_r50.onnx", ArcFaceNorm::ResNet)?;
    /// ```
    pub fn arcface_ort_with_norm(
        model_path: &str,
        norm: crate::backend_ort::ArcFaceNorm,
    ) -> Result<crate::backend_ort::ModelArcFaceOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelArcFaceOrt::new_from_file_with_norm(model_path, norm)
    }

    /// Creates a face pipeline using ONNX Runtime (CPU).
    ///
    /// Uses `MobileFaceNet` normalization by default.
    ///
    /// # Example
    /// ```ignore
    /// let mut pipeline = Model::face_pipeline(
    ///     "face_detection_yunet_2023mar.onnx",
    ///     "w600k_mbf.onnx",
    /// )?;
    /// ```
    pub fn face_pipeline(
        detector_path: &str,
        recognizer_path: &str,
    ) -> Result<crate::face_pipeline::FacePipeline, crate::backend_ort::OrtModelError> {
        crate::face_pipeline::FacePipeline::new(detector_path, recognizer_path)
    }

    /// Creates a face pipeline using ONNX Runtime (CPU) with explicit normalization.
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::ArcFaceNorm;
    /// let mut pipeline = Model::face_pipeline_with_norm(
    ///     "yunet.onnx", "w600k_r50.onnx", ArcFaceNorm::ResNet,
    /// )?;
    /// ```
    pub fn face_pipeline_with_norm(
        detector_path: &str,
        recognizer_path: &str,
        norm: crate::backend_ort::ArcFaceNorm,
    ) -> Result<crate::face_pipeline::FacePipeline, crate::backend_ort::OrtModelError> {
        crate::face_pipeline::FacePipeline::new_with_norm(detector_path, recognizer_path, norm)
    }
}

// ArcFace face recognition via ORT + CUDA
#[cfg(feature = "ort-cuda-backend")]
impl Model {
    /// Creates a new ArcFace model using ONNX Runtime with CUDA.
    pub fn arcface_ort_cuda(
        model_path: &str,
    ) -> Result<crate::backend_ort::ModelArcFaceOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelArcFaceOrt::new_from_file_cuda(model_path)
    }

    /// Creates a new ArcFace model using ONNX Runtime with CUDA and explicit normalization.
    pub fn arcface_ort_cuda_with_norm(
        model_path: &str,
        norm: crate::backend_ort::ArcFaceNorm,
    ) -> Result<crate::backend_ort::ModelArcFaceOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelArcFaceOrt::new_from_file_cuda_with_norm(model_path, norm)
    }

    /// Creates a face pipeline using ONNX Runtime with CUDA.
    pub fn face_pipeline_cuda(
        detector_path: &str,
        recognizer_path: &str,
    ) -> Result<crate::face_pipeline::FacePipeline, crate::backend_ort::OrtModelError> {
        crate::face_pipeline::FacePipeline::new_cuda(detector_path, recognizer_path)
    }

    /// Creates a face pipeline using ONNX Runtime with CUDA and explicit normalization.
    pub fn face_pipeline_cuda_with_norm(
        detector_path: &str,
        recognizer_path: &str,
        norm: crate::backend_ort::ArcFaceNorm,
    ) -> Result<crate::face_pipeline::FacePipeline, crate::backend_ort::OrtModelError> {
        crate::face_pipeline::FacePipeline::new_cuda_with_norm(detector_path, recognizer_path, norm)
    }
}

// ArcFace face recognition via ORT + TensorRT EP
#[cfg(feature = "ort-tensorrt-backend")]
impl Model {
    /// Creates a new ArcFace model using ONNX Runtime with TensorRT.
    pub fn arcface_ort_tensorrt(
        model_path: &str,
    ) -> Result<crate::backend_ort::ModelArcFaceOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelArcFaceOrt::new_from_file_tensorrt(model_path)
    }

    /// Creates a new ArcFace model using ONNX Runtime with TensorRT and explicit normalization.
    pub fn arcface_ort_tensorrt_with_norm(
        model_path: &str,
        norm: crate::backend_ort::ArcFaceNorm,
    ) -> Result<crate::backend_ort::ModelArcFaceOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelArcFaceOrt::new_from_file_tensorrt_with_norm(model_path, norm)
    }

    /// Creates a face pipeline using ONNX Runtime with TensorRT.
    pub fn face_pipeline_tensorrt(
        detector_path: &str,
        recognizer_path: &str,
    ) -> Result<crate::face_pipeline::FacePipeline, crate::backend_ort::OrtModelError> {
        crate::face_pipeline::FacePipeline::new_tensorrt(detector_path, recognizer_path)
    }

    /// Creates a face pipeline using ONNX Runtime with TensorRT and explicit normalization.
    pub fn face_pipeline_tensorrt_with_norm(
        detector_path: &str,
        recognizer_path: &str,
        norm: crate::backend_ort::ArcFaceNorm,
    ) -> Result<crate::face_pipeline::FacePipeline, crate::backend_ort::OrtModelError> {
        crate::face_pipeline::FacePipeline::new_tensorrt_with_norm(detector_path, recognizer_path, norm)
    }
}

// YuNet face detection via ORT + CUDA
#[cfg(feature = "ort-cuda-backend")]
impl Model {
    /// Creates a new YuNet face detection model using ONNX Runtime with CUDA.
    pub fn yunet_ort_cuda(
        model_path: &str,
    ) -> Result<crate::backend_ort::ModelYuNetOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYuNetOrt::new_from_file_cuda(model_path)
    }
}

// YuNet face detection via ORT + TensorRT EP
#[cfg(feature = "ort-tensorrt-backend")]
impl Model {
    /// Creates a new YuNet face detection model using ONNX Runtime with TensorRT.
    pub fn yunet_ort_tensorrt(
        model_path: &str,
    ) -> Result<crate::backend_ort::ModelYuNetOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYuNetOrt::new_from_file_tensorrt(model_path)
    }
}

#[cfg(feature = "ort-cuda-backend")]
impl Model {
    /// Creates a new Ultralytics YOLO model using ONNX Runtime with CUDA acceleration.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::ort_cuda("yolov8n.onnx", (640, 640))?;
    /// ```
    pub fn ort_cuda(
        model_path: &str,
        input_size: (u32, u32),
    ) -> Result<crate::backend_ort::ModelUltralyticsOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelUltralyticsOrt::new_from_file_cuda(model_path, input_size, vec![])
    }

    /// Creates a new Ultralytics YOLO model with class filtering using ONNX Runtime with CUDA.
    pub fn ort_cuda_filtered(
        model_path: &str,
        input_size: (u32, u32),
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_ort::ModelUltralyticsOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelUltralyticsOrt::new_from_file_cuda(model_path, input_size, class_filters)
    }

    /// Creates a new YOLOv5 model using ONNX Runtime with CUDA acceleration.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::yolov5_ort_cuda("yolov5s.onnx", (640, 640))?;
    /// ```
    pub fn yolov5_ort_cuda(
        model_path: &str,
        input_size: (u32, u32),
    ) -> Result<crate::backend_ort::ModelYOLOv5Ort, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYOLOv5Ort::new_from_file_cuda(model_path, input_size, vec![])
    }

    /// Creates a new YOLOv5 model with class filtering using ONNX Runtime with CUDA.
    pub fn yolov5_ort_cuda_filtered(
        model_path: &str,
        input_size: (u32, u32),
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_ort::ModelYOLOv5Ort, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYOLOv5Ort::new_from_file_cuda(model_path, input_size, class_filters)
    }
}

#[cfg(feature = "ort-tensorrt-backend")]
impl Model {
    /// Creates a new Ultralytics YOLO model using ONNX Runtime with TensorRT acceleration.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::ort_tensorrt("yolov8n.onnx", (640, 640))?;
    /// ```
    pub fn ort_tensorrt(
        model_path: &str,
        input_size: (u32, u32),
    ) -> Result<crate::backend_ort::ModelUltralyticsOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelUltralyticsOrt::new_from_file_tensorrt(model_path, input_size, vec![])
    }

    /// Creates a new Ultralytics YOLO model with class filtering using TensorRT.
    pub fn ort_tensorrt_filtered(
        model_path: &str,
        input_size: (u32, u32),
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_ort::ModelUltralyticsOrt, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelUltralyticsOrt::new_from_file_tensorrt(model_path, input_size, class_filters)
    }

    /// Creates a new YOLOv5 model using ONNX Runtime with TensorRT acceleration.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::yolov5_ort_tensorrt("yolov5s.onnx", (640, 640))?;
    /// ```
    pub fn yolov5_ort_tensorrt(
        model_path: &str,
        input_size: (u32, u32),
    ) -> Result<crate::backend_ort::ModelYOLOv5Ort, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYOLOv5Ort::new_from_file_tensorrt(model_path, input_size, vec![])
    }

    /// Creates a new YOLOv5 model with class filtering using TensorRT.
    pub fn yolov5_ort_tensorrt_filtered(
        model_path: &str,
        input_size: (u32, u32),
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_ort::ModelYOLOv5Ort, crate::backend_ort::OrtModelError> {
        crate::backend_ort::ModelYOLOv5Ort::new_from_file_tensorrt(model_path, input_size, class_filters)
    }
}

// ============================================================================
// OpenCV Backend
// ============================================================================

#[cfg(feature = "opencv-backend")]
impl Model {
    /// Creates a new Ultralytics YOLO model (v8/v9/v11) using OpenCV DNN.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend (e.g., `DnnBackend::Cuda`, `DnnBackend::OpenCV`)
    /// * `target` - DNN target device (e.g., `DnnTarget::Cuda`, `DnnTarget::Cpu`)
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::{Model, DnnBackend, DnnTarget};
    ///
    /// // CUDA inference
    /// let mut model = Model::opencv("yolov8n.onnx", (640, 640), DnnBackend::Cuda, DnnTarget::Cuda)?;
    ///
    /// // CPU inference
    /// let mut model = Model::opencv("yolov8n.onnx", (640, 640), DnnBackend::OpenCV, DnnTarget::Cpu)?;
    /// ```
    pub fn opencv(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
    ) -> Result<crate::backend_opencv::model_ultralytics::ModelUltralyticsV8, opencv::Error> {
        crate::backend_opencv::model_ultralytics::ModelUltralyticsV8::new_from_onnx_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
            vec![],
        )
    }

    /// Creates a new Ultralytics YOLO model with class filtering using OpenCV DNN.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend
    /// * `target` - DNN target device
    /// * `class_filters` - List of class indices to detect (empty for all)
    pub fn opencv_filtered(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_opencv::model_ultralytics::ModelUltralyticsV8, opencv::Error> {
        crate::backend_opencv::model_ultralytics::ModelUltralyticsV8::new_from_onnx_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
            class_filters,
        )
    }

    /// Creates a new classic YOLO model (v3/v4/v7) from Darknet files using OpenCV DNN.
    ///
    /// # Arguments
    /// * `cfg_path` - Path to the Darknet .cfg file
    /// * `weights_path` - Path to the Darknet .weights file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend
    /// * `target` - DNN target device
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::{Model, DnnBackend, DnnTarget};
    ///
    /// let mut model = Model::darknet(
    ///     "yolov4.cfg",
    ///     "yolov4.weights",
    ///     (416, 416),
    ///     DnnBackend::Cuda,
    ///     DnnTarget::Cuda
    /// )?;
    /// ```
    pub fn darknet(
        cfg_path: &str,
        weights_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
    ) -> Result<crate::backend_opencv::model_classic::ModelYOLOClassic, opencv::Error> {
        crate::backend_opencv::model_classic::ModelYOLOClassic::new_from_darknet_file(
            weights_path,
            cfg_path,
            input_size,
            backend.into(),
            target.into(),
            vec![],
        )
    }

    /// Creates a new classic YOLO model with class filtering from Darknet files using OpenCV DNN.
    ///
    /// # Arguments
    /// * `cfg_path` - Path to the Darknet .cfg file
    /// * `weights_path` - Path to the Darknet .weights file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend
    /// * `target` - DNN target device
    /// * `class_filters` - List of class indices to detect (empty for all)
    pub fn darknet_filtered(
        cfg_path: &str,
        weights_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_opencv::model_classic::ModelYOLOClassic, opencv::Error> {
        crate::backend_opencv::model_classic::ModelYOLOClassic::new_from_darknet_file(
            weights_path,
            cfg_path,
            input_size,
            backend.into(),
            target.into(),
            class_filters,
        )
    }

    /// Creates a new classic YOLO model (v3/v4/v7) from ONNX file using OpenCV DNN.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend
    /// * `target` - DNN target device
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::{Model, DnnBackend, DnnTarget};
    ///
    /// let mut model = Model::classic_onnx(
    ///     "yolov4-tiny.onnx",
    ///     (416, 416),
    ///     DnnBackend::Cuda,
    ///     DnnTarget::Cuda
    /// )?;
    /// ```
    pub fn classic_onnx(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
    ) -> Result<crate::backend_opencv::model_classic::ModelYOLOClassic, opencv::Error> {
        crate::backend_opencv::model_classic::ModelYOLOClassic::new_from_onnx_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
            vec![],
        )
    }

    /// Creates a new classic YOLO model with class filtering from ONNX using OpenCV DNN.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend
    /// * `target` - DNN target device
    /// * `class_filters` - List of class indices to detect (empty for all)
    pub fn classic_onnx_filtered(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_opencv::model_classic::ModelYOLOClassic, opencv::Error> {
        crate::backend_opencv::model_classic::ModelYOLOClassic::new_from_onnx_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
            class_filters,
        )
    }

    /// Creates a new YOLOv5 model using OpenCV DNN.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend (e.g., `DnnBackend::Cuda`, `DnnBackend::OpenCV`)
    /// * `target` - DNN target device (e.g., `DnnTarget::Cuda`, `DnnTarget::Cpu`)
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::{Model, DnnBackend, DnnTarget};
    ///
    /// let mut model = Model::yolov5_opencv("yolov5s.onnx", (640, 640), DnnBackend::Cuda, DnnTarget::Cuda)?;
    /// ```
    pub fn yolov5_opencv(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
    ) -> Result<crate::backend_opencv::model_yolov5::ModelYOLOv5OpenCV, opencv::Error> {
        crate::backend_opencv::model_yolov5::ModelYOLOv5OpenCV::new_from_onnx_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
            vec![],
        )
    }

    /// Creates a new YOLOv5 model with class filtering using OpenCV DNN.
    ///
    /// # Arguments
    /// * `model_path` - Path to the ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend
    /// * `target` - DNN target device
    /// * `class_filters` - List of class indices to detect (empty for all)
    pub fn yolov5_opencv_filtered(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_opencv::model_yolov5::ModelYOLOv5OpenCV, opencv::Error> {
        crate::backend_opencv::model_yolov5::ModelYOLOv5OpenCV::new_from_onnx_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
            class_filters,
        )
    }
}

// YuNet face detection via OpenCV (FaceDetectorYN)
#[cfg(feature = "opencv-backend")]
impl Model {
    /// Creates a new YuNet face detection model using OpenCV's FaceDetectorYN.
    ///
    /// Requires OpenCV 4.5.4+ with the `objdetect` module.
    ///
    /// # Arguments
    /// * `model_path` - Path to the YuNet ONNX model file
    /// * `input_size` - Model input size as (width, height)
    /// * `backend` - DNN backend (e.g., `DnnBackend::Cuda`, `DnnBackend::OpenCV`)
    /// * `target` - DNN target device (e.g., `DnnTarget::Cuda`, `DnnTarget::Cpu`)
    ///
    /// # Example
    /// ```ignore
    /// use od_opencv::{Model, DnnBackend, DnnTarget};
    ///
    /// let mut model = Model::yunet_opencv(
    ///     "face_detection_yunet_2023mar.onnx",
    ///     (320, 320),
    ///     DnnBackend::OpenCV,
    ///     DnnTarget::Cpu,
    /// )?;
    /// ```
    pub fn yunet_opencv(
        model_path: &str,
        input_size: (i32, i32),
        backend: crate::dnn_backend::DnnBackend,
        target: crate::dnn_backend::DnnTarget,
    ) -> Result<crate::backend_opencv::ModelYuNetOpenCV, opencv::Error> {
        crate::backend_opencv::ModelYuNetOpenCV::new_from_file(
            model_path,
            input_size,
            backend.into(),
            target.into(),
        )
    }
}

// ============================================================================
// TensorRT Backend
// ============================================================================

#[cfg(feature = "tensorrt-backend")]
impl Model {
    /// Creates a new Ultralytics YOLO model (v8/v9/v11) using TensorRT.
    ///
    /// Loads a pre-built `.engine` file. The engine must be built separately
    /// using `trtexec` for the target GPU. Input dimensions are read directly
    /// from the engine bindings.
    ///
    /// # Arguments
    /// * `engine_path` - Path to the pre-built `.engine` file
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::tensorrt("yolov8n.engine")?;
    /// ```
    pub fn tensorrt(
        engine_path: &str,
    ) -> Result<crate::backend_tensorrt::ModelUltralyticsRt, crate::backend_tensorrt::TrtModelError> {
        crate::backend_tensorrt::ModelUltralyticsRt::new_from_file(engine_path, vec![])
    }

    /// Creates a new Ultralytics YOLO model with class filtering using TensorRT.
    ///
    /// # Arguments
    /// * `engine_path` - Path to the pre-built `.engine` file
    /// * `class_filters` - List of class indices to detect (empty for all classes)
    pub fn tensorrt_filtered(
        engine_path: &str,
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_tensorrt::ModelUltralyticsRt, crate::backend_tensorrt::TrtModelError> {
        crate::backend_tensorrt::ModelUltralyticsRt::new_from_file(engine_path, class_filters)
    }

    /// Creates a new YuNet face detection model using TensorRT.
    ///
    /// # Arguments
    /// * `engine_path` - Path to the pre-built `.engine` file
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::yunet_tensorrt("face_detection_yunet.engine")?;
    /// ```
    pub fn yunet_tensorrt(
        engine_path: &str,
    ) -> Result<crate::backend_tensorrt::ModelYuNetRt, crate::backend_tensorrt::TrtModelError> {
        crate::backend_tensorrt::ModelYuNetRt::new_from_file(engine_path)
    }
}

// ============================================================================
// RKNN Backend
// ============================================================================

#[cfg(feature = "rknn-backend")]
impl Model {
    /// Creates a new Ultralytics YOLO model (v8/v9/v11) using RKNN NPU.
    ///
    /// Input size is read from the model automatically.
    ///
    /// # Arguments
    /// * `model_path` - Path to the `.rknn` model file
    /// * `num_classes` - Number of detection classes in the model
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::rknn("yolov8n.rknn", 4)?;
    /// ```
    pub fn rknn(
        model_path: &str,
        num_classes: usize,
    ) -> Result<crate::backend_rknn::ModelUltralyticsRknn, crate::backend_rknn::RknnModelError> {
        crate::backend_rknn::ModelUltralyticsRknn::new_from_file(
            model_path,
            num_classes,
            vec![],
        )
    }

    /// Creates a new Ultralytics YOLO model with class filtering using RKNN NPU.
    ///
    /// Input size is read from the model automatically.
    ///
    /// # Arguments
    /// * `model_path` - Path to the `.rknn` model file
    /// * `num_classes` - Number of detection classes in the model
    /// * `class_filters` - List of class indices to detect (empty for all classes)
    pub fn rknn_filtered(
        model_path: &str,
        num_classes: usize,
        class_filters: Vec<usize>,
    ) -> Result<crate::backend_rknn::ModelUltralyticsRknn, crate::backend_rknn::RknnModelError> {
        crate::backend_rknn::ModelUltralyticsRknn::new_from_file(
            model_path,
            num_classes,
            class_filters,
        )
    }

    /// Creates a new YuNet face detection model using RKNN NPU.
    ///
    /// # Arguments
    /// * `model_path` - Path to the `.rknn` model file
    ///
    /// # Example
    /// ```ignore
    /// let mut model = Model::yunet_rknn("face_detection_yunet.rknn")?;
    /// ```
    pub fn yunet_rknn(
        model_path: &str,
    ) -> Result<crate::backend_rknn::ModelYuNetRknn, crate::backend_rknn::RknnModelError> {
        crate::backend_rknn::ModelYuNetRknn::new_from_file(model_path)
    }
}