Spaces:
Sleeping
Sleeping
First commit
Browse files- .gitattributes +1 -0
- Notebooks/TrashClassifier.ipynb +0 -0
- app.py +68 -0
- examples/Compostable/compostable.jpg +0 -0
- examples/Compostable/compostable1.jpg +0 -0
- examples/Compostable/compostable2.jpg +0 -0
- examples/Recyclables/recycle0.jpg +0 -0
- examples/Recyclables/recycle1.jpg +0 -0
- examples/Recyclables/recycle2.png +0 -0
- examples/Trash/trash4.jpg +0 -0
- examples/Trash/trash5.jpg +0 -0
- final_trashnet_transfer_learning_model.keras +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
Notebooks/TrashClassifier.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 6 |
+
from tensorflow.keras.applications.imagenet_utils import preprocess_input
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Load your frozen model
|
| 10 |
+
model = tf.keras.models.load_model("final_trashnet_transfer_learning_model.keras")
|
| 11 |
+
|
| 12 |
+
# Mapping of original classes to broader categories
|
| 13 |
+
class_mapping = {
|
| 14 |
+
0: "Compostable", # compostable
|
| 15 |
+
1: "Recyclables", # recyclable
|
| 16 |
+
2: "Trash", #trash
|
| 17 |
+
}
|
| 18 |
+
# Define a function to preprocess the input image
|
| 19 |
+
def preprocess_image(image):
|
| 20 |
+
# Resize the image to 128*128 (as required by your model)
|
| 21 |
+
image = image.resize((128, 128))
|
| 22 |
+
# Convert the image to a NumPy array and normalize it
|
| 23 |
+
img_array = img_to_array(image)
|
| 24 |
+
img_array = preprocess_input(img_array)
|
| 25 |
+
# Ensure the image has the correct shape (32, 32, 3)
|
| 26 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 27 |
+
return img_array
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Define the prediction function
|
| 31 |
+
def classify_trash(image):
|
| 32 |
+
processed_image = preprocess_image(image)
|
| 33 |
+
predictions = model.predict(processed_image)
|
| 34 |
+
print(predictions)
|
| 35 |
+
class_index = np.argmax(predictions)
|
| 36 |
+
confidence = np.max(predictions)
|
| 37 |
+
predicted_class = class_mapping[class_index]
|
| 38 |
+
return f"Predicted Category: {predicted_class}", f"Confidence: {confidence*100:.2f}"
|
| 39 |
+
|
| 40 |
+
# Function to gather example images dynamically
|
| 41 |
+
def get_example_images():
|
| 42 |
+
example_images = []
|
| 43 |
+
base_dir = "examples"
|
| 44 |
+
categories = ["Compostable", "Recyclables", "Trash"]
|
| 45 |
+
for category in categories:
|
| 46 |
+
folder_path = os.path.join(base_dir, category)
|
| 47 |
+
if os.path.exists(folder_path):
|
| 48 |
+
example_images += [
|
| 49 |
+
os.path.join(folder_path, img) for img in os.listdir(folder_path) if img.endswith((".jpg", ".png"))
|
| 50 |
+
]
|
| 51 |
+
return example_images
|
| 52 |
+
|
| 53 |
+
# Example images from all categories
|
| 54 |
+
example_images = get_example_images()
|
| 55 |
+
|
| 56 |
+
# Define the Gradio interface
|
| 57 |
+
interface = gr.Interface(
|
| 58 |
+
fn=classify_trash,
|
| 59 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 60 |
+
outputs=[gr.Textbox(label="Predicted Category"), gr.Textbox(label="Confidence")],
|
| 61 |
+
examples= example_images,
|
| 62 |
+
title="Trash Classifier",
|
| 63 |
+
description="Upload an image of trash, and the model will classify it into 'Compostable', 'Recyclables' and 'Trash' based on its category."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Run the app
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
interface.launch()
|
examples/Compostable/compostable.jpg
ADDED
|
examples/Compostable/compostable1.jpg
ADDED
|
examples/Compostable/compostable2.jpg
ADDED
|
examples/Recyclables/recycle0.jpg
ADDED
|
examples/Recyclables/recycle1.jpg
ADDED
|
examples/Recyclables/recycle2.png
ADDED
|
examples/Trash/trash4.jpg
ADDED
|
examples/Trash/trash5.jpg
ADDED
|
final_trashnet_transfer_learning_model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8fb9b9aa6c4354259fcdfa66ab05e3d3a162be7f3b6306aa487e6f14ad8510fe
|
| 3 |
+
size 98138568
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.43.0
|
| 2 |
+
tensorflow==2.13.0
|
| 3 |
+
numpy==1.24.3
|
| 4 |
+
Pillow==9.5.0
|