Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,30 +2,38 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
'metal', 'organic', 'paper', 'plastic']
|
| 11 |
|
| 12 |
-
# Image preprocessing
|
| 13 |
-
def
|
| 14 |
-
img = tf.image.resize(img,
|
|
|
|
| 15 |
img = tf.cast(img, tf.float32) / 255.0
|
| 16 |
-
|
| 17 |
-
preds
|
| 18 |
-
pred_idx = np.argmax(preds)
|
| 19 |
-
confidence = float(np.max(preds)) * 100
|
| 20 |
-
return {class_names[pred_idx]: confidence}
|
| 21 |
|
| 22 |
-
# Gradio
|
| 23 |
-
|
| 24 |
-
fn=
|
| 25 |
-
inputs=gr.Image(type="numpy"),
|
| 26 |
outputs=gr.Label(num_top_classes=3),
|
| 27 |
-
title="♻️ Waste Classifier",
|
| 28 |
-
description="
|
| 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()
|
|
|
|
|
|