Skip to main content

apple_vision/classify/
mod.rs

1//! General-purpose image classification (`VNClassifyImageRequest`).
2
3use core::ffi::c_char;
4use core::ptr;
5use std::ffi::CString;
6use std::path::Path;
7
8use crate::error::{from_swift, VisionError};
9use crate::ffi;
10
11/// One classification — an identifier (e.g. `"animal"`, `"food"`)
12/// plus a confidence score in `0.0..=1.0`.
13#[derive(Debug, Clone, PartialEq)]
14#[allow(clippy::derive_partial_eq_without_eq)]
15pub struct Classification {
16    pub identifier: String,
17    pub confidence: f32,
18}
19
20/// Run Apple's pre-trained image classifier (1000+ generic concepts).
21///
22/// # Errors
23///
24/// Returns [`VisionError::ImageLoadFailed`] / [`VisionError::RequestFailed`].
25pub fn classify_image_in_path(path: impl AsRef<Path>) -> Result<Vec<Classification>, VisionError> {
26    let path_str = path
27        .as_ref()
28        .to_str()
29        .ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
30    let path_c = CString::new(path_str)
31        .map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
32
33    let mut out_array: *mut core::ffi::c_void = ptr::null_mut();
34    let mut out_count: usize = 0;
35    let mut err_msg: *mut c_char = ptr::null_mut();
36
37    // SAFETY: all pointer arguments are valid stack locations or null-initialised out-params; strings are valid C strings for the duration of the call.
38    let status = unsafe {
39        ffi::vn_classify_image_in_path(
40            path_c.as_ptr(),
41            &mut out_array,
42            &mut out_count,
43            &mut err_msg,
44        )
45    };
46    if status != ffi::status::OK {
47        // SAFETY: the error pointer is either null or a bridge-allocated C string; `from_swift` frees it.
48        return Err(unsafe { from_swift(status, err_msg) });
49    }
50    if out_array.is_null() || out_count == 0 {
51        return Ok(Vec::new());
52    }
53    let typed = out_array.cast::<ffi::ClassificationRaw>();
54    let mut v = Vec::with_capacity(out_count);
55    for i in 0..out_count {
56        // SAFETY: the pointer is valid for the reported element count; the index is in bounds.
57        let raw = unsafe { &*typed.add(i) };
58        let id = if raw.identifier.is_null() {
59            String::new()
60        } else {
61            // SAFETY: the C string pointer is non-null (checked above) and valid for the duration of this borrow.
62            unsafe { core::ffi::CStr::from_ptr(raw.identifier) }
63                .to_string_lossy()
64                .into_owned()
65        };
66        v.push(Classification {
67            identifier: id,
68            confidence: raw.confidence,
69        });
70    }
71    // SAFETY: the pointer/count pair was allocated by the bridge and is freed exactly once here.
72    unsafe { ffi::vn_classifications_free(out_array, out_count) };
73    Ok(v)
74}