File size: 1,099 Bytes
af17d6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pickle
import cv2
from PIL import Image

# Load model and label binarizer
@st.cache_resource
def load_model():
    model = pickle.load(open('cnn_model.pkl', 'rb'))
    label_binarizer = pickle.load(open('label_transform.pkl', 'rb'))
    return model, label_binarizer

model, lb = load_model()

# Prediction function
def predict(image):
    image = cv2.resize(image, (256, 256))
    image = image.astype("float") / 255.0
    image = np.expand_dims(image, axis=0)
    preds = model.predict(image)
    return lb.classes_[np.argmax(preds)]

# Streamlit UI
st.title("🌱 Plant Disease Detector")
uploaded_file = st.file_uploader("Upload a leaf image...", type=["jpg", "png"])

if uploaded_file is not None:
    image = Image.open(uploaded_file).convert("RGB")
    st.image(image, caption="Uploaded Image", use_column_width=True)
    
    if st.button("Predict"):
        img_array = np.array(image)
        img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
        prediction = predict(img_array)
        st.success(f"🔍 Prediction: {prediction}")