pushpinder06 commited on
Commit
c1a7e1d
·
verified ·
1 Parent(s): ebd9ae4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
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 load_img, img_to_array
4
  import numpy as np
 
5
 
6
- # Load model
7
- model = load_model("waste_classifier_model.h5")
 
 
8
  class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
9
 
10
- def predict_image(image):
11
- # Preprocess
12
- img = image.resize((224, 224))
13
- img_array = img_to_array(img) / 255.0
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
- # Gradio Interface
25
- iface = gr.Interface(
26
- fn=predict_image,
27
- inputs=gr.Image(type="pil"),
28
- outputs=gr.Label(num_top_classes=3),
29
- title="Waste Classification with MobileNetV2",
30
- description="Upload an image of waste (plastic, metal, paper, etc.), and this model will classify it."
 
 
 
 
 
 
31
  )
32
 
33
- iface.launch()
 
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()