Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,36 +3,34 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
model = tf.keras.models.load_model("model.h5")
|
| 8 |
|
| 9 |
-
# Define
|
| 10 |
-
class_names = ['batteries', '
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
class_name = class_names[class_index]
|
| 25 |
-
confidence = prediction[0][class_index]
|
| 26 |
-
return f"{class_name} ({confidence*100:.2f}%)"
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
model = tf.keras.models.load_model("model.h5")
|
| 8 |
|
| 9 |
+
# Define class names — replace with your actual folder names if different
|
| 10 |
+
class_names = ['batteries', 'cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 11 |
|
| 12 |
+
# Define image size used in training
|
| 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 if you did this during training
|
| 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 |
+
top_class = class_names[np.argmax(predictions)]
|
| 25 |
+
confidence = np.max(predictions) * 100
|
| 26 |
+
return f"{top_class} ({confidence:.2f}%)"
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=predict,
|
| 30 |
+
inputs=gr.Image(type="pil", label="Upload an image of a recyclable item to classify it."),
|
| 31 |
+
outputs=gr.Textbox(label="Output"),
|
| 32 |
+
title="Waste Image Classifier"
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|