Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,15 @@ import numpy as np
|
|
| 5 |
from PIL import Image
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
model = load_model("waste_classification(Mobilenetv2).h5")
|
| 10 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 11 |
|
| 12 |
-
# Prediction function:
|
| 13 |
def predict_with_chart(image):
|
|
|
|
|
|
|
|
|
|
| 14 |
image = image.resize((224, 224))
|
| 15 |
img_array = img_to_array(image) / 255.0
|
| 16 |
img_array = np.expand_dims(img_array, axis=0)
|
|
@@ -20,7 +23,7 @@ def predict_with_chart(image):
|
|
| 20 |
pred_label = class_names[pred_index]
|
| 21 |
confidence = float(np.max(prediction))
|
| 22 |
|
| 23 |
-
#
|
| 24 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 25 |
ax.bar(class_names, prediction, color='skyblue')
|
| 26 |
ax.set_ylabel('Probability')
|
|
@@ -31,15 +34,15 @@ def predict_with_chart(image):
|
|
| 31 |
|
| 32 |
return f"Prediction: {pred_label} ({confidence*100:.1f}%)", fig
|
| 33 |
|
| 34 |
-
# Gradio
|
| 35 |
-
gr.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
gr.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
+
# Load model (ignore compile warning — you're only predicting)
|
| 9 |
+
model = load_model("waste_classification(Mobilenetv2).h5", compile=False)
|
| 10 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 11 |
|
| 12 |
+
# Prediction function: outputs label + chart
|
| 13 |
def predict_with_chart(image):
|
| 14 |
+
if image is None:
|
| 15 |
+
return "No image received", None
|
| 16 |
+
|
| 17 |
image = image.resize((224, 224))
|
| 18 |
img_array = img_to_array(image) / 255.0
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
| 23 |
pred_label = class_names[pred_index]
|
| 24 |
confidence = float(np.max(prediction))
|
| 25 |
|
| 26 |
+
# Create bar chart
|
| 27 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 28 |
ax.bar(class_names, prediction, color='skyblue')
|
| 29 |
ax.set_ylabel('Probability')
|
|
|
|
| 34 |
|
| 35 |
return f"Prediction: {pred_label} ({confidence*100:.1f}%)", fig
|
| 36 |
|
| 37 |
+
# Gradio Interface (Gradio 4.x compatible)
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("## 🗑️ Waste Classifier — Upload or Capture an Image")
|
| 40 |
+
with gr.Row():
|
| 41 |
+
image_input = gr.Image(type="pil", label="Upload or Webcam (click camera icon)")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
label_output = gr.Textbox(label="Predicted Class")
|
| 44 |
+
plot_output = gr.Plot(label="Class Probability Chart")
|
| 45 |
+
|
| 46 |
+
image_input.change(fn=predict_with_chart, inputs=image_input, outputs=[label_output, plot_output])
|
| 47 |
+
|
| 48 |
+
demo.launch()
|