Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
-
from tensorflow.keras.preprocessing.image import
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
-
# Load model
|
| 7 |
-
model = load_model("
|
|
|
|
|
|
|
| 8 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
img_array = img_to_array(
|
| 14 |
img_array = np.expand_dims(img_array, axis=0)
|
| 15 |
-
|
| 16 |
-
# Predict
|
| 17 |
-
predictions = model.predict(img_array)[0]
|
| 18 |
-
pred_index = np.argmax(predictions)
|
| 19 |
-
confidence = float(np.max(predictions))
|
| 20 |
-
label = class_names[pred_index]
|
| 21 |
-
|
| 22 |
-
return {label: confidence}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 4 |
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# 🔹 Load your saved model
|
| 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 |
+
# 🔹 Prediction function
|
| 14 |
+
def predict_from_camera(image):
|
| 15 |
+
image = image.resize((224, 224)) # Resize for model input
|
| 16 |
+
img_array = img_to_array(image) / 255.0 # Normalize
|
| 17 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
prediction = model.predict(img_array)[0]
|
| 20 |
+
predicted_class = class_names[np.argmax(prediction)]
|
| 21 |
+
confidence = float(np.max(prediction))
|
| 22 |
+
|
| 23 |
+
return f"{predicted_class} ({confidence*100:.1f}%)"
|
| 24 |
+
|
| 25 |
+
# 🔹 Gradio live webcam interface
|
| 26 |
+
interface = gr.Interface(
|
| 27 |
+
fn=predict_from_camera,
|
| 28 |
+
inputs=gr.Image(source="webcam", streaming=True, type="pil"),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="Live Waste Classification",
|
| 31 |
+
description="Show waste to your webcam and the model will predict its type in real-time."
|
| 32 |
)
|
| 33 |
|
| 34 |
+
interface.launch()
|