Skip to main content
Open in Google Colab The Edge Impulse object detection model (FOMO) is effective at classifying objects and very lightweight (can run on MCUs). It does not however have any object persistence between frames. One common use of computer vision is for object counting- in order to achieve this you will need to add in some extra logic when deploying. This notebook takes you through how to count objects using the linux deployment block (and provides some pointers for how to achieve similar logic other firmware deployment options). Relevant links:

1. Download the linux deployment .eim for your project

To run your model locally, you need to deploy to a linux target in your project. First you need to enable all linux targets. Head to the deployment screen and click “Linux Boards” then in the following pop-up select “show all Linux deployment options on this page”:

Enable linux deployment

Then download the linux/mac target which is relevant to your machine:

Download your relevant .eim

Finally, follow the instructions shown as a pop-up to make your .eim file executable (for example for MacOS):
  • Open a terminal window and navigate to the folder where you downloaded this model.
  • Mark the model as executable: chmod +x path-to-model.eim
  • Remove the quarantine flag: xattr -d com.apple.quarantine ./path-to-model.eim

2. Object Detection

Dependencies

Ensure you have these libraries installed before starting:

2.1 Run Object Counting on a video file

(see next heading for running on a webcam) This program will run object detection on an input video file and count the objects going upwards which pass a threshold (TOP_Y). The sensitivity can be tuned with the number of columns (NUM_COLS) and the DETECT_FACTOR which is the factor of width/height of the object used to determine object permanence between frames. Ensure you have added the relevant paths to your model file and video file:
  • modelfile = ‘/path/to/modelfile.eim’
  • videofile = ‘/path/to/video.mp4’

2.2 Run Object Counting on a webcam stream

This program will run object detection on a webcam port and count the objects going upwards which pass a threshold (TOP_Y). The sensitivity can be tuned with the number of columns (NUM_COLS) and the DETECT_FACTOR which is the factor of width/height of the object used to determine object permanence between frames. Ensure you have added the relevant paths to your model file and video file:
  • modelfile = ‘/path/to/modelfile.eim’
  • [OPTIONAL] camera_port = ‘/camera_port’

3. Deploying to MCU firmware

While running object counting on linux hardware is fairly simple, it would be more useful to be able to deploy this to one of the firmware targets. This method varies per target but broadly speaking it is simple to add the object counting logic into existing firmware. Here are the main steps:

1. Find and clone the Edge Impulse firmware repository for your target hardware

This can be found on our github pages e.g. https://github.com/edgeimpulse/firmware-arduino-nicla-vision

2. Deploy your model to a C++ library

You’ll need to replace the “edge-impulse-sdk”, “model-parameters” and “tflite-model” folders within the cloned firmware with the ones you’ve just downloaded for your model.

3. Find the object detection bounding boxes printout code in your firmware

This will be in a .h or similar file somewhere in the firmware. Likely in the ei_image_nn.h file. It can be found by searching for these lines:
The following lines must be added into the logic in these files (For code itself see below, diff for clarity). Firstly these variables must be instantiated:
Then this logic must be inserted into the bounding box printing logic here:
Full code example for nicla vision (src/inference/ei_run_camera_impulse.cpp):

4. Build your firmware locally and flash to your device

Follow the instructions in the README.md file for the firmware repo you have been working in.

5. Run your impulse on device

Use the command below to see on-device inference (follow the local link to see bounding boxes and count output in the browser)