Skip to content

suyXcode/Plant-Disease-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌿 LeafScan AI β€” Plant Disease Detection App

A full-stack plant disease detection application using MobileNetV2 Transfer Learning, Flask, and a beautiful web UI. Upload a leaf photo and get instant disease diagnosis, confidence scores, and treatment advice.


πŸ“ Project Structure

plant_disease_app/
β”‚
β”œβ”€β”€ app.py                    # Flask web app & API endpoints
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ README.md                 # This file
β”‚
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ train_model.py        # Model training script (MobileNetV2)
β”‚   β”œβ”€β”€ plant_disease_model.h5 # Trained model (generated after training)
β”‚   └── class_labels.json     # Class index β†’ name mapping
β”‚
β”œβ”€β”€ utils/
β”‚   └── predictor.py          # Image preprocessing + prediction + disease DB
β”‚
β”œβ”€β”€ templates/
β”‚   └── index.html            # Beautiful single-page web UI
β”‚
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ uploads/              # Temporary uploaded images
β”‚   └── css/ js/              # (Optional) extra assets
β”‚
└── data/
    └── PlantVillage/         # Dataset folder (you download this)
        β”œβ”€β”€ Apple___Apple_scab/
        β”œβ”€β”€ Tomato___Early_blight/
        └── ... (38 folders)

⚑ Quick Start (5 minutes)

Step 1 β€” Clone / Download the project

cd plant_disease_app

Step 2 β€” Create a virtual environment

# Windows
python -m venv venv
venv\Scripts\activate

# Mac/Linux
python3 -m venv venv
source venv/bin/activate

Step 3 β€” Install dependencies

pip install -r requirements.txt

Step 4 β€” Run the app (Demo Mode)

python app.py

Open http://localhost:5000 in your browser. The app runs in Demo Mode (simulated predictions) until you train a model.


🧠 Training Your Own Model

Step 1 β€” Download the PlantVillage Dataset

Step 2 β€” Train the model

python model/train_model.py

This will:

  1. βœ… Load and augment the PlantVillage dataset
  2. βœ… Build MobileNetV2 transfer learning model
  3. βœ… Phase 1: Train new top layers (base frozen)
  4. βœ… Phase 2: Fine-tune top 30 layers
  5. βœ… Save best model to model/plant_disease_model.h5
  6. βœ… Save class labels to model/class_labels.json
  7. βœ… Plot training accuracy/loss curves

Training time: ~30–60 min on GPU, ~2–4 hours on CPU

Step 3 β€” Restart the Flask app

python app.py

The badge in the header will change to "Model Ready Β· 38 Classes"


πŸ”Œ API Reference

POST /api/predict

Upload a leaf image and get disease prediction.

File upload:

curl -X POST http://localhost:5000/api/predict \
  -F "image=@leaf.jpg"

Base64:

curl -X POST http://localhost:5000/api/predict \
  -H "Content-Type: application/json" \
  -d '{"image_base64": "data:image/jpeg;base64,/9j/4AAQ..."}'

Response:

{
  "success": true,
  "result": {
    "display_name": "Tomato Early Blight",
    "confidence": 0.9234,
    "confidence_pct": "92.3%",
    "is_healthy": false,
    "disease_info": {
      "type": "Fungal",
      "severity": "Moderate",
      "description": "...",
      "symptoms": ["..."],
      "treatment": ["..."],
      "prevention": "...",
      "farmer_tip": "..."
    },
    "top3": [...]
  }
}

GET /api/diseases

List all 38+ supported disease classes.

GET /api/health

Check server and model status.


🌱 Supported Plants & Diseases

Plant Diseases Supported
🍎 Apple Apple Scab, Black Rot, Cedar Apple Rust, Healthy
πŸ… Tomato Early Blight, Late Blight, Bacterial Spot, Septoria, Spider Mites, Target Spot, Leaf Miner, Yellow Leaf Curl Virus, Mosaic Virus, Healthy
πŸ₯” Potato Early Blight, Late Blight, Healthy
🌽 Corn Common Rust, Northern Leaf Blight, Healthy
πŸ‡ Grape Black Rot, Healthy
+ more 38 classes total from PlantVillage

πŸš€ Deploying to Production

Using Gunicorn (Linux/Mac)

gunicorn -w 4 -b 0.0.0.0:5000 app:app

Using Docker

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "-w", "2", "-b", "0.0.0.0:5000", "app:app"]

🎯 Model Architecture

Input (224Γ—224Γ—3)
    ↓
MobileNetV2 (pretrained ImageNet, ~2.2M params)
    ↓
GlobalAveragePooling2D
    ↓
Dense(256, ReLU)
    ↓
Dropout(0.5)
    ↓
Dense(38, Softmax)  ← Number of disease classes

Transfer Learning Strategy:

  • Phase 1: Freeze MobileNetV2, train only top layers (10 epochs)
  • Phase 2: Unfreeze last 30 layers of MobileNetV2, fine-tune with low LR (20 epochs)

πŸ“Š Expected Performance

Metric Value
PlantVillage Dataset ~54,000 images
Classes 38
Validation Accuracy ~96%
Inference Time < 100ms
Model Size ~14 MB

πŸ’‘ Tips for Better Results

  1. Image quality: Use clear, well-lit photos
  2. Leaf focus: Zoom in on the affected leaf area
  3. Single leaf: One leaf per photo works best
  4. Natural light: Avoid flash or harsh shadows
  5. Both surfaces: Try photographing leaf underside if top looks healthy

πŸ“ License

For educational and agricultural research use. PlantVillage dataset: CC BY 4.0 License.

Plant-Disease-Detection

About

A full-stack plant disease detection application using MobileNetV2 Transfer Learning, Flask, and a beautiful web UI. Upload a leaf photo and get instant disease diagnosis, confidence scores, and treatment advice.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors