edge_impulse_runner/inference/
model.rs1use crate::backends::{BackendConfig, InferenceBackend, create_backend};
2use crate::error::EdgeImpulseError;
3use crate::inference::messages::InferenceResponse;
4use crate::types::{ModelParameters, SensorType, VisualAnomalyResult};
5use std::path::Path;
6
7pub struct EdgeImpulseModel {
29 backend: Box<dyn InferenceBackend>,
30}
31
32impl EdgeImpulseModel {
33 pub fn new<P: AsRef<Path>>(model_path: P) -> Result<Self, EdgeImpulseError> {
35 let config = BackendConfig::Eim {
36 path: model_path.as_ref().to_path_buf(),
37 socket_path: None,
38 };
39 let backend = create_backend(config)?;
40 Ok(Self { backend })
41 }
42
43 pub fn new_with_socket<P: AsRef<Path>>(
45 model_path: P,
46 socket_path: P,
47 ) -> Result<Self, EdgeImpulseError> {
48 let config = BackendConfig::Eim {
49 path: model_path.as_ref().to_path_buf(),
50 socket_path: Some(socket_path.as_ref().to_path_buf()),
51 };
52 let backend = create_backend(config)?;
53 Ok(Self { backend })
54 }
55
56 pub fn new_with_debug<P: AsRef<Path>>(
58 model_path: P,
59 debug: bool,
60 ) -> Result<Self, EdgeImpulseError> {
61 let config = BackendConfig::Eim {
62 path: model_path.as_ref().to_path_buf(),
63 socket_path: None,
64 };
65 let mut backend = create_backend(config)?;
66 if debug {
67 backend.set_debug_callback(Box::new(|msg| println!("[DEBUG] {msg}")));
68 }
69 Ok(Self { backend })
70 }
71
72 pub fn new_ffi(debug: bool) -> Result<Self, EdgeImpulseError> {
74 let config = BackendConfig::Ffi { debug };
75 let mut backend = create_backend(config)?;
76 if debug {
77 backend.set_debug_callback(Box::new(|msg| println!("[DEBUG] {msg}")));
78 }
79 Ok(Self { backend })
80 }
81
82 pub fn infer(
84 &mut self,
85 features: Vec<f32>,
86 debug: Option<bool>,
87 ) -> Result<InferenceResponse, EdgeImpulseError> {
88 self.backend.infer(features, debug)
89 }
90
91 pub fn parameters(&self) -> Result<&ModelParameters, EdgeImpulseError> {
93 self.backend.parameters()
94 }
95
96 pub fn sensor_type(&self) -> Result<SensorType, EdgeImpulseError> {
98 self.backend.sensor_type()
99 }
100
101 pub fn input_size(&self) -> Result<usize, EdgeImpulseError> {
103 self.backend.input_size()
104 }
105
106 pub fn normalize_visual_anomaly(
108 &self,
109 anomaly: f32,
110 max: f32,
111 mean: f32,
112 regions: &[(f32, u32, u32, u32, u32)],
113 ) -> VisualAnomalyResult {
114 self.backend
115 .normalize_visual_anomaly(anomaly, max, mean, regions)
116 }
117}
118
119impl std::fmt::Debug for EdgeImpulseModel {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 f.debug_struct("EdgeImpulseModel")
122 .field("backend", &"<backend>")
123 .finish()
124 }
125}