Taidat commited on
Commit
2035a68
·
verified ·
1 Parent(s): 41e43b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -32
app.py CHANGED
@@ -1,39 +1,48 @@
1
  import gradio as gr
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()
 
 
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ import tensorflow_hub as hub
4
  import numpy as np
5
 
6
+ # Load your trained model
7
+ model = tf.keras.models.load_model("model.h5", custom_objects={'KerasLayer': hub.KerasLayer})
8
+
9
+ # Define your waste class labels
10
+ class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs',
11
+ 'metal', 'organic', 'paper', 'plastic']
12
+ IMG_SIZE = 224
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Image preprocessing function
15
+ def preprocess_image(image):
16
+ image = tf.convert_to_tensor(image, dtype=tf.float32)
17
+ image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE)) / 255.0
18
+ return tf.expand_dims(image, axis=0)
19
+
20
+ # Prediction function
21
+ def predict_waste(image):
22
+ processed = preprocess_image(image)
23
+ preds = model.predict(processed)
24
+ label = class_names[np.argmax(preds)]
25
+ confidence = float(np.max(preds))
26
+ return f"{label} ({confidence:.2f})"
27
+
28
+ # Example images – update these paths with actual examples in your Colab space
29
+ examples = [
30
+ ["examples/Zeitungen-und-Zeitschriften.png"],
31
+ ["examples/hazardous-toxic-electronic-waste-mixture-260nw-1593448036.jpg"],
32
+ ["examples/composting_medium-size.jpg"],
33
+ ["examples/zero-waste-recycling-wooden-thing-wooden-background-zero-waste-recycling-wooden-thing-wooden-background-flat-lay-158056259.jpg"],
34
+ ["examples/plastic waste.png"]
35
+ ]
36
+
37
+
38
+ # Launch Gradio Interface
39
+ iface = gr.Interface(
40
+ fn=predict_waste,
41
  inputs=gr.Image(type="numpy", label="Upload a Waste Image"),
42
+ outputs="text",
43
+ title="Waste Classification Model 🌱",
44
+ description="Upload an image of a waste item to classify it as one of 9 categories.",
45
+ examples=examples
46
+ )
47
+
48
+ iface.launch()