Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,22 @@ from tensorflow.keras.preprocessing.image import img_to_array
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load model
|
| 9 |
model = load_model("waste_classification(Mobilenetv2).h5", compile=False)
|
| 10 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def predict_with_chart(image):
|
| 14 |
if image is None:
|
| 15 |
return "No image received", None
|
|
@@ -23,7 +33,18 @@ def predict_with_chart(image):
|
|
| 23 |
pred_label = class_names[pred_index]
|
| 24 |
confidence = float(np.max(prediction))
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 28 |
ax.bar(class_names, prediction, color='skyblue')
|
| 29 |
ax.set_ylabel('Probability')
|
|
@@ -34,9 +55,9 @@ def predict_with_chart(image):
|
|
| 34 |
|
| 35 |
return f"Prediction: {pred_label} ({confidence*100:.1f}%)", fig
|
| 36 |
|
| 37 |
-
# Gradio
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("## 🗑️ Waste Classifier — Upload or
|
| 40 |
with gr.Row():
|
| 41 |
image_input = gr.Image(type="pil", label="Upload or Webcam (click camera icon)")
|
| 42 |
with gr.Row():
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
+
import csv
|
| 8 |
+
import os
|
| 9 |
+
from datetime import datetime
|
| 10 |
|
| 11 |
+
# Load model
|
| 12 |
model = load_model("waste_classification(Mobilenetv2).h5", compile=False)
|
| 13 |
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 14 |
|
| 15 |
+
# CSV header
|
| 16 |
+
csv_file = "predictions.csv"
|
| 17 |
+
if not os.path.exists(csv_file):
|
| 18 |
+
with open(csv_file, mode='w', newline='') as file:
|
| 19 |
+
writer = csv.writer(file)
|
| 20 |
+
writer.writerow(["timestamp", "predicted_class", "confidence", "probabilities", "source"])
|
| 21 |
+
|
| 22 |
+
# Prediction + save to CSV
|
| 23 |
def predict_with_chart(image):
|
| 24 |
if image is None:
|
| 25 |
return "No image received", None
|
|
|
|
| 33 |
pred_label = class_names[pred_index]
|
| 34 |
confidence = float(np.max(prediction))
|
| 35 |
|
| 36 |
+
# Log prediction to CSV
|
| 37 |
+
with open(csv_file, mode='a', newline='') as file:
|
| 38 |
+
writer = csv.writer(file)
|
| 39 |
+
writer.writerow([
|
| 40 |
+
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
| 41 |
+
pred_label,
|
| 42 |
+
round(confidence, 4),
|
| 43 |
+
[round(p, 4) for p in prediction.tolist()],
|
| 44 |
+
"upload_or_webcam"
|
| 45 |
+
])
|
| 46 |
+
|
| 47 |
+
# Plot class probabilities
|
| 48 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 49 |
ax.bar(class_names, prediction, color='skyblue')
|
| 50 |
ax.set_ylabel('Probability')
|
|
|
|
| 55 |
|
| 56 |
return f"Prediction: {pred_label} ({confidence*100:.1f}%)", fig
|
| 57 |
|
| 58 |
+
# Gradio UI
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("## 🗑️ Waste Classifier — Upload or Webcam\nAutomatically logs predictions to CSV.")
|
| 61 |
with gr.Row():
|
| 62 |
image_input = gr.Image(type="pil", label="Upload or Webcam (click camera icon)")
|
| 63 |
with gr.Row():
|