pushpinder06's picture
Update app.py
c1a7e1d verified
raw
history blame
1.12 kB
import gradio as gr
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
import numpy as np
from PIL import Image
# ๐Ÿ”น Load your saved model
model = load_model("")
# ๐Ÿ”น Define your class labels (must match model training)
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
# ๐Ÿ”น Prediction function
def predict_from_camera(image):
image = image.resize((224, 224)) # Resize for model input
img_array = img_to_array(image) / 255.0 # Normalize
img_array = np.expand_dims(img_array, axis=0)
prediction = model.predict(img_array)[0]
predicted_class = class_names[np.argmax(prediction)]
confidence = float(np.max(prediction))
return f"{predicted_class} ({confidence*100:.1f}%)"
# ๐Ÿ”น Gradio live webcam interface
interface = gr.Interface(
fn=predict_from_camera,
inputs=gr.Image(source="webcam", streaming=True, type="pil"),
outputs="text",
title="Live Waste Classification",
description="Show waste to your webcam and the model will predict its type in real-time."
)
interface.launch()