Getting Started
Making your first API request
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.
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.3streamlit==1.30.0tensorflow==2.15.0.post1
- The pre-trained model file
plant_disease_prediction_model.h5placed in the project root directory - The class index mapping file
class_indices.jsonplaced 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
The fastest path to a working prediction:
- Clone or download the project repository to your local machine.
- Navigate into the project directory:
cd plant-disease-predictor - Install all required dependencies:
pip install -r requirements.txt - Confirm that
plant_disease_prediction_model.h5andclass_indices.jsonare present in the project root. - Launch the application:
streamlit run main.py - Open your browser to the URL shown in the terminal (typically
http://localhost:8501). - Use the Upload an image file picker to select a leaf image, then click Classify to receive your first prediction.
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 weightsclass_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:
- Click the Browse files button under "Upload an image..."
- Select a leaf image in JPG, JPEG, or PNG format from your local machine.
- 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:
- Preprocess your image (resize to 224×224, normalize pixel values to the range [0, 1]).
- Run a forward pass through the CNN model.
- 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.
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).
Issue: Application fails to start with OSError: Unable to open file
- Symptom: Running
streamlit run main.pyimmediately raises anOSErrororFileNotFoundErrorreferencingplant_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.h5is in the same directory asmain.pyand that you are runningstreamlit run main.pyfrom that directory. Verify withls(macOS/Linux) ordir(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.jsonfile is missing, malformed, or does not match the class set the model was trained on. - Fix: Confirm
class_indices.jsonexists 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.txtinside the correct virtual environment. If you usecondaorvenv, 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.post1is installed. - Fix: Pin the exact version specified in
requirements.txt:If multiple TensorFlow versions are present, consider using a clean virtual environment.pip install tensorflow==2.15.0.post1