All pages
Powered by GitBook
1 of 8

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Serial daemon

The serial daemon is used to connect fully-supported devices to Edge Impulse so that data from their on-board sensors can be uploaded directly into Edge Impulse Studio. This is particularly helpful for devices without an IP connection, for which the serial daemon acts as a data upload proxy. You can also use the serial daemon to configure the upload parameters.

Getting Started

To use the daemon, connect a fully-supported development board to your computer and run:

$ edge-impulse-daemon

The daemon will prompt you to log in, and then configure the device. If your device does not have the right firmware yet, it will also prompt you to upgrade it.

This is an example of the output of the daemon:

Edge Impulse serial daemon v1.1.0
? What is your user name or e-mail address? jan@edgeimpulse.com
? What is your password? [hidden]
Endpoints:
    Websocket: wss://remote-mgmt.edgeimpulse.com
    API:       https://studio.edgeimpulse.com
    Ingestion: https://ingestion.edgeimpulse.com

[SER] Connecting to /dev/tty.usbmodem401203
[SER] Serial is connected, trying to read config...
[SER] Retrieved configuration
? To which project do you want to add this device? accelerometer-demo-1
Configuring API key in device... OK
Configuring HMAC key in device... OK
? What name do you want to give this device? Jan's DISCO-L475VG
Setting upload host in device... OK
Configuring remote management settings... OK
? WiFi is not connected, do you want to set up a WiFi network now? Yes
Scanning WiFi networks... OK
? Select WiFi network SSID: edgeimpulse-office, Security: WPA2 (3), RSSI: -60 dBm
? Enter password for network "edgeimpulse-office" 0624710192
Connecting to "edgeimpulse-office"... OK
[SER] Device is connected over WiFi to remote management API, no need to run the daemon. Exiting...

Note: Your credentials are never stored. When you log in, the serial daemon exchanges your credentials for a session token, which is used to further authenticate requests.

Switching projects

You can use one device for many projects. To switch projects run:

$ edge-impulse-daemon --clean

And select the new project. The device will remain listed in the old project, and if you switch back will retain the same name and last seen date.

Running --clean resets both the daemon configuration and the on-device configuration. If you run into issues, you can connect to the device using a serial console (with a baud rate of 115,200) and send the AT+CLEARCONFIG command to the device, to remove its configuration.

Command Options

Serial daemon options can be invoked as follows:

$ edge-impulse-daemon [options]

API authentication --api-key

Enables authentication using a project API key. API keys are long strings of random characters that start with ei_ and can be obtained from the project's dashboard on Edge Impulse Studio. Example:

$ edge-impulse-daemon --api-key ei_XXXXXXXXXXXXX

Baud Rate --baud-rate

Change the rate of the communication between the device and Edge Impulse Studio. Default is 115,200 baud. Example:

$ edge-impulse-daemon --baud-rate 9600

Clear Configuration --clean

Clears (resets) the daemon and device configurations.

Silent mode --silent

Skip all wizards (except for the login prompt). This is useful in headless environments where the session token has already been obtained, or authentication is requested via the --api-key option.

Verbose mode --verbose

Print additional information during execution. Useful for debugging.

Version --version

Prints the version of the Edge Impulse CLI (and therefore, the serial daemon) installed.

Troubleshooting

Unable to set up WiFi with ST B-L475E-IOT01A development board

? WiFi is not connected, do you want to set up a WiFi network now? Yes
Scanning WiFi networks...Error while setting up device Timeout

Edge Impulse CLI

This Edge Impulse CLI is used to control local devices, act as a proxy to synchronise data for devices that don't have an internet connection, and to upload and convert local files. The CLI consists of several tools:

Did you know you can also connect devices directly to your browser?

Installation

This Edge Impulse CLI is used to control local devices, act as a proxy to synchronise data for devices that don't have an internet connection, and to upload and convert local files. The CLI consists of seven tools:

Installation - Windows

It is highly recommended to use the Command Prompt (CMD) vs Powershell when installing and using the Edge Impulse CLI on Windows.

During installation you may get an error about needing Visual Studio Build Tools. This is needed for some parts of the Edge Impulse CLI. Please follow the error links to get the correct version from Microsoft.

    • For Windows users, install the Additional Node.js tools (called Tools for Native Modules on newer versions) when prompted.

  1. Install the CLI tools via:

    npm install -g edge-impulse-cli --force

You should now have the tools available in your PATH.

Installation - Windows Subsystem for Linux (WSL)

To install WSL, open PowerShell as an Administrator and run:

wsl --install

Once complete you can then enable WSL by running the following command in PowerShell as an Administrator, and follow the rest of the Linux instructions.:

wsl

Installation - Linux, Ubuntu, MacOS, and Raspbian OS

Important: Edge Impulse requires Node.js version 20.x or later. Using older versions may lead to installation issues or runtime errors. Please ensure you have the correct version installed before proceeding with the setup.

  1. Alternatively, run the following commands:

    curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash -
    sudo apt-get install -y nodejs
    node -v

    The last command should return the node version, v20 or above.

    Let's verify the node installation directory:

    npm config get prefix

    If it returns /usr/local/, run the following commands to change npm's default directory:

    mkdir ~/.npm-global
    npm config set prefix '~/.npm-global'
    echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile

    On MacOS you might be using zsh as default, so you will want to update the correct profile

    mkdir ~/.npm-global
    npm config set prefix '~/.npm-global'
    echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zprofile
  2. Install the CLI tools via:

    npm install -g edge-impulse-cli

You should now have the tools available in your PATH.

Troubleshooting

Error: Could not locate the bindings file. (Windows)

