File size: 2,978 Bytes
e51a9e2
3f5fa7c
e51a9e2
 
 
 
 
 
 
16ceb19
 
 
 
 
e51a9e2
 
a981952
e51a9e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f5fa7c
e51a9e2
 
 
 
 
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
# app.py  --- Grapevine Leaf Disease Classifier using CNN (without Transfer Learning)
import streamlit as st
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

st.set_page_config(
    page_title="Grapevine CNN Classifier (No Transfer Learning)",
    layout="centered"
)


# ------------------ SETTINGS ------------------
MODEL_PATH = "src/grape_cnn_final_optionA.keras"  # CNN model (trained from scratch)
IMG_SIZE = (224, 224)  # Based on your training setup

# Class labels (same order as during training)
CLASS_NAMES = [
    "Black Rot",
    "Esca (Black Measles)",
    "Leaf Blight (Isariopsis Leaf Spot)",
    "Healthy"
]

# ------------------ LOAD MODEL ------------------
@st.cache_resource
def load_model_cached():
    return load_model(MODEL_PATH)

model = load_model_cached()

# ------------------ IMAGE PREPROCESSING ------------------
def preprocess_image(image: Image.Image) -> np.ndarray:
    image = image.convert("RGB")
    image = image.resize(IMG_SIZE)
    img_array = np.array(image) / 255.0  # Normalization (used during training)
    return np.expand_dims(img_array, axis=0)

# ------------------ PREDICTION ------------------
def predict(image: Image.Image):
    processed_img = preprocess_image(image)
    preds = model.predict(processed_img)
    probs = tf.nn.softmax(preds[0]).numpy()  # In case final layer used raw logits
    top_idx = np.argmax(probs)
    return CLASS_NAMES[top_idx], probs[top_idx], probs

# ------------------ STREAMLIT UI ------------------
st.title("πŸ‡ Grapevine Leaf Disease Classifier (CNN – No Transfer Learning)")
st.write(
    "This app uses your custom Convolutional Neural Network trained **from scratch** "
    "to classify grapevine leaf images into disease categories."
)

# --- Model Info ---
st.sidebar.header("πŸ”§ Model Details")
st.sidebar.write(f"**Model file:** `{MODEL_PATH}`")
st.sidebar.write(f"**Input size:** {IMG_SIZE[0]} Γ— {IMG_SIZE[1]}")
st.sidebar.write("**Class labels:**")
for cls in CLASS_NAMES:
    st.sidebar.write(f"- {cls}")

# --- File Upload ---
uploaded_file = st.file_uploader("πŸ“€ Upload a grapevine leaf image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="πŸ“Έ Uploaded Image", use_container_width=True)

    if st.button("πŸ” Predict Disease"):
        with st.spinner("Analyzing image..."):
            top_class, top_prob, probabilities = predict(image)

        st.subheader("πŸ”Ž Prediction Result")
        st.write(f"**Predicted Disease:** `{top_class}`")
        st.write(f"**Confidence:** {top_prob:.2%}")

        st.subheader("πŸ“Š Probability per Class")
        for cls, prob in zip(CLASS_NAMES, probabilities):
            st.write(f"- **{cls}** β†’ {prob:.2%}")
else:
    st.info("Please upload a grapevine leaf image to start the prediction.")