Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 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
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
inputs=gr.Image(type="numpy", label="Upload a Waste Image"),
|
| 36 |
-
outputs=
|
| 37 |
-
title="
|
| 38 |
-
description="Upload an image of waste to classify it
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|