GrapeDiseaseDetectionCNN / src /streamlit_app.py
Basak Tamer
Update src/streamlit_app.py
4be1f80 verified
Raw History Blame Contribute Delete
2.33 kB
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
# --- PAGE CONFIGURATION ---
st.set_page_config(
page_title="Grape Disease Detector",
page_icon="๐Ÿ‡",
layout="centered"
)
# --- MODEL LOADING ---
@st.cache_resource
def load_grape_model():
# Load your best-performing CNN model
return tf.keras.models.load_model('src/grape_disease_cnnmodel.h5')
try:
model = load_grape_model()
# Explicit class names based on your dataset folders
CLASS_NAMES = ['Black Rot', 'ESCA', 'Healthy', 'Leaf Blight']
except Exception as e:
st.error(f"Error loading model: {e}")
# --- USER INTERFACE ---
st.title("๐Ÿ‡ Grape Leaf Disease Diagnosis")
st.markdown("""
Upload a leaf image to identify the health status.
This system uses a **Custom CNN** (98.5% Accuracy) to detect diseases.
""")
uploaded_file = st.file_uploader("Upload a Grape Leaf Image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file).convert('RGB')
st.image(image, caption='Uploaded Sample', use_container_width=True)
# --- PREPROCESSING ---
# Resizing to 170x170 to match your CNN training input
img_resized = image.resize((170, 170))
img_array = np.array(img_resized)
# Rescaling (Matches rescale=1./255 used in ImageDataGenerator)
img_array = img_array.astype('float32') / 255.0
img_array = np.expand_dims(img_array, axis=0)
# --- PREDICTION ---
with st.spinner('Running Diagnostic...'):
predictions = model.predict(img_array)
score = predictions[0]
result_idx = np.argmax(score)
confidence = score[result_idx] * 100
result_label = CLASS_NAMES[result_idx]
# --- RESULTS DISPLAY ---
st.divider()
if result_label == 'Healthy':
st.success(f"### Result: {result_label}")
else:
st.error(f"### Result: {result_label}")
st.metric(label="Confidence Score", value=f"{confidence:.2f}%")
# Bar chart for all classes
st.write("#### Diagnostic Probability Breakdown")
prob_dict = {CLASS_NAMES[i]: float(score[i]) for i in range(len(CLASS_NAMES))}
st.bar_chart(prob_dict)
st.divider()
st.caption("Developed for Deep Learning Assignment 2026. Models: CNN vs MobileNetV2.")