title: Potato_Diseases_Detection_with_Deep_Learning
emoji: 🐨
colorFrom: red
colorTo: blue
sdk: docker
pinned: true
license: apache-2.0
thumbnail: >-
https://cdn-uploads.huggingface.co/production/uploads/68650ad8b3fa513b48afad7f/2dUQzI09CvADaIJX_pPeE.png
🥔 Potato Skin Disease Detection Using Deep Learning
🔬 An AI-powered computer vision system for detecting and classifying potato skin diseases using deep learning techniques.
📋 Table of Contents
- 🎯 Project Overview
- 🌟 Features
- 📊 Dataset
- 🚀 Getting Started
- 💻 Usage
- 🏗️ Model Architecture
- 📈 Results
- 🚀 Next Steps
- 📄 License
🎯 Project Overview
This project implements a Convolutional Neural Network (CNN) using TensorFlow/Keras to automatically detect and classify potato skin diseases from digital images. The system can identify three main categories:
- 🍃 Healthy Potatoes
- 🦠 Early Blight Disease
- 🍄 Late Blight Disease
🎥 Demo
Click to see sample predictions
Input: potato_image.jpg
Output: "Early Blight Disease" (Confidence: 94.2%)
🌟 Features
- ✅ Multi-class Classification: Detects 3 types of potato conditions
- ✅ Data Augmentation: Improves model robustness with image transformations
- ✅ Interactive Visualization: Displays sample images with predictions
- ✅ Optimized Performance: Uses caching and prefetching for faster training
- ✅ Scalable Architecture: Easy to extend to more disease types
- ✅ Real-time Inference: Fast prediction on new images
📊 Dataset
📈 Dataset Statistics
- Total Images: 2,152
- Classes: 3 (Early Blight, Late Blight, Healthy)
- Image Size: 256×256 pixels
- Color Channels: RGB (3 channels)
- Data Split: 80% Train, 10% Validation, 10% Test
📁 Project Structure
potato-disease-detection/
├── 📓 POTATO_Skin_Diseases_Detection_Using_Deep_Learning.ipynb
├── 📄 README.md
├── 📋 requirements.txt
├── 📁 PlantVillage/
│ ├── 📁 Potato___Early_blight/
│ ├── 📁 Potato___Late_blight/
│ └── 📁 Potato___healthy/
├── 📁 models/
│ └── 💾 trained_model.h5
└── 📁 results/
├── 📊 training_plots.png
└── 📈 confusion_matrix.png
📂 Root Directory/ ├── 🐍 app.py # Main Flask application ├── 📦 requirements.txt # Dependencies ├── 🚀 run_flask_app.bat # Easy startup script ├── 📚 README_Flask.md # Complete documentation ├── 📂 templates/ │ └── 🌐 index.html # Web interface └── 📂 static/ ├── 📂 css/ │ └── 💄 style.css # Beautiful styling └── 📂 js/ └── ⚡ script.js # Interactive functionality
🚀 Getting Started
📋 Prerequisites
Python 3.8+
TensorFlow 2.x
Matplotlib
NumPy
⚡ Quick Start and Installation
🐍 Environment Setup
# Create virtual environment
python -m venv potato_env
# Activate environment
# Windows:
potato_env\Scripts\activate
# macOS/Linux:
source potato_env/bin/activate
# Install packages
pip install -r requirements.txt
Run Application
Step 1: Install Dependencies
pip install -r requirements.txt
Step 2: Run the Application
python app.py
Step 3: Open Your Browser
- Main App: http://localhost:5000
- Health Check: http://localhost:5000/health
💻 Usage
🔧 Training the Model
The notebook includes the complete pipeline:
Data Loading & Preprocessing
# Load dataset dataset = tf.keras.preprocessing.image_dataset_from_directory( "PlantVillage", image_size=(256, 256), batch_size=32 )
Data Augmentation
# Apply data augmentation data_augmentation = tf.keras.Sequential([ tf.keras.layers.RandomFlip("horizontal_and_vertical"), tf.keras.layers.RandomRotation(0.2) ])
Model Configuration
IMAGE_SIZE = 256 BATCH_SIZE = 32 CHANNELS = 3 EPOCHS = 50
🎯 Making Predictions
# Load your trained model
model = tf.keras.models.load_model('potato_disease_model.h5')
# Make prediction
prediction = model.predict(new_image)
predicted_class = class_names[np.argmax(prediction)]
🏗️ Model Architecture
🧠 Network Components
- Input Layer: 256×256×3 RGB images
- Preprocessing:
- Image resizing and rescaling (1.0/255)
- Data augmentation (RandomFlip, RandomRotation)
- Feature Extraction: CNN layers for pattern recognition
- Classification: Dense layers for final prediction
⚙️ Training Configuration
- Optimizer: Adam (recommended)
- Loss Function: Sparse Categorical Crossentropy
- Metrics: Accuracy
- Epochs: 50
- Batch Size: 32
📈 Results
📊 Performance Metrics
Metric | Score |
---|---|
Training Accuracy | XX.X% |
Validation Accuracy | XX.X% |
Test Accuracy | XX.X% |
F1-Score | XX.X% |
🎨 Visualization
The notebook includes:
- ✅ Sample image visualization
- ✅ Training/validation loss curves
- ✅ Confusion matrix
- ✅ Class-wise accuracy
🥔 Potato Disease Detection - Flask Web Application
A modern Flask web application for detecting potato diseases using deep learning. Upload images or use your camera to get instant disease predictions with confidence scores and treatment recommendations.
✨ Features
🖼️ Dual Input Methods
- 📁 File Upload: Drag & drop or browse to select images
- 📸 Camera Capture: Take photos directly from your device camera
🧠 AI-Powered Detection
- 🎯 Accurate Predictions: Uses trained CNN model for disease detection
- 📊 Confidence Scores: Shows prediction confidence with color-coded badges
- 📈 Probability Breakdown: Displays probabilities for all disease classes
💡 Smart Recommendations
- 🏥 Treatment Advice: Provides specific recommendations for each condition
- 🚨 Urgency Levels: Different advice based on disease severity
- 📋 Downloadable Reports: Generate and download analysis reports
🎨 Modern Interface
- 📱 Responsive Design: Works perfectly on mobile and desktop
- 🌟 Beautiful UI: Modern design with smooth animations
- 🔄 Real-time Analysis: Instant predictions with loading indicators
🦠 Detected Diseases
- 🍂 Early Blight - Common fungal disease affecting potato leaves
- 💀 Late Blight - Serious disease that can destroy entire crops
- ✅ Healthy - No disease detected
🎯 How to Use
📁 Upload Method
- Select Upload tab (default)
- Drag & drop an image or click to browse
- Click "Analyze Disease" button
- View results with predictions and recommendations
📸 Camera Method
- Click Camera tab
- Click "Start Camera" (allow permissions)
- Click "Capture Photo" when ready
- Click "Analyze Disease" button
- View results with predictions and recommendations
📊 Understanding Results
- 🎯 Primary Diagnosis: Main prediction with confidence score
- 📈 Probability Breakdown: All disease probabilities
- 💡 Recommendations: Treatment and care advice
- 📋 Download Report: Save results as text file
🔧 Technical Details
- 🐍 Backend: Flask 2.3+ with Python
- 🧠 AI Model: TensorFlow/Keras CNN
- 🖼️ Image Processing: PIL/Pillow for preprocessing
- 🎨 Frontend: HTML5, CSS3, Vanilla JavaScript
- 📱 Camera: WebRTC getUserMedia API
- 💾 Storage: Local file system for uploads
📋 Requirements
- 🐍 Python: 3.8+ (Recommended: 3.10+)
- 💻 OS: Windows, macOS, or Linux
- 🧠 Memory: 4GB+ RAM (8GB recommended)
- 💾 Storage: ~2GB for dependencies and models
- 🌐 Browser: Chrome, Firefox, Safari, Edge (latest versions)
🛠️ Troubleshooting
❌ Model Not Loading
Error: Model not loaded! Please check the model file path.
Solution:
- Ensure
models/1.h5
exists - Check TensorFlow installation:
pip install tensorflow>=2.13.0
❌ Camera Not Working
Could not access camera. Please check permissions.
Solution:
- Allow camera permissions in your browser
- Use HTTPS for camera access (or localhost)
- Check if another app is using the camera
❌ Port Already in Use
Address already in use
Solution:
- Close other Flask applications
- Change port in
app.py
:app.run(port=5001)
- Kill process:
taskkill /f /im python.exe
(Windows)
❌ File Upload Issues
Invalid file type or File too large
Solution:
- Use supported formats: PNG, JPG, JPEG
- Keep file size under 16MB
- Check image isn't corrupted
🎨 Customization
🎯 Add New Disease Classes
- Update
CLASS_NAMES
inapp.py
- Add descriptions in
CLASS_DESCRIPTIONS
- Update recommendations in
get_recommendations()
- Retrain model with new classes
📱 Mobile Responsiveness
The application is now fully responsive and optimized for mobile devices:
📲 Mobile Features:
- ✅ Touch-friendly interface with larger touch targets (44px minimum)
- ✅ Responsive design that adapts to screen sizes from 320px to desktop
- ✅ Mobile camera support with environment (back) camera preference
- ✅ Optimized image display for mobile viewports
- ✅ Landscape/Portrait orientation support
- ✅ iOS Safari compatibility with viewport fixes
- ✅ Prevent accidental zoom on form inputs
- ✅ Touch-optimized drag & drop for file uploads
🎨 Modify UI
- Colors: Edit CSS variables in
style.css
- Layout: Modify templates in
templates/
- Functionality: Update JavaScript in
static/js/
⚙️ Configuration
- Upload size: Change
MAX_CONTENT_LENGTH
inapp.py
- Image size: Modify
IMAGE_SIZE
parameter - Port: Update
app.run(port=5000)
line
🔒 Security Notes
- 🚫 Production Use: This is for development/research only
- 🔐 Secret Key: Change
app.secret_key
for production - 📁 File Validation: Only accepts image files
- 💾 File Cleanup: Consider automatic cleanup of old uploads
📈 Performance Tips
- 📸 Image Quality: Use clear, well-lit potato leaf images
- 🎯 Focus: Ensure leaves fill most of the frame
- 📏 Size: Optimal size is 256x256 pixels or larger
- 🌟 Lighting: Good natural lighting gives best results
🌐 Browser Compatibility
- ✅ Chrome: 90+
- ✅ Firefox: 88+
- ✅ Safari: 14+
- ✅ Edge: 90+
- ⚠️ Mobile: Camera features may vary
📄 API Endpoints
GET /
- Main web interfacePOST /predict
- Upload image predictionPOST /predict_camera
- Camera image predictionGET /health
- Application health check
🤝 Support
For issues or questions:
- Check the troubleshooting section above
- Verify your Python and dependencies versions
- Ensure model files are in the correct location
- Test with the provided sample images
🚀 Next Steps
🔮 Future Enhancements
- Model Optimization: Implement transfer learning with pre-trained models
- Web Application: Create a Flask/Streamlit web interface
- Mobile App: Develop a mobile application for field use
- More Diseases: Expand to detect additional potato diseases
- Real-time Detection: Implement live camera feed processing
- API Development: Create REST API for integration
🎯 Improvement Ideas
- Hyperparameter Tuning: Optimize model parameters
- Cross-validation: Implement k-fold cross-validation
- Ensemble Methods: Combine multiple models
- Data Balancing: Handle class imbalance if present
🐛 Bug Reports
If you find a bug, please create an issue with:
- Description of the problem
- Steps to reproduce
- Expected vs actual behavior
- System information
💡 Feature Requests
For new features, please provide:
- Clear description of the feature
- Use case and benefits
- Implementation suggestions```
==================DEBUGGING AND TROUBLESHOOTING GUIDE:===========================
🥔 Potato Disease Detection - Upload Functionality Guide
🚀 Quick Start
Run the Application:
python app.py
Or double-click
run_and_test.bat
Access the App:
- Main app: http://localhost:5000
- Debug upload page: http://localhost:5000/debug
- Health check: http://localhost:5000/health
📋 Testing Upload Functionality
Step 1: Check System Health
- Go to http://localhost:5000/debug
- Click "🔍 Check System Health"
- Verify all items show ✅:
- Status: healthy
- Model Loaded: Yes
- Upload Dir Exists: Yes
- Upload Dir Writable: Yes
Step 2: Test Upload Directory
- Click "📂 Test Upload Directory"
- Should show "Upload directory is working correctly"
Step 3: Test Image Upload
- Click "📁 Click here to select an image" or drag an image
- Select a potato leaf image (JPG, PNG, JPEG)
- Preview should appear
- Click "🔬 Analyze Disease"
- Results should show:
- Disease name and confidence
- Recommendations
- The analyzed image displayed
🔧 Troubleshooting Upload Issues
Issue: "No file uploaded" Error
Solutions:
- Ensure you're clicking the upload area or browse link
- Check browser console for JavaScript errors (F12)
- Try the debug page: http://localhost:5000/debug
- Mobile: Tap firmly on upload area, wait for file picker
Issue: File Not Saving
Solutions:
- Check upload directory permissions:
mkdir static/uploads
- Run as administrator if on Windows
- Check disk space
- Mobile: Ensure stable network connection
Issue: Camera Not Working (Mobile)
Solutions:
- Grant camera permissions when prompted
- Use HTTPS for camera access on mobile (required by browsers)
- Check camera availability - some devices block camera access
- Try different browsers (Chrome/Safari work best)
- Close other camera apps that might be using the camera
Issue: Touch/Tap Not Working (Mobile)
Solutions:
- Clear browser cache and reload
- Disable browser zoom if enabled
- Try two-finger tap if single tap doesn't work
- Check touch targets - buttons should be at least 44px
- Restart browser app on mobile device
Issue: Image Too Small/Large on Mobile
Solutions:
- Portrait orientation usually works better
- Pinch to zoom on images if needed
- Landscape mode available for wider screens
- Image auto-resizes based on screen size
Issue: Slow Performance on Mobile
Solutions:
- Close other browser tabs to free memory
- Use smaller image files (under 5MB recommended)
- Ensure good network connection for uploads
- Clear browser cache regularly
- Restart browser if app becomes unresponsive
Issue: Model Not Loading
Solutions:
- Verify model file exists:
models/1.h5
- Install required packages:
pip install tensorflow pillow flask
Issue: JavaScript Errors
Solutions:
- Clear browser cache (Ctrl+F5)
- Check browser console (F12)
- Try a different browser
- Disable browser extensions
Issue: Image Not Displaying in Results
Solutions:
- Check browser network tab (F12) for failed requests
- Verify uploaded file in
static/uploads/
folder - Check Flask console for file save errors
🧪 Debug Features
Console Logging
The JavaScript includes extensive console logging. Open browser developer tools (F12) to see:
- File selection events
- Upload progress
- Server responses
- Error details
Debug Endpoints
/health
- System status/debug/upload-test
- Upload directory test/debug
- Interactive upload test page
Manual Testing
File Input Test:
document.getElementById("fileInput").click();
Check Selected File:
console.log(selectedFile);
Test FormData:
const formData = new FormData(); formData.append("file", selectedFile); console.log([...formData.entries()]);
💡 Tips for Success
- Use supported image formats: JPG, PNG, JPEG, GIF
- Keep file size under 16MB
- Use clear potato leaf images
- Check browser compatibility (modern browsers work best)
- Enable JavaScript
- Allow camera permissions (for camera capture feature)
🆘 Getting Help
If upload functionality still doesn't work:
- Check Flask console output for error messages
- Check browser console (F12 → Console tab)
- Try the debug page at
/debug
- Test with different image files
- Restart the Flask app
- Check file permissions on the upload directory
🎯 Expected Results
After successful upload and analysis:
- ✅ Disease classification (Early Blight, Late Blight, or Healthy)
- ✅ Confidence percentage
- ✅ Treatment recommendations
- ✅ Analyzed image displayed in results
- ✅ Timestamp of analysis
PDF Report Download Upgrade Guide
🎉 New Features Added
✨ PDF Format
- Professional PDF reports instead of simple text files
- Includes header, footer, tables, and proper formatting
- Company branding and professional layout
📁 Folder Selection
- Choose where to save your PDF reports
- Modern file picker dialog (supported browsers)
- Automatic fallback to default downloads folder
🎨 Enhanced Report Content
- Report Header: Timestamp, analysis method, model version
- Analyzed Image: Embedded image (if available)
- Diagnosis Section: Disease name, confidence, risk assessment
- Probability Breakdown: Table showing all class probabilities
- Treatment Recommendations: Numbered list of actionable advice
- Professional Footer: Branding and copyright information
🚀 Installation Requirements
Add to your requirements.txt
:
reportlab>=4.0.0
Install the new dependency:
pip install reportlab>=4.0.0
PDF Generation Troubleshooting Guide
🔧 If PDF Generation is Failing
Quick Fix Steps
Install ReportLab Library
pip install reportlab>=4.0.0
Run Installation Script
- Windows: Double-click
install_pdf_deps.bat
- Linux/Mac: Run
bash install_pdf_deps.sh
- Windows: Double-click
Restart the Application
python app.py
Common Issues and Solutions
❌ "ReportLab not available" Error
Problem: ReportLab library is not installed.
Solution:
pip install reportlab
# or
pip install reportlab>=4.0.0
Alternative: Use virtual environment
python -m venv pdf_env
source pdf_env/bin/activate # Linux/Mac
# or
pdf_env\Scripts\activate # Windows
pip install reportlab
❌ "Permission denied" or "Access denied" Errors
Problem: Insufficient permissions to install packages.
Solutions:
Use --user flag:
pip install --user reportlab
Run as administrator (Windows):
- Right-click Command Prompt → "Run as administrator"
- Then run:
pip install reportlab
Use sudo (Linux/Mac):
sudo pip install reportlab
❌ "Module not found" Error Despite Installation
Problem: ReportLab installed in different Python environment.
Solutions:
Check Python version:
python --version which python # Linux/Mac where python # Windows
Install for specific Python version:
python3 -m pip install reportlab # or python3.9 -m pip install reportlab
Verify installation:
python -c "import reportlab; print('ReportLab available')"
❌ PDF Generation Works but Images Missing
Problem: Image files not accessible or corrupted.
Solutions:
Check upload folder permissions:
ls -la static/uploads/ # Linux/Mac dir static\uploads\ # Windows
Verify image exists:
- Check browser developer tools for 404 errors
- Ensure images are properly saved during upload
Check image format:
- Ensure images are JPG, PNG, or supported formats
- ReportLab may have issues with some image formats
❌ Client-side PDF Generation Fails
Problem: jsPDF library not loading.
Solutions:
Check internet connection (jsPDF loads from CDN)
Check browser console for JavaScript errors
❌ Folder Selection Not Working
Problem: File System Access API not supported.
Solutions:
Update browser:
- Chrome 86+ or Edge 86+ required for folder selection
- Firefox and Safari will use default download folder
Enable experimental features (Chrome):
- Go to
chrome://flags
- Enable "Experimental Web Platform features"
- Go to
Accept automatic download to default folder
The system should work with any clear image of a potato plant leaf!
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- PlantVillage Dataset: For providing the potato disease dataset
- TensorFlow Team: For the amazing deep learning framework
- Open Source Community: For inspiration and resources
📞 Contact
- Author: Lucky Sharma
- Email: [email protected]
- LinkedIn: https://www.linkedin.com/in/lucky-sharma918894599977
- GitHub: https://github.com/itsluckysharma01
⭐ Star this repository if you found it helpful!
🍀 Happy coding and may your potatoes be healthy!