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.
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)
cd plant_disease_app# Windows
python -m venv venv
venv\Scripts\activate
# Mac/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtpython app.pyOpen http://localhost:5000 in your browser. The app runs in Demo Mode (simulated predictions) until you train a model.
- Go to: https://www.kaggle.com/datasets/emmarex/plantdisease
- Download and extract to:
data/PlantVillage/ - Structure should be:
data/PlantVillage/Apple___Apple_scab/image001.jpgetc.
python model/train_model.pyThis will:
- β Load and augment the PlantVillage dataset
- β Build MobileNetV2 transfer learning model
- β Phase 1: Train new top layers (base frozen)
- β Phase 2: Fine-tune top 30 layers
- β
Save best model to
model/plant_disease_model.h5 - β
Save class labels to
model/class_labels.json - β Plot training accuracy/loss curves
Training time: ~30β60 min on GPU, ~2β4 hours on CPU
python app.pyThe badge in the header will change to "Model Ready Β· 38 Classes"
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": [...]
}
}List all 38+ supported disease classes.
Check server and model status.
| 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 |
gunicorn -w 4 -b 0.0.0.0:5000 app:appFROM 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"]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)
| Metric | Value |
|---|---|
| PlantVillage Dataset | ~54,000 images |
| Classes | 38 |
| Validation Accuracy | ~96% |
| Inference Time | < 100ms |
| Model Size | ~14 MB |
- Image quality: Use clear, well-lit photos
- Leaf focus: Zoom in on the affected leaf area
- Single leaf: One leaf per photo works best
- Natural light: Avoid flash or harsh shadows
- Both surfaces: Try photographing leaf underside if top looks healthy
For educational and agricultural research use. PlantVillage dataset: CC BY 4.0 License.