import streamlit as st from PIL import Image import joblib import numpy as np import tensorflow as tf from tensorflow import keras from keras.layers import Dense from keras.models import Sequential, load_model from tensorflow.keras.layers import BatchNormalization from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions from tensorflow.keras.preprocessing import image as keras_image from skimage import io, color, transform from skimage.feature import hog # from keras.layers import Dense,Flatten,BatchNormalization,Dropout from scipy.special import softmax # ... (Previous imports and model loading code remain the same) def preprocess_image(img, model_name): # if model_name == 'Resnet': # # Resize image to match ResNet input size (256x256) # img = img.resize((256, 256)) # img_array = keras_image.img_to_array(img) # img_array = np.expand_dims(img_array, axis=0) # img_array = preprocess_input(img_array) # return img_array if model_name == 'SVM': resized_image = transform.resize(np.array(img), (100, 100), mode='reflect') if resized_image.shape[2] == 4: resized_image = color.rgba2rgb(resized_image) grayscale_image = color.rgb2gray(resized_image) hog_features = hog(grayscale_image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm='L2-Hys') return hog_features elif model_name == "ANN" or model_name == "CNN": resized_image = img.resize((256, 256)) img_array = keras_image.img_to_array(resized_image) img_array = np.expand_dims(img_array, axis=0) img_array = img_array / 255.0 # Normalize pixel values to be between 0 and 1 return img_array def predict(model, img, model_name): img_array = preprocess_image(img, model_name) # if model_name == 'Resnet': # predictions = model.predict(img_array) # decoded_predictions = custom_decode_predictions(predictions) # return decoded_predictions if model_name == 'SVM': # Flatten the array before passing it to the SVM model img_array_flattened = img_array.flatten().reshape(1, -1) prediction = model.predict(img_array_flattened) return prediction[0] elif model_name == 'ANN' or model_name == 'CNN': prediction = model.predict(img_array) confidence_scores = softmax(prediction, axis=1) # Apply softmax to get confidence scores predicted_class = np.argmax(confidence_scores, axis=1)[0] # Get the class with the highest confidence confidence = confidence_scores[0, predicted_class] return predicted_class, confidence def custom_decode_predictions(predictions): # Assuming predictions is a 2D array with shape (1, num_classes) num_classes = predictions.shape[1] # Assuming predictions contain class probabilities confidence_scores = predictions[0] # Get the index of the class with the highest probability predicted_class_index = np.argmax(confidence_scores) # Return the class index, confidence score, and probability distribution return [ predicted_class_index, confidence_scores[predicted_class_index], ] # Load the models pper_model_CNN = load_model("../pepper/models/pepper_CNN.h5") pper_model_SVM = joblib.load('../pepper/models/pepper_hog_svm_model.pkl') # pper_model_resnet = load_model('../pepper/models/pepper_resnet50.h5') potato_model_CNN = load_model('../potato/models/potato_CNN.h5') potato_model_SVM = joblib.load('../potato/models/potato_hog_svm_model.pkl') # potato_model_resnet = load_model('../potato/models/potato_resnet50.h5') tomato_model_CNN = load_model('../tomato/models/tomato_CNN.h5') tomato_model_SVM = joblib.load('../tomato/models/tomato_hog_svm_model.pkl') tomato_model_ANN = load_model('../tomato/models/ANN_tomato.h5') # tomato_model_resnet = load_model('../tomato/models/tomato_resnet50.h5') Potato_class = 3 Pepper_class = 2 Tomato_class = 10 # Set the title of the app st.title('Plant Disease Detection App') # Create a dropdown menu for the user to choose a plant plant = st.selectbox('Choose a plant', ('Pepper', 'Potato', 'Tomato')) # Create a radio button for the user to choose a model if plant == 'Pepper': no_of_classes=2 class_name = ['pepper_bacterial_spot', 'pepper_bell_healthy'] model = st.radio('Choose a model', ('CNN', 'SVM', )) if model == 'SVM': model_func = pper_model_SVM elif model == 'CNN': model_func = pper_model_CNN # elif model == 'Resnet': # model_func = pper_model_resnet elif plant == 'Potato': class_name=['EarlyBlight','Healthy', 'LateBlight'] no_of_classes=2 model = st.radio('Choose a model', ('SVM', 'CNN', )) if model == 'SVM': model_func =potato_model_SVM elif model == 'CNN': model_func =potato_model_CNN # elif model == 'Resnet': # model_func = potato_model_resnet elif plant == 'Tomato': class_name=['Tomato___Bacterial_spot', 'Tomato___Early_blight', 'Tomato___Late_blight', 'Tomato___Leaf_Mold', 'Tomato___Septoria_leaf_spot', 'Tomato___Spider_mites Two-spotted_spider_mite', 'Tomato___Target_Spot', 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 'Tomato___Tomato_mosaic_virus', 'Tomato___healthy'] no_of_classes=10 model = st.radio('Choose a model', ('SVM','CNN', 'ANN', )) if model == 'SVM': model_func = tomato_model_SVM elif model == 'CNN': model_func = tomato_model_CNN elif model == 'ANN': model_func = tomato_model_ANN # elif model== 'Resnet': # model_func = tomato_model_resnet # Create a file uploader for the user to upload an image uploaded_file = st.file_uploader('Upload an image', type='jpg') # Display the selected model and plant st.write('Selected plant:', plant) st.write('Selected model:', model) # If an image is uploaded, display it and make a prediction if uploaded_file is not None: image = Image.open(uploaded_file) # st.image(image, caption='Uploaded Image', use_column_width=True) # Make a prediction using the selected model prediction_result = predict(model_func, image, model) print(no_of_classes) # st.write(prediction_result) # Display the result based on the model type if model == 'SVM': st.write(f'Prediction: {prediction_result}') else: predicted_class, confidence = prediction_result st.write(f'Prediction: {class_name[predicted_class]}') st.write(f'Confidence: {confidence}')