Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,78 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
-
import tensorflow as tf
|
4 |
-
from PIL import Image, ImageDraw
|
5 |
-
import gdown
|
6 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
7 |
import os
|
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 |
-
# Theme selection
|
50 |
-
st.sidebar.title("Settings and Preferences")
|
51 |
-
theme = st.sidebar.selectbox("Select Theme", ["Light", "Dark"])
|
52 |
-
if theme == "Dark":
|
53 |
-
st.markdown(
|
54 |
-
"<style>body { background-color: #0e1117; color: white; }</style>",
|
55 |
-
unsafe_allow_html=True,
|
56 |
-
)
|
57 |
-
|
58 |
-
# Title
|
59 |
-
st.title("Yellow Rust Disease Classification Dashboard")
|
60 |
-
|
61 |
-
# User Input Section
|
62 |
-
st.sidebar.header("Upload Image of Plant Leaf")
|
63 |
-
image_file = st.sidebar.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
64 |
-
|
65 |
-
if image_file:
|
66 |
-
st.subheader("Uploaded Image")
|
67 |
-
image = Image.open(image_file)
|
68 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
69 |
-
|
70 |
-
# Preprocess the image for prediction
|
71 |
-
def preprocess_image(img):
|
72 |
-
img = img.resize((224, 224)) # Adjust to model input size
|
73 |
-
img_array = np.array(img) / 255.0 # Normalize
|
74 |
-
return np.expand_dims(img_array, axis=0) # Add batch dimension
|
75 |
-
|
76 |
-
processed_image = preprocess_image(image)
|
77 |
-
|
78 |
-
# Prediction Results
|
79 |
-
predictions = model.predict(processed_image, batch_size=1)
|
80 |
-
class_names = ["0", "MR", "MRMS", "MS", "R", "S"] # Replace with actual class labels from dataset
|
81 |
predicted_class = class_names[np.argmax(predictions)]
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
st.write(f"**Severity Level:** {severity}")
|
91 |
|
92 |
-
|
93 |
-
st.
|
94 |
-
draw = ImageDraw.Draw(image)
|
95 |
-
# Example bounding box coordinates, adjust as necessary
|
96 |
-
draw.rectangle([50, 50, 150, 150], outline="red", width=3) # Example bounding box
|
97 |
-
st.image(image, caption="Highlighted Regions", use_column_width=True)
|
98 |
|
99 |
-
#
|
100 |
-
st.
|
101 |
-
|
102 |
-
st.write(f"{class_name}: {predictions[0][i] * 100:.2f}%")
|
103 |
|
104 |
-
#
|
105 |
-
|
106 |
-
|
107 |
-
st.write("**Precision:** 94.7%") # Replace with actual metric
|
108 |
-
st.write("**Recall:** 93.5%") # Replace with actual metric
|
109 |
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
#
|
115 |
-
st.
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
+
import tensorflow as tf
|
|
|
|
|
4 |
from tensorflow.keras.models import load_model
|
5 |
+
from PIL import Image
|
6 |
+
import requests
|
7 |
+
import tempfile
|
8 |
import os
|
9 |
|
10 |
+
def download_model_from_gdrive():
|
11 |
+
"""Downloads the model from Google Drive."""
|
12 |
+
gdrive_url = "https://drive.google.com/uc?id=1EnokggrC6ymrSibtj2t7IHWb9QVtrehS"
|
13 |
+
output_path = "final_meta_model.keras"
|
14 |
+
|
15 |
+
with requests.get(gdrive_url, stream=True) as r:
|
16 |
+
r.raise_for_status()
|
17 |
+
with open(output_path, "wb") as f:
|
18 |
+
for chunk in r.iter_content(chunk_size=8192):
|
19 |
+
f.write(chunk)
|
20 |
+
|
21 |
+
return output_path
|
22 |
+
|
23 |
+
def load_or_download_model():
|
24 |
+
"""Loads the model from Hugging Face or Google Drive."""
|
25 |
+
hf_model_path = "final_meta_model.keras"
|
26 |
+
|
27 |
+
# Check if model exists locally (from Hugging Face Space storage)
|
28 |
+
if os.path.exists(hf_model_path):
|
29 |
+
model = load_model(hf_model_path)
|
30 |
+
else:
|
31 |
+
st.write("Model not found locally. Downloading from Google Drive...")
|
32 |
+
model_path = download_model_from_gdrive()
|
33 |
+
model = load_model(model_path)
|
34 |
+
|
35 |
+
return model
|
36 |
+
|
37 |
+
def preprocess_image(image):
|
38 |
+
"""Preprocesses the uploaded image for model prediction."""
|
39 |
+
image = image.resize((224, 224)) # Assuming input size of 224x224 for the model
|
40 |
+
image_array = np.array(image)
|
41 |
+
image_array = image_array / 255.0 # Normalize pixel values to [0, 1]
|
42 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
43 |
+
return image_array
|
44 |
+
|
45 |
+
def predict_severity(model, image):
|
46 |
+
"""Predicts the severity using the model."""
|
47 |
+
predictions = model.predict(image)
|
48 |
+
class_names = ['0', 'MR', 'MRMS', 'MS', 'R', 'S']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
predicted_class = class_names[np.argmax(predictions)]
|
50 |
+
return predicted_class, predictions
|
51 |
|
52 |
+
# Streamlit App
|
53 |
+
st.title("Disease Severity Prediction App")
|
54 |
+
st.write("Upload an image to predict the severity of the disease.")
|
55 |
|
56 |
+
# Image Upload
|
57 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
|
58 |
|
59 |
+
if uploaded_image:
|
60 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
# Load model
|
63 |
+
with st.spinner("Loading model..."):
|
64 |
+
model = load_or_download_model()
|
|
|
65 |
|
66 |
+
# Preprocess image
|
67 |
+
image = Image.open(uploaded_image)
|
68 |
+
preprocessed_image = preprocess_image(image)
|
|
|
|
|
69 |
|
70 |
+
# Predict severity
|
71 |
+
with st.spinner("Predicting severity..."):
|
72 |
+
predicted_class, prediction_scores = predict_severity(model, preprocessed_image)
|
73 |
|
74 |
+
# Display results
|
75 |
+
st.success(f"Predicted Class: {predicted_class}")
|
76 |
+
st.write("Prediction Scores:")
|
77 |
+
for class_name, score in zip(['0', 'MR', 'MRMS', 'MS', 'R', 'S'], prediction_scores[0]):
|
78 |
+
st.write(f"{class_name}: {score:.4f}")
|