LeafMedic: Embedded Computer Vision & Machine Learning System is an end-to-end edge AI application that performs real-time plant disease classification using computer vision and machine learning, fully deployed on a Raspberry Pi. Built with Python, the system integrates camera-based image acquisition, an optimized TensorFlow Lite MobileNet model for on-device inference, and a modular software architecture designed for performance, scalability, and maintainability.
The application captures high-resolution leaf images, dynamically preprocesses them to match model requirements, and executes low-latency inference directly on embedded hardware. A responsive PyQt5 graphical interface presents confidence-ranked predictions along with structured treatment recommendations sourced from a curated disease knowledge base.
This project demonstrates practical, production-oriented engineering skills across machine learning deployment, software architecture, and edge computing, highlighting the ability to move beyond experimental notebooks and deliver robust, real-world AI systems. It reflects hands-on experience with ML model integration, hardware–software interaction, performance optimization, and user-focused application design.
- Features
- Hardware Requirements
- Software Dependencies
- Installation
- Usage
- Supported Plants & Diseases
- How It Works
- Project Structure
- Troubleshooting
- Educational Purpose
- Contributing
- License
- Acknowledgments
- Contact
- Real-time Camera Preview: Live preview from Raspberry Pi Camera Module
- Automated Disease Detection: ML-powered identification of 16 plant disease classes (90%+ accuracy)
- Treatment Recommendations: Detailed treatment and prevention information for 43 diseases
- User-Friendly GUI: PyQt5-based graphical interface
- Batch Processing: Analyze images from files
- Educational: Learn about plant diseases, computer vision, and edge AI deployment
- Fast Inference: ~145ms per image on Raspberry Pi 4
- Raspberry Pi 4 Model B (4GB recommended)
- Camera: Arducam 5MP OV5647 Camera Module V1 (or compatible)
- Display: Monitor/screen for GUI (1200x800 minimum recommended)
- Storage: 2GB free space for model and dependencies
- Power: 5V 3A USB-C power supply
- Power off Raspberry Pi
- Locate camera connector (between HDMI ports and audio jack)
- Pull up plastic clip gently
- Insert ribbon cable (blue side facing audio jack, contacts facing HDMI)
- Push clip down to secure
- Power on Raspberry Pi
- OS: Raspberry Pi OS (64-bit recommended)
- Python: 3.7 or higher
- Camera: libcamera enabled
tensorflow(2.x) - Machine learning frameworkPyQt5- GUI frameworkopencv-python- Image processingpicamera2- Camera interfacenumpy- Numerical operations
# Method 1: Using raspi-config
sudo raspi-config
# Navigate to: Interface Options → Camera → Enable
# Method 2: Edit config (already done if camera works)
sudo nano /boot/firmware/config.txt
# Ensure: camera_auto_detect=1# Update system
sudo apt update && sudo apt upgrade -y
# Install Python dependencies
pip3 install tensorflow opencv-python --break-system-packages
# Verify Picamera2 and PyQt5 (usually pre-installed)
python3 -c "import picamera2; import PyQt5; print('✓ Libraries OK')"https://github.com/mariarodr1136/LeafMedic.gitCurrent Model: The project includes the Kaggle AgriPredict Disease Classification model (12MB TFLite, trained and production-ready).
- Source: Kaggle AgriPredict Disease Classification
- Architecture: MobileNetV1 (optimized for edge devices)
- Classes: 16 disease categories across 4 crops
- Accuracy: 90%+ confidence on well-captured images
- Inference: ~145ms on Raspberry Pi 4
- Input: 300×300 RGB images (uint8, no normalization needed)
The model file is located at:
models/plant_disease_model.tflite
Plan to expand disease coverage by:
- Training on PlantVillage dataset (54,000+ images, 38 classes)
- Adding support for more crops (Apple, Grape, Potato, Strawberry, etc.)
- Creating ensemble models for improved accuracy
- Supporting multi-disease detection per image
python3 main.py- Live Preview: Camera preview appears in left panel
- Capture & Analyze: Click button to capture and analyze leaf
- View Results: See diagnosis, confidence, and treatment recommendations
- Load Image: Analyze saved images using "Load Image File" button
- Distance: 20-30 cm from leaf
- Lighting: Natural daylight or bright LED (avoid shadows)
- Focus: Single leaf, fill most of frame
- Angle: Perpendicular to leaf surface
- Background: Plain, contrasting background helps
Test individual modules:
# Test camera
python3 camera_module.py
# Test ML model
python3 ml_module.py
# Test disease database
python3 disease_database.pyThe system currently detects the following plant diseases with 90%+ accuracy:
- ✓ Healthy - No disease present
⚠️ Bacterial Spot - Dark spots with yellow halos (High severity)⚠️ Septoria Leaf Spot - Small circular spots with gray centers (High severity)⚠️ Late Blight - Devastating disease, can destroy crops rapidly (Critical severity)⚠️ Leaf Mold - Yellow spots with fuzzy growth underneath (Medium severity)⚠️ Spider Mites - Pest damage causing yellow stippling (Medium severity)⚠️ Yellow Leaf Curl Virus (TYLCV) - Viral disease spread by whiteflies (Critical severity)- (1 additional class in model)
- ✓ Healthy - No disease present
⚠️ Common Rust - Reddish-brown pustules on leaves (Medium severity)⚠️ Gray Leaf Spot (Cercospora) - Long narrow gray lesions (High severity)⚠️ Lethal Necrosis (MLN) - Devastating viral disease, no cure (Critical severity)
- ✓ Healthy - No disease present
⚠️ Frogeye Leaf Spot - Circular lesions with gray centers (Medium severity)⚠️ Downy Mildew - Yellow spots with fuzzy growth (Medium severity)
- ✓ Healthy - No disease present
⚠️ Black Rot - V-shaped yellow lesions, bacterial disease (High severity)
Total: 16 disease classes across 4 crop types
Prepared treatment information for 43 total diseases in the database. Future model versions will support:
- Apple Scab, Black Rot, Cedar Apple Rust, Healthy
- Healthy
- Powdery Mildew, Healthy
- Black Rot, Esca (Black Measles), Leaf Blight, Healthy
- Huanglongbing (Citrus Greening) - Critical
- Bacterial Spot, Healthy
- Bacterial Spot, Healthy
- Early Blight, Late Blight, Healthy
- Leaf Scorch, Healthy
- Powdery Mildew
- Healthy
Plus additional Tomato diseases (Early Blight, Target Spot, Mosaic Virus) and expanded Corn variants.
┌─────────────┐ ┌──────────────┐ ┌────────────────┐
│ Camera │─────▶│ Preprocessing│─────▶│ TFLite Model │
│ (OV5647) │ │ (300x300 RGB)│ │ (MobileNetV1) │
└─────────────┘ └──────────────┘ └────────┬───────┘
│
▼
┌─────────────┐ ┌──────────────┐ ┌────────────────┐
│ GUI │◀─────│ Treatment │◀─────│ Predictions │
│ Display │ │ Database │ │ (Top 3 + %) │
└─────────────┘ └──────────────┘ └────────────────┘
- Capture: Camera captures 2592x1944 RGB image
- Preprocessing:
- Resize to 300x300 pixels (Kaggle model requirement)
- Convert BGR to RGB color space
- Keep uint8 format (0-255) - no normalization needed
- No ImageNet normalization (model uses raw pixel values)
- Inference:
- TensorFlow Lite model processes image
- Outputs probabilities for 16 classes
- Takes ~145ms on Raspberry Pi 4
- Post-processing:
- Convert uint8 output to float probabilities
- Filter predictions above confidence threshold (30%)
- Sort by confidence (highest first)
- Return top 3 predictions
- Display:
- Show disease name and confidence percentage
- Retrieve treatment information from JSON database
- Display symptoms, treatments, and prevention methods
Current Production Model (Kaggle AgriPredict):
- Architecture: MobileNetV1 (lightweight CNN optimized for mobile/edge)
- Input: 300×300×3 RGB image (uint8, range 0-255)
- Output: 16 classes with softmax probabilities (uint8, converted to 0-1)
- Size: 12MB (unquantized)
- Dataset: AgriPredict internal dataset (proprietary)
- Training: Pre-trained on crops: Tomato, Corn, Soybean, Cabbage
- Inference Speed: ~145ms on Raspberry Pi 4 (4GB)
- Accuracy: 90%+ confidence on well-captured leaf images
Model Development Journey:
Tested multiple models during development:
-
Demo Model (Initial): 2.6MB untrained model for testing infrastructure
- ❌ Poor accuracy (<5% confidence)
- ✓ Good for validating system architecture
-
PlantAi GitHub Model: 11MB model with PyTorch-style preprocessing
- ❌ Required channels-first format [1,3,H,W]
- ❌ Required ImageNet normalization (mean/std)
- ❌ Only 6-7% confidence (undertrained)
- ✓ Demonstrated different preprocessing requirements
-
Kaggle AgriPredict Model (Final): 12MB production model
- ✓ 90% confidence - well-trained!
- ✓ Simple preprocessing (no normalization)
- ✓ Channels-last format [1,H,W,3] (TensorFlow standard)
- ✓ Fast inference (~145ms)
- ✓ Proven accuracy in production
Key Technical Lessons:
ML module (ml_module.py) now intelligently handles:
- Automatic format detection: Checks if model expects uint8 or float32
- Channels-first vs channels-last: Auto-transposes if needed [1,H,W,3] ↔ [1,3,H,W]
- ImageNet normalization: Applies mean/std normalization only for PyTorch models
- Dynamic input sizing: Supports 224×224, 300×300, or any model input size
- Output conversion: Handles uint8 outputs (0-255 → 0-1 probabilities)
21_PlantDiseaseDetection/
├── main.py # Main application (run this!)
├── camera_module.py # Camera control with Picamera2
├── ml_module.py # ML inference with TensorFlow Lite
├── gui_module.py # PyQt5 graphical interface
├── disease_database.py # Disease information management
├── download_model.py # Helper script for model setup (unused)
├── models/
│ ├── plant_disease_model.tflite # AgriPredict model (12MB, 16 classes)
│ └── labels.txt # 16 class labels (Kaggle order)
├── data/
│ └── treatments.json # Disease treatment database (43 diseases)
├── test_images/ # Sample test images (Tomato, Corn, Soybean only)
└── README.md # This file
- main.py: Entry point, coordinates all modules
- camera_module.py:
CameraControllerclass for camera operations - ml_module.py:
DiseaseDetectorclass for ML inference - gui_module.py:
PlantDiseaseGUIclass for user interface - disease_database.py:
TreatmentDatabaseclass for treatment info
Problem: "Camera not available"
# Check camera is detected
libcamera-hello --list-cameras
# Should show: ov5647 [2592x1944]
# If not detected:
sudo raspi-config # Enable camera
sudo rebootProblem: "Camera in use by another process"
# Find and kill process
sudo pkill -9 libcamera
sudo pkill -9 rpicamProblem: "Model file not found"
- Ensure
models/plant_disease_model.tfliteexists (should be 12MB Kaggle model) - The model is included in the project repository
Problem: "No module named 'tensorflow'"
pip3 install tensorflow --break-system-packages
# Note: May need to upgrade flatbuffers if you get import errors
pip3 install --upgrade flatbuffers --break-system-packagesProblem: Slow inference (>300ms)
- Current model runs at ~145ms on Pi 4, which is optimal
- Close other applications to free memory if slower
- Ensure you're using the Kaggle AgriPredict model (not an older demo model)
Problem: Low confidence predictions (<50%)
- Good news: Current model achieves 90%+ on proper images!
- Ensure you're analyzing the correct plant types (Tomato, Corn, Soybean, Cabbage only)
- See "Optimal Image Capture Tips" above for best results
Problem: GUI doesn't appear
# Ensure DISPLAY is set (if SSH)
export DISPLAY=:0
# Or run directly on Pi with monitorProblem: "cannot connect to X server"
- Must run on Pi desktop, not headless SSH
- Or use VNC with desktop forwarding
If the current model (90%+ accuracy) gives low confidence:
- Ensure good lighting (natural daylight is best)
- Capture close-up of single leaf filling most of the frame
- Check leaf has clear disease symptoms (not just aged/dying leaves)
- Verify you're analyzing supported crops: Tomato, Corn, Soybean, or Cabbage only
- Try different angle or distance (20-30cm optimal)
- Avoid blurry images - hold camera steady
If analyzing unsupported plants (Apple, Grape, Potato, etc.):
- Model will give random low-confidence predictions
- Expansion to more crops planned for future versions
- See "Future Planned Additions" section above
Problem: "Out of memory" errors
# Check available memory
free -h
# Close other applications
# Use lighter model (INT8 quantized)This project is designed for educational purposes to demonstrate:
- Raspberry Pi hardware integration
- Computer vision with OpenCV
- Machine learning with TensorFlow Lite
- GUI development with PyQt5
- Modular Python programming
- Edge AI deployment
- Hardware: Camera interfacing, GPIO basics
- Software: Python OOP, threading, event handling
- AI/ML: Image preprocessing, model inference, confidence thresholds
- Data: JSON databases, structured information retrieval
- UX: GUI design, user feedback, error handling
- This is a learning project, not a professional diagnostic tool
- Current model limited to 4 crops (Tomato, Corn, Soybean, Cabbage) with 16 disease classes
- Predictions achieve 90%+ accuracy on well-captured images, but:
- Should NOT replace expert agricultural advice
- False positives/negatives can occur
- Some diseases look similar and may be confused
- For real crop management decisions, consult agricultural extension services or plant pathologists
- Works best with clear, well-lit leaf images showing obvious symptoms
- Cannot detect multiple diseases on single leaf (picks highest confidence)
- Only detects diseases in training set (won't identify novel diseases)
Phase 1: Demo Model (2.6MB)
- Purpose: Test system architecture
- Result: <5% confidence (untrained model)
- Lesson: Infrastructure worked, but model quality is crucial
Phase 2: PlantAi GitHub Model (11MB)
- Challenges:
- Required channels-first format: [1, 3, 224, 224] (PyTorch style)
- Needed ImageNet normalization (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
- Dimension mismatch errors initially
- Solutions:
- Added automatic transposition: [1,H,W,3] → [1,3,H,W]
- Implemented ImageNet normalization
- Result: Only 6-7% confidence (model undertrained)
- Lesson: Preprocessing must match training exactly
Phase 3: Kaggle AgriPredict Model (12MB) - PRODUCTION
- Specifications:
- Input: 300×300 uint8 (no normalization!)
- Output: uint8 probabilities (need /255.0 conversion)
- Channels-last format (TensorFlow standard)
- Result: 90.2% confidence - SUCCESS!
- Lesson: Production-quality trained models make all the difference
The ml_module.py evolved to handle multiple model formats:
# Key features added:
1. Automatic dtype detection (uint8 vs float32)
2. Conditional ImageNet normalization
3. Dynamic channels transposition
4. Flexible input sizing (224x224, 300x300, etc.)
5. Output format conversiontreatments.json Evolution:
- Started with 38 PlantVillage diseases
- Expanded to 43 diseases to include:
- Soybean Frogeye Leaf Spot
- Soybean Downy Mildew
- Corn Lethal Necrosis (Critical severity)
- Cabbage Healthy
- Cabbage Black Rot
Each entry includes:
- Common name and scientific name
- Detailed symptoms list
- Treatment recommendations
- Prevention strategies
- Severity rating (none, medium, high, critical)
Inference Speed:
- Current: ~145ms per image
- Well within acceptable range for interactive use
- Camera warm-up: 2 seconds on startup
- GUI stays responsive (QThread for non-blocking inference)
Memory Usage:
- Model: 12MB
- TensorFlow runtime: ~200-300MB
- GUI application: ~100-150MB
- Camera buffers: ~50MB
- Total: ~400-500MB (well within Pi 4 4GB capacity)
-
Always verify model preprocessing requirements
- Check: uint8 vs float32
- Check: Channels-first vs channels-last
- Check: Normalization method (raw, [0-1], ImageNet)
- Check: Input dimensions
-
Model quality matters more than code optimization
- 90% trained model >>> perfectly optimized preprocessing pipeline for 5% model
-
Label order must match training exactly
- Even with correct preprocessing, wrong labels = wrong diagnosis
- Always verify against model documentation
-
Test with real data early
- Demo models don't reveal preprocessing bugs
- Real confidence scores reveal training quality
-
Modular design enables experimentation
- Separate camera, ML, GUI, database modules
- Easy to swap models and test different approaches
- Each module testable independently
This is an educational project. Contributions welcome:
- Improved models with higher accuracy
- Additional plant species and diseases
- Better treatment recommendations
- GUI improvements
- Bug fixes and optimizations
Educational Use Only
This project is part of the SunFounder Raspberry Pi Electronic Kit and is intended for educational purposes. The code is provided as-is for learning computer vision and machine learning concepts.
Model & Data:
- Kaggle AgriPredict: Disease Classification model
- AgriPredict internal dataset (proprietary, 16 classes)
- PlantVillage dataset: CC0 (Public Domain) - for future expansion
- Treatment information: Compiled from public agricultural resources
Dependencies:
- TensorFlow: Apache 2.0 License
- PyQt5: GPL v3
- OpenCV: Apache 2.0 License
- AgriPredict: Disease Classification TFLite model on Kaggle
- PlantVillage: Dataset for future model training (54,000+ images)
- SunFounder: Electronic kit and educational platform
- TensorFlow: ML framework and TFLite runtime
- Raspberry Pi Foundation: Hardware platform and Picamera2 library
- PyQt5: GUI framework for desktop applications
If you have any questions or feedback, feel free to reach out at mrodr.contact@gmail.com.
Educational Project - Learn, Experiment, Innovate

