Species Model — ESP32-CAM Export (Legacy)
The data collection, labeling, and PyTorch training steps are platform-independent — see the current training guide for those sections. This page preserves only the ESP32-CAM-specific export and deployment workflow.
Export (TFLite Path)
Section titled “Export (TFLite Path)”Export the trained model for ESP32-CAM deployment:
# Export to ONNX with INT8 quantizationuv run smartpot-export \ --checkpoint models/checkpoints/best_model.pth \ --output models/species_model
# Optionally generate a C header for firmware embeddinguv run smartpot-export \ --checkpoint models/checkpoints/best_model.pth \ --output models/species_model \ --generate-headerThe export pipeline:
- Tries litert-torch for direct PyTorch → TFLite conversion
- Falls back to PyTorch → ONNX → INT8 quantized ONNX
- Attempts ONNX → TFLite conversion via onnx2tf subprocess (if available)
- Validates output shapes and runs a test inference
- Optionally generates
species_model.hwith the model as a C byte array
If the automatic TFLite conversion isn’t available, you can convert manually:
# Install onnx2tf in an isolated environment and convertuvx onnx2tf -i models/species_model.onnx -o tflite_output -oiqtDeployment (ESP32-CAM)
Section titled “Deployment (ESP32-CAM)”- Copy the
.tflitemodel file to the ESP32-CAM’s MicroSD card as/species_model.tflite - The firmware loads the model on boot from SD card into PSRAM
- Verify with the serial monitor:
Model loaded: 923648 bytes from SDTFLite ready | input: [1,96,96,3] type=9 | arena: 186432/262144 bytes
Firmware Integration
Section titled “Firmware Integration”The model runs inside classify_catch() in firmware/src/trap_cam/main.cpp:
- Camera switches from QVGA grayscale (motion detection) to 96×96 RGB565 (classification)
- RGB565 pixels are unpacked, ImageNet-normalized, and quantized to INT8
- TFLite Micro runs inference (~200–300ms on ESP32)
- Output tensor is dequantized, softmax applied, argmax selects species
- Camera switches back to grayscale for the next motion detection cycle
Build and Flash
Section titled “Build and Flash”cd firmwarepio run -e trap_cam --target uploadpio device monitor -e trap_camSee Also
Section titled “See Also”- Train the Species Model — Current training guide (platform-independent sections + ESP32-P4 export)
- Flash the ESP32-CAM — Legacy flash guide
- On-Device Vision — ESP32-CAM — Legacy vision architecture