$ show mocleiri/tensorflow-micropython-examples
This repository provides a custom MicroPython firmware that integrates TensorFlow Lite for Microcontrollers (TFLM), enabling TinyML inference on microcontrollers such as ESP32, RP2040, and STM32. It exposes a βmicroliteβ Python module with classes for loading TFLite models (interpreter, tensor) and an audio frontend for spectrogram extraction. The repo includes three pre-trained example applications: hello-world (sine regression), micro-speech (keyword spotting), and person detection (camera-based). The architecture is monolithic per board, built via CMake (ESP-IDF, RP2040 SDK) or Make (STM32). The build system and board definitions are deeply coupled to specific MicroPython and TFLM versions, and the code demonstrates careful memory management (custom GC heap allocators, arena buffer patterns) to run ML models on resource-constrained devices. While primarily a firmware/application project, it also contains pre-processed audio data and model files, giving it a slight hybrid nature. The codebase is functional and structured, but has several rough edges: duplicate model files, hardcoded source lists, and incomplete board support (STM32 noted as broken). It is actively maintained with CI workflows and recent commits, but technical drift is medium due to reliance on upstream main branches.
The LLM identified 3 files worth highlighting for reuse:
Implements a custom memory allocator (xalloc) that allows C++ objects (like TFLM's interpreter) to allocate memory from MicroPython's garbage-collected heap. This is essential for running C++ libraries in MicroPython's constrained environment and demonstrates deep understanding of both runtime internals. The pattern is self-contained and could be reused for integrating other C++ libraries with MicroPython.
A thin C++ class that bridges TFLM's `ErrorReporter` interface to MicroPython's `mp_printf` for logging. This is a reusable adapter pattern that can be applied to any error-reporting abstraction in embedded systems, allowing library errors to be surfaced in MicroPython's console.
A Python module that implements audio slicing and feature data generation using the `audio_frontend` C module. It defines `FeatureData` and `sliceAudio` classes that abstract the spectrogram extraction pipeline. This code is clean, well-commented, and can be extracted for any keyword-spotting project using the same TFLM frontend on embedded MicroPython devices.