Potato Leaf Disease Detection Project: A Complete Guide (2025)

Healthy and diseased potato leaves showing signs of Early Blight and Late Blight for potato leaf disease detection project.

Potato crops are vital to global food security; however, diseases like Early Blight and Late Blight can significantly reduce yields. In this guide, we will explore how to build and deploy a deep learning model to detect potato leaf diseases using a publicly available dataset. Moreover, this process will demonstrate the practical power of artificial intelligence in modern agriculture.

Fortunately, advances in deep learning offer a promising solution. Specifically, Convolutional Neural Networks (CNNs) have proven highly effective in image classification tasks, making them well-suited for detecting plant diseases from leaf images. By automating the detection process, farmers and researchers can benefit from faster, more accurate diagnoses. In turn, this can lead to better disease control, higher yields, and more sustainable agricultural practices.

Throughout this comprehensive guide, we will walk through each step of the this project—from data preparation and model training to web deployment and real-world usage. Ultimately, the goal is to build a reliable, scalable system that empowers users with instant and accurate disease insights.

Introduction

Potato leaf diseases, particularly Early Blight and Late Blight, pose significant threats to potato production. Traditional methods of disease detection are time-consuming and often inaccurate. Deep learning, especially Convolutional Neural Networks (CNNs), offers a promising solution for automated and accurate disease detection.​

Dataset Overview

We’ll utilize the Potato Leaf Disease Dataset, which contains images categorized into three classes:​

  • Healthy
  • Early Blight
  • Late Blight

Download the dataset from here: Potato Leaf Disease Dataset – Kaggle.

This dataset includes images of healthy potato leaves as well as leaves affected by Early Blight and Late Blight, helping us train a robust deep learning model for accurate potato leaf disease detection.

Environment Setup

Make sure you have the required libraries installed before beginning to write code.

pip install tensorflow keras matplotlib numpy scikit-learn

Explanation:

  • tensorflow and keras: Libraries for building and training deep learning models.
  • matplotlib: For visualizing data and training progress.
  • numpy: For numerical operations.
  • scikit-learn: Provides tools for model evaluation.

Data Preprocessing

Model performance depends on proper data preprocessing.

import os
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Define paths
train_dir = 'path_to_dataset/train'
val_dir = 'path_to_dataset/val'
test_dir = 'path_to_dataset/test'

# Image dimensions
IMG_HEIGHT = 256
IMG_WIDTH = 256
BATCH_SIZE = 32

# Data augmentation and rescaling
train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    zoom_range=0.2,
    horizontal_flip=True
)

val_datagen = ImageDataGenerator(rescale=1./255)

# Load images from directories
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    class_mode='categorical'
)

val_generator = val_datagen.flow_from_directory(
    val_dir,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    class_mode='categorical'

Explanation:

  • ImageDataGenerator:  Produces tensor image data in batches while augmenting the data in real time.
  • rescale=1./255: Normalizes pixel values to the [0,1] range.
  • rotation_range, zoom_range, horizontal_flip: Apply random transformations to augment the dataset.
  • flow_from_directory: Loads images from the specified directory and applies the defined transformations.

Model Building

We’ll build a CNN model using Keras.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)),
    MaxPooling2D(2,2),
    
    Conv2D(64, (3,3), activation='relu'),
    MaxPooling2D(2,2),
    
    Conv2D(128, (3,3), activation='relu'),
    MaxPooling2D(2,2),
    
    Flatten(),
    Dense(512, activation='relu'),
    Dropout(0.5),
    Dense(3, activation='softmax')
])

Explanation:

  • Conv2D: Applies convolutional filters to the input.
  • MaxPooling2D: Reduces spatial dimensions, retaining the most important features.
  • Dense: Fully connected layers for classification.
  • Dropout: Prevents overfitting by randomly setting input units to 0 during training.
  • softmax: Activation function for multi-class classification, outputs probabilities for each class.​

Model Training

Compile and train the model.

from tensorflow.keras.optimizers import Adam

