Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
model = load_model("waste_classifier_model.h5")
|
| 8 |
+
class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 9 |
+
|
| 10 |
+
def predict_image(image):
|
| 11 |
+
# Preprocess
|
| 12 |
+
img = image.resize((224, 224))
|
| 13 |
+
img_array = img_to_array(img) / 255.0
|
| 14 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 15 |
+
|
| 16 |
+
# Predict
|
| 17 |
+
predictions = model.predict(img_array)[0]
|
| 18 |
+
pred_index = np.argmax(predictions)
|
| 19 |
+
confidence = float(np.max(predictions))
|
| 20 |
+
label = class_names[pred_index]
|
| 21 |
+
|
| 22 |
+
return {label: confidence}
|
| 23 |
+
|
| 24 |
+
# Gradio Interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=predict_image,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=gr.Label(num_top_classes=3),
|
| 29 |
+
title="Waste Classification with MobileNetV2",
|
| 30 |
+
description="Upload an image of waste (plastic, metal, paper, etc.), and this model will classify it."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|