pushpinder06 commited on
Commit
94d5bed
·
verified ·
1 Parent(s): 59c568c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
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
- # 🔹 Load your saved model
8
- model = load_model("waste_classification(Mobilenetv2).h5")
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]
@@ -22,13 +20,13 @@ def predict_from_camera(image):
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()
 
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()