EimModel

Struct EimModel 

Source
pub struct EimModel { /* private fields */ }
Expand description

Edge Impulse Model Runner for Rust

This module provides functionality for running Edge Impulse machine learning models on Linux systems. It handles model lifecycle management, communication, and inference operations.

§Key Components

  • EimModel: Main struct for managing Edge Impulse models
  • SensorType: Enum representing supported sensor input types
  • ContinuousState: Internal state management for continuous inference mode
  • MovingAverageFilter: Smoothing filter for continuous inference results

§Features

  • Model process management and Unix socket communication
  • Support for both single-shot and continuous inference modes
  • Debug logging and callback system
  • Moving average filtering for continuous mode results
  • Automatic retry mechanisms for socket connections
  • Visual anomaly detection (FOMO AD) support with normalized scores

§Example Usage

use edge_impulse_runner::{EimModel, InferenceResult};

// Create a new model instance
let mut model = EimModel::new("path/to/model.eim").unwrap();

// Run inference with some features
let features = vec![0.1, 0.2, 0.3];
let result = model.infer(features, None).unwrap();

// For visual anomaly detection models, normalize the results
if let InferenceResult::VisualAnomaly { anomaly, visual_anomaly_max, visual_anomaly_mean, visual_anomaly_grid } = result.result {
    let (normalized_anomaly, normalized_max, normalized_mean, normalized_regions) =
        model.normalize_visual_anomaly(
            anomaly,
            visual_anomaly_max,
            visual_anomaly_mean,
            &visual_anomaly_grid.iter()
                .map(|bbox| (bbox.value, bbox.x as u32, bbox.y as u32, bbox.width as u32, bbox.height as u32))
                .collect::<Vec<_>>()
        );
    println!("Anomaly score: {:.2}%", normalized_anomaly * 100.0);
}

§Communication Protocol

The model communicates with the Edge Impulse process using JSON messages over Unix sockets:

  1. Hello message for initialization
  2. Model info response
  3. Classification requests
  4. Inference responses

§Error Handling

The module uses a custom EimError type for error handling, covering:

  • Invalid file paths
  • Socket communication errors
  • Model execution errors
  • JSON serialization/deserialization errors

§Visual Anomaly Detection

For visual anomaly detection models (FOMO AD):

  • Scores are normalized relative to the model’s minimum anomaly threshold
  • Results include overall anomaly score, maximum score, mean score, and anomalous regions
  • Region coordinates are provided in the original image dimensions
  • All scores are clamped to [0,1] range and displayed as percentages
  • Debug mode provides detailed information about thresholds and regions

§Threshold Configuration

Models can be configured with different thresholds:

  • Anomaly detection: min_anomaly_score threshold for visual anomaly detection
  • Object detection: min_score threshold for object confidence
  • Object tracking: keep_grace, max_observations, and threshold parameters

Thresholds can be updated at runtime using set_learn_block_threshold.

Implementations§

Source§

impl EimModel

Source

pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, EimError>

Creates a new EimModel instance from a path to the .eim file.

This is the standard way to create a new model instance. The function will:

  1. Validate the file extension
  2. Spawn the model process
  3. Establish socket communication
  4. Initialize the model
§Arguments
  • path - Path to the .eim file. Must be a valid Edge Impulse model file.
§Returns

Returns Result<EimModel, EimError> where:

  • Ok(EimModel) - Successfully created and initialized model
  • Err(EimError) - Failed to create model (invalid path, process spawn failure, etc.)
§Examples
use edge_impulse_runner::EimModel;

let model = EimModel::new("path/to/model.eim").unwrap();
Source

pub fn new_with_socket<P: AsRef<Path>, S: AsRef<Path>>( path: P, socket_path: S, ) -> Result<Self, EimError>

Creates a new EimModel instance with a specific Unix socket path.

Similar to new(), but allows specifying the socket path for communication. This is useful when you need control over the socket location or when running multiple models simultaneously.

§Arguments
  • path - Path to the .eim file
  • socket_path - Custom path where the Unix socket should be created
Source

pub fn new_with_debug<P: AsRef<Path>>( path: P, debug: bool, ) -> Result<Self, EimError>

Create a new EimModel instance with debug output enabled

Source

pub fn new_with_socket_and_debug<P: AsRef<Path>, S: AsRef<Path>>( path: P, socket_path: S, debug: bool, ) -> Result<Self, EimError>

Create a new EimModel instance with debug output enabled and a specific socket path

Source

pub fn set_debug_callback<F>(&mut self, callback: F)
where F: Fn(&str) + Send + Sync + 'static,

Set a debug callback function to receive debug messages

When debug mode is enabled, this callback will be invoked with debug messages from the model runner. This is useful for logging or displaying debug information in your application.

§Arguments
  • callback - Function that takes a string slice and handles the debug message
Source

pub fn path(&self) -> &Path

Get the path to the EIM file

Source

pub fn socket_path(&self) -> &Path

Get the socket path used for communication

Source

pub fn sensor_type(&self) -> Result<SensorType, EimError>

Get the sensor type for this model

Source

pub fn parameters(&self) -> Result<&ModelParameters, EimError>

Get the model parameters

Source

pub fn infer( &mut self, features: Vec<f32>, debug: Option<bool>, ) -> Result<InferenceResponse, EimError>

Run inference on the input features

This method automatically handles both continuous and non-continuous modes:

§Non-Continuous Mode
  • Each call is independent
  • All features must be provided in a single call
  • Results are returned immediately
§Continuous Mode (automatically enabled for supported models)
  • Features are accumulated across calls
  • Internal buffer maintains sliding window of features
  • Moving average filter smooths results
  • Initial calls may return empty results while buffer fills
§Arguments
  • features - Vector of input features
  • debug - Optional debug flag to enable detailed output for this inference
§Returns

Returns Result<InferenceResponse, EimError> containing inference results

Source

pub fn input_size(&self) -> Result<usize, EimError>

Get the required number of input features for this model

Returns the number of features expected by the model for each classification. This is useful for:

  • Validating input size before classification
  • Preparing the correct amount of data
  • Padding or truncating inputs to match model requirements
§Returns

The number of input features required by the model

Source

pub async fn set_learn_block_threshold( &mut self, threshold: ThresholdConfig, ) -> Result<(), EimError>

Set a threshold for a specific learning block

This method allows updating thresholds for different types of blocks:

  • Anomaly detection (GMM)
  • Object detection
  • Object tracking
§Arguments
  • threshold - The threshold configuration to set
§Returns

Returns Result<(), EimError> indicating success or failure

Source

pub fn normalize_visual_anomaly( &self, anomaly: f32, max: f32, mean: f32, regions: &[(f32, u32, u32, u32, u32)], ) -> VisualAnomalyResult

Normalize a visual anomaly result

Trait Implementations§

Source§

impl Debug for EimModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more