This error indicates an issue occurred when installing the edge-impulse-cli for the first time or you have not selected to install the addition tools when installing NodeJS (not selected by default).

Remove NodeJS and install it again selecting the option:

Re-install the CLI via

npm uninstall -g edge-impulse-cli
npm install -g edge-impulse-cli

Tools version "2.0" is unrecognized (Windows)

If you receive the following error: The tools version "2.0" is unrecognized. Available tools versions are "4.0", launch a new command window as administrator and run:

npm install --global --production windows-build-tools
npm config set msvs_version 2015 --global

EACCES: permission denied, access '/usr/local/lib/node_modules' (macOS)

This is indication that the node_modules is not owned by you, but rather by root. This is probably not what you want. To fix this, run:

sudo chown -R $USER /usr/local/lib/node_modules

EACCES user "nobody" does not have permission to access the dev dir (Linux)

Try to set the npm user to root and re-run the installation command. You can do this via:

npm config set user root

Error: Can’t find Python executable (Windows)

If you receive an error such as:

gyp ERR! stack Error: Can’t find Python executable “C:\Users\vale.windows-build-tools\python27\python.exe”, you can set the PYTHON env variable.

You're running an older version of node-gyp (a way to build binary packages). Upgrade via:

npm install node-gyp@latest -g

The module XXX was compiled against a different Node.js version

This error occurs when you have upgraded Node.js since installing the Edge Impulse CLI. Re-install the CLI via:

npm uninstall -g edge-impulse-cli
npm install -g edge-impulse-cli

Which will rebuild the dependencies.

Error: "gyp: No Xcode or CLT version detected!" (macOS)

Failed to authenticate with Edge Impulse read ECONNRESET

If you see this error message and you're behind a proxy you will need to set your proxy settings via:

Windows

set HTTPS_PROXY=...
edge-impulse-daemon

macOS, Linux

HTTPS_PROXY=... edge-impulse-daemon

ENOENT: no such file or directory, access ‘~/.npm-global/lib/node_modules/edge-impulse-cli’ (Linux)

Manually delete the Edge Impulse directory from node_modules and reinstall:

cd ~/.npm-global/lib/node_modules
rm -rf edge-impulse-cli
npm install -g edge-impulse-cli

Uploader

The uploader currently handles these types of files:

  • .wav - Lossless audio files. It's recommended to use the same frequency for all files in your data set, as signal processing output might be dependent on the frequency.

  • .jpg and .png - Image files. It's recommended to use the same ratio for all files in your data set.

  • .mp4 and .avi- Video file. You can then from the studio split this video file into images at a configurable frame per second.

Need more?

Upload data from the studio

Uploading via the CLI

You can upload files via the Edge Impulse CLI via:

edge-impulse-uploader path/to/a/file.wav

You can upload multiple files in one go via glob patterns:

# Upload any wave files in a single directory
edge-impulse-uploader path/to/many/*.wav
# Another example, upload any wave in any subdirectory
edge-impulse-uploader **/*.wav

Or by specifying the directory:

edge-impulse-uploader --directory {path}

Category

Files are automatically uploaded to the training category, but you can override the category with the --category option. E.g.:

edge-impulse-uploader --category testing path/to/a/file.wav

Or set the category to split to automatically split data between training and testing sets (recommended for a balanced dataset). This is based on the hash of the file, so this is a deterministic process.

Labeling

edge-impulse-uploader --label noise path/to/a/file.wav

Custom labeling and metadata

When a labeling method is not provided, the labels are automatically inferred from the filename through the following regex: ^[a-zA-Z0-9\s-_]+. For example: idle.01 will yield the label idle.

Thus, if you want to use labels (string values) containing float values (e.g. "0.01", "5.02", etc...), automatic labeling won't work.

To bypass this limitation, you can make a JSON file containing your dataset files' info. We also support adding metadata to your samples:

info.labels

{
    "version": 1,
    "files": [{
        "path": "path1.csv",
        "category": "split",
        "label": { "type": "label", "label": "0.5" },
        "metadata": {
            "site_collected": "Amsterdam"
        }
    }, {
        "path": "path2.csv",
        "category": "split",
        "label": { "type": "label", "label": "0.7" },
        "metadata": {
            "site_collected": "Paris"
        }
    }]
}
  • Metadata field is optional

  • To upload unlabeled data use "label": { "type": "unlabeled" }

  • To upload multi-label data use

