A modern, production-ready plant disease detection application that uses deep learning to identify plant diseases from leaf images. Built with FastAPI backend and React TypeScript frontend, this system can detect diseases across multiple plant species including Apple, Corn, Grape, Potato, Tomato, Mango, Citrus, and Rice.
- π― AI-Powered Detection: Deep learning model trained on 30+ plant disease classes
- πΌοΈ Image Upload: Drag-and-drop interface for easy image submission
- β‘ Fast Predictions: Real-time disease classification with confidence scores
- π Multiple Model Support: TensorFlow, PyTorch, and ONNX inference adapters
- π Modern Web Interface: React + TypeScript frontend with responsive design
- π Production Ready: FastAPI backend with comprehensive testing and monitoring
- βοΈ Cloud Deployment: Ready for deployment on Render (backend) and Vercel (frontend)
- π Secure: Built-in rate limiting, CORS protection, and input validation
Plant Disease Detection/
βββ π PlantDiseaseDetection.ipynb # Model training notebook
βββ π backend/ # FastAPI backend application
β βββ app/ # Main application code
β βββ models/ # Trained ML models
β βββ tests/ # Backend tests
βββ π frontend/ # React TypeScript frontend
β βββ src/ # Frontend source code
β βββ public/ # Static assets
βββ π€ ML_Model/ # Model artifacts and test images
βββ π scripts/ # Utility scripts
The model can detect 30+ diseases across multiple plant species:
- Apple Scab, Black Rot, Cedar Apple Rust, Healthy
- Cercospora Leaf Spot, Common Rust, Northern Leaf Blight, Healthy
- Black Rot, Esca (Black Measles), Leaf Blight, Healthy
- Early Blight, Late Blight, Healthy
- Bacterial Spot, Early Blight, Late Blight, Leaf Mold, Septoria Leaf Spot, Spider Mites, Target Spot, Yellow Leaf Curl Virus, Mosaic Virus, Healthy
- Mango Anthracnose, Citrus Canker, Rice Blast
- React 18 with TypeScript
- Vite for blazing fast development
- Tailwind CSS for styling
- React Router for navigation
- TanStack Query for data fetching
- React Dropzone for file uploads
- Lucide Icons for UI icons
- FastAPI for high-performance API
- Pydantic for data validation
- Uvicorn/Gunicorn for ASGI serving
- Redis for caching and rate limiting
- Pillow for image processing
- Multi-model support (TensorFlow, PyTorch, ONNX)
- Python 3.11+
- Node.js 18+ and npm
- Git
- 4GB RAM minimum (8GB recommended)
git clone https://github.com/pkparthk/Plant-Disease-Detection.git
cd "Plant Disease Detection"cd backend
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Start the API server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000cd frontend
# Install dependencies
npm install
# Start development server
npm run dev- Frontend: http://localhost:5173 (Vite dev server)
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
- Open http://localhost:5173 in your browser
- Upload a plant leaf image using drag-and-drop
- View AI prediction results with confidence scores
The PlantDiseaseDetection.ipynb notebook contains the complete model training pipeline:
# Install Jupyter if not already installed
pip install jupyter notebook
# Launch the training notebook
jupyter notebook PlantDiseaseDetection.ipynbNotebook Contents:
- π Data loading and preprocessing with augmentation
- ποΈ CNN model architecture with transfer learning
- π― Training loop with validation monitoring
- πΎ Model export to multiple formats (H5, Keras, SavedModel)
- π Performance evaluation and visualization
Additional utilities in scripts/ directory:
# Enhanced model creation
python scripts/create_improved_model.py
# Quick model evaluation
python scripts/quick_eval.py
# Dataset expansion tools
python scripts/expand_diseases.py
# Comprehensive evaluation
python scripts/evaluate.pycd backend
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
pip install -r requirements-deploy.txt
# Start development server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000cd frontend
# Install dependencies
npm install
# Start development server
npm run devcd frontend
# Build
npm run buildCreate a .env file in the backend directory:
# backend/.env
ENVIRONMENT=development
LOG_LEVEL=debug
MODEL_PATH=models/best_model.h5
CORS_ORIGINS=http://localhost:5173, http://localhost:3000
MAX_FILE_SIZE=10485760 # 10MB in bytes
RATE_LIMIT_REQUESTS=100
RATE_LIMIT_WINDOW=60The application supports multiple ML frameworks through adapters:
- TensorFlow:
.h5,.keras, SavedModel directory - PyTorch:
.pth,.pt(coming soon) - ONNX:
.onnx(coming soon)
backend/models/
βββ best_model.h5 # Main TensorFlow model
βββ class_mapping.json # Class index mappings
βββ class_names.txt # Human-readable labels
βββ tensorflow_model/ # SavedModel format
βββ saved_model.pb
βββ assets/
βββ variables/The backend is configured for deployment on Render using render.yaml:
- Fork this repository to your GitHub account
- Connect your GitHub account to Render
- Create a new Web Service from your forked repository
- Render will automatically deploy using the configuration in
render.yaml
Environment Variables for Render:
ENVIRONMENT: productionMODEL_PATH: models/best_model.h5CORS_ORIGINS: Your frontend URLLOG_LEVEL: info
The frontend is configured for deployment on Vercel using vercel.json:
- Install Vercel CLI:
npm install -g vercel - Navigate to frontend directory:
cd frontend - Deploy:
vercel --prod
Alternatively, connect your GitHub repository to Vercel dashboard for automatic deployments.
- Default: Core services (frontend, backend, redis)
- database: Add PostgreSQL for data persistence
- monitoring: Add Prometheus and Grafana
- production: Production-optimized settings
The API provides health monitoring endpoints:
# Check application health
curl http://localhost:8000/api/health
# Get model information
curl http://localhost:8000/model/info- Prediction Latency: ~100-500ms (CPU), ~50-200ms (GPU)
- Supported Image Formats: JPG, PNG, JPEG
- Max Image Size: 10MB
- Model Size: ~85MB (TensorFlow H5 format)
- Classes Supported: 30+ plant disease types
The application includes structured logging:
- Request/response logging
- Error tracking with stack traces
- Performance timing
- Model prediction confidence scores
- File type verification (images only)
- File size limits (10MB maximum)
- Image format validation
- Content-type verification
- CORS configuration for cross-origin requests
- Rate limiting (configurable)
- Input sanitization
- Error message sanitization
Backend Tests:
cd backend
# Run all tests
pytestFrontend Tests:
cd frontend
# Run all tests
npm test
# Run with coverage
npm run test:coveragebackend/tests/- Backend API and model testsfrontend/src/__tests__/- Frontend component tests
- Fork the repository
- Create feature branch:
git checkout -b feature/amazing-feature - Make changes and add tests
- Ensure all tests pass:
npm test && pytest - Commit with conventional commits:
git commit -m "feat: add amazing feature" - Push to branch:
git push origin feature/amazing-feature - Create Pull Request
- TypeScript for frontend
- Python type hints for backend
- 100% test coverage for new features
- ESLint/Prettier for frontend formatting
- Black/isort for Python formatting
feat:New featuresfix:Bug fixesdocs:Documentationtest:Testingrefactor:Code refactoringperf:Performance improvements
GET /healthReturns application health status.
GET /model/infoReturns model metadata and supported classes.
POST /predict
Content-Type: multipart/form-data
{
"file": <image_file>
}Returns prediction results with confidence scores and treatment recommendations.
{
"predictions": [
{
"class": "Apple___healthy",
"confidence": 0.95,
"treatment": "No treatment needed - plant is healthy!"
}
],
"top_prediction": "Apple___healthy",
"confidence": 0.95,
"processing_time": 0.123
}# Check if model files exist
ls backend/models/
# Verify TensorFlow model
python -c "import tensorflow as tf; model = tf.keras.models.load_model('backend/models/best_model.h5'); print('Model loaded successfully')"
# Check labels file
cat backend/labels.json# Check backend logs
cd backend
uvicorn app.main:app --reload --log-level debug
# Test API directly
curl http://localhost:8000/api/health
curl http://localhost:8000/model/info# Check frontend console for errors
cd frontend
npm run dev
# Build for production
npm run build- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- Issues: GitHub Issues
- Repository: GitHub Repository
This project is licensed under the MIT License - see the LICENSE file for details.
Made with β€οΈ for plant health monitoring By Parth Kothari