Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from random import choices
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
from glob import glob
|
5 |
+
import tensorflow as tf
|
6 |
+
from tensorflow import keras
|
7 |
+
|
8 |
+
# Model & Pre-requisites
|
9 |
+
model_path = './FastFood.keras'
|
10 |
+
ffc = keras.models.load_model(model_path, compile=False)
|
11 |
+
|
12 |
+
class_names_path = './Fast Food-ClassNames.txt'
|
13 |
+
class_names = []
|
14 |
+
with open(class_names_path, mode='r') as f:
|
15 |
+
class_names = f.read().split(',')[:-1]
|
16 |
+
|
17 |
+
# Utility Functions
|
18 |
+
def predict_fast_food(image, labels=class_names, model=ffc):
|
19 |
+
image = tf.cast(image, tf.float32)
|
20 |
+
|
21 |
+
if image.shape[-2]!=224:
|
22 |
+
image = tf.image.resize(image, (224,224))
|
23 |
+
|
24 |
+
if np.max(image)==255:
|
25 |
+
image = image/255.
|
26 |
+
|
27 |
+
if len(image.shape) == 3:
|
28 |
+
image = tf.squeeze(image)[tf.newaxis, ...]
|
29 |
+
|
30 |
+
pred_proba = model.predict(image, verbose=0)[0]
|
31 |
+
label = tf.argmax(pred_proba, axis=-1)
|
32 |
+
pred_class = labels[int(label)]
|
33 |
+
return pred_class, pred_proba[label]
|
34 |
+
else:
|
35 |
+
pred_probas = model.predict(image, verbose=0)
|
36 |
+
labels = tf.argmax(pred_probas, axis=-1)
|
37 |
+
pred_classes = [class_names[label] for label in labels]
|
38 |
+
probas = tf.math.reduce_max(pred_probas, axis=-1)
|
39 |
+
return pred_classes, probas
|
40 |
+
|
41 |
+
def load_image(image_path):
|
42 |
+
image = tf.io.read_file(image_path)
|
43 |
+
image = tf.image.decode_jpeg(image, channels=3)
|
44 |
+
image = tf.image.resize(image, (224,224))
|
45 |
+
image = tf.image.convert_image_dtype(image, tf.float32)
|
46 |
+
image = image/255.
|
47 |
+
return image
|
48 |
+
|
49 |
+
# Load Example Images
|
50 |
+
subset_ds_path = './Fast FoodSubset'
|
51 |
+
|
52 |
+
# Select 5 images per class
|
53 |
+
example_image_paths = []
|
54 |
+
for class_ss_path in glob(subset_ds_path + '/*'):
|
55 |
+
image_paths = glob(class_ss_path + '/*')
|
56 |
+
selected_images = choices(image_paths, k=5)
|
57 |
+
example_image_paths.extend(selected_images)
|
58 |
+
|
59 |
+
example_images = [load_image(path).numpy() for path in example_image_paths]
|
60 |
+
|
61 |
+
# Define Interface
|
62 |
+
with gr.Blocks(theme='ocean') as app:
|
63 |
+
|
64 |
+
# Title or header (optional)
|
65 |
+
gr.Markdown("### 🍔 Fast Food Classifier Demo")
|
66 |
+
|
67 |
+
# Take Image Input
|
68 |
+
image_input = gr.Image(label='Image Input')
|
69 |
+
|
70 |
+
# Prediction Button
|
71 |
+
pred_btn = gr.Button('Predict')
|
72 |
+
|
73 |
+
# 2 Outputs
|
74 |
+
with gr.Row():
|
75 |
+
|
76 |
+
# Output of the Predicted Class
|
77 |
+
class_out = gr.Textbox(label='Predicted Class', placeholder='Hmm... Looking for something yummy.')
|
78 |
+
proba_out = gr.Textbox(label='Predicted Class Probability', placeholder='I believe on myself but numbers don\'t lie.')
|
79 |
+
|
80 |
+
# Add example images
|
81 |
+
gr.Examples(
|
82 |
+
examples=example_images,
|
83 |
+
inputs=image_input,
|
84 |
+
label="Try these example images"
|
85 |
+
)
|
86 |
+
|
87 |
+
def predict_fast_food_wrapper(image):
|
88 |
+
class_label, proba = predict_fast_food(image)
|
89 |
+
return class_label, f'{proba:.3%}'
|
90 |
+
|
91 |
+
# On Click Action
|
92 |
+
pred_btn.click(
|
93 |
+
fn=predict_fast_food_wrapper,
|
94 |
+
inputs=image_input,
|
95 |
+
outputs=[class_out, proba_out]
|
96 |
+
)
|
97 |
+
|
98 |
+
if __name__ == '__main__':
|
99 |
+
# Launch Application
|
100 |
+
app.launch()
|