"label": {
    "type": "multi-label",
    "labels": [
        {
            "label": "noise",
            "startIndex": 0,
            "endIndex": 5000
        },
        {
            "label": "nominal_mode",
            "startIndex": 5001
            "endIndex": 60000
        },
        {
            "label": "defect",
            "startIndex": 60001
            "endIndex": 60200
        }
    ],

And then use:

edge-impulse-uploader --info-file info.labels

Other dataset annotation formats

(available since Edge Impulse CLI v1.21)

You can upload directories of data in a range of different formats:

edge-impulse-uploader --directory {path}
edge-impulse-uploader --directory {path} --dataset-format {name}

Clearing configuration

To clear the configuration, run:

edge-impulse-uploader --clean

This resets the uploader configuration and will prompt you to log in again.

API Key

You can use an API key to authenticate with:

edge-impulse-uploader --api-key ei_...

Note that this resets the uploader configuration and automatically configures the uploader's account and project.

Bounding boxes

{
    "version": 1,
    "type": "bounding-box-labels",
    "boundingBoxes": {
        "mypicture.jpg": [{
            "label": "jan",
            "x": 119,
            "y": 64,
            "width": 206,
            "height": 291
        }, {
            "label": "sami",
            "x": 377,
            "y": 270,
            "width": 158,
            "height": 165
        }]
    }
}

You can have multiple keys under the boundingBoxes object, one for each file name. If you have data in multiple folders, you can create a bounding_boxes.labels in each folder.

You don't need to upload bounding_boxes.labels

When uploading one or more images, we check whether a labels file is present in the same folder, and automatically attach the bounding boxes to the image.

So you can just do:

edge-impulse-uploader yourimage.jpg

or

edge-impulse-uploader *

Let the Studio do the work for you!

Unsure about the structure of the bounding boxes file? Label some data in the studio, then export this data by selecting Dashboard > Export. The bounding_boxes.labels file will be included in the exported archive.

Upload data from OpenMV datasets

The uploader data in the OpenMV dataset format. Pass in the option --format-openmv and pass the folder of your dataset in to automatically upload data. Data is automatically split between testing and training sets. E.g.:

$ edge-impulse-uploader --format-openmv path/to/your-openmv-dataset

Other options

  • --silent - omits information on startup. Still prints progress information.

  • --dev - lists development servers, use in conjunction with --clean.

  • --hmac-key <key> - set the HMAC key, only used for files that need to be signed such as wav files.

  • --concurrency <count> - number of files to uploaded in parallel (default: 20).

  • --progress-start-ix <index> - when set, the progress index will start at this number. Useful to split up large uploads in multiple commands while the user still sees this as one command.

  • --progress-end-ix <index> - when set, the progress index will end at this number. Useful to split up large uploads in multiple commands while the user still sees this as one command.

  • --progress-interval <interval> - when set, the uploader will not print an update for every line, but every interval period (in ms.).

  • --allow-duplicates - to avoid pollution of your dataset with duplicates, the hash of a file is checked before uploading against known files in your dataset. Enable this flag to skip this check.

Uploading large datasets

When using command line wildcards to upload large datasets you may encounter an error similar to this one:

edge-impulse-uploader *.wav
zsh: argument list too long: edge-impulse-uploader

This happens if the number of .wav files exceeds the total number of arguments allowed for a single command on your shell. You can easily work around this shell limitation by using the find command to call the uploader for manageable batches of files:

find . -name "*.wav" -print0 | xargs -0 edge-impulse-uploader

You can include any necessary flags by appending them to the xargs portion, for example if you wish to specify a category:

find training -name "*.wav" -print0 | xargs -0 edge-impulse-uploader --category training

Data forwarder

$ edge-impulse-data-forwarder

The data forwarder will ask you for the server you want to connect to, prompt you to log in, and then configure the device.

This is an example of the output of the forwarder:

Edge Impulse data forwarder v1.5.0
? What is your user name or e-mail address (edgeimpulse.com)? jan@edgeimpulse.com
? What is your password? [hidden]
Endpoints:
    Websocket: wss://remote-mgmt.edgeimpulse.com
    API:       https://studio.edgeimpulse.com
    Ingestion: https://ingestion.edgeimpulse.com

[SER] Connecting to /dev/tty.usbmodem401203
[SER] Serial is connected
[WS ] Connecting to wss://remote-mgmt.edgeimpulse.com
[WS ] Connected to wss://remote-mgmt.edgeimpulse.com
? To which project do you want to add this device? accelerometer-demo-1
? 3 sensor axes detected. What do you want to call them? Separate the names with ',': accX, accY, accZ
? What name do you want to give this device? Jan's DISCO-L475VG
[WS ] Authenticated

Note: Your credentials are never stored. When you log in these are exchanged for a token. This token is used to further authenticate requests.

Clearing configuration

To clear the configuration, run:

$ edge-impulse-data-forwarder --clean

Overriding the frequency

To override the frequency, use:

$ edge-impulse-data-forwarder --frequency 100

Overriding the baud rate

To set a different baud rate, use:

$ edge-impulse-data-forwarder --baud-rate 460800

Protocol

The protocol is very simple. The device should send data on baud rate 115,200 with one line per reading, and individual sensor data should be split with either a , or a TAB. For example, this is data from a 3-axis accelerometer:

-0.12,-6.20,7.90
-0.13,-6.19,7.91
-0.14,-6.20,7.92
-0.13,-6.20,7.90
-0.14,-6.20,7.91

The data forwarder will automatically determine the sampling rate and the number of sensors based on the output. If you load a new application where the sampling frequency or the number of axes changes, the data forwarder will automatically be reconfigured.

Example (Arduino)

This is an example of a sketch that reads data from an accelerometer (tested on the Arduino Nano 33 BLE):

#include <Arduino_LSM9DS1.h>

#define CONVERT_G_TO_MS2    9.80665f
#define FREQUENCY_HZ        50
#define INTERVAL_MS         (1000 / (FREQUENCY_HZ + 1))

static unsigned long last_interval_ms = 0;

void setup() {
    Serial.begin(115200);
    Serial.println("Started");

    if (!IMU.begin()) {
        Serial.println("Failed to initialize IMU!");
        while (1);
    }
}

void loop() {
    float x, y, z;

    if (millis() > last_interval_ms + INTERVAL_MS) {
        last_interval_ms = millis();

        IMU.readAcceleration(x, y, z);

        Serial.print(x * CONVERT_G_TO_MS2);
        Serial.print('\t');
        Serial.print(y * CONVERT_G_TO_MS2);
        Serial.print('\t');
        Serial.println(z * CONVERT_G_TO_MS2);
    }
}

Example (Mbed OS)

This is an example of an Mbed OS application that reads data from an accelerometer (tested on the ST IoT Discovery Kit):

#include "mbed.h"
#include "stm32l475e_iot01_accelero.h"

static int64_t sampling_freq = 104; // in Hz.
static int64_t time_between_samples_us = (1000000 / (sampling_freq - 1));

// set baud rate of serial port to 115200
static BufferedSerial serial_port(USBTX, USBRX, 115200);
FileHandle *mbed::mbed_override_console(int fd) {
    return &serial_port;
}

int main()
{
    int16_t pDataXYZ[3] = {0};
    Timer t;
    t.start();

    BSP_ACCELERO_Init();

    while(1) {
        int64_t next_tick = t.read_us() + time_between_samples_us;

        BSP_ACCELERO_AccGetXYZ(pDataXYZ);
        pc.printf("%d\t%d\t%d\n", pDataXYZ[0], pDataXYZ[1], pDataXYZ[2]);

        while (t.read_us() < next_tick) {
            /* busy loop */
        }
    }
}

Example (Zephyr)

#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/sensor.h>
#include <stdio.h>
#include <stdlib.h>

static int64_t sampling_freq = 104; // in Hz.
static int64_t time_between_samples_us = (1000000 / (sampling_freq - 1));

int main() {
    // output immediately without buffering
    setvbuf(stdout, NULL, _IONBF, 0);

    // get driver for the accelerometer
    const struct device *iis2dlpc = device_get_binding(DT_LABEL(DT_INST(0, st_iis2dlpc)));
    if (iis2dlpc == NULL) {
        printf("Could not get IIS2DLPC device\n");
        return 1;
    }

    struct sensor_value accel[3];

    while (1) {
        // start a timer that expires when we need to grab the next value
        struct k_timer next_val_timer;
        k_timer_init(&next_val_timer, NULL, NULL);
        k_timer_start(&next_val_timer, K_USEC(time_between_samples_us), K_NO_WAIT);

        // read data from the sensor
        if (sensor_sample_fetch(iis2dlpc) < 0) {
            printf("IIS2DLPC Sensor sample update error\n");
            return 1;
        }

        sensor_channel_get(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ, accel);

        // print over stdout
        printf("%.3f\t%.3f\t%.3f\r\n",
            sensor_value_to_double(&accel[0]),
            sensor_value_to_double(&accel[1]),
            sensor_value_to_double(&accel[2]));

        // busy loop until next value should be grabbed
        while (k_timer_status_get(&next_val_timer) <= 0);
    }
}

Sensor fusion

You may also have sensors with different sampling frequencies, such as:

  • accelerometer: 3 axis sampled at 100Hz

  • RMS current sensor: 1 axis sampled at 5Hz

In this case, you should first upscale to the highest frequency to keep the finest granularity: upscale RMS sensor to 100 Hz by duplicating each value 20 times (100/5). You could also smooth values over between samples.

Classifying data

Classifying data (Arduino)

// Include the Arduino library here (something like your_project_inference.h) 
// In the Arduino IDE see **File > Examples > Your project name - Edge Impulse > Static buffer** to get the exact name
#include <your_project_inference.h>
#include <Arduino_LSM9DS1.h>

#define CONVERT_G_TO_MS2    9.80665f
#define FREQUENCY_HZ        EI_CLASSIFIER_FREQUENCY
#define INTERVAL_MS         (1000 / (FREQUENCY_HZ + 1))

static unsigned long last_interval_ms = 0;
// to classify 1 frame of data you need EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE values
float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];
// keep track of where we are in the feature array
size_t feature_ix = 0;

