Skip to content

Robotron2/wasteAI---ML-FastAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart Waste Classification System (FastAPI + ESP32)

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).


Features

  • 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.

System Architecture

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.


Requirements

Python dependencies

  • 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
      

Endpoints

HTTP Endpoints

Method Endpoint Description
GET /health Checks if the server is running.
GET /latest Returns the latest image captured by the ESP32-CAM.

WebSocket Endpoints

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.

Data Flow

  1. ESP32 detects an object and sends "object_detected"/ws/sensor.
  2. Server tells ESP32-CAM to "capture"/ws/image.
  3. ESP32-CAM sends the image as binary data → /ws/image.
  4. Server saves the image (latest.jpg) and classifies it using tf_model.h5.
  5. Server sends classification result ("0" or "1") → /ws/sensor.
  6. ESP32 actuates servo or performs next action.
  7. Server instructs ESP32 to "resume_sensing" after processing.

Example Console Logs

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).

Model Integration

  • The ML model file tf_model.h5 should be placed inside the /model directory.
  • Input shape: (150, 150, 3)
  • Output: A single sigmoid neuron → value <0.5 = biodegradable (0), >=0.5 = non-biodegradable (1)

Notes

  • 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.

Viewing Latest Captured Image

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.


🧑‍💻 Author

Developed by: [Theophilus Adeleke]

Tech Stack: FastAPI · TensorFlow · Keras · ESP32 · WebSockets · Python


📜 License

This project is licensed under the MIT License — feel free to use, modify, and distribute.


About

A fastAPI server with websocket capabilities. It runs the ML model that classifies images into biodegradable and non-biodegradable classes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors