edge_impulse_runner/backends/
mod.rs1use crate::error::EdgeImpulseError;
13use crate::inference::messages::InferenceResponse;
14use crate::types::ModelParameters;
15use std::path::PathBuf;
16
17#[derive(Debug, Clone)]
19pub enum BackendConfig {
20 Eim {
22 path: PathBuf,
24 socket_path: Option<PathBuf>,
26 },
27 Ffi {
29 debug: bool,
31 },
32}
33
34pub trait InferenceBackend: Send + Sync {
36 fn new(config: BackendConfig) -> Result<Self, EdgeImpulseError>
38 where
39 Self: Sized;
40
41 fn infer(
43 &mut self,
44 features: Vec<f32>,
45 debug: Option<bool>,
46 ) -> Result<InferenceResponse, EdgeImpulseError>;
47
48 fn parameters(&self) -> Result<&ModelParameters, EdgeImpulseError>;
50
51 fn sensor_type(&self) -> Result<crate::types::SensorType, EdgeImpulseError>;
53
54 fn input_size(&self) -> Result<usize, EdgeImpulseError>;
56
57 fn set_debug_callback(&mut self, callback: Box<dyn Fn(&str) + Send + Sync>);
59
60 fn normalize_visual_anomaly(
62 &self,
63 anomaly: f32,
64 max: f32,
65 mean: f32,
66 regions: &[(f32, u32, u32, u32, u32)],
67 ) -> crate::types::VisualAnomalyResult;
68}
69
70#[cfg(feature = "eim")]
71pub mod eim;
72
73#[cfg(feature = "ffi")]
74pub mod ffi;
75
76pub fn create_backend(
78 config: BackendConfig,
79) -> Result<Box<dyn InferenceBackend>, EdgeImpulseError> {
80 match config {
81 #[cfg(feature = "eim")]
82 BackendConfig::Eim { path, socket_path } => {
83 if let Some(ext) = path.extension() {
85 if ext != "eim" {
86 return Err(EdgeImpulseError::InvalidPath);
87 }
88 } else {
89 return Err(EdgeImpulseError::InvalidPath);
90 }
91
92 use eim::EimBackend;
93 Ok(Box::new(EimBackend::new(BackendConfig::Eim {
94 path,
95 socket_path,
96 })?))
97 }
98 #[cfg(feature = "ffi")]
99 BackendConfig::Ffi { debug } => {
100 use ffi::FfiBackend;
101 Ok(Box::new(FfiBackend::new(BackendConfig::Ffi { debug })?))
102 }
103 #[cfg(not(feature = "eim"))]
104 BackendConfig::Eim { .. } => Err(EdgeImpulseError::InvalidOperation(
105 "EIM backend not enabled. Enable the 'eim' feature.".to_string(),
106 )),
107 #[cfg(not(feature = "ffi"))]
108 BackendConfig::Ffi { .. } => Err(EdgeImpulseError::InvalidOperation(
109 "FFI backend not enabled. Enable the 'ffi' feature.".to_string(),
110 )),
111 }
112}