void setup() {
    Serial.begin(115200);
    Serial.println("Started");

    if (!IMU.begin()) {
        Serial.println("Failed to initialize IMU!");
        while (1);
    }
}

void loop() {
    float x, y, z;

    if (millis() > last_interval_ms + INTERVAL_MS) {
        last_interval_ms = millis();

        // read sensor data in exactly the same way as in the Data Forwarder example
        IMU.readAcceleration(x, y, z);

        // fill the features buffer
        features[feature_ix++] = x * CONVERT_G_TO_MS2;
        features[feature_ix++] = y * CONVERT_G_TO_MS2;
        features[feature_ix++] = z * CONVERT_G_TO_MS2;

        // features buffer full? then classify!
        if (feature_ix == EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE) {
            ei_impulse_result_t result;

            // create signal from features frame
            signal_t signal;
            numpy::signal_from_buffer(features, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);

            // run classifier
            EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);
            ei_printf("run_classifier returned: %d\n", res);
            if (res != 0) return;

            // print predictions
            ei_printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n",
                result.timing.dsp, result.timing.classification, result.timing.anomaly);

            // print the predictions
            for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
                ei_printf("%s:\t%.5f\n", result.classification[ix].label, result.classification[ix].value);
            }
        #if EI_CLASSIFIER_HAS_ANOMALY == 1
            ei_printf("anomaly:\t%.3f\n", result.anomaly);
        #endif

            // reset features frame
            feature_ix = 0;
        }
    }
}

void ei_printf(const char *format, ...) {
    static char print_buf[1024] = { 0 };

    va_list args;
    va_start(args, format);
    int r = vsnprintf(print_buf, sizeof(print_buf), format, args);
    va_end(args);

    if (r > 0) {
        Serial.write(print_buf);
    }
}

Classifying data (Mbed OS)

#include "mbed.h"
#include "stm32l475e_iot01_accelero.h"
#include "ei_run_classifier.h"

static int64_t sampling_freq = EI_CLASSIFIER_FREQUENCY; // in Hz.
static int64_t time_between_samples_us = (1000000 / (sampling_freq - 1));

// to classify 1 frame of data you need EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE values
static float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];

