Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
model = tf.keras.models.load_model("
|
| 7 |
-
IMG_SIZE = (224, 224)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
0: "paper", 1: "e-waste", 2: "metal", 3: "light blubs",
|
| 12 |
-
4: "organic", 5: "plastic", 6: "clothes", 7: "glass", 8: "batteries"
|
| 13 |
-
}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
fn=classify_image,
|
| 24 |
-
inputs=gr.Image(type="
|
| 25 |
-
outputs=
|
| 26 |
-
title="
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load the model
|
| 7 |
+
model = tf.keras.models.load_model("model.h5")
|
|
|
|
| 8 |
|
| 9 |
+
# Define the class labels (update based on your project)
|
| 10 |
+
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic'] # Example
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Preprocess image
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
image = image.resize((224, 224)) # Adjust based on your model input
|
| 15 |
+
image = np.array(image) / 255.0 # Normalize if required
|
| 16 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 17 |
+
return image
|
| 18 |
|
| 19 |
+
# Prediction function
|
| 20 |
+
def classify_image(image):
|
| 21 |
+
image = preprocess_image(image)
|
| 22 |
+
prediction = model.predict(image)
|
| 23 |
+
class_index = np.argmax(prediction)
|
| 24 |
+
class_name = class_names[class_index]
|
| 25 |
+
confidence = prediction[0][class_index]
|
| 26 |
+
return f"{class_name} ({confidence*100:.2f}%)"
|
| 27 |
+
|
| 28 |
+
# Gradio interface
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
fn=classify_image,
|
| 31 |
+
inputs=gr.Image(type="pil"),
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Waste Image Classifier",
|
| 34 |
+
description="Upload an image of a recyclable item to classify it."
|
| 35 |
)
|
| 36 |
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
interface.launch()
|