File size: 1,637 Bytes
8ef30c1 3b85cb8 8ef30c1 3b85cb8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
import pickle
# Load the saved model
model = load_model("best_model.h5")
# Load the class labels from a pickle file
with open("mod_class_labels.pkl", "rb") as f:
class_indices = pickle.load(f)
# Function to preprocess the image
def preprocess_image(image):
image = load_img(image, target_size=(256, 256)) # Load the image with target size
image = img_to_array(image) # Convert the image to array
image = np.expand_dims(image, axis=0) # Expand dimensions to match the input shape
image = image / 255.0 # Rescale the image
return image
# Function to make a prediction and get the label
def predict_image(image):
image = preprocess_image(image)
prediction = model.predict(image)
predicted_class = np.argmax(prediction, axis=1)[0]
predicted_label = class_indices[predicted_class]
return predicted_label
# Streamlit App
st.title("Rice Leaf Disease Classification")
st.write("Upload an image of a rice leaf and the model will predict its disease category.")
# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
image = load_img(uploaded_file, target_size=(256, 256))
st.image(image, caption='Uploaded Image', use_column_width=True)
st.write("")
st.write("Classifying...")
# Make a prediction
predicted_label = predict_image(uploaded_file)
st.write(f"Predicted label: {predicted_label}")
|