Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,30 +6,30 @@ from PIL import Image
|
|
| 6 |
# Load model
|
| 7 |
model = tf.keras.models.load_model("model.h5")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic']
|
| 11 |
-
|
| 12 |
-
# Match training input size
|
| 13 |
IMG_SIZE = (224, 224)
|
| 14 |
|
| 15 |
def preprocess(image):
|
| 16 |
image = image.resize(IMG_SIZE)
|
| 17 |
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
| 18 |
-
image_array = image_array / 255.0 # Normalize
|
| 19 |
return np.expand_dims(image_array, axis=0)
|
| 20 |
|
| 21 |
def predict(image):
|
| 22 |
input_tensor = preprocess(image)
|
| 23 |
predictions = model.predict(input_tensor)[0]
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
demo = gr.Interface(
|
| 29 |
fn=predict,
|
| 30 |
-
inputs=gr.Image(type="pil", label="Upload an image of a recyclable item
|
| 31 |
-
outputs=gr.Textbox(label="
|
| 32 |
-
title="Waste Image Classifier"
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
|
|
|
| 6 |
# Load model
|
| 7 |
model = tf.keras.models.load_model("model.h5")
|
| 8 |
|
| 9 |
+
# Classes in training order
|
| 10 |
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic']
|
|
|
|
|
|
|
| 11 |
IMG_SIZE = (224, 224)
|
| 12 |
|
| 13 |
def preprocess(image):
|
| 14 |
image = image.resize(IMG_SIZE)
|
| 15 |
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
| 16 |
+
image_array = image_array / 255.0 # Normalize
|
| 17 |
return np.expand_dims(image_array, axis=0)
|
| 18 |
|
| 19 |
def predict(image):
|
| 20 |
input_tensor = preprocess(image)
|
| 21 |
predictions = model.predict(input_tensor)[0]
|
| 22 |
+
top_idx = np.argmax(predictions)
|
| 23 |
+
top_class = class_names[top_idx]
|
| 24 |
+
confidence = predictions[top_idx] * 100
|
| 25 |
+
return f"Predicted: {top_class} ({confidence:.2f}%)"
|
| 26 |
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=predict,
|
| 29 |
+
inputs=gr.Image(type="pil", label="Upload an image of a recyclable item"),
|
| 30 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 31 |
+
title="♻️ Waste Image Classifier",
|
| 32 |
+
description="Classify recyclable waste images into 9 categories using a MobileNetV2-based model."
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|