File size: 1,838 Bytes
a5f21ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db03642
a5f21ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
from PIL import Image
import io

st.set_page_config(
    page_title="Waste Classifier",
    layout="centered"
)

@st.cache_resource
def load_model():
    return tf.keras.models.load_model('CNN_Prak4_ML.h5')

def preprocess_image(img):
    img = img.resize((244, 244))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = img / 255.0
    return img

LABEL_CLASS = {
    0: "Cardboard",
    1: "Glass",
    2: "Metal",
    3: "Paper",
    4: "Textile Trash",
    5: "Vegetation"
}


def main():
    st.title("Waste Classifier")
    st.write("Upload an image and the model will predict waste image")
    
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image', use_column_width=True)
        
        if st.button('Predict'):
            model = load_model()
            
            processed_image = preprocess_image(image)
            
            with st.spinner('Predicting...'):
                prediction = model.predict(processed_image)
                pred_class = LABEL_CLASS[np.argmax(prediction)]
                confidence = float(prediction.max()) * 100
            
            st.success(f'Prediction: {pred_class.upper()}')
            st.info(f'Confidence: {confidence:.2f}%')
            
            st.write("Class Probabilities:")
            for i, prob in enumerate(prediction[0]):
                st.progress(float(prob))
                st.write(f"{LABEL_CLASS[i]}: {float(prob)*100:.2f}%")

if __name__ == "__main__":
    main()