This project implements a real-time smart waste classification system using FastAPI, ESP32-CAM, and ESP32 microcontrollers.
The ESP32-CAM captures waste images and sends them to this FastAPI server via WebSocket.
The server uses a pre-trained TensorFlow/Keras model to classify the image as non-biodegradable (0) or biodegradable (1), and sends the classification back to the ESP32 for actuation (e.g., servo bin sorting).
- FastAPI backend with WebSocket support.
- Real-time communication between ESP32-CAM and ESP32.
- TensorFlow/Keras model for waste image classification.
- Automatic image saving and classification feedback.
- Health check and image retrieval HTTP endpoints.
- Modular design — can integrate with IoT dashboards or cloud services.
ESP32-CAM → Captures image → sends to → FastAPI Server (this code) → classifies image → sends result to → ESP32 (servo/sensor)
The ESP32 controls sorting mechanisms or provides visual feedback.
-
Ensure you are in the waste-AI directory
-
Install required packages:
pip install -r requirements.txt
-
Create a virtual environment and activate it
python -m venv env
-
Activate the virtual environment by
env\Scripts\activate
-
To start the server
python -m waste
-
| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Checks if the server is running. |
GET |
/latest |
Returns the latest image captured by the ESP32-CAM. |
| Endpoint | Connected Device | Description |
|---|---|---|
/ws/image |
ESP32-CAM | Receives binary image data and sends classification result. |
/ws/sensor |
ESP32 | Receives sensor data (e.g., object detected) and triggers ESP32-CAM capture. |
- ESP32 detects an object and sends
"object_detected"→/ws/sensor. - Server tells ESP32-CAM to
"capture"→/ws/image. - ESP32-CAM sends the image as binary data →
/ws/image. - Server saves the image (
latest.jpg) and classifies it usingtf_model.h5. - Server sends classification result (
"0"or"1") →/ws/sensor. - ESP32 actuates servo or performs next action.
- Server instructs ESP32 to
"resume_sensing"after processing.
INFO: ESP32-CAM (image) connected via WebSocket.
INFO: Received image data from /ws/image: 24567 bytes
INFO: Latest image saved to latest.jpg
INFO: Classified as: Non-biodegradable (1)
INFO: Sending classification '1' to Esp32 (/ws/sensor).
INFO: Sent 'resume_sensing' signal to Esp32 (/ws/sensor).
- The ML model file
tf_model.h5should be placed inside the/modeldirectory. - Input shape:
(150, 150, 3) - Output: A single sigmoid neuron → value
<0.5= biodegradable (0),>=0.5= non-biodegradable (1)
- If the model fails to load, the server will return a neutral value (
0.5) for predictions. - Images are resized to 150x150 pixels and normalized to [0, 1].
- Temporary image files are automatically deleted after prediction.
After at least one capture, open your browser at:
http://<server-ip>:8000/latest
You’ll see the most recent image sent by the ESP32-CAM.
Developed by: [Theophilus Adeleke]
Tech Stack: FastAPI · TensorFlow · Keras · ESP32 · WebSockets · Python
This project is licensed under the MIT License — feel free to use, modify, and distribute.