apple_vision/objectness_saliency/
mod.rs1#![allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
2#![allow(clippy::too_long_first_doc_paragraph)]
3use std::ffi::{CStr, CString};
7use std::path::Path;
8use std::ptr;
9
10use crate::error::VisionError;
11use crate::ffi;
12
13#[derive(Debug, Clone, Copy, PartialEq)]
15pub struct ObjectnessRegion {
16 pub x: f64,
17 pub y: f64,
18 pub w: f64,
19 pub h: f64,
20 pub confidence: f32,
21}
22
23pub fn objectness_saliency(path: impl AsRef<Path>) -> Result<Vec<ObjectnessRegion>, VisionError> {
30 let path_str = path.as_ref().to_str().ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
31 let cpath = CString::new(path_str).map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
32 let mut rects_ptr: *mut ffi::SimpleRectRaw = ptr::null_mut();
33 let mut count: isize = 0;
34 let mut err: *mut std::ffi::c_char = ptr::null_mut();
35 let status = unsafe {
36 ffi::vn_objectness_saliency_in_path(
37 cpath.as_ptr(),
38 &mut rects_ptr,
39 &mut count,
40 &mut err,
41 )
42 };
43 if status != ffi::status::OK {
44 let msg = unsafe { take_err(err) };
45 return Err(VisionError::RequestFailed(msg));
46 }
47 let mut out = Vec::with_capacity(count.max(0) as usize);
48 for i in 0..count {
49 let r = unsafe { rects_ptr.offset(i).read() };
50 out.push(ObjectnessRegion {
51 x: r.x,
52 y: r.y,
53 w: r.w,
54 h: r.h,
55 confidence: r.confidence,
56 });
57 }
58 if !rects_ptr.is_null() {
59 unsafe { ffi::vn_simple_rects_free(rects_ptr, count) };
60 }
61 Ok(out)
62}
63
64unsafe fn take_err(p: *mut std::ffi::c_char) -> String {
65 if p.is_null() {
66 return String::new();
67 }
68 let s = unsafe { CStr::from_ptr(p) }.to_string_lossy().into_owned();
69 unsafe { libc::free(p.cast()) };
70 s
71}