General-purpose TensorFlow Lite Micro implementation with image processing support for ESPHome
This project provides a robust, modular framework for running TensorFlow Lite Micro models on ESP32 devices within the ESPHome ecosystem. While originally designed for analog/digital meter reading, it is suitable for various computer vision tasks.
New in v2.0: The project has been refactored into modular components for better maintainability and reusability.
The repository allows you to use specific components based on your needs:
| Component | Description |
|---|---|
| meter_reader_tflite | AI-powered meter reader using TensorFlow Lite models. Orchestrates capture, inference, and reporting for digital meters. |
| ssocr_reader | (Alpha dev !) Seven-segment OCR reader using SSOCR algorithm. Reads digital displays without AI models. |
| analog_reader | (Alpha dev!) Analog dial/gauge reader using radial intensity sum algorithm. Reads pointer positions without AI. |
| Component | Description |
|---|---|
| value_validator | Robust validation engine for meter readings. Eliminates outliers, tracks history, and prevents impossible value jumps. |
| esp32_camera_utils | Powerful image processing utilities. Handles cropping, scaling, rotation (JPEG/Raw), and format conversion using esp_new_jpeg library. |
| tflite_micro_helper | Wrapper for TensorFlow Lite Micro runtime (checking model CRC32, etc..) and esp-nn optimizations. Manages tensor arena and model loading. |
| flash_light_controller | Manages flash light timing for optimal image capture conditions. |
| Component | Description |
|---|---|
| legacy_meter_reader_tflite | The previous monolithic version, kept for backward compatibility. |
A detailed procedure for setting up is available in wiki/setup page
Add the components to your ESPHome configuration:
external_components:
- source:
type: git
url: https://github.com/nliaudat/esphome_ai_component
ref: main
components:
# Choose the reader type you need:
- meter_reader_tflite # AI-powered digit recognition
# - ssocr_reader # Seven-segment OCR (no AI)
# - analog_reader # Analog gauge/dial reader (no AI)
# Supporting components:
- value_validator # Validation engine (recommended)
- tflite_micro_helper # Required for meter_reader_tflite
- esp32_camera_utils # Image processing utilities
- flash_light_controlleresp32_camera:
name: "My Camera"
resolution: 640x480
pixel_format: JPEG
value_validator:
id: my_validator
allow_negative_rates: false
max_absolute_diff: 300
meter_reader_tflite:
id: my_meter_reader
model: "digit_recognizer.tflite"
camera_id: my_camera
# Optional: Validator
validator: my_validator
# If no validator is set, use simple threshold:
# confidence_threshold: 0.85
update_interval: 60s
# Optional: Link to other components
flash_light_controller: my_flash_controller
crop_zones_global: my_crop_zones
# Image Rotation (Dev)
rotation: "90" # Options: "0", "90", "180", "270"value_validator:
id: my_validator
allow_negative_rates: false
max_absolute_diff: 50
ssocr_reader:
id: my_ssocr
camera_id: my_camera
validator: my_validator
update_interval: 60s
threshold_level: 128
digit_count: 8value_validator:
id: my_validator
allow_negative_rates: true # Analog gauges can go both ways
max_rate_change: 0.25
analog_reader:
id: my_analog
camera_id: my_camera
validator: my_validator
update_interval: 60s
dials:
- min_value: 0
max_value: 100
radius_min: 20
radius_max: 80Note
Rotation behavior: The rotation setting only affects the AI processing. The internal webserver and Home Assistant live view will likely remain unrotated (sideways) as the camera hardware only supports 180Β° flips. This is normal behavior.
Most parameters are now exposed to Home Assistant for real-time adjustment without recompiling:
- Calibration: Camera Window (Offset X/Y, Width, Height)
- Timing: Flash Pre/Post times, Update Interval
- Debug: Toggle debug logging and image generation
- Settings: Meter units and thresholds
These entities normally appear under the device in Home Assistant.
- π€ TensorFlow Lite Micro: Full TFLite Micro runtime support (with operators detection and auto loading)
- π· Camera Integration: State of the art ESP32 camera integration with TrackedBuffer for better memory management and windowing for OV2640 like camera
- πΌοΈ Image Preprocessing: Automatic cropping, scaling, and format conversion
- β‘ Optimized Performance: ESP-NN accelerated operations
- π― Multi-Zone Processing: Process multiple regions of interest
- π§ Flexible Configuration: Support for various model types and input formats
- π Advanced Debugging: Real-time image analysis and model output inspection
- π Image Rotation (Dev): Full support for 0Β°, 90Β°, 180Β°, 270Β° rotation on both JPEG and Raw formats (via
esp32_camera_utils).
- Water, electricity, gas meter digit recognition
- Analog gauge reading
- Digital display extraction
- Object Detection: Identify objects in camera frames
- Image Classification: Categorize images into classes
- Anomaly Detection: Detect unusual patterns or events
- Quality Control: Inspect products or components
β Model loading fails
- Cause: Tensor arena size is too small.
- Solution: Increase
tensor_arena_sizeinmeter_reader_tfliteconfig.
meter_reader_tflite:
tensor_arena_size: 768KB # Default is 512KBβ Poor inference results
- Cause: Input image poor quality or wrong crop.
- Solution: Enable debug mode to see what the model actually "sees".
meter_reader_tflite:
debug: trueβ Camera frame issues
- Cause: Low memory or bandwidth issues.
- Solution: Lower resolution or framerate in
esp32_camera.
- Use quantized models (int8) for better performance
- Enable ESP-NN optimizations (enabled by default)
- Use appropriate tensor_arena_size (already calculated per model)
If you need to use the old version, specify the legacy component:
external_components:
- source:
type: git
url: https://github.com/nliaudat/esphome_ai_component
ref: main
components: [legacy_meter_reader_tflite]New optimized models are available for digit recognition that significantly improve performance:
- Source: nliaudat/digit_recognizer
- Performance: Capable of full image processing and 8-digit inference in less than 270 ms [esp32-S3] (2700 ms for legacy esp32).
These models are recommended for faster response times and lower power consumption.
- Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC-BY-NC-SA)
- No commercial use