Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,14 @@ from tensorflow.keras.preprocessing.image import img_to_array
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
model = load_model("
|
| 9 |
-
|
| 10 |
-
# 🔹 Define your class labels (must match model training)
|
| 11 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
def
|
| 15 |
-
image = image.resize((224, 224))
|
| 16 |
-
img_array = img_to_array(image) / 255.0
|
| 17 |
img_array = np.expand_dims(img_array, axis=0)
|
| 18 |
|
| 19 |
prediction = model.predict(img_array)[0]
|
|
@@ -22,13 +20,13 @@ def predict_from_camera(image):
|
|
| 22 |
|
| 23 |
return f"{predicted_class} ({confidence*100:.1f}%)"
|
| 24 |
|
| 25 |
-
#
|
| 26 |
interface = gr.Interface(
|
| 27 |
-
fn=
|
| 28 |
-
inputs=gr.
|
| 29 |
outputs="text",
|
| 30 |
-
title="Live Waste
|
| 31 |
-
description="
|
| 32 |
)
|
| 33 |
|
| 34 |
interface.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load model
|
| 8 |
+
model = load_model("waste_classifier_model.h5")
|
|
|
|
|
|
|
| 9 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 10 |
|
| 11 |
+
# Prediction function
|
| 12 |
+
def predict_from_webcam(image):
|
| 13 |
+
image = image.resize((224, 224))
|
| 14 |
+
img_array = img_to_array(image) / 255.0
|
| 15 |
img_array = np.expand_dims(img_array, axis=0)
|
| 16 |
|
| 17 |
prediction = model.predict(img_array)[0]
|
|
|
|
| 20 |
|
| 21 |
return f"{predicted_class} ({confidence*100:.1f}%)"
|
| 22 |
|
| 23 |
+
# Use gr.Camera instead of gr.Image
|
| 24 |
interface = gr.Interface(
|
| 25 |
+
fn=predict_from_webcam,
|
| 26 |
+
inputs=gr.Camera(type="pil"),
|
| 27 |
outputs="text",
|
| 28 |
+
title="Live Waste Classifier",
|
| 29 |
+
description="Point your webcam at a waste item. The model will predict its type in real-time."
|
| 30 |
)
|
| 31 |
|
| 32 |
interface.launch()
|