pushpinder06 commited on
Commit
f8e17cc
·
verified ·
1 Parent(s): 61cf9bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
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 your trained model
9
- model = load_model("waste_classification(Mobilenetv2).h5")
10
  class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
11
 
12
- # Prediction function: returns label + chart
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
- # Plot bar chart
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 interface: webcam + upload
35
- gr.Interface(
36
- fn=predict_with_chart,
37
- inputs=gr.Image(type="pil", label="Upload or Capture Waste Image", source="upload"),
38
- outputs=[
39
- gr.Textbox(label="Predicted Class"),
40
- gr.Plot(label="Class Probability Chart")
41
- ],
42
- title="Waste Classifier - Upload or Webcam",
43
- description="Upload a waste image or capture using webcam. The model shows predicted class and probability chart.",
44
- live=True
45
- ).launch()
 
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()