model.compile(optimizer=Adam(learning_rate=0.0001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(
    train_generator,
    epochs=25,
    validation_data=val_generator
)

Explanation:

  • Adam: An optimizer that adjusts the learning rate during training.
  • categorical_crossentropy: Loss function for multi-class classification.
  • metrics=['accuracy']: Tracks the accuracy of the model during training.

Model Evaluation

Analyze how well the model performed on the test set.

test_generator = val_datagen.flow_from_directory(
    test_dir,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    class_mode='categorical',
    shuffle=False
)

# Evaluate the model
loss, accuracy = model.evaluate(test_generator)
print(f'Test Accuracy: {accuracy*100:.2f}%')

Explanation:

  • flow_from_directory: Loads test images without applying data augmentation.
  • evaluate: calculates the test data’s accuracy and loss.

Model Deployment

Save and deploy the model for real-world use.

# Save the model
model.save('potato_disease_model.h5')

Explanation:

  • model.save: Saves the entire model, including architecture, weights, and optimizer state.

For deployment, you can use platforms like TensorFlow Serving, Flask API, or convert the model to TensorFlow Lite for mobile applications.

Flask API (app.py)

from flask import Flask, render_template, request
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
import os

# Initialize app
app = Flask(__name__)

# Load model
model = load_model('model/potato_disease_model.h5')

# Class labels
classes = ['Early Blight', 'Late Blight', 'Healthy']

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def upload():
    if request.method == 'POST':
        file = request.files['image']
        file_path = os.path.join('static', file.filename)
        file.save(file_path)

        # Load and preprocess image
        img = load_img(file_path, target_size=(256, 256))
        img_array = img_to_array(img) / 255.0
        img_array = np.expand_dims(img_array, axis=0)

        # Predict
        prediction = model.predict(img_array)
        class_index = np.argmax(prediction)
        result = classes[class_index]

        return render_template('index.html', prediction=result, image=file_path)

if __name__ == '__main__':
    app.run(debug=True)

Explanation

  • Flask: Serves the model on a web server.
  • render_template: Loads HTML pages.
  • request.files: Gets image input from user.
  • predict: Performs classification and returns the result.

HTML Template (templates/index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Potato Leaf Disease Detection</title>
</head>
<body>
    <h1>Upload a Potato Leaf Image</h1>
    <form method="POST" action="/predict" enctype="multipart/form-data">
        <input type="file" name="image" required>
        <input type="submit" value="Predict">
    </form>
    {% if prediction %}
        <h2>Prediction: {{ prediction }}</h2>
        <img src="{{ image }}" width="300">
    {% endif %}
</body>
</html>

3. requirements.txt

Flask==2.3.2
tensorflow==2.12.0
numpy
Pillow
gunicorn

4. Procfile (For Heroku)

web: gunicorn app:ap

6. Deploy to Heroku (Step-by-Step)

1. Login to Heroku

heroku login

2. Create App

heroku create potato-leaf-detector

3. Push Code

git init
heroku git:remote -a potato-leaf-detector
git add .
git commit -m "Initial commit"
git push heroku master

4: Open App

heroku open

✅ Final Result

You now have a deployed web app that:

  • Accepts image uploads
  • Detects disease type
  • Displays the prediction visually

Conclusion: Potato Leaf Disease Detection Using Deep Learning

The rise of deep learning in agriculture marks a revolutionary transformation in the way farmers and researchers address crop health challenges. In this potato leaf disease detection project, we successfully developed and deployed a model capable of classifying potato leaves into three categories: Early Blight, Late Blight, and Healthy. This was achieved using convolutional neural networks (CNNs), which are exceptionally well-suited for image recognition tasks.

The project began with careful preparation of a publicly available dataset. Images were preprocessed, normalized, and augmented to improve the model’s generalization capability. Consequently, our trained CNN model achieved high accuracy in identifying disease types. Moreover, the use of Keras and TensorFlow streamlined the model-building process, making it both efficient and powerful.

Importantly, we didn’t stop at building the model. We also created a web-based deployment using Flask, allowing real-time interaction. As a result, users can upload potato leaf images and receive immediate feedback about potential diseases. This not only enhances accessibility but also brings the model into practical use.

To highlight the key benefits:

  • 🧠 AI-Driven Diagnosis: Real-time disease classification using leaf images.
  • 🌍 Accessible Deployment: Easy-to-use web interface for non-technical users.
  • 🌱 Sustainable Agriculture: Early detection can reduce pesticide usage and crop loss.

Additionally, the project opens doors for enhancements. For instance, we can expand it by including other crop diseases or integrating it into mobile applications. Thus, the potato leaf disease detection project can evolve into a multi-crop disease detection system, benefiting a wider farming community.

Join the BiStartX Internship Program!
Work on real projects like the Potato Leaf Disease Detection Project, gain hands-on experience, and boost your career.
🔗 Apply Now at BiStartX!

Leave a Reply

Your email address will not be published. Required fields are marked *