apple_vision/classify/
mod.rs1use 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#[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
20pub 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 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 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 let raw = unsafe { &*typed.add(i) };
58 let id = if raw.identifier.is_null() {
59 String::new()
60 } else {
61 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 unsafe { ffi::vn_classifications_free(out_array, out_count) };
73 Ok(v)
74}