An AI-driven web application for real-time crop disease detection and fertilizer recommendations using deep learning and large language models.
This system detects plant diseases from leaf images using a ResNet50 CNN model and generates actionable fertilizer recommendations using the Gemini Large Language Model (LLM).
It is designed to be lightweight, fast, and usable directly by farmers via a mobile-friendly web interface.
Crop Health Diagnosis & Fertilizer Recommendation System is an intelligent agricultural solution that combines the power of deep learning computer vision with Google's advanced AI language models. The system enables farmers to instantly diagnose crop diseases by simply uploading a photo of a plant leaf.
Using a fine-tuned ResNet50 convolutional neural network, the application accurately identifies 38 different plant diseases across multiple crop types (tomatoes, apples, corn, grapes, etc.) with 97.86% accuracy. Once a disease is detected, Google's Gemini AI generates personalized, region-specific fertilizer recommendations and treatment plans in natural language, making expert agricultural advice accessible to farmers without requiring specialized knowledge.
The solution is designed with privacy and simplicity in mind—no user accounts, no data storage, and all processing happens in-memory. The lightweight architecture ensures fast inference times (200-400ms) even on standard hardware, making it practical for real-world deployment in agricultural communities.
- Image-based plant disease detection (38 disease classes)
- Deep Learning using ResNet50 (TensorFlow/Keras)
- Fertilizer recommendations via Gemini LLM
- Real-time inference (200–400 ms on CPU)
- Flask-based REST API
- No authentication, no database, no data storage
- Privacy-first, in-memory image processing
- Robust label mapping via
class_names.json
- Python
- Flask
- TensorFlow / Keras
- ResNet50 (Transfer Learning)
- Gemini LLM
- HTML, TailwindCSS, JavaScript
- Client: Image upload + result display (Web UI)
- Backend: Flask API for preprocessing, inference & LLM calls
- Model Layer: Fine-tuned ResNet50 CNN
No database or persistent storage is used.
Crop-Health-AI/
├── app.py # Flask application & API endpoints
├── class_names.json # Class index → label mapping (must match model output order)
├── plant_disease_model.h5 # Pre-trained ResNet50 model
├── wsgi.py # Production entrypoint (Waitress)
├── requirements.txt # Python dependencies
├── requirements-dev.txt # Dev-only deps (tests)
├── templates/ # HTML templates
│ ├── index.html # Main UI
│ ├── library.html # Disease library
│ └── vision_checker.html # Vision checker page
├── static/ # Static assets (CSS, JS)
├── assets/ # Image assets
└── .env # Environment variables (create this)
- Dataset: PlantVillage
- Input size: 224 × 224
- Output: Multi-class disease prediction
- Activation: Softmax
| Model | Accuracy |
|---|---|
| EfficientNetB0 | 97.16% |
| ResNet50 | 97.86% |
| Custom CNN | 95.84% |
| MobileNetV2 | 93.80% |
ResNet50 was selected for deployment due to accuracy–speed balance.
This repo expects the model output index mapping to match class_names.json.
- Train ResNet50 + write matching
class_names.json:python train_resnet50_model.py
- Baseline custom CNN trainer (not ResNet50):
python train_vision_model.py
Note: model weights (*.h5) and the dataset folder are intentionally ignored by git via .gitignore.
- Generated using Gemini LLM
- Input: Crop type + predicted disease
- Output: Fertilizers, dosages, and preventive steps
- Human-readable, no rule-based logic
- User uploads a leaf image
- Image sent to
/predict_diseaseendpoint - Image preprocessing (resize, normalize)
- Disease prediction via ResNet50
- Fertilizer advice generated by Gemini LLM
- Response returned as JSON
- Image discarded from memory
- Python 3.9 or higher
- Gemini API key (for fertilizer recommendations)
git clone https://github.com/sohamx0/Crop-Health-AI.git
cd Crop-Health-AIpip install -r requirements.txt- Get your Gemini API key from Google AI Studio
- Create a
.envfile in the root directory:GEMINI_API_KEY=your_actual_api_key_here
- The
.envfile is already in.gitignore, so your API key won't be committed to GitHub.
Note: Disease detection will work without the API key, but fertilizer recommendations require it.
python app.pyThe application will start on http://127.0.0.1:5000/
Open in your browser:
http://127.0.0.1:5000/
POST /predict_disease- Upload image and get disease prediction + fertilizer recommendationGET /- Main application interfaceGET /library- Disease library with sample imagesGET /healthz- Health check (model/class names/API key status)
GEMINI_API_KEY(optional): enables chat + image validation + fertilizer recommendationsDISEASE_MODEL_PATH(optional): defaults toplant_disease_model.h5CLASS_NAMES_PATH(optional): defaults toclass_names.jsonMAX_CONTENT_LENGTH(optional): max upload size in bytes (default 8MB)FLASK_DEBUG(optional): set to1for debug mode (default off)
For a production-like server on Windows, use Waitress:
pip install -r requirements.txt
waitress-serve --listen=0.0.0.0:5000 wsgi:app- Weather & soil sensor integration
- Offline inference mode
- Multilingual native UI
- IoT-based monitoring
- Region-specific retraining
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License