Spaces:
Sleeping
Sleeping
import streamlit as st | |
from tensorflow.keras.models import load_model | |
from tensorflow.keras.preprocessing.image import img_to_array | |
from PIL import Image | |
import numpy as np | |
import os | |
from huggingface_hub import hf_hub_download, login | |
# Set page configuration | |
st.set_page_config(page_title="Yellow Rust Severity Prediction", layout="wide", initial_sidebar_state="expanded") | |
# main content | |
st.markdown('<p style="text-align: center; color: #eaf26f; font-size: 40px; font-weight: bold;">Yellow Rust Severity Prediction Dashboard</p>', unsafe_allow_html=True) | |
# Authentication using Hugging Face token | |
authkey = os.getenv('YellowRust') | |
login(token=authkey) | |
model_id = os.getenv('model_repo_id') | |
model_file = os.getenv('model_path') | |
# Download the model file from Hugging Face | |
model_path = hf_hub_download(repo_id=model_id, filename=model_file) | |
# Load the pre-trained model | |
loaded_model = load_model(model_path) | |
# Function to preprocess the uploaded image | |
def preprocess_image(image): | |
image = image.resize((224, 224)) # Resize to match model input size | |
image = img_to_array(image) # Convert image to numpy array | |
image = image / 255.0 # Normalize pixel values to [0, 1] | |
image = np.expand_dims(image, axis=0) # Add batch dimension | |
return image | |
# Sidebar layout with a colorful menu | |
st.sidebar.markdown('<p style="font-size: 24px; color: #2F4F4F; font-weight: bold;">Yellow Rust Prediction</p>', unsafe_allow_html=True) | |
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) | |
# Sidebar elements | |
uploaded_file = st.sidebar.file_uploader("Upload Wheat Leaf Image", type=["jpg", "jpeg", "png"]) | |
st.sidebar.markdown("---") | |
# Display the uploaded image | |
if uploaded_file is not None: | |
image = Image.open(uploaded_file) | |
st.sidebar.image(image, caption="Uploaded Wheat Leaf", use_container_width=True) | |
# Preprocess the image | |
processed_image = preprocess_image(image) | |
# Predict severity with a spinner | |
with st.spinner("Predicting..."): | |
prediction = loaded_model.predict(processed_image) | |
predicted_class = np.argmax(prediction, axis=1)[0] # Get the class index | |
class_labels = ['Healthy', 'Mild Rust (MR)', 'Moderate Rust (MRMS)', 'Severe Rust (MS)', 'Very Severe Rust (R)', 'Extremely Severe Rust (S)'] | |
st.header("Predicted Severity Class") | |
# Conditional statements for displaying the prediction with styled headers and colors | |
if predicted_class == 0: | |
st.markdown('<p style="color: green; font-size: 22px; font-weight: bold;">Healthy</p>', unsafe_allow_html=True) | |
st.write("The leaf appears healthy. There is no immediate action required. Continue monitoring as needed.") | |
elif predicted_class == 1: | |
st.markdown('<p style="color: orange; font-size: 22px; font-weight: bold;">Mild Rust (MR)</p>', unsafe_allow_html=True) | |
st.write("Mild rust detected. Applying fungicides will help control further spread.") | |
elif predicted_class == 2: | |
st.markdown('<p style="color: #FFA500; font-size: 22px; font-weight: bold;">Moderate Rust (MRMS)</p>', unsafe_allow_html=True) | |
st.write("Moderate rust detected. Monitor regularly and treat with fungicides.") | |
elif predicted_class == 3: | |
st.markdown('<p style="color: #FF4500; font-size: 22px; font-weight: bold;">Severe Rust (MS)</p>', unsafe_allow_html=True) | |
st.write("Severe rust detected. Prompt fungicide application and continued monitoring are recommended.") | |
elif predicted_class == 4: | |
st.markdown('<p style="color: red; font-size: 22px; font-weight: bold;">Very Severe Rust (R)</p>', unsafe_allow_html=True) | |
st.write("Very severe rust detected. Intensive control measures and frequent monitoring are required.") | |
elif predicted_class == 5: | |
st.markdown('<p style="color: darkred; font-size: 22px; font-weight: bold;">Extremely Severe Rust (S)</p>', unsafe_allow_html=True) | |
st.write("Extremely severe rust detected. Apply aggressive control strategies and seek expert advice.") | |
confidence = np.max(prediction) * 100 | |
st.markdown(f'<p style="color: #17a2b8; font-size: 18px; font-weight: bold;">Confidence Level: {confidence:.2f}%</p>', unsafe_allow_html=True) | |
# Footer | |
st.info("MPHIL Final Year Project By Mr. Asim Khattak", icon="π") | |