Image Classification
Keras
LiteRT
English
computer-vision
waste-management
mobilenetv2
sustainability
Instructions to use tahzaya/trash-sorter-ai with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use tahzaya/trash-sorter-ai with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://tahzaya/trash-sorter-ai") - Notebooks
- Google Colab
- Kaggle
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model = tf.keras.models.load_model("trash_sorter.keras")
|
| 9 |
+
|
| 10 |
+
def classify_waste(image):
|
| 11 |
+
# Preprocess image
|
| 12 |
+
img = cv2.resize(image, (224, 224))
|
| 13 |
+
img = img / 255.0
|
| 14 |
+
img = np.expand_dims(img, axis=0)
|
| 15 |
+
|
| 16 |
+
# Predict
|
| 17 |
+
predictions = model.predict(img, verbose=0)[0]
|
| 18 |
+
|
| 19 |
+
# Get results
|
| 20 |
+
class_names = ["🗑️ ORGANIC", "♻️ RECYCLABLE"]
|
| 21 |
+
confidence = np.max(predictions)
|
| 22 |
+
predicted_class = class_names[np.argmax(predictions)]
|
| 23 |
+
|
| 24 |
+
# Format output
|
| 25 |
+
result = f"""
|
| 26 |
+
## {predicted_class}
|
| 27 |
+
|
| 28 |
+
**Confidence:** {confidence:.2%}
|
| 29 |
+
|
| 30 |
+
**Breakdown:**
|
| 31 |
+
- Organic: {predictions[0]:.2%}
|
| 32 |
+
- Recyclable: {predictions[1]:.2%}
|
| 33 |
+
|
| 34 |
+
**What to do:**
|
| 35 |
+
{ "Compost or dispose in green bin" if predicted_class == "🗑️ ORGANIC" else "Clean and place in recycling bin" }
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
return result
|
| 39 |
+
|
| 40 |
+
# Create interface
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=classify_waste,
|
| 43 |
+
inputs=gr.Image(type="numpy"),
|
| 44 |
+
outputs=gr.Markdown(),
|
| 45 |
+
title="🗑️ AI Trash Sorter",
|
| 46 |
+
description="Upload a photo of waste to classify as Organic or Recyclable",
|
| 47 |
+
examples=[
|
| 48 |
+
["example_organic.jpg"],
|
| 49 |
+
["example_recyclable.jpg"]
|
| 50 |
+
]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Launch
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|