// set baud rate of serial port to 115200
static BufferedSerial serial_port(USBTX, USBRX, 115200);
FileHandle *mbed::mbed_override_console(int fd) {
    return &serial_port;
}

int main()
{
    int16_t pDataXYZ[3] = {0};
    Timer t;
    t.start();

    BSP_ACCELERO_Init();

    while (1) {
        // fill the features array
        for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME) {
            int64_t next_tick = t.read_us() + time_between_samples_us;
            BSP_ACCELERO_AccGetXYZ(pDataXYZ);

            // copy accelerometer data into the features array
            features[ix + 0] = (float)pDataXYZ[0];
            features[ix + 1] = (float)pDataXYZ[0];
            features[ix + 2] = (float)pDataXYZ[0];

            while (t.read_us() < next_tick) {
                /* busy loop */
            }
        }

        // frame full? then classify
        ei_impulse_result_t result = { 0 };

        // create signal from features frame
        signal_t signal;
        numpy::signal_from_buffer(features, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);

        // run classifier
        EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);
        ei_printf("run_classifier returned: %d\n", res);
        if (res != 0) return 1;

        // print predictions
        ei_printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n",
            result.timing.dsp, result.timing.classification, result.timing.anomaly);

        // print the predictions
        for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
            ei_printf("%s:\t%.5f\n", result.classification[ix].label, result.classification[ix].value);
        }
    #if EI_CLASSIFIER_HAS_ANOMALY == 1
        ei_printf("anomaly:\t%.3f\n", result.anomaly);
    #endif
    }
}

Classifying (Zephyr)

Before adding the classifier in Zephyr:

  1. Copy the extracted C++ library into your Zephyr project, and add the following to your CMakeLists.txt file (where ./model is where you extracted the library).

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(./model)
  1. Enable C++ and set the stack size of the main thread to at least 4K, by adding the following to prj.conf:

CONFIG_CPLUSPLUS=y
CONFIG_LIB_CPLUSPLUS=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

CONFIG_MAIN_STACK_SIZE=8192
  1. If you're on a Cortex-M target, enable hardware acceleration by adding the following defines to your CMakeLists.txt file:

add_definitions(-DEIDSP_USE_CMSIS_DSP=1
                -DEIDSP_LOAD_CMSIS_DSP_SOURCES=1
                -DEI_CLASSIFIER_TFLITE_ENABLE_CMSIS_NN=1
                -DARM_MATH_LOOPUNROLL)

Then, run the following application:

#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/sensor.h>
#include <stdio.h>
#include <stdlib.h>
#include "ei_run_classifier.h"

static int64_t sampling_freq = EI_CLASSIFIER_FREQUENCY; // in Hz.
static int64_t time_between_samples_us = (1000000 / (sampling_freq - 1));

// to classify 1 frame of data you need EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE values
static float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];

int main() {
    // output immediately without buffering
    setvbuf(stdout, NULL, _IONBF, 0);

    // get driver for the accelerometer
    const struct device *iis2dlpc = device_get_binding(DT_LABEL(DT_INST(0, st_iis2dlpc)));
    if (iis2dlpc == NULL) {
        printf("Could not get IIS2DLPC device\n");
        return 1;
    }

    struct sensor_value accel[3];

    while (1) {
        for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME) {
            // start a timer that expires when we need to grab the next value
            struct k_timer next_val_timer;
            k_timer_init(&next_val_timer, NULL, NULL);
            k_timer_start(&next_val_timer, K_USEC(time_between_samples_us), K_NO_WAIT);

            // read data from the sensor
            if (sensor_sample_fetch(iis2dlpc) < 0) {
                printf("IIS2DLPC Sensor sample update error\n");
                return 1;
            }

            sensor_channel_get(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ, accel);

            // fill the features array
            features[ix + 0] = sensor_value_to_double(&accel[0]);
            features[ix + 1] = sensor_value_to_double(&accel[1]);
            features[ix + 2] = sensor_value_to_double(&accel[2]);

            // busy loop until next value should be grabbed
            while (k_timer_status_get(&next_val_timer) <= 0);
        }

        // frame full? then classify
        ei_impulse_result_t result = { 0 };

        // create signal from features frame
        signal_t signal;
        numpy::signal_from_buffer(features, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);

        // run classifier
        EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);
        printf("run_classifier returned: %d\n", res);
        if (res != 0) return 1;

        // print predictions
        printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n",
            result.timing.dsp, result.timing.classification, result.timing.anomaly);

        // print the predictions
        for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
            printf("%s:\t%.5f\n", result.classification[ix].label, result.classification[ix].value);
        }
    #if EI_CLASSIFIER_HAS_ANOMALY == 1
        printf("anomaly:\t%.3f\n", result.anomaly);
    #endif
    }
}

Troubleshooting

"The execution of scripts is disabled on this system" (Windows)

If you are running the data forwarder on a Windows system, you need to update PowerShell's execution policy to allow running scripts:

Set-ExecutionPolicy unrestricted

Impulse runner

The impulse runner shows the results of your impulse running on your development board. This only applies to ready-to-go binaries built from the studio.

You start the impulse via:

$ edge-impulse-run-impulse

This will sample data from your real sensors, classify the data, then print the results. E.g.:

edge-impulse-run-impulse
Edge Impulse impulse runner v1.7.3
[SER] Connecting to /dev/tty.usbmodem401103
[SER] Serial is connected, trying to read config...
[SER] Retrieved configuration
[SER] Device is running AT command version 1.3.0
[SER] Started inferencing...
Inferencing settings:
        Interval: 16.00 ms.
        Frame size: 375
        Sample length: 2000 ms.
        No. of classes: 4
