Skip to content

DeeThunder/Rice-object-detection-esp32-cam

Repository files navigation

Rice Object Detection with ESP32-CAM

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.

🌾 Features

  • 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

🔍 Detected Rice Defects

The system can identify the following rice quality issues:

  1. Damaged - Physically damaged grains
  2. Discolored - Color abnormalities
  3. Broken - Fractured or incomplete grains
  4. Chalky - Opaque or chalky appearance
  5. Organic Foreign Matters - Non-rice contaminants

🛠️ Hardware Requirements

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

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

🚀 Quick Start

1. Server Setup

# Navigate to server directory
cd server

# Install dependencies
pip install -r requirements.txt

# Run the Flask server
python app.py

The server will start on http://0.0.0.0:5000

2. ESP32-CAM Setup

  1. Configure WiFi and Server:

    cd esp32_cam_firmware
    cp config.h.template config.h
  2. Edit config.h with your settings:

    const char *WIFI_SSID = "YourWiFiName";
    const char *WIFI_PASSWORD = "YourPassword";
    const char* SERVER_URL = "http://YOUR_SERVER_IP:5000/predict";
  3. Upload to ESP32-CAM:

    • Open rice_detection_esp32cam.ino in Arduino IDE
    • Select Board: "AI Thinker ESP32-CAM"
    • Select Port: Your USB-to-Serial adapter port
    • Click Upload
  4. Monitor via WebSerial:

    • After upload, connect ESP32-CAM to power
    • Open Serial Monitor to see the IP address
    • Navigate to http://ESP32_IP/webserial for web-based monitoring

📖 Documentation

🔧 Configuration

ESP32-CAM Settings

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

Server Settings

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

🧪 How It Works

  1. Image Capture: ESP32-CAM captures an image of rice grains
  2. Transmission: Image is sent via HTTP POST to the Flask server
  3. AI Analysis: YOLOv8 model processes the image and detects defects
  4. Response: Server returns JSON with detection results and bad_rice_detected flag
  5. Sorting: If bad rice is detected, ESP32-CAM activates the relay to trigger sorting mechanism
  6. Repeat: Process continues every 10 seconds

📊 Performance

  • Image Capture: ~50-150ms
  • Network Transmission: Varies by connection
  • Model Inference: ~100-500ms (depends on server hardware)
  • Total Cycle: ~10 seconds (configurable)

🤝 Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

📄 License

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]

🙏 Acknowledgments

📞 Support

For questions or issues:

  • Open an issue on GitHub
  • Check the documentation
  • Review existing issues and discussions

Made with ❤️ for agricultural quality control

About

A real-time rice quality detection system using ESP32-CAM and YOLOv8 object detection. Automatically analyzes and sorts defective rice grains using AI-powered computer vision.

Topics

Resources

License

Stars

Watchers

Forks

Contributors