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
//! Error types for computer vision operations.
//!
//! This module provides the [`CvError`] type which represents all errors
//! that can occur during computer vision operations, and the [`CvResult`]
//! type alias for convenient use.
use oximedia_core::OxiError;
/// Error type for computer vision operations.
///
/// This enum covers all possible errors that can occur during image processing,
/// detection, and transformation operations.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::{CvError, CvResult};
///
/// fn process_image(width: u32, height: u32) -> CvResult<()> {
/// if width == 0 || height == 0 {
/// return Err(CvError::InvalidDimensions { width, height });
/// }
/// Ok(())
/// }
/// ```
#[derive(Debug, thiserror::Error)]
pub enum CvError {
/// Invalid image dimensions (width or height is zero).
#[error("Invalid image dimensions: {width}x{height}")]
InvalidDimensions {
/// Image width.
width: u32,
/// Image height.
height: u32,
},
/// Invalid region of interest.
#[error("Invalid ROI: ({x}, {y}, {width}, {height})")]
InvalidRoi {
/// ROI x coordinate.
x: u32,
/// ROI y coordinate.
y: u32,
/// ROI width.
width: u32,
/// ROI height.
height: u32,
},
/// Invalid kernel size for filter operations.
#[error("Invalid kernel size: {size} (must be odd and >= 3)")]
InvalidKernelSize {
/// The invalid kernel size.
size: usize,
},
/// Color space conversion error.
#[error("Color conversion error: {message}")]
ColorConversion {
/// Description of the error.
message: String,
},
/// Unsupported pixel format.
#[error("Unsupported pixel format: {format}")]
UnsupportedFormat {
/// The unsupported format name.
format: String,
},
/// Detection failed.
#[error("Detection failed: {message}")]
DetectionFailed {
/// Description of the failure.
message: String,
},
/// Transform computation failed.
#[error("Transform error: {message}")]
TransformError {
/// Description of the error.
message: String,
},
/// Tracking operation failed.
#[error("Tracking error: {message}")]
TrackingError {
/// Description of the error.
message: String,
},
/// Insufficient data for operation.
#[error("Insufficient data: expected {expected} bytes, got {actual}")]
InsufficientData {
/// Expected number of bytes.
expected: usize,
/// Actual number of bytes.
actual: usize,
},
/// Matrix operation error.
#[error("Matrix error: {message}")]
MatrixError {
/// Description of the error.
message: String,
},
/// Invalid parameter value.
#[error("Invalid parameter: {name} = {value}")]
InvalidParameter {
/// Parameter name.
name: String,
/// Invalid value description.
value: String,
},
/// Core error from oximedia-core.
#[error("Core error: {0}")]
Core(#[from] OxiError),
/// ONNX Runtime error.
#[error("ONNX Runtime error: {message}")]
OnnxRuntime {
/// Description of the error.
message: String,
},
/// Model loading error.
#[error("Model load error: {message}")]
ModelLoad {
/// Description of the error.
message: String,
},
/// Tensor operation error.
#[error("Tensor error: {message}")]
TensorError {
/// Description of the error.
message: String,
},
/// Shape mismatch error.
#[error("Shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeMismatch {
/// Expected shape.
expected: Vec<usize>,
/// Actual shape.
actual: Vec<usize>,
},
/// I/O error.
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
}
impl CvError {
/// Creates a new invalid dimensions error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::invalid_dimensions(0, 100);
/// assert!(matches!(err, CvError::InvalidDimensions { width: 0, height: 100 }));
/// ```
#[must_use]
pub const fn invalid_dimensions(width: u32, height: u32) -> Self {
Self::InvalidDimensions { width, height }
}
/// Creates a new invalid ROI error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::invalid_roi(10, 20, 0, 50);
/// assert!(matches!(err, CvError::InvalidRoi { .. }));
/// ```
#[must_use]
pub const fn invalid_roi(x: u32, y: u32, width: u32, height: u32) -> Self {
Self::InvalidRoi {
x,
y,
width,
height,
}
}
/// Creates a new invalid kernel size error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::invalid_kernel_size(4);
/// assert!(matches!(err, CvError::InvalidKernelSize { size: 4 }));
/// ```
#[must_use]
pub const fn invalid_kernel_size(size: usize) -> Self {
Self::InvalidKernelSize { size }
}
/// Creates a new color conversion error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::color_conversion("Invalid color values");
/// assert!(matches!(err, CvError::ColorConversion { .. }));
/// ```
#[must_use]
pub fn color_conversion(message: impl Into<String>) -> Self {
Self::ColorConversion {
message: message.into(),
}
}
/// Creates a new unsupported format error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::unsupported_format("YUV444");
/// assert!(matches!(err, CvError::UnsupportedFormat { .. }));
/// ```
#[must_use]
pub fn unsupported_format(format: impl Into<String>) -> Self {
Self::UnsupportedFormat {
format: format.into(),
}
}
/// Creates a new detection failed error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::detection_failed("No faces found");
/// assert!(matches!(err, CvError::DetectionFailed { .. }));
/// ```
#[must_use]
pub fn detection_failed(message: impl Into<String>) -> Self {
Self::DetectionFailed {
message: message.into(),
}
}
/// Creates a new transform error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::transform_error("Matrix is singular");
/// assert!(matches!(err, CvError::TransformError { .. }));
/// ```
#[must_use]
pub fn transform_error(message: impl Into<String>) -> Self {
Self::TransformError {
message: message.into(),
}
}
/// Creates a new tracking error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::tracking_error("Tracker not initialized");
/// assert!(matches!(err, CvError::TrackingError { .. }));
/// ```
#[must_use]
pub fn tracking_error(message: impl Into<String>) -> Self {
Self::TrackingError {
message: message.into(),
}
}
/// Creates a new insufficient data error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::insufficient_data(1024, 512);
/// assert!(matches!(err, CvError::InsufficientData { expected: 1024, actual: 512 }));
/// ```
#[must_use]
pub const fn insufficient_data(expected: usize, actual: usize) -> Self {
Self::InsufficientData { expected, actual }
}
/// Creates a new matrix error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::matrix_error("Matrix dimensions mismatch");
/// assert!(matches!(err, CvError::MatrixError { .. }));
/// ```
#[must_use]
pub fn matrix_error(message: impl Into<String>) -> Self {
Self::MatrixError {
message: message.into(),
}
}
/// Creates a new invalid parameter error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::invalid_parameter("sigma", "-1.0");
/// assert!(matches!(err, CvError::InvalidParameter { .. }));
/// ```
#[must_use]
pub fn invalid_parameter(name: impl Into<String>, value: impl Into<String>) -> Self {
Self::InvalidParameter {
name: name.into(),
value: value.into(),
}
}
/// Creates a new ONNX Runtime error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::onnx_runtime("Session initialization failed");
/// assert!(matches!(err, CvError::OnnxRuntime { .. }));
/// ```
#[must_use]
pub fn onnx_runtime(message: impl Into<String>) -> Self {
Self::OnnxRuntime {
message: message.into(),
}
}
/// Creates a new model load error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::model_load("File not found");
/// assert!(matches!(err, CvError::ModelLoad { .. }));
/// ```
#[must_use]
pub fn model_load(message: impl Into<String>) -> Self {
Self::ModelLoad {
message: message.into(),
}
}
/// Creates a new tensor error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::tensor_error("Invalid data type");
/// assert!(matches!(err, CvError::TensorError { .. }));
/// ```
#[must_use]
pub fn tensor_error(message: impl Into<String>) -> Self {
Self::TensorError {
message: message.into(),
}
}
/// Creates a new shape mismatch error.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvError;
///
/// let err = CvError::shape_mismatch(vec![1, 3, 224, 224], vec![1, 3, 256, 256]);
/// assert!(matches!(err, CvError::ShapeMismatch { .. }));
/// ```
#[must_use]
pub fn shape_mismatch(expected: Vec<usize>, actual: Vec<usize>) -> Self {
Self::ShapeMismatch { expected, actual }
}
}
/// Result type alias for computer vision operations.
///
/// This is a convenience alias for `Result<T, CvError>`.
///
/// # Examples
///
/// ```
/// use oximedia_cv::error::CvResult;
///
/// fn process() -> CvResult<u32> {
/// Ok(42)
/// }
/// ```
pub type CvResult<T> = Result<T, CvError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_invalid_dimensions() {
let err = CvError::invalid_dimensions(0, 100);
assert!(matches!(
err,
CvError::InvalidDimensions {
width: 0,
height: 100
}
));
let msg = format!("{err}");
assert!(msg.contains("0x100"));
}
#[test]
fn test_invalid_roi() {
let err = CvError::invalid_roi(10, 20, 0, 50);
assert!(matches!(
err,
CvError::InvalidRoi {
x: 10,
y: 20,
width: 0,
height: 50
}
));
}
#[test]
fn test_invalid_kernel_size() {
let err = CvError::invalid_kernel_size(4);
assert!(matches!(err, CvError::InvalidKernelSize { size: 4 }));
let msg = format!("{err}");
assert!(msg.contains('4'));
}
#[test]
fn test_color_conversion_error() {
let err = CvError::color_conversion("Invalid color");
assert!(matches!(err, CvError::ColorConversion { .. }));
let msg = format!("{err}");
assert!(msg.contains("Invalid color"));
}
#[test]
fn test_unsupported_format() {
let err = CvError::unsupported_format("YUV444");
assert!(format!("{err}").contains("YUV444"));
}
#[test]
fn test_detection_failed() {
let err = CvError::detection_failed("No faces");
assert!(format!("{err}").contains("No faces"));
}
#[test]
fn test_transform_error() {
let err = CvError::transform_error("Singular matrix");
assert!(format!("{err}").contains("Singular matrix"));
}
#[test]
fn test_insufficient_data() {
let err = CvError::insufficient_data(1024, 512);
let msg = format!("{err}");
assert!(msg.contains("1024"));
assert!(msg.contains("512"));
}
#[test]
fn test_matrix_error() {
let err = CvError::matrix_error("Dimension mismatch");
assert!(format!("{err}").contains("Dimension mismatch"));
}
#[test]
fn test_invalid_parameter() {
let err = CvError::invalid_parameter("sigma", "-1.0");
let msg = format!("{err}");
assert!(msg.contains("sigma"));
assert!(msg.contains("-1.0"));
}
}