Taidat commited on
Commit
c8fc9ed
·
verified ·
1 Parent(s): 6b48789

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +30 -22
app.py CHANGED
@@ -2,30 +2,38 @@ import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
 
5
- # Load model
6
- model = tf.keras.models.load_model("model.h5")
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Labels
9
- class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs',
10
- 'metal', 'organic', 'paper', 'plastic']
11
 
12
- # Image preprocessing and prediction
13
- def predict_image(img):
14
- img = tf.image.resize(img, (224, 224))
 
15
  img = tf.cast(img, tf.float32) / 255.0
16
- img = tf.expand_dims(img, axis=0)
17
- preds = model.predict(img)
18
- pred_idx = np.argmax(preds)
19
- confidence = float(np.max(preds)) * 100
20
- return {class_names[pred_idx]: confidence}
21
 
22
- # Gradio app
23
- demo = gr.Interface(
24
- fn=predict_image,
25
- inputs=gr.Image(type="numpy"),
26
  outputs=gr.Label(num_top_classes=3),
27
- title="♻️ Waste Classifier",
28
- description="Classify waste into 9 categories: batteries, clothes, e-waste, glass, light bulbs, metal, organic, paper, plastic."
29
- )
30
-
31
- demo.launch()
 
2
  import tensorflow as tf
3
  import numpy as np
4
 
5
+ # Image size used in training
6
+ IMG_SIZE = (224, 224)
7
+
8
+ # Final label mapping
9
+ index_to_label = {
10
+ 0: "paper",
11
+ 1: "plastic",
12
+ 2: "batteries",
13
+ 3: "metal",
14
+ 4: "glass",
15
+ 5: "clothes",
16
+ 6: "organic",
17
+ 7: "light bulbs",
18
+ 8: "e-waste"
19
+ }
20
 
21
+ # Load trained model
22
+ model = tf.keras.models.load_model("model.h5")
 
23
 
24
+ # Image preprocessing function
25
+ def classify_image(img):
26
+ img = tf.image.resize(img, IMG_SIZE)
27
+ img = tf.expand_dims(img, 0)
28
  img = tf.cast(img, tf.float32) / 255.0
29
+ preds = model.predict(img)[0]
30
+ return {index_to_label[i]: float(preds[i]) for i in range(len(preds))}
 
 
 
31
 
32
+ # Gradio Interface
33
+ gr.Interface(
34
+ fn=classify_image,
35
+ inputs=gr.Image(type="numpy", label="Upload a Waste Image"),
36
  outputs=gr.Label(num_top_classes=3),
37
+ title="♻️ Smart Waste Classifier",
38
+ description="Upload an image of waste to classify it into 9 categories (paper, plastic, batteries, metal, glass, clothes, organic, light bulbs, e-waste). Built with TensorFlow + MobileNetV2"
39
+ ).launch()