Spaces:
Sleeping
Sleeping
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"keras_model.h5", compile=False, custom_objects={"DepthwiseConv2D": PatchedDepthwiseConv2D}) | |
# Load class labels | |
with open(r"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) |