import streamlit as st from PIL import Image import numpy as np import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt import tensorflow_hub as hub hide_streamlit_style = """ """ st.markdown(hide_streamlit_style, unsafe_allow_html=True) st.title('Plant Disease Prediction') st.write("This model is capable of predicting plant disease as a demo") def main(): file_uploaded = st.file_uploader('Choose an image...', type='jpg') if file_uploaded is not None: image = Image.open(file_uploaded) st.write("Uploaded Image.") figure = plt.figure() plt.imshow(image) plt.axis('off') st.pyplot(figure) result, confidence = predict_class(image) st.write('Prediction : {}'.format(result)) st.write('Confidence : {}%'.format(confidence)) imgSize = 256 def predict_class(image): with st.spinner('Loading Model...'): classifier_model = keras.models.load_model( r'plant_model_v5-beta.h5', compile=False) shape = ((imgSize, imgSize, 3)) model = keras.Sequential( [hub.KerasLayer(classifier_model, input_shape=shape)]) test_image = image.resize((imgSize, imgSize)) test_image = keras.preprocessing.image.img_to_array(test_image) test_image /= 220.0 test_image = np.expand_dims(test_image, axis=0) class_name = {0: 'Apple___Apple_scab', 1: 'Apple___Black_rot', 2: 'Apple___Cedar_apple_rust', 3: 'Apple___healthy', 4: 'Not a plant', 5: 'Blueberry___healthy', 6: 'Cherry___Powdery_mildew', 7: 'Cherry___healthy', 8: 'Corn___Cercospora_leaf_spot Gray_leaf_spot', 9: 'Corn___Common_rust', 10: 'Corn___Northern_Leaf_Blight', 11: 'Corn___healthy', 12: 'Grape___Black_rot', 13: 'Grape___Esca_(Black_Measles)', 14: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', 15: 'Grape___healthy', 16: 'Orange___Haunglongbing_(Citrus_greening)', 17: 'Peach___Bacterial_spot', 18: 'Peach___healthy', 19: 'Pepper,_bell___Bacterial_spot', 20: 'Pepper,_bell___healthy', 21: 'Potato___Early_blight', 22: 'Potato___Late_blight', 23: 'Potato___healthy', 24: 'Raspberry___healthy', 25: 'Soybean___healthy', 26: 'Squash___Powdery_mildew', 27: 'Strawberry___Leaf_scorch', 28: 'Strawberry___healthy', 29: 'Tomato___Bacterial_spot', 30: 'Tomato___Early_blight', 31: 'Tomato___Late_blight', 32: 'Tomato___Leaf_Mold', 33: 'Tomato___Septoria_leaf_spot', 34: 'Tomato___Spider_mites Two-spotted_spider_mite', 35: 'Tomato___Target_Spot', 36: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 37: 'Tomato___Tomato_mosaic_virus', 38: 'Tomato___healthy'} prediction = model.predict_generator(test_image) confidence = round(100 * (np.max(prediction[0])), 2) final_pred = class_name[np.argmax(prediction)] if confidence < 60: final_pred = 'Can\'t Predict the disease' if final_pred != class_name[4] and confidence == 100: final_pred = 'Not sure about the disease' return final_pred, confidence footer = """ """ st.markdown(footer, unsafe_allow_html=True) if __name__ == "__main__": main()