Skip to main content

apple_vision/rectangles/
mod.rs

1//! Rectangle + document-segmentation detection.
2//!
3//! `VNDetectRectanglesRequest` finds quadrilaterals (signs, photos,
4//! book pages); `VNDetectDocumentSegmentationRequest` finds full
5//! document boundaries — both return the same observation shape.
6
7use core::ffi::c_char;
8use core::ptr;
9use std::ffi::CString;
10use std::path::Path;
11
12use crate::error::{from_swift, VisionError};
13use crate::face_landmarks::LandmarkPoint;
14use crate::ffi;
15use crate::recognize_text::BoundingBox;
16
17/// A detected quadrilateral with axis-aligned bounding box plus
18/// individual corner points (in normalised image coordinates,
19/// bottom-left origin).
20#[derive(Debug, Clone, PartialEq)]
21pub struct RectangleObservation {
22    pub bounding_box: BoundingBox,
23    pub confidence: f32,
24    pub top_left: LandmarkPoint,
25    pub top_right: LandmarkPoint,
26    pub bottom_left: LandmarkPoint,
27    pub bottom_right: LandmarkPoint,
28}
29
30/// Optional tuning for `detect_rectangles_in_path`. Pass `default()`
31/// to use Apple's defaults.
32#[derive(Debug, Clone, Copy, Default)]
33pub struct RectangleOptions {
34    /// `0` ⇒ Apple default.
35    pub max_observations: usize,
36    /// `0` ⇒ Apple default.
37    pub minimum_aspect_ratio: f32,
38    /// `0` ⇒ Apple default.
39    pub maximum_aspect_ratio: f32,
40    /// `0` ⇒ Apple default (normalised size of smallest rectangle).
41    pub minimum_size: f32,
42    /// `0` ⇒ Apple default.
43    pub minimum_confidence: f32,
44}
45
46/// Detect rectangles in the image at `path`.
47///
48/// # Errors
49///
50/// Returns [`VisionError::ImageLoadFailed`] / [`VisionError::RequestFailed`].
51pub fn detect_rectangles_in_path(
52    path: impl AsRef<Path>,
53    options: RectangleOptions,
54) -> Result<Vec<RectangleObservation>, VisionError> {
55    let path_str = path
56        .as_ref()
57        .to_str()
58        .ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
59    let path_c = CString::new(path_str)
60        .map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
61
62    let mut out_array: *mut core::ffi::c_void = ptr::null_mut();
63    let mut out_count: usize = 0;
64    let mut err_msg: *mut c_char = ptr::null_mut();
65
66    let status = unsafe {
67        ffi::vn_detect_rectangles_in_path(
68            path_c.as_ptr(),
69            options.max_observations,
70            options.minimum_aspect_ratio,
71            options.maximum_aspect_ratio,
72            options.minimum_size,
73            options.minimum_confidence,
74            &mut out_array,
75            &mut out_count,
76            &mut err_msg,
77        )
78    };
79    if status != ffi::status::OK {
80        return Err(unsafe { from_swift(status, err_msg) });
81    }
82    Ok(unsafe { collect_rects(out_array, out_count) })
83}
84
85/// Detect a full document's boundary in the image at `path`. Returns
86/// at most one rectangle (the document outline).
87///
88/// # Errors
89///
90/// Returns [`VisionError::ImageLoadFailed`] / [`VisionError::RequestFailed`].
91pub fn detect_document_segmentation_in_path(
92    path: impl AsRef<Path>,
93) -> Result<Vec<RectangleObservation>, VisionError> {
94    let path_str = path
95        .as_ref()
96        .to_str()
97        .ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
98    let path_c = CString::new(path_str)
99        .map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
100
101    let mut out_array: *mut core::ffi::c_void = ptr::null_mut();
102    let mut out_count: usize = 0;
103    let mut err_msg: *mut c_char = ptr::null_mut();
104
105    let status = unsafe {
106        ffi::vn_detect_document_segmentation_in_path(
107            path_c.as_ptr(),
108            &mut out_array,
109            &mut out_count,
110            &mut err_msg,
111        )
112    };
113    if status != ffi::status::OK {
114        return Err(unsafe { from_swift(status, err_msg) });
115    }
116    Ok(unsafe { collect_rects(out_array, out_count) })
117}
118
119unsafe fn collect_rects(
120    out_array: *mut core::ffi::c_void,
121    out_count: usize,
122) -> Vec<RectangleObservation> {
123    if out_array.is_null() || out_count == 0 {
124        return Vec::new();
125    }
126    let typed = out_array.cast::<ffi::RectangleObservationRaw>();
127    let mut v = Vec::with_capacity(out_count);
128    for i in 0..out_count {
129        let r = unsafe { &*typed.add(i) };
130        v.push(RectangleObservation {
131            bounding_box: BoundingBox {
132                x: r.bbox_x,
133                y: r.bbox_y,
134                width: r.bbox_w,
135                height: r.bbox_h,
136            },
137            confidence: r.confidence,
138            top_left: LandmarkPoint {
139                x: r.tl_x,
140                y: r.tl_y,
141            },
142            top_right: LandmarkPoint {
143                x: r.tr_x,
144                y: r.tr_y,
145            },
146            bottom_left: LandmarkPoint {
147                x: r.bl_x,
148                y: r.bl_y,
149            },
150            bottom_right: LandmarkPoint {
151                x: r.br_x,
152                y: r.br_y,
153            },
154        });
155    }
156    unsafe { ffi::vn_rectangle_observations_free(out_array, out_count) };
157    v
158}