Skip to main content

qualia_vision/recipes/
challenge_pad_from_mesh_trace.rs

1//! Recipe: consent → MediaPipe/mesh landmark trace → pure-landmark challenge PAD.
2//!
3//! Orchestration only. No ONNX / MediaPipe runtime. Fail closed without consent.
4//! Delegates packing + evaluation to `evaluate_pad_from_mediapipe_trace`, or
5//! directly to `evaluate_landmark_pad` when frames are already packed.
6
7use crate::biosense::liveness::BlendRow;
8use crate::biosense::{
9    evaluate_landmark_pad, evaluate_pad_from_mediapipe_trace, BiosenseConsent,
10    CameraStreamAttestation, ChallengeKind, LandmarkBufferLayout, LandmarkFrame, PadResult,
11    PadThresholds, MAX_MEDIAPIPE_PAD_FRAMES,
12};
13use crate::cv::error::CvError;
14
15/// Challenge PAD from a MediaPipe-layout flat landmark trajectory.
16///
17/// Fail closed: denied consent yields `PadResult { passed: false, reason: NoConsent }`
18/// (via the evaluator). Empty/oversized traces return `CvError`.
19pub fn challenge_pad_from_mesh_trace(
20    consent: BiosenseConsent,
21    challenge: ChallengeKind,
22    layout: LandmarkBufferLayout,
23    frame_times_ms: &[u32],
24    flat_landmarks: &[&[f32]],
25    blends: &[BlendRow],
26    stream: CameraStreamAttestation,
27    thr: &PadThresholds,
28    image_size: Option<(f32, f32)>,
29) -> Result<PadResult, CvError> {
30    if !consent.may_process() {
31        // Explicit recipe-level fail-closed (evaluator also checks).
32        return Ok(PadResult {
33            passed: false,
34            reason: crate::biosense::PadReason::NoConsent,
35            challenge,
36        });
37    }
38    if flat_landmarks.is_empty() || frame_times_ms.is_empty() {
39        return Err(CvError::EmptyInput);
40    }
41    if flat_landmarks.len() > MAX_MEDIAPIPE_PAD_FRAMES {
42        return Err(CvError::BufferTooSmall);
43    }
44    evaluate_pad_from_mediapipe_trace(
45        consent,
46        challenge,
47        layout,
48        frame_times_ms,
49        flat_landmarks,
50        blends,
51        stream,
52        thr,
53        image_size,
54    )
55}
56
57/// Challenge PAD when landmarks are already packed into [`LandmarkFrame`] slots.
58pub fn challenge_pad_from_landmark_frames(
59    consent: BiosenseConsent,
60    challenge: ChallengeKind,
61    frames: &[LandmarkFrame],
62    blends: &[BlendRow],
63    stream: CameraStreamAttestation,
64    thr: &PadThresholds,
65) -> Result<PadResult, CvError> {
66    if !consent.may_process() {
67        return Ok(PadResult {
68            passed: false,
69            reason: crate::biosense::PadReason::NoConsent,
70            challenge,
71        });
72    }
73    evaluate_landmark_pad(consent, challenge, frames, blends, stream, thr)
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use crate::biosense::liveness::landmark_types::Landmark2;
80    use crate::biosense::liveness::profile_asymmetry_ratio::{
81        synthetic_3d_par_frame, synthetic_flat_par_frame,
82    };
83    use crate::biosense::{
84        landmarks_from_normalized, pack_landmark_frame, BiosensePurpose, PadLandmarkId, PadReason,
85        MEDIAPIPE_FACE_MESH_COUNT, PAD_LANDMARK_IDS,
86    };
87
88    fn security_consent() -> BiosenseConsent {
89        BiosenseConsent::grant_security_template(42)
90    }
91
92    fn frame_to_mp_xy(frame: &LandmarkFrame) -> Vec<f32> {
93        let mut buf = vec![0.0f32; MEDIAPIPE_FACE_MESH_COUNT * 2];
94        for id in PAD_LANDMARK_IDS {
95            if let Some(p) = frame.get(id) {
96                let i = id.mediapipe_index() as usize;
97                buf[i * 2] = p.x;
98                buf[i * 2 + 1] = p.y;
99            }
100        }
101        buf
102    }
103
104    fn yaw_mp_trace(live3d: bool) -> (Vec<u32>, Vec<Vec<f32>>) {
105        let mut times = Vec::new();
106        let mut flats = Vec::new();
107        for i in 0..12 {
108            let t = 40 + i as u32 * 80;
109            let yaw = i as f32 * 4.0;
110            let (mut f, _) = if live3d {
111                synthetic_3d_par_frame(yaw, t)
112            } else {
113                synthetic_flat_par_frame(yaw, t)
114            };
115            f.t_ms = t;
116            if live3d {
117                let j = ((i as f32) * 2.1).sin() * 0.45;
118                if let Some(n) = f.get(PadLandmarkId::NoseTip) {
119                    f.set(
120                        PadLandmarkId::NoseTip,
121                        Landmark2::new(n.x + j, n.y + j * 0.3),
122                    );
123                }
124            }
125            times.push(t);
126            flats.push(frame_to_mp_xy(&f));
127        }
128        (times, flats)
129    }
130
131    #[test]
132    fn no_consent_fails_closed() {
133        let (times, flats) = yaw_mp_trace(true);
134        let refs: Vec<&[f32]> = flats.iter().map(|v| v.as_slice()).collect();
135        let r = challenge_pad_from_mesh_trace(
136            BiosenseConsent::denied(BiosensePurpose::Security),
137            ChallengeKind::YawLeft,
138            LandmarkBufferLayout::XyInterleaved,
139            &times,
140            &refs,
141            &[],
142            CameraStreamAttestation::default(),
143            &PadThresholds::default(),
144            None,
145        )
146        .unwrap();
147        assert!(!r.passed);
148        assert_eq!(r.reason, PadReason::NoConsent);
149    }
150
151    #[test]
152    fn synthetic_flat_par_does_not_pass_rotation_pad() {
153        let (times, flats) = yaw_mp_trace(false);
154        let refs: Vec<&[f32]> = flats.iter().map(|v| v.as_slice()).collect();
155        let mut thr = PadThresholds::default();
156        thr.require_jitter = false;
157        thr.action.yaw_deg = 15.0;
158        let r = challenge_pad_from_mesh_trace(
159            security_consent(),
160            ChallengeKind::YawLeft,
161            LandmarkBufferLayout::XyInterleaved,
162            &times,
163            &refs,
164            &[],
165            CameraStreamAttestation::default(),
166            &thr,
167            None,
168        )
169        .unwrap();
170        assert!(!r.passed);
171        assert!(
172            matches!(
173                r.reason,
174                PadReason::FlatSurface | PadReason::WrongAction | PadReason::StaticMesh
175            ),
176            "got {:?}",
177            r.reason
178        );
179    }
180
181    #[test]
182    fn packed_landmark_path_respects_consent() {
183        let (f, _) = synthetic_3d_par_frame(10.0, 50);
184        let frames = [f; 8];
185        let blends = [None; 8];
186        let r = challenge_pad_from_landmark_frames(
187            BiosenseConsent::denied(BiosensePurpose::Security),
188            ChallengeKind::YawLeft,
189            &frames,
190            &blends,
191            CameraStreamAttestation::default(),
192            &PadThresholds::default(),
193        )
194        .unwrap();
195        assert!(!r.passed);
196        assert_eq!(r.reason, PadReason::NoConsent);
197    }
198
199    #[test]
200    fn empty_trace_errors() {
201        let r = challenge_pad_from_mesh_trace(
202            security_consent(),
203            ChallengeKind::Smile,
204            LandmarkBufferLayout::XyInterleaved,
205            &[],
206            &[],
207            &[],
208            CameraStreamAttestation::default(),
209            &PadThresholds::default(),
210            None,
211        );
212        assert!(matches!(r, Err(CvError::EmptyInput)));
213    }
214
215    #[test]
216    fn normalized_coords_scale_path_compiles_through_recipe() {
217        // Minimal non-empty synthetic: build one frontal pad frame in normalized
218        // space via pack + denorm (recipe image_size path). Full PAD still needs
219        // a multi-frame trajectory; this only checks the denorm branch accepts input.
220        let mut buf = vec![0.0f32; MEDIAPIPE_FACE_MESH_COUNT * 2];
221        let set = |buf: &mut [f32], id: PadLandmarkId, x: f32, y: f32| {
222            let i = id.mediapipe_index() as usize;
223            buf[i * 2] = x;
224            buf[i * 2 + 1] = y;
225        };
226        set(&mut buf, PadLandmarkId::NoseTip, 0.5, 0.5);
227        set(&mut buf, PadLandmarkId::Chin, 0.5, 0.8);
228        set(&mut buf, PadLandmarkId::LeftEyeOuter, 0.35, 0.4);
229        set(&mut buf, PadLandmarkId::RightEyeOuter, 0.65, 0.4);
230        set(&mut buf, PadLandmarkId::LeftCheek, 0.25, 0.55);
231        set(&mut buf, PadLandmarkId::RightCheek, 0.75, 0.55);
232        let packed = pack_landmark_frame(0, &buf, LandmarkBufferLayout::XyInterleaved).unwrap();
233        let px = landmarks_from_normalized(packed, 200.0, 100.0);
234        let n = px.get(PadLandmarkId::NoseTip).unwrap();
235        assert!((n.x - 100.0).abs() < 1e-3);
236        // Recipe with single frame → insufficient frames (not NoConsent).
237        let times = [0u32];
238        let refs: [&[f32]; 1] = [buf.as_slice()];
239        let r = challenge_pad_from_mesh_trace(
240            security_consent(),
241            ChallengeKind::Smile,
242            LandmarkBufferLayout::XyInterleaved,
243            &times,
244            &refs,
245            &[],
246            CameraStreamAttestation::default(),
247            &PadThresholds::default(),
248            Some((200.0, 100.0)),
249        )
250        .unwrap();
251        assert!(!r.passed);
252        assert_eq!(r.reason, PadReason::InsufficientFrames);
253    }
254}