File size: 2,031 Bytes
ae70fe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import DepthwiseConv2D
from PIL import Image, ImageOps
import numpy as np

# Optional: Patch DepthwiseConv2D if needed
class PatchedDepthwiseConv2D(DepthwiseConv2D):
    def __init__(self, *args, groups=1, **kwargs):
        super().__init__(*args, **kwargs)

# Load model
model = load_model(r"D:\garbage\keras_model.h5", compile=False, custom_objects={"DepthwiseConv2D": PatchedDepthwiseConv2D})

# Load class labels
with open(r"D:\garbage\labels.txt", "r") as f:
    class_names = f.readlines()

st.title("♻️ Garbage Classification Predictor")

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

if st.button("🧪 Predict Waste Type"):
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, use_container_width=True)


        # Preprocess image
        image = image.convert("RGB")
        image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
        image_array = np.asarray(image)
        normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
        data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
        data[0] = normalized_image_array

        # Make prediction
        prediction = model.predict(data)
        index = np.argmax(prediction)
        predicted_label = class_names[index].strip()
        confidence = prediction[0][index]

        # Display result
        st.success(f"Predicted Waste Type: **{predicted_label.upper()}**")
        st.write(f"Confidence Score: **{confidence:.2f}**")
        st.write("♻️ Dispose responsibly!")
    else:
        st.warning("⚠️ Please upload an image before predicting.")
# 🔚 Footer
st.markdown("---")
st.markdown("<p style='text-align: center; font-size: 18px;'>Developed with ❤️ By Twinkle Ghangare for EDUNET FOUNDATION </p>", unsafe_allow_html=True)