A real-time rice quality detection system using ESP32-CAM and YOLOv8 object detection. The system captures images of rice grains, sends them to a Flask server for AI-powered quality analysis, and activates a relay to automatically sort defective grains.
- Real-time Detection: Continuous monitoring and classification of rice grains
- AI-Powered Analysis: YOLOv8 model trained to detect 5 types of rice defects
- Automated Sorting: Relay-controlled mechanism to separate bad rice
- Web Monitoring: WebSerial interface for remote monitoring and debugging
- Low Latency: Optimized for fast inference and response times
The system can identify the following rice quality issues:
- Damaged - Physically damaged grains
- Discolored - Color abnormalities
- Broken - Fractured or incomplete grains
- Chalky - Opaque or chalky appearance
- Organic Foreign Matters - Non-rice contaminants
- ESP32-CAM (AI-Thinker module)
- Relay Module (for sorting mechanism)
- Power Supply (5V for ESP32-CAM)
- USB-to-Serial Adapter (for programming)
- Optional: Flash LED (built-in on GPIO 4)
Project-ESPCAM/
├── esp32_cam_firmware/ # ESP32-CAM Arduino code
│ ├── rice_detection_esp32cam.ino
│ ├── config.h.template # Configuration template
│ └── config.h # Your actual config (gitignored)
├── server/ # Flask API server
│ ├── app.py
│ └── requirements.txt
├── models/ # Trained YOLOv8 models
│ └── best.pt
├── dataset/ # Training dataset
│ └── rice_object_detection.v1i.yolov8/
├── docs/ # Documentation
│ ├── HARDWARE_SETUP.md
│ ├── DEPLOYMENT.md
│ └── API_DOCUMENTATION.md
└── README.md
# Navigate to server directory
cd server
# Install dependencies
pip install -r requirements.txt
# Run the Flask server
python app.pyThe server will start on http://0.0.0.0:5000
-
Configure WiFi and Server:
cd esp32_cam_firmware cp config.h.template config.h -
Edit
config.hwith your settings:const char *WIFI_SSID = "YourWiFiName"; const char *WIFI_PASSWORD = "YourPassword"; const char* SERVER_URL = "http://YOUR_SERVER_IP:5000/predict";
-
Upload to ESP32-CAM:
- Open
rice_detection_esp32cam.inoin Arduino IDE - Select Board: "AI Thinker ESP32-CAM"
- Select Port: Your USB-to-Serial adapter port
- Click Upload
- Open
-
Monitor via WebSerial:
- After upload, connect ESP32-CAM to power
- Open Serial Monitor to see the IP address
- Navigate to
http://ESP32_IP/webserialfor web-based monitoring
- Hardware Setup Guide - Detailed wiring and assembly instructions
- Deployment Guide - Server deployment options (local, cloud)
- API Documentation - Flask API endpoints and usage
Edit esp32_cam_firmware/config.h:
| Parameter | Description | Default |
|---|---|---|
WIFI_SSID |
WiFi network name | - |
WIFI_PASSWORD |
WiFi password | - |
SERVER_URL |
Flask server endpoint | http://SERVER_IP:5000/predict |
RELAY_GPIO_NUM |
GPIO pin for relay | 13 |
RELAY_ACTIVATION_DURATION_MS |
Relay on-time (ms) | 1000 |
Edit server/app.py:
| Parameter | Description | Default |
|---|---|---|
MODEL_PATH |
Path to YOLOv8 model | ../models/best.pt |
BAD_RICE_LABELS |
List of defect classes | See code |
host |
Server host | 0.0.0.0 |
port |
Server port | 5000 |
- Image Capture: ESP32-CAM captures an image of rice grains
- Transmission: Image is sent via HTTP POST to the Flask server
- AI Analysis: YOLOv8 model processes the image and detects defects
- Response: Server returns JSON with detection results and
bad_rice_detectedflag - Sorting: If bad rice is detected, ESP32-CAM activates the relay to trigger sorting mechanism
- Repeat: Process continues every 10 seconds
- Image Capture: ~50-150ms
- Network Transmission: Varies by connection
- Model Inference: ~100-500ms (depends on server hardware)
- Total Cycle: ~10 seconds (configurable)
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License - see the LICENSE file for details.
Copyright (c) 2026 Atanda Isaac-Great Peace and Ilori Joshua Ayomide
For commercial use or derivative works, please contact: [email protected]
- Dataset provided by Roboflow Universe
- YOLOv8 by Ultralytics
- ESP32-CAM community and resources
For questions or issues:
- Open an issue on GitHub
- Check the documentation
- Review existing issues and discussions
Made with ❤️ for agricultural quality control