Starting inferencing, press 'b' to break
Sampling... Storing in file name: /fs/device-classification.4
Predictions (DSP: 16 ms., Classification: 1 ms., Anomaly: 2 ms.):
    idle: 0.91016
    snake: 0.08203
    updown: 0.00391
    wave: 0.00391
    anomaly score: -0.067
Finished inferencing, raw data is stored in '/fs/device-classification.4'. Use AT+UPLOADFILE to send back to Edge Impulse.

Other options

  • --debug - run the impulse in debug mode, this will print the intermediate DSP results. For image models, a live feed of the camera and inference results will also be locally hosted and available in your browser (More on this below.)

  • --continuous - run the impulse in continuous mode (not available on all platforms).

Embedded API Server

The Linux CLI Runner has an embedded API server that allows you to interact with the model easily from any application, environment, or framework that implements an HTTP client. This feature is started with the runner using the --run-http-server <port> option.

To start the API server:

$ edge-impulse-linux-runner --debug

Which will share the link to the web page where you can see the live feed of the camera and inference results.

or

$ edge-impulse-linux-runner --run-http-server 3000

This will start the API server on port 3000, if you don't have an image model you will not see the http server web page.

API Endpoints

Once the server is running, you can send HTTP requests to interact with the model. Here is a simple example using Python:

For time series data...

import requests
import json

url = 'http://localhost:3000/classify'
data = {'data': [/* {"timestamp": 0, "x": 0.01, "y": -0.02, "z": 9.81},
        {"timestamp": 16, "x": 0.02, "y": -0.01, "z": 9.80},
        {"timestamp": 32, "x": 0.00, "y": 0.00, "z": 9.79} . . . modify here with your captured data to send to your project */]}

response = requests.post(url, json=data)
print(response.json())

For image data, and how to get data from a webcam...

import cv2
import os
import requests

image_path = "snapshot.jpeg"

# Initialize the webcam
cam = cv2.VideoCapture(0)

# Check if the webcam is opened successfully
if not cam.isOpened():
    raise IOError("Cannot open webcam")

# Capture a frame
ret, frame = cam.read()

# Check if frame is captured successfully
if not ret:
    raise IOError("Cannot read frame")

# Save the frame as an image
cv2.imwrite(image_path, frame)

# Release the webcam
cam.release()

print("Snapshot saved as snapshot.jpeg")

url = 'http://localhost:3000/api/image'
response = requests.post(url, files={'file': open(image_path, 'rb')})
print(response.json())

How would you use this?

Here are a few examples of how you could use this embedded API server:

Custom Applications: A custom app running on the same Linux device can interact with the model using an HTTP client, simplifying the integration process.

IoT Devices: Small IoT devices with an HTTP client in the firmware can send data to the inference server (the runner) in the local network, get results, and leverage powerful ML models without the need for local model storage and inference.

Web Applications: Web applications can interact with the model running on the Linux device using the HTTP client, enabling powerful ML models in web applications without the need for cloud services.

Mobile Applications: Mobile applications can interact with the model running on the Linux device using the HTTP client, enabling powerful ML models in mobile applications without the need for cloud services.

Summary

The impulse runner is a powerful tool that allows you to run your impulse on your development board and interact with it using an embedded API server. This feature is useful for custom applications, IoT devices, web applications, and mobile applications that need to interact with the model running on the Linux device.

Himax flash tool

You upload a new binary via:

This will yield a response like this:

Other options

  • --baud-rate <n> - sets the baud rate of the bootloader. This should only be used during development.

  • --verbose - enable debug logs, including all communication received from the device.

  • --device <device> - select the device type: WE-I-Plus (default) or WiseEye2 (Grove Vision AI Module V2)

  • --skip-reset - skip the reset procedure (in case the device is already in bootloader mode)

  • --version - output the version number

  • --help - display help for command

  • --firmware-path <file> - firmware path (required)

Recent versions of Google Chrome and Microsoft Edge can connect directly to fully-supported development boards, without the serial daemon. See for more information.

The serial daemon is part of the . In order to use the daemon, you first have to .

If you are using the development board, you may experience the following error when attempting to connect to a WiFi network:

There is a with the firmware for this development board's WiFi module that results in a timeout during network scanning if there are more than 20 WiFi access points detected. If you are experiencing this issue, you can work around it by attempting to reduce the number of access points within range of the device, or by skipping WiFi configuration.

- configures devices over serial, and acts as a proxy for devices that do not have an IP connection.

- allows uploading and signing local files.

- a very easy way to collect data from any device over a serial connection, and forward the data to Edge Impulse.

- show the impulse running on your device.

- create organizational transformation, custom dsp, custom deployment and custom transfer learning blocks.

- to flash the Himax boards and microcontrollers.

Recent versions of Google Chrome and Microsoft Edge can connect directly to fully-supported development boards, without the CLI. See for more information.

- configures devices over serial, and acts as a proxy for devices that do not have an IP connection.

- allows uploading and signing local files.

- a very easy way to collect data from any device over a serial connection, and forward the data to Edge Impulse.

- show the impulse running on your device.

- create organizational transformation, custom dsp, custom deployment and custom transfer learning blocks.

- to flash the Himax boards and microcontrollers.

Connect to devices without the CLI? Recent versions of Google Chrome and Microsoft Edge can connect directly to fully-supported development boards, without the CLI. See for more information.

Create an .

Install on your host computer.

Install v20 or above on your host computer.

Create an .

Install on your Windows machine.

You can find full instructions on how to install WSL .

