File size: 9,370 Bytes
54d9682 |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
import os
import pandas as pd
import io
def import_and_predict(image_data, model, class_labels):
size = (256, 256)
if image_data is not None:
image = Image.open(io.BytesIO(image_data.read()))
image = image.resize(size)
image = np.array(image)
img_reshape = image / 255.0
img_reshape = np.expand_dims(img_reshape, axis=0)
prediction = model.predict(img_reshape)
st.image(image, width=300)
predictions_label = class_labels[np.argmax(prediction[0])]
return predictions_label
else:
st.warning("Please upload an image.")
return None
st.title("Plant Disease Detection")
selected_item = st.radio("Select an item:", ["Apple", "Mango", "Grape", "Tomato"])
if selected_item:
st.write(f"You selected: {selected_item}")
uploaded_image = st.file_uploader(f"Upload an image of {selected_item.lower()}", type=["jpg", "jpeg", "png"])
df = pd.read_excel('./final format.xlsx')
models_path = [
'./models/best_model_50_apple_plant (1).h5','./models/best_model_100_subset (1).h5','./models/best_model_50_grape_plant2.h5','./models/model_inception_epoch_50_mango.h5']
@st.cache_resource
def load_model(models_path):
model = tf.keras.models.load_model(models_path)
return model
if selected_item == 'Apple':
CLASS_LABELS = ['apple scab', 'Apple black rot', 'cedar apple rust', 'Apple healthy']
model =load_model(models_path[0])
prediction = import_and_predict(uploaded_image, model, CLASS_LABELS)
st.write("disease name: ", prediction)
if prediction != None:
new_title = '<p style="font-size: 38px">Measures you can take to control: </p>'
st.markdown(new_title, unsafe_allow_html=True)
if prediction == CLASS_LABELS[0]:
response = df['Measures'].dropna().head(28)
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[1]:
response = df['Measures'].dropna()[28:46]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[2]:
response = df['Measures'].dropna()[46:63]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[3]:
st.write("Plant is healthy take good care of it")
elif selected_item == 'Tomato':
CLASS_LABELS = ['Tomato Early blight', 'Tomato Leaf Mold', 'Tomato YellowLeaf Curl Virus',
'Tomato mosaic virus', 'Tomato healthy']
model =load_model(models_path[1])
prediction = import_and_predict(uploaded_image, model, CLASS_LABELS)
st.write("disease name: ", prediction)
if prediction != None:
new_title = '<p style="font-size: 38px">Measures you can take to control: </p>'
st.markdown(new_title, unsafe_allow_html=True)
if prediction == CLASS_LABELS[0]:
response = df['Measures'].dropna()[63:79]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[1]: # remaining
response = df['Measures'].dropna()[187:196]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[2]:
response = df['Measures'].dropna()[99:113]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[3]:
response = df['Measures'].dropna()[79:99]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[4]:
st.write("Plant is healthy take good care of it")
elif selected_item == 'Grape':
CLASS_LABELS = ['Grape Black rot', 'Grape Black Measles','Grape Leaf blight', 'Grape healthy']
model =load_model(models_path[2])
prediction = import_and_predict(uploaded_image, model, CLASS_LABELS)
st.write("disease name: ", prediction)
if prediction != None:
new_title = '<p style="font-size: 38px">Measures you can take to control: </p>'
st.markdown(new_title, unsafe_allow_html=True)
if prediction == CLASS_LABELS[0]:
response = df['Measures'].dropna()[123:134]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[1]:
response = df['Measures'].dropna()[196:204] #Remaining
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[2]:
response = df['Measures'].dropna()[113:123]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[3]:
st.write("Plant is healthy take good care of it")
elif selected_item == 'Mango':
CLASS_LABELS = ['Anthracnose', 'Bacterial Canker', 'Cutting Weevil', 'Die Back', 'Gall Midge', 'Healthy','Sooty Mould' ,'Powdery Mildew']
model =load_model(models_path[3])
prediction = import_and_predict(uploaded_image, model, CLASS_LABELS)
st.write("disease name: ", prediction)
if prediction != None:
new_title = '<p style="font-size: 38px">Measures you can take to control: </p>'
st.markdown(new_title, unsafe_allow_html=True)
if prediction == CLASS_LABELS[0]:
response = df['Measures'].dropna()[134:151]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[1]:
response = df['Measures'].dropna()[151:165]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[2]:
response = df['Measures'].dropna()[164:173]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response]) #remaining 2,3,4
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[3]:
response = df['Measures'].dropna()[172:180]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[4]:
response = df['Measures'].dropna()[181:189]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[5]:
st.write("Plant is healthy take good care of it")
elif prediction == CLASS_LABELS[6]:
response = df['Measures'].dropna()[164:174]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
elif prediction == CLASS_LABELS[7]:
response = df['Measures'].dropna()[174:189]
response_text = " ".join([f"<p style='font-size: 18px;'>{text}</p>" for text in response])
st.write(response_text, unsafe_allow_html=True)
|