|
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 scipy.special import softmax |
|
|
|
|
|
|
|
|
|
def preprocess_image(img, model_name): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
return img_array |
|
|
|
|
|
|
|
def predict(model, img, model_name): |
|
img_array = preprocess_image(img, model_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if model_name == 'SVM': |
|
|
|
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) |
|
predicted_class = np.argmax(confidence_scores, axis=1)[0] |
|
confidence = confidence_scores[0, predicted_class] |
|
return predicted_class, confidence |
|
|
|
def custom_decode_predictions(predictions): |
|
|
|
num_classes = predictions.shape[1] |
|
|
|
|
|
confidence_scores = predictions[0] |
|
|
|
|
|
predicted_class_index = np.argmax(confidence_scores) |
|
|
|
|
|
return [ |
|
predicted_class_index, |
|
confidence_scores[predicted_class_index], |
|
] |
|
|
|
|
|
pper_model_CNN = load_model("../pepper/models/pepper_CNN.h5") |
|
pper_model_SVM = joblib.load('../pepper/models/pepper_hog_svm_model.pkl') |
|
|
|
potato_model_CNN = load_model('../potato/models/potato_CNN.h5') |
|
potato_model_SVM = joblib.load('../potato/models/potato_hog_svm_model.pkl') |
|
|
|
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') |
|
|
|
Potato_class = 3 |
|
Pepper_class = 2 |
|
Tomato_class = 10 |
|
|
|
|
|
st.title('Plant Disease Detection App') |
|
|
|
|
|
plant = st.selectbox('Choose a plant', ('Pepper', 'Potato', 'Tomato')) |
|
|
|
|
|
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 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 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 |
|
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader('Upload an image', type='jpg') |
|
|
|
|
|
st.write('Selected plant:', plant) |
|
st.write('Selected model:', model) |
|
|
|
|
|
if uploaded_file is not None: |
|
image = Image.open(uploaded_file) |
|
|
|
|
|
|
|
prediction_result = predict(model_func, image, model) |
|
print(no_of_classes) |
|
|
|
|
|
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}') |
|
|
|
|