Then follow the linux installation instructions .

Create an .

Install on your host computer.

Install v20.x+ or above on your host computer.

If you have issues installing the CLI you can also collect data from directly using recent versions of Google Chrome and Microsoft Edge. See on how to get started.

This can happen even though you have Xcode CLT installed if you've updated macOS since your install. Follow to reinstall Xcode CLT.

The uploader signs local files and uploads them to the . This is useful to upload existing data samples and entire datasets, or to migrate data between Edge Impulse instances.

.cbor - Files in the Edge Impulse . The uploader will not resign these files, only upload them.

.json - Files in the Edge Impulse . The uploader will not resign these files, only upload them.

.csv - Files in the Edge Impulse . If you have configured the "CSV wizard", the settings will be used to parse your CSV files.

The uploader currently handles these types of :

If none of these above choices are suitable for your project, you can also have a look at the Transformation blocks to parse your data samples to create a dataset supported by Edge Impulse. See

You can also upload data directly from the studio, see . Go to the Data acquisition page, and click the 'upload' icon. You can select files or folders, the category and the label directly from here.

The first time you'll be prompted for a server, and your login credentials (see for more information).

A label is automatically inferred from the file name, see the . You can override this with the --label option. E.g.:

By default, we try to automatically detect your dataset annotation format from the . If we cannot detect it, the uploader will output the list of formats. You can then use:

In July 2023, we added support for many other . Below is an example of the default Edge Impulse object detection format.

If you want to upload data for , the uploader can label the data for you as it uploads it. To do this, all you need is to create a bounding_boxes.labels file in the same folder as your image files. The contents of this file are formatted as JSON with the following structure:

The data forwarder is used to easily relay data from any device to Edge Impulse over serial. Devices write sensor values over a serial connection, and the data forwarder collects the data, signs the data and sends the data to the ingestion service. The data forwarder is useful to quickly enable data collection from a wide variety of development boards without having to port the full and , but only supports collecting data at relatively low frequencies.

To use the data forwarder, load an application (examples for , and below) on your development board, and run:

There's also a complete example that samples data from both the accelerometer and the gyroscope here: .

This is an example of a Zephyr application that reads data from an accelerometer (tested on the Nordic Semiconductor nRF52840 DK with ST X-NUCLEO-IKS02A1 shield), based on the example:

There's also a complete example that samples data from the accelerometer here: .

Using the Data Forwarder, you can relay data from multiple sensors. You can check for a complete example using NO2, CO, C2H5OH and VOC sensors on a WIO Terminal.

To classify data you first deploy your project by following the steps in - which contains examples for a wide variety of platforms. Then, declare a features array, fill it with sensor data, and run the classifier. Here are examples for Arduino, Mbed and Zephyr - but the same applies to any other platform.

Note: These examples collect a full frame of data, then classify this data. This might not be what you want (as classification blocks the collection thread). See for an example on how to implement continuous classification.

For more information on the impulse runner, or to discuss how you may use this please reach us on the

The Himax flash tool uploads new binaries to the or Grove Vision AI Module V2 (Himax WiseEye2) over a serial connection.

this blog post
Edge Impulse CLI
install the CLI
ST B-L475E-IOT01A
known issue
edge-impulse-daemon
edge-impulse-uploader
edge-impulse-data-forwarder
edge-impulse-run-impulse
edge-impulse-blocks
himax-flash-tool
this blog post
edge-impulse-daemon
edge-impulse-uploader
edge-impulse-data-forwarder
edge-impulse-run-impulse
edge-impulse-blocks
himax-flash-tool
this blog post
Edge Impulse account
Python 3
Node.js
Edge Impulse account
WSL
here
Edge Impulse account
Python 3
Node.js
fully-supported development boards
this blog post
this guide
ingestion service
Data Acquisition format
Data Acquisition format
Comma Separated Values (CSV) format
Building your Transformation Blocks
Studio uploader
Edge Impulse Daemon
Ingestion service documentation
object detection
remote management protocol
serial protocol
edgeimpulse/example-dataforwarder-mbed
sensorhub
edgeimpulse/example-dataforwarder-zephyr
Benjamin Cabe's artificial nose
Running your impulse locally
Continuous audio sampling
Forum
$ himax-flash-tool -f path/to/a/firmware.img
[HMX] Connecting to /dev/tty.usbserial-DT04551Q...
[HMX] Connected, press the **RESET** button on your Himax WE-I now
[HMX] Restarted into bootloader. Sending file.
[HMX] Sending 2964 blocks
 ████████████████████████████████████████ 100% | ETA: 0s | 2964/2964
[HMX] Firmware update complete
[HMX] Press **RESET** to start the application

Flashed your Himax WE-I Plus development board.
To set up your development with Edge Impulse, run 'edge-impulse-daemon'
To run your impulse on your development board, run 'edge-impulse-run-impulse'
below
Arduino
Mbed OS
Zephyr
Himax WE-I Plus

Blocks

The blocks CLI tool creates different blocks types that are used in organizational features such as:

With the blocks CLI tool, you can create new blocks, run them locally, and push them to Edge Impulse infrastructure so we can host them for you. Edge Impulse blocks can be written in any language, and are based on Docker container for maximum flexibility.

As an example here, we will show how to create a transformation block.

You can create a new block by running:

