Spaces:
Sleeping
Sleeping
File size: 4,444 Bytes
2c5cec5 749cc5e 0b86400 dfbae18 02acf5d dfbae18 0b86400 19d3a8a 2b40245 e997fea a820da6 dfbae18 0b86400 02acf5d 730cb6b c0beb83 dfbae18 c0beb83 dfbae18 c0beb83 02acf5d 730cb6b 02acf5d 730cb6b c0beb83 ac847e4 c0beb83 0b86400 c0beb83 ac847e4 0b86400 c0beb83 0b86400 c0beb83 0b86400 c0beb83 0b86400 c0beb83 0b86400 c0beb83 0b86400 c0beb83 0b86400 ac847e4 |
1 2 3 4 5 6 7 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array # Import this function
from PIL import Image
import numpy as np
import os
from huggingface_hub import hf_hub_download, login
# Title of the Streamlit app
st.title("Yellow Rust Severity Prediction")
# Authentication using Hugging Face token
authkey = os.getenv('YellowRust')
login(token=authkey)
# Download the model file from Hugging Face
model_path = hf_hub_download(repo_id="shaheer-data/Yellow-Rust-Prediction", filename="final_meta_model.keras")
# Load the pre-trained model
loaded_model = load_model(model_path)
# Function to preprocess the uploaded image
def preprocess_image(image):
# Resize the image to match the model input size (e.g., 224x224 for many pre-trained models)
image = image.resize((224, 224)) # Adjust size based on your model input
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
# Streamlit file uploader
uploaded_file = st.file_uploader("Upload a wheat leaf image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
st.subheader("Uploaded Image")
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True, width=500)
# Preprocess the image
processed_image = preprocess_image(image)
# Perform prediction
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")
# Colorful headings and styled responses
if predicted_class == 0:
st.markdown('<p style="color: #28a745; 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: #FFA500; font-size: 22px; font-weight: bold;">Mild Rust (MR)</p>', unsafe_allow_html=True)
st.write("Mild rust detected.")
st.write("1. Apply fungicides to control rust growth.")
st.write("2. Regularly monitor the leaf for further signs of infection.")
elif predicted_class == 2:
st.markdown('<p style="color: #FF6347; font-size: 22px; font-weight: bold;">Moderate Rust (MRMS)</p>', unsafe_allow_html=True)
st.write("Moderate rust detected.")
st.write("1. Continue monitoring the leaf for any progression.")
st.write("2. Treat with fungicides as required.")
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.")
st.write("1. Apply fungicides promptly to control rust spread.")
st.write("2. Ensure regular monitoring to prevent further spread.")
elif predicted_class == 4:
st.markdown('<p style="color: #dc3545; font-size: 22px; font-weight: bold;">Very Severe Rust (R)</p>', unsafe_allow_html=True)
st.write("Very severe rust detected.")
st.write("1. Implement intensive control measures.")
st.write("2. Apply fungicides multiple times.")
st.write("3. Frequent monitoring is essential.")
elif predicted_class == 5:
st.markdown('<p style="color: #8B0000; font-size: 22px; font-weight: bold;">Extremely Severe Rust (S)</p>', unsafe_allow_html=True)
st.write("Extremely severe rust detected.")
st.write("1. Apply aggressive control strategies.")
st.write("2. Seek expert advice for advanced interventions.")
st.write("3. Frequent, close monitoring is critical.")
confidence = np.max(prediction) * 100
st.markdown(f'<p style="color: #17a2b8; font-size: 18px;">**Confidence Level:** {confidence:.2f}%</p>', unsafe_allow_html=True)
# Footer
st.info("MPHIL Final Year Project By Mr. Asim Khattak")
|