|
|
|
|
|
from PIL import Image, ImageOps |
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
import streamlit as st |
|
|
import tensorflow as tf |
|
|
|
|
|
|
|
|
import warnings |
|
|
warnings.filterwarnings('ignore', category=UserWarning) |
|
|
warnings.filterwarnings('ignore', category=FutureWarning) |
|
|
|
|
|
|
|
|
np.set_printoptions(suppress=True) |
|
|
|
|
|
def load_model_teachable(): |
|
|
|
|
|
model = tf.keras.models.load_model("cnn_model.h5") |
|
|
return model |
|
|
|
|
|
with st.spinner('Model is being loaded..'): |
|
|
model = load_model_teachable() |
|
|
|
|
|
st.write(""" |
|
|
# Teachable Machine Model |
|
|
""" |
|
|
) |
|
|
|
|
|
st.sidebar.info("You should be happy if it classifies as - Healthy plant π") |
|
|
|
|
|
file = st.file_uploader("Upload the image to be classified", type=["jpg", "png"]) |
|
|
st.set_option('deprecation.showfileUploaderEncoding', False) |
|
|
|
|
|
|
|
|
def upload_predict_teachable(image, model, class_names): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32) |
|
|
|
|
|
|
|
|
image_RGB = image.convert("RGB") |
|
|
|
|
|
|
|
|
image_resized = image_RGB.resize((224, 224), resample=Image.LANCZOS) |
|
|
|
|
|
|
|
|
image_array = np.asarray(image_resized) |
|
|
|
|
|
|
|
|
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1 |
|
|
|
|
|
|
|
|
data[0] = normalized_image_array |
|
|
|
|
|
|
|
|
prediction = model.predict(data) |
|
|
return prediction |
|
|
|
|
|
|
|
|
if file is None: |
|
|
st.text("Please upload an image file") |
|
|
else: |
|
|
image = Image.open(file) |
|
|
st.image(image, use_column_width=True) |
|
|
|
|
|
|
|
|
class_names = open("pages/labels.txt", "r").readlines() |
|
|
prediction = upload_predict_teachable(image, model,class_names) |
|
|
index = np.argmax(prediction) |
|
|
class_name = class_names[index] |
|
|
confidence_score = prediction[0][index] |
|
|
|
|
|
|
|
|
result = f"Your plant is suffering from: {class_name[2:]}" |
|
|
result_score = "β
Accurate prediction score is: {} / 100".format("%.2f" % confidence_score) |
|
|
st.success(result) |
|
|
st.info(result_score) |
|
|
|
|
|
|
|
|
st.markdown(""" |
|
|
#### Know more about this disease and suggestions to prevent them from spreading. |
|
|
|
|
|
- *Just copy the disease name (result) from above & paste it in the below input box as it is!* |
|
|
""") |
|
|
|
|
|
|
|
|
df = pd.read_csv('pages/Disease_solutions.csv') |
|
|
disease = st.text_input('Enter the disease name below π', '') |
|
|
|
|
|
|
|
|
about = df.loc[df['Diseases'] == disease, ['Type', 'Description']] |
|
|
strategies = df.loc[df['Diseases'] == disease, 'Management Strategies'] |
|
|
|
|
|
|
|
|
tab1, tab2 = st.tabs(["π Description", "β Solution"]) |
|
|
|
|
|
with tab1: |
|
|
st.dataframe(about, use_container_width=True) |
|
|
|
|
|
with tab2: |
|
|
st.dataframe(strategies, use_container_width=True) |