Spaces:
Runtime error
Runtime error
Commit ·
d3bffe5
0
Parent(s):
Duplicate from viola77data/recycling-demo
Browse filesCo-authored-by: viola meli <viola77data@users.noreply.huggingface.co>
- .gitattributes +31 -0
- README.md +14 -0
- app.py +94 -0
- requirements.txt +2 -0
.gitattributes
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Recycling Demo
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.3.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
duplicated_from: viola77data/recycling-demo
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
|
| 3 |
+
from keras.losses import SparseCategoricalCrossentropy
|
| 4 |
+
from keras.metrics import SparseCategoricalAccuracy
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from huggingface_hub import from_pretrained_keras
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# prepare model
|
| 15 |
+
model = from_pretrained_keras("viola77data/recycling")
|
| 16 |
+
optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
|
| 17 |
+
cls_loss = SparseCategoricalCrossentropy()
|
| 18 |
+
cls_acc = SparseCategoricalAccuracy()
|
| 19 |
+
model.compile(optimizer=optimizer, loss=cls_loss, metrics=[cls_acc])
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# prepare the categories
|
| 23 |
+
categories = ['aluminium', 'batteries', 'cardboad',
|
| 24 |
+
'disposable plates', 'glass', 'hard plastic',
|
| 25 |
+
'paper', 'paper towel', 'polystyrene',
|
| 26 |
+
'soft plastics', 'takeaway cups']
|
| 27 |
+
|
| 28 |
+
dict_recycle = {
|
| 29 |
+
'aluminium': 'recycle',
|
| 30 |
+
'batteries': 'recycle',
|
| 31 |
+
'cardboad': 'recycle',
|
| 32 |
+
'disposable plates': 'dont recycle',
|
| 33 |
+
'glass': 'recycle',
|
| 34 |
+
'hard plastic': 'recycle',
|
| 35 |
+
'paper': 'recycle',
|
| 36 |
+
'paper towel': 'recycle',
|
| 37 |
+
'polystyrene': ' dont recycle',
|
| 38 |
+
'soft plastics': 'dont recycle',
|
| 39 |
+
'takeaway cups': 'dont recycle'
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# prediction functions
|
| 44 |
+
def preprocess_image(im):
|
| 45 |
+
""" Pass in a numpy image an it returns a
|
| 46 |
+
TF Image"""
|
| 47 |
+
im = tf.cast(im, tf.float32) / 255.0
|
| 48 |
+
if len(im.shape) < 3:
|
| 49 |
+
im = tf.expand_dims(im, axis=-1) # add the channel dimension
|
| 50 |
+
im = tf.image.grayscale_to_rgb(im)
|
| 51 |
+
im = tf.image.resize(im, (224, 224))
|
| 52 |
+
im = tf.expand_dims(im, axis=0)
|
| 53 |
+
|
| 54 |
+
return im
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def classify_image(input):
|
| 58 |
+
input_processed = preprocess_image(input)
|
| 59 |
+
preds = model.predict(input_processed)[0]
|
| 60 |
+
|
| 61 |
+
cls_preds = dict(zip(categories, map(float, preds)))
|
| 62 |
+
|
| 63 |
+
predicted_class = categories[np.argmax(preds)]
|
| 64 |
+
recycle_preds = dict_recycle[predicted_class]
|
| 65 |
+
|
| 66 |
+
return cls_preds, recycle_preds
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Defining the Gradio Interface
|
| 71 |
+
# This is how the Demo will look like.
|
| 72 |
+
title = "Should I Recycle This?"
|
| 73 |
+
description = """
|
| 74 |
+
|
| 75 |
+
This app was created to help people recycle the right type of waste.
|
| 76 |
+
|
| 77 |
+
You can use it at the comfort of your own home. Just take a picture of the waste material you want to know if
|
| 78 |
+
its recyclible and upload it to this app and using Artificial Intelligence it will determine if you should
|
| 79 |
+
throw the waste in the recycling bin or the normal bin.
|
| 80 |
+
|
| 81 |
+
Enjoy!
|
| 82 |
+
|
| 83 |
+
Made by Viola, you can reach out to me here: <a href="violameli7@gmail.com">Send Email</a>
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
image = gr.Image(shape=(224,224))
|
| 89 |
+
label = gr.Label(num_top_classes=3, label='Prediction Material')
|
| 90 |
+
recycle = gr.Textbox(label='Should you recycle?')
|
| 91 |
+
outputs = [label, recycle]
|
| 92 |
+
intf = gr.Interface(fn=classify_image, inputs=image, outputs=outputs, title = title, description = description,
|
| 93 |
+
cache_examples=False)
|
| 94 |
+
intf.launch(enable_queue=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow==2.9.1
|
| 2 |
+
keras==2.9.0
|