Spaces:
Sleeping
Sleeping
shaheer-data
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
from tensorflow.keras.models import load_model
|
3 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import os
|
7 |
from huggingface_hub import hf_hub_download, login
|
8 |
|
9 |
-
#
|
10 |
-
st.
|
11 |
|
12 |
# Authentication using Hugging Face token
|
13 |
authkey = os.getenv('YellowRust')
|
@@ -21,66 +21,63 @@ loaded_model = load_model(model_path)
|
|
21 |
|
22 |
# Function to preprocess the uploaded image
|
23 |
def preprocess_image(image):
|
24 |
-
# Resize
|
25 |
-
image = image.resize((224, 224)) # Adjust size based on your model input
|
26 |
image = img_to_array(image) # Convert image to numpy array
|
27 |
image = image / 255.0 # Normalize pixel values to [0, 1]
|
28 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
29 |
return image
|
30 |
|
31 |
-
#
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
|
|
34 |
if uploaded_file is not None:
|
35 |
-
st.subheader("Uploaded Image")
|
36 |
image = Image.open(uploaded_file)
|
37 |
-
st.image(image, caption="Uploaded
|
38 |
|
39 |
# Preprocess the image
|
40 |
processed_image = preprocess_image(image)
|
41 |
|
42 |
-
#
|
43 |
with st.spinner("Predicting..."):
|
44 |
prediction = loaded_model.predict(processed_image)
|
45 |
predicted_class = np.argmax(prediction, axis=1)[0] # Get the class index
|
|
|
46 |
class_labels = ['Healthy', 'Mild Rust (MR)', 'Moderate Rust (MRMS)', 'Severe Rust (MS)', 'Very Severe Rust (R)', 'Extremely Severe Rust (S)']
|
47 |
-
|
48 |
-
st.header("Predicted Severity Class")
|
49 |
|
50 |
-
|
|
|
|
|
51 |
if predicted_class == 0:
|
52 |
-
st.markdown('<p style="color:
|
53 |
st.write("The leaf appears healthy. There is no immediate action required. Continue monitoring as needed.")
|
54 |
elif predicted_class == 1:
|
55 |
-
st.markdown('<p style="color:
|
56 |
-
st.write("Mild rust detected.")
|
57 |
-
st.write("1. Apply fungicides to control rust growth.")
|
58 |
-
st.write("2. Regularly monitor the leaf for further signs of infection.")
|
59 |
elif predicted_class == 2:
|
60 |
-
st.markdown('<p style="color: #
|
61 |
-
st.write("Moderate rust detected.")
|
62 |
-
st.write("1. Continue monitoring the leaf for any progression.")
|
63 |
-
st.write("2. Treat with fungicides as required.")
|
64 |
elif predicted_class == 3:
|
65 |
st.markdown('<p style="color: #FF4500; font-size: 22px; font-weight: bold;">Severe Rust (MS)</p>', unsafe_allow_html=True)
|
66 |
-
st.write("Severe rust detected.")
|
67 |
-
st.write("1. Apply fungicides promptly to control rust spread.")
|
68 |
-
st.write("2. Ensure regular monitoring to prevent further spread.")
|
69 |
elif predicted_class == 4:
|
70 |
-
st.markdown('<p style="color:
|
71 |
-
st.write("Very severe rust detected.")
|
72 |
-
st.write("1. Implement intensive control measures.")
|
73 |
-
st.write("2. Apply fungicides multiple times.")
|
74 |
-
st.write("3. Frequent monitoring is essential.")
|
75 |
elif predicted_class == 5:
|
76 |
-
st.markdown('<p style="color:
|
77 |
-
st.write("Extremely severe rust detected.")
|
78 |
-
|
79 |
-
st.write("2. Seek expert advice for advanced interventions.")
|
80 |
-
st.write("3. Frequent, close monitoring is critical.")
|
81 |
-
|
82 |
confidence = np.max(prediction) * 100
|
83 |
-
st.markdown(f'<p style="color: #17a2b8; font-size: 18px;"
|
84 |
|
85 |
# Footer
|
86 |
-
st.info("MPHIL Final Year Project By Mr. Asim Khattak")
|
|
|
1 |
import streamlit as st
|
2 |
from tensorflow.keras.models import load_model
|
3 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import os
|
7 |
from huggingface_hub import hf_hub_download, login
|
8 |
|
9 |
+
# Set page configuration
|
10 |
+
st.set_page_config(page_title="Yellow Rust Severity Prediction", layout="wide", initial_sidebar_state="expanded")
|
11 |
|
12 |
# Authentication using Hugging Face token
|
13 |
authkey = os.getenv('YellowRust')
|
|
|
21 |
|
22 |
# Function to preprocess the uploaded image
|
23 |
def preprocess_image(image):
|
24 |
+
image = image.resize((224, 224)) # Resize to match model input size
|
|
|
25 |
image = img_to_array(image) # Convert image to numpy array
|
26 |
image = image / 255.0 # Normalize pixel values to [0, 1]
|
27 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
28 |
return image
|
29 |
|
30 |
+
# Sidebar layout with a colorful menu
|
31 |
+
st.sidebar.markdown('<p style="font-size: 24px; color: #2F4F4F; font-weight: bold;">Yellow Rust Prediction</p>', unsafe_allow_html=True)
|
32 |
+
st.sidebar.markdown('<p style="color: #555;">Upload an image of the wheat leaf to predict the severity of yellow rust.</p>', unsafe_allow_html=True)
|
33 |
+
|
34 |
+
# Sidebar elements
|
35 |
+
uploaded_file = st.sidebar.file_uploader("Upload Wheat Leaf Image", type=["jpg", "jpeg", "png"])
|
36 |
+
st.sidebar.markdown("---")
|
37 |
+
|
38 |
+
# Main content
|
39 |
+
st.title("Yellow Rust Severity Prediction")
|
40 |
+
st.markdown('<p style="text-align: center; color: #2F4F4F; font-size: 30px; font-weight: bold;">Yellow Rust Severity Prediction Dashboard</p>', unsafe_allow_html=True)
|
41 |
|
42 |
+
# Display the uploaded image
|
43 |
if uploaded_file is not None:
|
|
|
44 |
image = Image.open(uploaded_file)
|
45 |
+
st.image(image, caption="Uploaded Wheat Leaf", use_column_width=True)
|
46 |
|
47 |
# Preprocess the image
|
48 |
processed_image = preprocess_image(image)
|
49 |
|
50 |
+
# Predict severity with a spinner
|
51 |
with st.spinner("Predicting..."):
|
52 |
prediction = loaded_model.predict(processed_image)
|
53 |
predicted_class = np.argmax(prediction, axis=1)[0] # Get the class index
|
54 |
+
|
55 |
class_labels = ['Healthy', 'Mild Rust (MR)', 'Moderate Rust (MRMS)', 'Severe Rust (MS)', 'Very Severe Rust (R)', 'Extremely Severe Rust (S)']
|
|
|
|
|
56 |
|
57 |
+
st.header("Predicted Severity Class")
|
58 |
+
|
59 |
+
# Conditional statements for displaying the prediction with styled headers and colors
|
60 |
if predicted_class == 0:
|
61 |
+
st.markdown('<p style="color: green; font-size: 22px; font-weight: bold;">Healthy</p>', unsafe_allow_html=True)
|
62 |
st.write("The leaf appears healthy. There is no immediate action required. Continue monitoring as needed.")
|
63 |
elif predicted_class == 1:
|
64 |
+
st.markdown('<p style="color: orange; font-size: 22px; font-weight: bold;">Mild Rust (MR)</p>', unsafe_allow_html=True)
|
65 |
+
st.write("Mild rust detected. Applying fungicides will help control further spread.")
|
|
|
|
|
66 |
elif predicted_class == 2:
|
67 |
+
st.markdown('<p style="color: #FFA500; font-size: 22px; font-weight: bold;">Moderate Rust (MRMS)</p>', unsafe_allow_html=True)
|
68 |
+
st.write("Moderate rust detected. Monitor regularly and treat with fungicides.")
|
|
|
|
|
69 |
elif predicted_class == 3:
|
70 |
st.markdown('<p style="color: #FF4500; font-size: 22px; font-weight: bold;">Severe Rust (MS)</p>', unsafe_allow_html=True)
|
71 |
+
st.write("Severe rust detected. Prompt fungicide application and continued monitoring are recommended.")
|
|
|
|
|
72 |
elif predicted_class == 4:
|
73 |
+
st.markdown('<p style="color: red; font-size: 22px; font-weight: bold;">Very Severe Rust (R)</p>', unsafe_allow_html=True)
|
74 |
+
st.write("Very severe rust detected. Intensive control measures and frequent monitoring are required.")
|
|
|
|
|
|
|
75 |
elif predicted_class == 5:
|
76 |
+
st.markdown('<p style="color: darkred; font-size: 22px; font-weight: bold;">Extremely Severe Rust (S)</p>', unsafe_allow_html=True)
|
77 |
+
st.write("Extremely severe rust detected. Apply aggressive control strategies and seek expert advice.")
|
78 |
+
|
|
|
|
|
|
|
79 |
confidence = np.max(prediction) * 100
|
80 |
+
st.markdown(f'<p style="color: #17a2b8; font-size: 18px; font-weight: bold;">Confidence Level: {confidence:.2f}%</p>', unsafe_allow_html=True)
|
81 |
|
82 |
# Footer
|
83 |
+
st.info("MPHIL Final Year Project By Mr. Asim Khattak", icon="📚")
|