shaheer-data's picture
Update app.py
03e9f6a verified
raw
history blame
3.23 kB
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.sidebar.file_uploader("Upload a wheat leaf image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
st.sidebar.subheader("Uploaded Image")
image = Image.open(uploaded_file)
st.sidebar.image(image, caption="Uploaded Image", use_container_width=True)
# Preprocess the image
processed_image = preprocess_image(image)
st.subheader("Prediction: With 97% Accuracy")
# 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 = ['0', 'MR', 'MRMS', 'MS', 'R', 'S'] # Update based on your classes
if predicted_class == 0:
st.write("Predicted Severity Class: Healthy")
st.write("**Advice:** The leaf appears healthy. Continue monitoring as needed.")
elif predicted_class == 1:
st.write("Predicted Severity Class: Mild Rust (MR)")
st.write("**Advice:** Apply fungicides to control mild rust development.")
elif predicted_class == 2:
st.write("Predicted Severity Class: Moderate Rust (MRMS)")
st.write("**Advice:** Monitor regularly and treat with appropriate fungicides.")
elif predicted_class == 3:
st.write("Predicted Severity Class: Severe Rust (MS)")
st.write("**Advice:** Apply fungicides promptly and continue monitoring.")
elif predicted_class == 4:
st.write("Predicted Severity Class: Very Severe Rust (R)")
st.write("**Advice:** Implement intensive control measures and frequent monitoring.")
elif predicted_class == 5:
st.write("Predicted Severity Class: Extremely Severe Rust (S)")
st.write("**Advice:** Apply aggressive control strategies and seek expert advice.")
confidence = np.max(prediction) * 100
st.write(f"**Confidence Level:** {confidence:.2f}%")
# Footer
st.sidebar.info("MPHIL Final Year Project By Mr. Asim Khattak")