Create test.py
Browse files
test.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Trained on 80 epochs at Batch size 256 with Learning Rate of 0.001 under 50 seconds!!
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
# Suppress warnings
|
| 9 |
+
import warnings
|
| 10 |
+
warnings.filterwarnings('ignore', category=UserWarning)
|
| 11 |
+
warnings.filterwarnings('ignore', category=FutureWarning)
|
| 12 |
+
|
| 13 |
+
# Disable scientific notation for clarity
|
| 14 |
+
np.set_printoptions(suppress=True)
|
| 15 |
+
|
| 16 |
+
def load_model_teachable():
|
| 17 |
+
# Load the model
|
| 18 |
+
model = tf.keras.models.load_model("cnn_model.h5")
|
| 19 |
+
return model
|
| 20 |
+
|
| 21 |
+
with st.spinner('Model is being loaded..'):
|
| 22 |
+
model = load_model_teachable()
|
| 23 |
+
|
| 24 |
+
st.write("""
|
| 25 |
+
# Teachable Machine Model
|
| 26 |
+
"""
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
st.sidebar.info("You should be happy if it classifies as - Healthy plant π")
|
| 30 |
+
|
| 31 |
+
file = st.file_uploader("Upload the image to be classified", type=["jpg", "png"])
|
| 32 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def upload_predict_teachable(image, model, class_names):
|
| 36 |
+
|
| 37 |
+
# Create the array of the right shape to feed into the keras model
|
| 38 |
+
# The 'length' or number of images you can put into the array is
|
| 39 |
+
# determined by the first position in the shape tuple, in this case 1
|
| 40 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 41 |
+
|
| 42 |
+
# Replace this with the path to your image
|
| 43 |
+
image_RGB = image.convert("RGB")
|
| 44 |
+
|
| 45 |
+
# resizing the image to be at least 224x224 and then cropping from the center
|
| 46 |
+
image_resized = image_RGB.resize((224, 224), resample=Image.LANCZOS)
|
| 47 |
+
|
| 48 |
+
# turn the image into a numpy array
|
| 49 |
+
image_array = np.asarray(image_resized)
|
| 50 |
+
|
| 51 |
+
# Normalize the image
|
| 52 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
|
| 53 |
+
|
| 54 |
+
# Load the image into the array
|
| 55 |
+
data[0] = normalized_image_array
|
| 56 |
+
|
| 57 |
+
# Predicts the model
|
| 58 |
+
prediction = model.predict(data)
|
| 59 |
+
return prediction
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if file is None:
|
| 63 |
+
st.text("Please upload an image file")
|
| 64 |
+
else:
|
| 65 |
+
image = Image.open(file)
|
| 66 |
+
st.image(image, use_column_width=True)
|
| 67 |
+
|
| 68 |
+
# Load the labels
|
| 69 |
+
class_names = open("pages/labels.txt", "r").readlines()
|
| 70 |
+
prediction = upload_predict_teachable(image, model,class_names)
|
| 71 |
+
index = np.argmax(prediction)
|
| 72 |
+
class_name = class_names[index]
|
| 73 |
+
confidence_score = prediction[0][index]
|
| 74 |
+
|
| 75 |
+
# Print prediction and confidence score
|
| 76 |
+
result = f"Your plant is suffering from: {class_name[2:]}"
|
| 77 |
+
result_score = "β
Accurate prediction score is: {} / 100".format("%.2f" % confidence_score)
|
| 78 |
+
st.success(result)
|
| 79 |
+
st.info(result_score)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
st.markdown("""
|
| 83 |
+
#### Know more about this disease and suggestions to prevent them from spreading.
|
| 84 |
+
|
| 85 |
+
- *Just copy the disease name (result) from above & paste it in the below input box as it is!*
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
# Loading diseases info & management strategies
|
| 89 |
+
df = pd.read_csv('pages/Disease_solutions.csv')
|
| 90 |
+
disease = st.text_input('Enter the disease name below π', '')
|
| 91 |
+
|
| 92 |
+
# Selecting rows based on condition
|
| 93 |
+
about = df.loc[df['Diseases'] == disease, ['Type', 'Description']]
|
| 94 |
+
strategies = df.loc[df['Diseases'] == disease, 'Management Strategies']
|
| 95 |
+
|
| 96 |
+
# Print diseases' other info
|
| 97 |
+
tab1, tab2 = st.tabs(["π Description", "β Solution"])
|
| 98 |
+
|
| 99 |
+
with tab1:
|
| 100 |
+
st.dataframe(about, use_container_width=True)
|
| 101 |
+
|
| 102 |
+
with tab2:
|
| 103 |
+
st.dataframe(strategies, use_container_width=True)
|