Jimmie commited on
Commit
65ab792
·
1 Parent(s): 9679c79

added demo files

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +2 -0
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:
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