Plant Health Prediction Using CNNConvolutional Neural Network
Tutorial

Getting Started

Making your first API request


Overview
Making Your First API Request Plant Disease Predictor — Getting Started Start: Obtain API Access Prepare Leaf Image JPG, JPEG, or PNG format Preprocess Image Resize to 224×224, normalize to [0,1] Send POST Request Upload image to /predict endpoint Valid Response? HTTP 200 OK Yes Read Prediction Result Disease class name returned No Handle Error Check format, size, or auth credentials Retry Legend Start / Result Action step Decision

This page walks you through making your first request to the Plant Disease Predictor application — from installation to receiving a disease prediction for a leaf image. By the end of this tutorial, you will have the application running locally, successfully uploaded a leaf image, and interpreted the prediction response. Getting this baseline working is essential before you integrate the prediction workflow into any downstream tooling or pipeline.


Prerequisites

Before you begin, make sure you have the following in place:

  • Python 3.8 or later installed and available on your PATH
  • pip (Python package manager) for installing dependencies
  • The following Python packages at the specified versions (see requirements.txt):
    • numpy==1.26.3
    • streamlit==1.30.0
    • tensorflow==2.15.0.post1
  • The pre-trained model file plant_disease_prediction_model.h5 placed in the project root directory
  • The class index mapping file class_indices.json placed in the project root directory
  • A leaf image in JPG, JPEG, or PNG format to use as your test input
  • (Optional) Docker if you prefer a containerized setup instead of a local Python environment

Quick start

The fastest path to a working prediction:

  1. Clone or download the project repository to your local machine.
  2. Navigate into the project directory:
    cd plant-disease-predictor
    
  3. Install all required dependencies:
    pip install -r requirements.txt
    
  4. Confirm that plant_disease_prediction_model.h5 and class_indices.json are present in the project root.
  5. Launch the application:
    streamlit run main.py
    
  6. Open your browser to the URL shown in the terminal (typically http://localhost:8501).
  7. Use the Upload an image file picker to select a leaf image, then click Classify to receive your first prediction.

Steps

Follow these steps to go from a fresh checkout to a verified prediction result.

Step 1 — Install dependencies

From the project root, run:

pip install -r requirements.txt

This installs numpy, streamlit, and tensorflow at the exact versions the model was built and tested with. Using different versions — especially for TensorFlow — may cause model-loading errors.

Step 2 — Verify required files are present

Confirm both of the following files exist in the project root before starting the server:

  • plant_disease_prediction_model.h5 — the pre-trained CNN model weights
  • class_indices.json — a JSON mapping from integer class indices to human-readable disease names

If either file is missing, the application will fail to start.

Step 3 — Start the Streamlit server

Run:

streamlit run main.py

On success, you will see output similar to:

You can now view your Streamlit app in your browser.
Local URL:  http://localhost:8501
Network URL: http://192.168.x.x:8501

Your terminal will remain active — this is expected; Streamlit runs as a foreground process.

Step 4 — Upload a leaf image

In your browser at http://localhost:8501:

  1. Click the Browse files button under "Upload an image..."
  2. Select a leaf image in JPG, JPEG, or PNG format from your local machine.
  3. The image will appear in the left column of the interface, resized to a 150×150 preview.

The application accepts images of any original resolution; it internally resizes them to 224×224 pixels before passing them to the model.

Step 5 — Run classification and read the result

Click the Classify button. The application will:

  1. Preprocess your image (resize to 224×224, normalize pixel values to the range [0, 1]).
  2. Run a forward pass through the CNN model.
  3. Return the class name corresponding to the highest predicted probability.

A green success banner will appear in the right column displaying:

Prediction: <disease name>

This disease name is looked up from class_indices.json using the model's predicted class index.


Examples

Example 1 — Successful classification of a diseased leaf

Assume you have a file named tomato_leaf.jpg and the application is running at http://localhost:8501.

After uploading tomato_leaf.jpg and clicking Classify, the success banner displays:

Prediction: Tomato___Early_blight

This output means the model matched the uploaded image to the Tomato___Early_blight class with the highest confidence among all classes defined in class_indices.json.


Example 2 — Inspecting class_indices.json to understand predictions

The class_indices.json file maps integer indices (as strings) to class labels. A typical excerpt looks like this:

{
  "0": "Apple___Apple_scab",
  "1": "Apple___Black_rot",
  "2": "Apple___Cedar_apple_rust",
  "3": "Apple___healthy",
  "...": "..."
}

When the model outputs a predicted index of 0, the application returns Apple___Apple_scab as the disease name. You can inspect this file directly to understand all possible prediction values your integration may receive.


Example 3 — Programmatic preprocessing (for reference)

If you are building a script that calls the prediction logic directly, the preprocessing applied to every image before inference is:

from PIL import Image
import numpy as np

def load_and_preprocess_image(image_path, target_size=(224, 224)):
    img = Image.open(image_path)
    img = img.resize(target_size)
    img_array = np.array(img)
    img_array = np.expand_dims(img_array, axis=0)  # add batch dimension
    img_array = img_array.astype('float32') / 255.  # normalize to [0, 1]
    return img_array

Expected output shape fed to the model: (1, 224, 224, 3).


Troubleshooting

Issue: Application fails to start with OSError: Unable to open file

  • Symptom: Running streamlit run main.py immediately raises an OSError or FileNotFoundError referencing plant_disease_prediction_model.h5.
  • Likely cause: The model file is not present in the directory from which you launched the command.
  • Fix: Ensure plant_disease_prediction_model.h5 is in the same directory as main.py and that you are running streamlit run main.py from that directory. Verify with ls (macOS/Linux) or dir (Windows).

Issue: Application starts but prediction returns an error or wrong class

  • Symptom: The Classify button triggers an exception, or the predicted class name looks garbled.
  • Likely cause: The class_indices.json file is missing, malformed, or does not match the class set the model was trained on.
  • Fix: Confirm class_indices.json exists in the project root and is valid JSON. You can validate it quickly with:
    python -c "import json; json.load(open('class_indices.json')); print('OK')"
    

Issue: ModuleNotFoundError for tensorflow, streamlit, or numpy

  • Symptom: Running the app produces ModuleNotFoundError: No module named 'tensorflow' (or similar).
  • Likely cause: Dependencies were not installed, or were installed into a different Python environment than the one currently active.
  • Fix: Run pip install -r requirements.txt inside the correct virtual environment. If you use conda or venv, activate the environment first, then re-run the install command.

Issue: Uploaded image produces no output after clicking Classify

  • Symptom: Clicking Classify does nothing or the page reloads without showing a prediction.
  • Likely cause: The uploaded file is not in a supported format, or the image file is corrupted.
  • Fix: Confirm your image is one of the accepted formats: JPG, JPEG, or PNG. Try opening the file with an image viewer to rule out corruption. Re-export or re-save the image if needed.

Issue: TensorFlow version conflict warning or crash on model load

  • Symptom: Warning messages about op compatibility, or a crash when loading plant_disease_prediction_model.h5.
  • Likely cause: A TensorFlow version other than 2.15.0.post1 is installed.
  • Fix: Pin the exact version specified in requirements.txt:
    pip install tensorflow==2.15.0.post1
    
    If multiple TensorFlow versions are present, consider using a clean virtual environment.
plant-disease-predictor | Plant Health Prediction Using CNN Docs