Spaces:
Sleeping
Sleeping
Commit ·
922b371
1
Parent(s): 5ad47c9
minor change app,py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,18 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 6 |
-
import tensorflow as tf
|
| 7 |
-
from huggingface_hub import hf_hub_download
|
| 8 |
from datasets import load_dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
| 10 |
ds = load_dataset("dvk65/TrashTypes")
|
| 11 |
class_names = ds["train"].features["label"].names
|
| 12 |
|
|
|
|
|
|
|
| 13 |
REPO_ID = "dvk65/trash-classifier-resnet50"
|
| 14 |
FILENAME = "trashclassify_13.keras"
|
| 15 |
|
|
@@ -21,46 +25,25 @@ model = tf.keras.models.load_model(
|
|
| 21 |
)
|
| 22 |
|
| 23 |
|
| 24 |
-
# ---
|
| 25 |
def preprocess(image):
|
| 26 |
-
image = image.resize((224, 224))
|
| 27 |
-
image = np.array(image)
|
| 28 |
-
image =
|
|
|
|
| 29 |
return image
|
| 30 |
|
| 31 |
-
|
| 32 |
-
# --- PREDICTION FUNCTION ---
|
| 33 |
def predict(img):
|
| 34 |
img = preprocess(img)
|
| 35 |
-
preds = model.predict(img)[0]
|
| 36 |
-
|
| 37 |
-
class_names = [
|
| 38 |
-
"apples",
|
| 39 |
-
"bananas",
|
| 40 |
-
"bottles",
|
| 41 |
-
"cans",
|
| 42 |
-
"cardboard",
|
| 43 |
-
"cups",
|
| 44 |
-
"eggshells",
|
| 45 |
-
"generalcompost", # mixed leftover food
|
| 46 |
-
"mixers", # wooden coffee stirrers
|
| 47 |
-
"peels", # oranges
|
| 48 |
-
"platicbags", # typo in original dataset? keep as-is
|
| 49 |
-
"plastics", # plastic wrappers
|
| 50 |
-
"tissue papers"
|
| 51 |
-
]
|
| 52 |
|
| 53 |
-
result = {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
| 54 |
-
return result
|
| 55 |
|
| 56 |
-
# --- GRADIO UI ---
|
| 57 |
demo = gr.Interface(
|
| 58 |
fn=predict,
|
| 59 |
inputs=gr.Image(type="pil"),
|
| 60 |
outputs=gr.Label(num_top_classes=3),
|
| 61 |
-
title="Trash Classifier
|
| 62 |
-
description="Upload an image of trash and get the predicted type."
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
demo.launch(debug=False)
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
from datasets import load_dataset
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 9 |
+
|
| 10 |
|
| 11 |
+
# --- LOAD CLASS LABELS FROM DATASET ---
|
| 12 |
ds = load_dataset("dvk65/TrashTypes")
|
| 13 |
class_names = ds["train"].features["label"].names
|
| 14 |
|
| 15 |
+
|
| 16 |
+
# --- LOAD MODEL ---
|
| 17 |
REPO_ID = "dvk65/trash-classifier-resnet50"
|
| 18 |
FILENAME = "trashclassify_13.keras"
|
| 19 |
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
+
# --- PREPROCESS AND PREDICT ---
|
| 29 |
def preprocess(image):
|
| 30 |
+
image = image.resize((224, 224))
|
| 31 |
+
image = np.array(image)
|
| 32 |
+
image = preprocess_input(image)
|
| 33 |
+
image = np.expand_dims(image, axis=0)
|
| 34 |
return image
|
| 35 |
|
|
|
|
|
|
|
| 36 |
def predict(img):
|
| 37 |
img = preprocess(img)
|
| 38 |
+
preds = model.predict(img)[0]
|
| 39 |
+
return {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
demo = gr.Interface(
|
| 43 |
fn=predict,
|
| 44 |
inputs=gr.Image(type="pil"),
|
| 45 |
outputs=gr.Label(num_top_classes=3),
|
| 46 |
+
title="Trash Classifier",
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
demo.launch()
|
|
|