Spaces:
Sleeping
Sleeping
Commit ·
4d8779f
1
Parent(s): affebe1
init
Browse files- src/__pycache__/eda.cpython-310.pyc +0 -0
- src/__pycache__/eda.cpython-312.pyc +0 -0
- src/__pycache__/prediction.cpython-310.pyc +0 -0
- src/eda.py +78 -0
- src/prediction.py +72 -0
- src/streamlit_app.py +12 -36
src/__pycache__/eda.cpython-310.pyc
ADDED
|
Binary file (2.68 kB). View file
|
|
|
src/__pycache__/eda.cpython-312.pyc
ADDED
|
Binary file (4.63 kB). View file
|
|
|
src/__pycache__/prediction.cpython-310.pyc
ADDED
|
Binary file (1.92 kB). View file
|
|
|
src/eda.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import seaborn as sns
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
def run():
|
| 9 |
+
st.title('Tomato Leaf Health Classification')
|
| 10 |
+
st.subheader("this page contains the EDA about tomato leaf health classification")
|
| 11 |
+
|
| 12 |
+
# image = Image.open("./src/credit_card.jpg")
|
| 13 |
+
# st.image(image, caption="Credit Card")
|
| 14 |
+
|
| 15 |
+
# write
|
| 16 |
+
st.write("the EDA will explore and analyse classifier tomato leaf health")
|
| 17 |
+
|
| 18 |
+
# fetch dataset
|
| 19 |
+
dataset_dict = load_dataset("Heizsenberg/leaf-image-dataset")
|
| 20 |
+
label_names = dataset_dict["train"].features["label"].names
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
dataset_df = dataset_dict['train'].to_pandas()
|
| 24 |
+
dataset_df["label_name"] = dataset_df["label"].map(dict(enumerate(label_names)))
|
| 25 |
+
|
| 26 |
+
st.write("sample from the dataframe")
|
| 27 |
+
st.write(dataset_df.sample(15))
|
| 28 |
+
|
| 29 |
+
st.write("content of the dataframe")
|
| 30 |
+
st.write("Total images:", len(dataset_df))
|
| 31 |
+
st.write("Total classes:", dataset_df["label"].nunique())
|
| 32 |
+
|
| 33 |
+
st.write("Tomato Leaf Training dataset class distribution")
|
| 34 |
+
fig, ax = plt.subplots(figsize=(10,5))
|
| 35 |
+
sns.countplot(data=dataset_df, x="label_name", order=dataset_df["label_name"].value_counts().index, ax=ax)
|
| 36 |
+
plt.xticks(rotation=90)
|
| 37 |
+
plt.title("Class Distribution")
|
| 38 |
+
|
| 39 |
+
st.pyplot(fig)
|
| 40 |
+
|
| 41 |
+
st.write("sample image size and mode")
|
| 42 |
+
sample_path_obj = random.choice(dataset_df["image"].values)
|
| 43 |
+
sample_path = sample_path_obj['path']
|
| 44 |
+
img = Image.open(sample_path)
|
| 45 |
+
|
| 46 |
+
st.write("Image size:", img.size)
|
| 47 |
+
st.write("Image mode:", img.mode)
|
| 48 |
+
|
| 49 |
+
st.write("sample from each classes")
|
| 50 |
+
fig_samp, ax_samp = plt.subplots(4, 3, figsize=(12,12))
|
| 51 |
+
|
| 52 |
+
# samples = dataset_df.sample(10)
|
| 53 |
+
samples = dataset_df.groupby("label_name").sample(1, random_state=42)
|
| 54 |
+
|
| 55 |
+
for ax, (_, row) in zip(ax_samp.flatten(), samples.iterrows()):
|
| 56 |
+
image_path = row['image']
|
| 57 |
+
img = Image.open(image_path['path'])
|
| 58 |
+
ax.imshow(img)
|
| 59 |
+
ax.set_title(row["label_name"])
|
| 60 |
+
ax.axis("off")
|
| 61 |
+
|
| 62 |
+
plt.tight_layout()
|
| 63 |
+
|
| 64 |
+
# Show inside Streamlit
|
| 65 |
+
st.pyplot(fig_samp)
|
| 66 |
+
|
| 67 |
+
st.write("""
|
| 68 |
+
## Insight
|
| 69 |
+
|
| 70 |
+
1. dataset contains around 16.011 in 10 classes
|
| 71 |
+
2. class distribution generally spread evenly with few exceptions on `tomato_tomato_mosaic_virus` has lowest samples and `Tomato_YellowLeaf_curl_virus` having the largest samples, showing complexity in detecting the diseases and easier detection of tomato mosaic virus
|
| 72 |
+
3. the dataset images is on size (256x256) which needs to be rescaled for lower GPU load
|
| 73 |
+
4. several samples is shown from 10 different classes, showing both healthy and disease afflicted leaves
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
run()
|
src/prediction.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
class_names = [
|
| 7 |
+
'Tomato_Bacterial_spot',
|
| 8 |
+
'Tomato_Early_blight',
|
| 9 |
+
'Tomato_Late_blight',
|
| 10 |
+
'Tomato_Leaf_Mold',
|
| 11 |
+
'Tomato_Septoria_leaf_spot',
|
| 12 |
+
'Tomato_Spider_mites_Two_spotted_spider_mite',
|
| 13 |
+
'Tomato__Target_Spot',
|
| 14 |
+
'Tomato__Tomato_YellowLeaf__Curl_Virus',
|
| 15 |
+
'Tomato__Tomato_mosaic_virus',
|
| 16 |
+
'Tomato_healthy'
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_my_model():
|
| 21 |
+
return tf.keras.models.load_model("leaf_detection_model.keras")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def predict_image(model, img):
|
| 25 |
+
# Preprocess
|
| 26 |
+
img = img.resize((128, 128))
|
| 27 |
+
img_array = np.array(img) / 255.0
|
| 28 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 29 |
+
|
| 30 |
+
# 4. Predict
|
| 31 |
+
predictions = model.predict(img_array)
|
| 32 |
+
predicted_index = np.argmax(predictions[0])
|
| 33 |
+
|
| 34 |
+
predicted_label = class_names[predicted_index]
|
| 35 |
+
|
| 36 |
+
confidence = np.max(predictions[0])
|
| 37 |
+
|
| 38 |
+
return img, predicted_label, confidence
|
| 39 |
+
|
| 40 |
+
def run():
|
| 41 |
+
classifier_model = load_my_model()
|
| 42 |
+
|
| 43 |
+
st.write("upload a tomato leaf image to be predicted")
|
| 44 |
+
|
| 45 |
+
uploaded_file = st.file_uploader(
|
| 46 |
+
"Choose an tomato leaf image to be uploaded",
|
| 47 |
+
type=["JPG", "jpg", "jpeg"] # Specify accepted file types
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if uploaded_file is not None:
|
| 51 |
+
st.success("File uploaded successfully!")
|
| 52 |
+
st.write("Filename:", uploaded_file.name)
|
| 53 |
+
st.write(uploaded_file)
|
| 54 |
+
|
| 55 |
+
st.write("Image")
|
| 56 |
+
# To read image file buffer as a PIL Image:
|
| 57 |
+
img = Image.open(uploaded_file)
|
| 58 |
+
|
| 59 |
+
image, predicted_class, probs = predict_image(
|
| 60 |
+
classifier_model, img
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
confidence = np.max(probs) * 100
|
| 64 |
+
|
| 65 |
+
# show result
|
| 66 |
+
st.image(img, caption="Uploaded Image", use_container_width=True)
|
| 67 |
+
st.success(f"Tomato Leaf Prediction: {predicted_class}")
|
| 68 |
+
st.write(f"with confidence level of: {confidence}")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
run()
|
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,16 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import eda
|
| 3 |
+
import prediction
|
| 4 |
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title="Tomato Leaf Classifier",
|
| 7 |
+
layout = 'wide',
|
| 8 |
+
initial_sidebar_state='expanded'
|
| 9 |
+
)
|
| 10 |
|
| 11 |
+
page = st.sidebar.selectbox('Pilih Page: ', ('EDA', 'Prediction'))
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
if page == 'EDA':
|
| 14 |
+
eda.run()
|
| 15 |
+
else:
|
| 16 |
+
prediction.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|