File size: 2,504 Bytes
f67515f
b60b858
 
fe42aa1
 
b60b858
 
fe42aa1
 
b60b858
 
fe42aa1
87197c0
f67515f
 
 
 
 
 
87197c0
fe42aa1
 
 
b60b858
fe42aa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1a831
 
a6efeab
1a1a831
b60b858
fe42aa1
b60b858
fe42aa1
1a1a831
fe42aa1
1a1a831
 
 
f0f139a
1a1a831
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b60b858
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
import os
import streamlit as st
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.resnet50 import preprocess_input
from io import StringIO



# ========== Load Model ==========

MODEL_PATH = os.path.join("src", "models", "model.keras")

if not os.path.exists(MODEL_PATH):
    st.error(f"❌ Model file not found at: {MODEL_PATH}")
    st.stop()
else:
    model = tf.keras.models.load_model(MODEL_PATH)

# ========== Class Labels ==========
# Replace with your actual class names if needed
class_names = ["battery", "biological", "cardboard", "clothes", "glass", "metal", "paper", "plastic", "shoes", "trash"]

# ========== Sidebar ==========
st.sidebar.title("βš™οΈ Options")
st.sidebar.markdown("""
Upload an image for classification.
- Supported formats: JPG, PNG, JPEG
- Input image will be resized to 224x224
""")

if st.sidebar.button("Show Model Summary"):
    string_io = StringIO()
    model.summary(print_fn=lambda x: string_io.write(x + "\n"))
    summary_string = string_io.getvalue()
    st.sidebar.subheader("πŸ“‹ Model Architecture")
    st.sidebar.text(summary_string)

# ========== Main Title ==========
st.title("🧠 Garbage Classifier App")
st.markdown("Upload an image, and the model will predict the class.")

# ========== File Upload ==========
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

# ========== Preprocessing ==========
IMG_SIZE = (224, 224)
def preprocess_image(img):
    return preprocess_input(np.expand_dims(img_to_array(img.resize(IMG_SIZE)), axis=0))

# ========== Inference ==========
if uploaded_file is not None:
    image = Image.open(uploaded_file).convert("RGB")
    
    # Show uploaded image
    st.image(image, caption="πŸ“· Uploaded Image", use_container_width=True)

    # Preprocess and predict
    processed = preprocess_image(image)
    prediction = model.predict(processed)[0]
    predicted_index = np.argmax(prediction)
    predicted_class = class_names[predicted_index]
    confidence = prediction[predicted_index] * 100

    # Show result
    st.markdown(f"### 🎯 Prediction: `{predicted_class}`")
    st.markdown(f"**Confidence:** {confidence:.2f}%")

    # Display all class probabilities in a table
    st.subheader("πŸ“Š Class Probabilities")
    prob_dict = {class_names[i]: float(f"{prediction[i]:.4f}") for i in range(len(class_names))}
    st.dataframe(prob_dict, use_container_width=True)