$ edge-impulse-blocks init
? What is your user name or e-mail address (edgeimpulse.com)? jan@edgeimpulse.com
? What is your password? [hidden]
? In which organization do you want to create this block? EdgeImpulse Inc.
Attaching block to organization 'EdgeImpulse Inc.'
? Choose a type of block Transformation block
? Choose an option Create a new block
? Enter the name of your block Extract voice
? Enter the description of your block Extracts voice from video files
Creating block with config: {
  name: 'Extract voice',
  type: 'transform',
  description: 'Extracts voice from video files',
  organizationId: 4
}
? Would you like to download and load the example repository (Python)? yes
Template repository fetched!
Your new block 'Extract voice' has been created in '/Users/janjongboom/repos/custom-transform-block'.
When you have finished building your transformation block, run "edge-impulse-blocks push" to update the block in Edge Impulse.

When you're done developing the block you can push it to Edge Impulse via:

$ edge-impulse-blocks push
Archiving 'custom-transform-block'...
Archiving 'custom-transform-block' OK (2 KB)

Uploading block 'Extract voice' to organization 'EdgeImpulse Inc.'...
Uploading block 'Extract voice' to organization 'EdgeImpulse Inc.' OK

Building transformation block 'Extract voice'...
INFO[0000] Retrieving image manifest python:3.7.5-stretch
INFO[0000] Retrieving image python:3.7.5-stretch

...

Building transformation block 'Extract voice' OK

Your block has been updated, go to https://studio.edgeimpulse.com/organization/4/data to run a new transformation

The metadata about the block (which organization it belongs to, block ID) is saved in .ei-block-config, which you should commit. To view this data in a convenient format, run:

$ edge-impulse-blocks info
Name: TestDataItemTransform
Description: Data item transformation example
Organization ID: 1
Not pushed
Block type: transform
Operates on: dataitem
Bucket mount points:
    - ID: 1, Mount point: /path/to/bucket

Block runner

Rather than only running custom blocks in the cloud, the edge-impulse-blocks runner command lets developers download, configure, and run custom blocks entirely on their local machine, making testing and development much faster. The options depend on the type of block being run, and they can be viewed by using the help menu:

$ edge-impulse-blocks runner -h
Usage: edge-impulse-blocks runner [options]
Run the current block locally
Options:
  --data-item <dataItem>          Tranformation block: Name of data item
  --file <filename>               File tranformation block: Name of file in data item
  --epochs <number>               Transfer learning: # of epochs to train
  --learning-rate <learningRate>  Transfer learning: Learning rate while training
  --validation-set-size <size>    Transfer learning: Size of validation set
  --input-shape <shape>           Transfer learning: List of axis dimensions. Example: "(1, 4, 2)"
  --download-data                 Transfer learning or deploy: Only download data and don't run the block
  --extra-args <args>             Pass extra arguments/options to the Docker container
  -h, --help                      display help for command

As seen above, the runner accepts a list of relevant option flags along with a variable number of extra arguments that get passed to the Docker container at runtime for extra flexibility. As an example, here is what happens when edge-impulse-blocks runner is used on a file transformation block:

$ edge-impulse-blocks runner --data-item item1 --file sample_1.cbor
Found data item item1 with id=1, metadata={}
Downloading file sample_1.cbor to /path/to/block/data/dataset_1/item1...
File downloaded
...

Best of all, the runner only downloads data when it isn't present locally, thus saving time and bandwidth.

$ edge-impulse-blocks runner --data-item item1 --file sample_1.cbor
Found data item item1 with id=1, metadata={}
File already present; skipping download...
...

Block structure

Transformation blocks use Docker containers, a virtualization technique which lets developers package up an application with all dependencies in a single package. Thus, every block needs at least a Dockerfile. This is a file describing how to build the container that powers the block, and it has information about the dependencies for the block - like a list of Python packages your block needs. This Dockerfile needs to declare an ENTRYPOINT: a command that needs to run when the container starts.

An example of a Python container is:

FROM python:3.7.5-stretch

WORKDIR /app

# Python dependencies
COPY requirements.txt ./
RUN pip3 --no-cache-dir install -r requirements.txt

COPY . ./

ENTRYPOINT [ "python3",  "transform.py" ]

Which takes a base-image with Python 3.7.5, then installs all dependencies listed in requirements.txt, and finally starts a script called transform.py.

Note: Do not use a WORKDIR under /home! The /home path will be mounted in by Edge Impulse, making your files inaccessible.

Note: If you use a different programming language, make sure to use ENTRYPOINT to specify the application to execute, rather than RUN or CMD.

Besides your Dockerfile you'll also need the application files, in the example above transform.py and requirements.txt. You can place these in the same folder.

Excluding files

When pushing a new block all files in your folder are archived and sent to Edge Impulse, where the container is built. You can exclude files by creating a file called .ei-ignore in the root folder of your block. You can either set absolute paths here, or use wildcards to exclude many files. For example:

a-large-folder/*
some-path-to-a-text-file.txt

Clearing configuration

To clear the configuration, run:

$ edge-impulse-blocks --clean

This resets the CLI configuration and will prompt you to log in again.

API Key

You can use an API key to authenticate with:

$ edge-impulse-blocks --api-key ei_...

Note that this resets the CLI configuration and automatically configures your organization.

Other options

  • --dev - lists development servers, use in conjunction with --clean.

- to transform large sets of data efficiently.

- to build personalized firmware using your own data or to create custom libraries.

- to create and host your custom signal processing techniques and use them directly in your projects.

- to use your custom neural network architectures and load pre-trained weights, with Keras, PyTorch and scikit-learn.

Transformation blocks
Deployment blocks
Custom processing blocks
Custom machine learning models
dataset annotation formats
Unlabeled
Edge Impulse exporter format
Edge Impulse object detection dataset
COCO JSON
Open Images CSV
Pascal VOC XML
Plain CSV
YOLO TXT
supported ones
image dataset annotation formats