qualia_vision/weights/
resolve_vision_asset.rs1use std::path::{Path, PathBuf};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum VisionAssetId {
8 Yunet,
9 Sface,
10 MediapipeFaceLandmarker,
11 YoloNasS,
12 EmotionsRetail,
13 MediapipePose,
14 MediapipeHands,
15}
16
17impl VisionAssetId {
18 pub const fn as_str(self) -> &'static str {
20 match self {
21 Self::Yunet => "yunet",
22 Self::Sface => "sface",
23 Self::MediapipeFaceLandmarker => "mediapipe_face_landmarker",
24 Self::YoloNasS => "yolo_nas_s",
25 Self::EmotionsRetail => "emotions_retail_0003",
26 Self::MediapipePose => "mediapipe_pose",
27 Self::MediapipeHands => "mediapipe_hands",
28 }
29 }
30
31 pub const fn licence_tag(self) -> AssetLicenceTag {
33 AssetLicenceTag::PermissiveReady
35 }
36
37 pub const fn licence_spdx(self) -> &'static str {
39 match self {
40 Self::Yunet => "MIT",
41 Self::Sface
42 | Self::MediapipeFaceLandmarker
43 | Self::YoloNasS
44 | Self::EmotionsRetail
45 | Self::MediapipePose
46 | Self::MediapipeHands => "Apache-2.0",
47 }
48 }
49
50 pub const fn rel_parts(self) -> (&'static str, &'static str) {
52 match self {
53 Self::Yunet => ("face/yunet", "face_detection_yunet_2023mar.onnx"),
54 Self::Sface => ("face/sface", "face_recognition_sface_2021dec.onnx"),
55 Self::MediapipeFaceLandmarker => ("face/mediapipe_landmarker", "face_landmarker.task"),
56 Self::YoloNasS => ("detect/yolo_nas", "yolo_nas_s.onnx"),
57 Self::EmotionsRetail => (
58 "affect/openvino_emotions_retail",
59 "emotions-recognition-retail-0003.onnx",
60 ),
61 Self::MediapipePose => ("pose/mediapipe_pose", "pose_landmarker_lite.task"),
62 Self::MediapipeHands => ("pose/mediapipe_hands", "hand_landmarker.task"),
63 }
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub enum AssetLicenceTag {
70 PermissiveReady,
72 LicenceHostile,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub struct ResolvedAsset {
78 pub id: VisionAssetId,
79 pub path: PathBuf,
80 pub licence_spdx: &'static str,
81 pub licence_tag: AssetLicenceTag,
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub enum VisionAssetError {
86 WeightAbsent,
88 NoRoots,
90}
91
92pub fn resolve_vision_asset(
96 id: VisionAssetId,
97 roots: &[&Path],
98) -> Result<ResolvedAsset, VisionAssetError> {
99 if roots.is_empty() {
100 return Err(VisionAssetError::NoRoots);
101 }
102 let (rel_dir, filename) = id.rel_parts();
103 for root in roots {
104 let candidate = root.join(rel_dir).join(filename);
105 if candidate.is_file() {
106 return Ok(ResolvedAsset {
107 id,
108 path: candidate,
109 licence_spdx: id.licence_spdx(),
110 licence_tag: id.licence_tag(),
111 });
112 }
113 let flat = root.join(filename);
115 if flat.is_file() {
116 return Ok(ResolvedAsset {
117 id,
118 path: flat,
119 licence_spdx: id.licence_spdx(),
120 licence_tag: id.licence_tag(),
121 });
122 }
123 }
124 Err(VisionAssetError::WeightAbsent)
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130 use std::fs;
131 use std::time::{SystemTime, UNIX_EPOCH};
132
133 #[test]
134 fn yunet_is_mit_permissive() {
135 assert_eq!(VisionAssetId::Yunet.licence_spdx(), "MIT");
136 assert_eq!(
137 VisionAssetId::Yunet.licence_tag(),
138 AssetLicenceTag::PermissiveReady
139 );
140 }
141
142 #[test]
143 fn sface_is_apache_permissive() {
144 assert_eq!(VisionAssetId::Sface.licence_spdx(), "Apache-2.0");
145 assert_eq!(
146 VisionAssetId::Sface.licence_tag(),
147 AssetLicenceTag::PermissiveReady
148 );
149 }
150
151 #[test]
152 fn absent_without_file() {
153 let tmp = std::env::temp_dir().join(format!(
154 "qv_asset_none_{}",
155 SystemTime::now()
156 .duration_since(UNIX_EPOCH)
157 .unwrap()
158 .as_nanos()
159 ));
160 let _ = fs::create_dir_all(&tmp);
161 let r = resolve_vision_asset(VisionAssetId::Yunet, &[tmp.as_path()]);
162 assert_eq!(r, Err(VisionAssetError::WeightAbsent));
163 let _ = fs::remove_dir_all(&tmp);
164 }
165
166 #[test]
167 fn finds_nested_layout() {
168 let tmp = std::env::temp_dir().join(format!(
169 "qv_asset_ok_{}",
170 SystemTime::now()
171 .duration_since(UNIX_EPOCH)
172 .unwrap()
173 .as_nanos()
174 ));
175 let (rel, name) = VisionAssetId::Yunet.rel_parts();
176 let dir = tmp.join(rel);
177 fs::create_dir_all(&dir).unwrap();
178 let path = dir.join(name);
179 fs::write(&path, b"fake-onnx-bytes-for-test").unwrap();
180 let r = resolve_vision_asset(VisionAssetId::Yunet, &[tmp.as_path()]).unwrap();
181 assert_eq!(r.path, path);
182 assert_eq!(r.licence_tag, AssetLicenceTag::PermissiveReady);
183 let _ = fs::remove_dir_all(&tmp);
184 }
185}