-
Notifications
You must be signed in to change notification settings - Fork 48
/
app.py
266 lines (236 loc) · 7.48 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import tensorflow
from flask import Flask, request, render_template
import csv
import math
import os
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.python.keras.models import load_model
from werkzeug.utils import secure_filename
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app = Flask(__name__, template_folder=tmpl_dir)
UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# define label meaning
label = ['apple pie',
'baby back ribs',
'baklava',
'beef carpaccio',
'beef tartare',
'beet salad',
'beignets',
'bibimbap',
'bread pudding',
'breakfast burrito',
'bruschetta',
'caesar salad',
'cannoli',
'caprese salad',
'carrot cake',
'ceviche',
'cheese plate',
'cheesecake',
'chicken curry',
'chicken quesadilla',
'chicken wings',
'chocolate cake',
'chocolate mousse',
'churros',
'clam chowder',
'club sandwich',
'crab cakes',
'creme brulee',
'croque madame',
'cup cakes',
'deviled eggs',
'donuts',
'dumplings',
'edamame',
'eggs benedict',
'escargots',
'falafel',
'filet mignon',
'fish and_chips',
'foie gras',
'french fries',
'french onion soup',
'french toast',
'fried calamari',
'fried rice',
'frozen yogurt',
'garlic bread',
'gnocchi',
'greek salad',
'grilled cheese sandwich',
'grilled salmon',
'guacamole',
'gyoza',
'hamburger',
'hot and sour soup',
'hot dog',
'huevos rancheros',
'hummus',
'ice cream',
'lasagna',
'lobster bisque',
'lobster roll sandwich',
'macaroni and cheese',
'macarons',
'miso soup',
'mussels',
'nachos',
'omelette',
'onion rings',
'oysters',
'pad thai',
'paella',
'pancakes',
'panna cotta',
'peking duck',
'pho',
'pizza',
'pork chop',
'poutine',
'prime rib',
'pulled pork sandwich',
'ramen',
'ravioli',
'red velvet cake',
'risotto',
'samosa',
'sashimi',
'scallops',
'seaweed salad',
'shrimp and grits',
'spaghetti bolognese',
'spaghetti carbonara',
'spring rolls',
'steak',
'strawberry shortcake',
'sushi',
'tacos',
'octopus balls',
'tiramisu',
'tuna tartare',
'waffles']
nu_link = 'https://www.nutritionix.com/food/'
# Loading the best saved model to make predictions.
tensorflow.keras.backend.clear_session()
model_best = load_model('best_model_101class.hdf5', compile=False)
print('model successfully loaded!')
start = [0]
passed = [0]
pack = [[]]
num = [0]
nutrients = [
{'name': 'protein', 'value': 0.0},
{'name': 'calcium', 'value': 0.0},
{'name': 'fat', 'value': 0.0},
{'name': 'carbohydrates', 'value': 0.0},
{'name': 'vitamins', 'value': 0.0}
]
with open('nutrition101.csv', 'r') as file:
reader = csv.reader(file)
nutrition_table = dict()
for i, row in enumerate(reader):
if i == 0:
name = ''
continue
else:
name = row[1].strip()
nutrition_table[name] = [
{'name': 'protein', 'value': float(row[2])},
{'name': 'calcium', 'value': float(row[3])},
{'name': 'fat', 'value': float(row[4])},
{'name': 'carbohydrates', 'value': float(row[5])},
{'name': 'vitamins', 'value': float(row[6])}
]
@app.route('/')
def index():
img = 'static/profile.jpg'
return render_template('index.html', img=img)
@app.route('/recognize')
def magic():
return render_template('recognize.html', img=file)
@app.route('/upload', methods=['POST'])
def upload():
file = request.files.getlist("img")
for f in file:
filename = secure_filename(str(num[0] + 500) + '.jpg')
num[0] += 1
name = os.path.join(app.config['UPLOAD_FOLDER'], filename)
print('save name', name)
f.save(name)
pack[0] = []
return render_template('recognize.html', img=file)
@app.route('/predict')
def predict():
result = []
# pack = []
print('total image', num[0])
for i in range(start[0], num[0]):
pa = dict()
filename = f'{UPLOAD_FOLDER}/{i + 500}.jpg'
print('image filepath', filename)
pred_img = filename
pred_img = image.load_img(pred_img, target_size=(200, 200))
pred_img = image.img_to_array(pred_img)
pred_img = np.expand_dims(pred_img, axis=0)
pred_img = pred_img / 255.
pred = model_best.predict(pred_img)
print("Pred")
print(pred)
if math.isnan(pred[0][0]) and math.isnan(pred[0][1]) and \
math.isnan(pred[0][2]) and math.isnan(pred[0][3]):
pred = np.array([0.05, 0.05, 0.05, 0.07, 0.09, 0.19, 0.55, 0.0, 0.0, 0.0, 0.0])
top = pred.argsort()[0][-3:]
label.sort()
_true = label[top[2]]
pa['image'] = f'{UPLOAD_FOLDER}/{i + 500}.jpg'
x = dict()
x[_true] = float("{:.2f}".format(pred[0][top[2]] * 100))
x[label[top[1]]] = float("{:.2f}".format(pred[0][top[1]] * 100))
x[label[top[0]]] = float("{:.2f}".format(pred[0][top[0]] * 100))
pa['result'] = x
pa['nutrition'] = nutrition_table[_true]
pa['food'] = f'{nu_link}{_true}'
pa['idx'] = i - start[0]
pa['quantity'] = 100
pack[0].append(pa)
passed[0] += 1
start[0] = passed[0]
print('successfully packed')
# compute the average source of calories
for p in pack[0]:
nutrients[0]['value'] = (nutrients[0]['value'] + p['nutrition'][0]['value'])
nutrients[1]['value'] = (nutrients[1]['value'] + p['nutrition'][1]['value'])
nutrients[2]['value'] = (nutrients[2]['value'] + p['nutrition'][2]['value'])
nutrients[3]['value'] = (nutrients[3]['value'] + p['nutrition'][3]['value'])
nutrients[4]['value'] = (nutrients[4]['value'] + p['nutrition'][4]['value'])
nutrients[0]['value'] = nutrients[0]['value'] / num[0]
nutrients[1]['value'] = nutrients[1]['value'] / num[0]
nutrients[2]['value'] = nutrients[2]['value'] / num[0]
nutrients[3]['value'] = nutrients[3]['value'] / num[0]
nutrients[4]['value'] = nutrients[4]['value'] / num[0]
return render_template('results.html', pack=pack[0], whole_nutrition=nutrients)
@app.route('/update', methods=['POST'])
def update():
return render_template('index.html', img='static/P2.jpg')
if __name__ == "__main__":
import click
@click.command()
@click.option('--debug', is_flag=True)
@click.option('--threaded', is_flag=True)
@click.argument('HOST', default='127.0.0.1')
@click.argument('PORT', default=5000, type=int)
def run(debug, threaded, host, port):
"""
This function handles command line parameters.
Run the server using
python server.py
Show the help text using
python server.py --help
"""
HOST, PORT = host, port
app.run(host=HOST, port=PORT, debug=debug, threaded=threaded)
run()