Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load the fine‑tuned model (adjust path if needed)
|
| 7 |
+
model = tf.keras.models.load_model("model/O_R_tlearn_fine_tune_vgg16.keras")
|
| 8 |
+
|
| 9 |
+
# Class names
|
| 10 |
+
CLASS_NAMES = ["Organic (O)", "Recyclable (R)"]
|
| 11 |
+
|
| 12 |
+
def predict_image(image):
|
| 13 |
+
"""
|
| 14 |
+
image: PIL Image or numpy array (H, W, 3)
|
| 15 |
+
Returns: label string and confidence score
|
| 16 |
+
"""
|
| 17 |
+
# Resize to 150x150 (the model's input size)
|
| 18 |
+
img = image.resize((150, 150))
|
| 19 |
+
img_array = np.array(img) / 255.0 # rescale as during training
|
| 20 |
+
img_array = np.expand_dims(img_array, axis=0) # add batch dimension
|
| 21 |
+
|
| 22 |
+
pred = model.predict(img_array)[0][0] # sigmoid output
|
| 23 |
+
confidence = pred if pred > 0.5 else 1 - pred
|
| 24 |
+
label = CLASS_NAMES[0] if pred < 0.5 else CLASS_NAMES[1]
|
| 25 |
+
return f"{label} (confidence: {confidence:.2f})"
|
| 26 |
+
|
| 27 |
+
# Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict_image,
|
| 30 |
+
inputs=gr.Image(type="pil"),
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="Waste Classifier (Organic vs Recyclable)",
|
| 33 |
+
description="Upload an image of waste to classify it as Organic (O) or Recyclable (R)."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|