File size: 2,334 Bytes
0de6eb2
8f976ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4be1f80
8f976ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0de6eb2
8f976ee
 
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
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.")