Skip to content

sohamx0/Crop-Health-AI

Repository files navigation

🌱 Crop Health Diagnosis & Fertilizer Recommendation System

An AI-driven web application for real-time crop disease detection and fertilizer recommendations using deep learning and large language models.

📌 Overview

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.

📝 Brief Description

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.

🚀 Features

  • 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

🧠 Tech Stack

  • Python
  • Flask
  • TensorFlow / Keras
  • ResNet50 (Transfer Learning)
  • Gemini LLM
  • HTML, TailwindCSS, JavaScript

🏗 System Architecture

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

Project Structure

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)

🔬 Model Details

  • Dataset: PlantVillage
  • Input size: 224 × 224
  • Output: Multi-class disease prediction
  • Activation: Softmax

Model Accuracy Comparison

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.

🏋️ Training (Optional)

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.

🌾 Fertilizer Recommendation

  • Generated using Gemini LLM
  • Input: Crop type + predicted disease
  • Output: Fertilizers, dosages, and preventive steps
  • Human-readable, no rule-based logic

🔁 Workflow

  1. User uploads a leaf image
  2. Image sent to /predict_disease endpoint
  3. Image preprocessing (resize, normalize)
  4. Disease prediction via ResNet50
  5. Fertilizer advice generated by Gemini LLM
  6. Response returned as JSON
  7. Image discarded from memory

⚙️ Installation & Setup

Prerequisites

  • Python 3.9 or higher
  • Gemini API key (for fertilizer recommendations)

Step 1: Clone the Repository

git clone https://github.com/sohamx0/Crop-Health-AI.git
cd Crop-Health-AI

Step 2: Install Dependencies

pip install -r requirements.txt

Step 3: Configure Gemini API Key

  1. Get your Gemini API key from Google AI Studio
  2. Create a .env file in the root directory:
    GEMINI_API_KEY=your_actual_api_key_here
  3. The .env file 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.

Step 4: Run the Application

python app.py

The application will start on http://127.0.0.1:5000/

Open in your browser:

http://127.0.0.1:5000/

📡 API Endpoints

  • POST /predict_disease - Upload image and get disease prediction + fertilizer recommendation
  • GET / - Main application interface
  • GET /library - Disease library with sample images
  • GET /healthz - Health check (model/class names/API key status)

🔧 Configuration (Environment Variables)

  • GEMINI_API_KEY (optional): enables chat + image validation + fertilizer recommendations
  • DISEASE_MODEL_PATH (optional): defaults to plant_disease_model.h5
  • CLASS_NAMES_PATH (optional): defaults to class_names.json
  • MAX_CONTENT_LENGTH (optional): max upload size in bytes (default 8MB)
  • FLASK_DEBUG (optional): set to 1 for debug mode (default off)

🏭 Production Run

For a production-like server on Windows, use Waitress:

pip install -r requirements.txt
waitress-serve --listen=0.0.0.0:5000 wsgi:app

🛣 Future Scope

  • Weather & soil sensor integration
  • Offline inference mode
  • Multilingual native UI
  • IoT-based monitoring
  • Region-specific retraining

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📜 License

MIT License

👤 Author

sohamx0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published