1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use core::panic;
use std::fmt::Display;

use serde::{Serialize, Serializer};
use strum_macros::{Display, IntoStaticStr};
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::js_sys::{Object, Reflect};

#[derive(Display)]
pub enum PoseNetArchitecture {
    ResNet50,
    MobileNetV1,
}

#[repr(i32)]
pub enum PoseNetOutputStride {
    Is32 = 32,
    Is16 = 16,
    Is8 = 8,
}

pub enum MobileNetMultiplier {
    Is1,
    Is0Point5,
    Is0Point75,
}
impl Into<f64> for MobileNetMultiplier {
    fn into(self) -> f64 {
        match self {
            MobileNetMultiplier::Is1 => 1.0,
            MobileNetMultiplier::Is0Point5 => 0.5,
            MobileNetMultiplier::Is0Point75 => 0.75,
        }
    }
}

#[repr(i32)]
pub enum QuantBytes {
    Is1 = 1,
    Is2 = 2,
    Is4 = 4,
}

pub struct InputResolution {
    pub width: i32,
    pub height: i32,
}

pub struct PoseNetModelConfig {
    pub architecture: PoseNetArchitecture,
    pub output_stride: PoseNetOutputStride,
    pub input_resolution: InputResolution,
    pub multiplier: Option<MobileNetMultiplier>,
    pub model_url: Option<String>,
    pub quant_bytes: Option<QuantBytes>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BlazePoseMediaPipeModelConfig {
    pub solution_path: Option<String>,
}
impl Into<JsValue> for BlazePoseMediaPipeModelConfig {
    fn into(self) -> JsValue {
        serde_wasm_bindgen::to_value(&self).unwrap()
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BlazePoseTfjsModelConfig {
    // TODO: Also allow io.IOHandler
    pub detector_model_url: Option<String>,
    // TODO: Also allow io.IOHandler
    pub landmark_model_url: Option<String>,
}
impl Into<JsValue> for BlazePoseTfjsModelConfig {
    fn into(self) -> JsValue {
        serde_wasm_bindgen::to_value(&self).unwrap()
    }
}

pub enum Runtime {
    Mediapipe(BlazePoseMediaPipeModelConfig),
    Tfjs(BlazePoseTfjsModelConfig),
}
impl Display for Runtime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let string = match &self {
            Self::Mediapipe(_) => "mediapipe",
            Self::Tfjs(_) => "tfjs",
        };
        write!(f, "{string}")
    }
}
impl Into<JsValue> for Runtime {
    fn into(self) -> JsValue {
        let runtime = self.to_string();
        let o: JsValue = match self {
            Self::Mediapipe(c) => c.into(),
            Self::Tfjs(c) => c.into(),
        };
        Reflect::set(&o, &"runtime".into(), &runtime.into()).unwrap();
        o
    }
}

pub enum BlazePoseModelType {
    Lite,
    Full,
    Heavy,
}
impl Display for BlazePoseModelType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let string = match &self {
            Self::Lite => "lite",
            Self::Full => "full",
            Self::Heavy => "heavy",
        };
        write!(f, "{string}")
    }
}
impl Serialize for BlazePoseModelType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string()[..])
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BlazePoseModelConfig {
    #[serde(skip_serializing)]
    pub runtime: Runtime,
    pub enable_smoothing: Option<bool>,
    pub enable_segmentation: Option<bool>,
    pub smooth_segmentation: Option<bool>,
    pub model_type: Option<BlazePoseModelType>,
}

impl Into<JsValue> for BlazePoseModelConfig {
    fn into(self) -> JsValue {
        let config = Object::from(serde_wasm_bindgen::to_value(&self).unwrap());
        Object::assign(
            &config,
            &Object::from({
                let runtime_config: JsValue = self.runtime.into();
                runtime_config
            }),
        );
        config.into()
    }
}

pub enum TrackerType {
    Keypoint,
    BoundingBox,
}
impl Display for TrackerType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let string = match &self {
            Self::Keypoint => "keypoint",
            Self::BoundingBox => "boundingBox",
        };
        write!(f, "{string}")
    }
}
impl Into<JsValue> for TrackerType {
    fn into(self) -> JsValue {
        self.to_string().into()
    }
}
impl Serialize for TrackerType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string()[..])
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct KeypointTrackerConfig {
    pub keypoint_confidence_threshold: i32,
    pub keypoint_falloff: Vec<i32>,
    pub min_number_of_keypoints: i32,
}

#[derive(Serialize)]
pub struct BoundingBoxTrackerConfig;

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TrackerConfig {
    pub max_tracks: i32,
    pub max_age: i32,
    pub min_similarity: i32,
    pub keypoint_tracker_params: Option<KeypointTrackerConfig>,
    pub bounding_box_tracker_params: Option<BoundingBoxTrackerConfig>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MoveNetModelConfig {
    pub enable_smoothing: Option<bool>,
    pub model_type: Option<String>,
    // TODO: Also allow io.IOHandler
    pub model_url: Option<String>,
    pub min_pose_score: Option<f64>,
    pub multi_pose_max_dimension: Option<i32>,
    pub enable_tracking: Option<bool>,
    pub tracker_type: Option<TrackerType>,
    pub tracker_config: Option<TrackerConfig>,
}
impl Into<JsValue> for MoveNetModelConfig {
    fn into(self) -> JsValue {
        serde_wasm_bindgen::to_value(&self).unwrap()
    }
}

#[derive(IntoStaticStr, PartialEq, Eq, Hash)]
pub enum Model {
    PoseNet,
    BlazePose,
    MoveNet,
}
impl Into<JsValue> for Model {
    fn into(self) -> JsValue {
        let value: &'static str = self.into();
        value.into()
    }
}

pub enum ModelWithConfig {
    PoseNet(Option<PoseNetModelConfig>),
    BlazePose(Option<BlazePoseModelConfig>),
    MoveNet(Option<MoveNetModelConfig>),
}

impl ModelWithConfig {
    pub fn get_name(&self) -> &'static str {
        match self {
            Self::PoseNet(_) => "PoseNet",
            Self::BlazePose(_) => "BlazePose",
            Self::MoveNet(_) => "MoveNet",
        }
    }

    pub fn get_config(self) -> JsValue {
        match self {
            Self::BlazePose(blaze_pose_model_config) => blaze_pose_model_config
                .map(|config| config.into())
                .unwrap_or(JsValue::UNDEFINED),
            Self::MoveNet(move_net_model_config) => move_net_model_config
                .map(|config| config.into())
                .unwrap_or(JsValue::UNDEFINED),
            _ => panic!("Not implemented. Make an issue :)"),
        }
    }
}