Update app.py
Browse files
app.py
CHANGED
@@ -3,22 +3,31 @@ import numpy as np
|
|
3 |
import time
|
4 |
|
5 |
import tensorflow as tf
|
6 |
-
from utils import
|
7 |
from FoodNoFood import food_not_food
|
8 |
from PIL import Image
|
9 |
|
10 |
import sys
|
|
|
11 |
from RecipeData import fetchRecipeData
|
12 |
|
13 |
IMG_SIZE = (224, 224)
|
14 |
model_V1 = 'Seefood_model_v1.tflite'
|
15 |
model_V2 = 'Seefood_model_V2.tflite'
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return prediction, sorceCode, recipe_data
|
23 |
|
24 |
|
@@ -33,17 +42,18 @@ def main():
|
|
33 |
st.title('SeeFood🍔')
|
34 |
st.write('Upload a food image and get the recipe for that food and other details of that food')
|
35 |
|
36 |
-
col1, col2 = st.columns(2)
|
37 |
|
38 |
with col1:
|
39 |
# image uploading button
|
40 |
uploaded_file = st.file_uploader("Choose a file")
|
41 |
-
selected_model = st.selectbox('Select Model',('model 1', 'model 2'), index=
|
|
|
42 |
if uploaded_file is not None:
|
43 |
uploaded_img = uploaded_file.read()
|
44 |
pil_img = Image.open(uploaded_file)
|
45 |
|
46 |
-
col2.image(uploaded_file, width=
|
47 |
|
48 |
# butoon to make predictions
|
49 |
predict = st.button('Get Recipe!')
|
@@ -57,29 +67,37 @@ def main():
|
|
57 |
with st.spinner('Please Wait 👩🍳'):
|
58 |
|
59 |
# setting model and rescalling
|
60 |
-
if selected_model
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
pred_rescale = True
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
# makeing prediction and fetching food recipe form api
|
68 |
-
food, source_code, recipe_data = model_prediction(pred_model, uploaded_img, pred_rescale)
|
69 |
-
|
70 |
# asssigning caleoric breakdown data
|
71 |
percent_Protein = recipe_data['percentProtein']
|
72 |
percent_fat = recipe_data['percentFat']
|
73 |
percent_carbs = recipe_data['percentCarbs']
|
74 |
-
|
75 |
# food name message
|
76 |
col1.success(f"It's an {food}")
|
77 |
-
|
78 |
if source_code == 200:
|
79 |
# desplay food recipe
|
80 |
st.header(recipe_data['title']+" Recipe")
|
81 |
-
|
82 |
-
col3, col4 = st.columns(2)
|
83 |
|
84 |
with col3:
|
85 |
# Ingridents of recipie
|
@@ -99,11 +117,12 @@ def main():
|
|
99 |
* Protien: {percent_Protein}%
|
100 |
* Fat: {percent_fat}%
|
101 |
* Carbohydrates: {percent_carbs}%
|
102 |
-
|
|
|
103 |
|
104 |
-
|
105 |
else:
|
106 |
st.error('Something went wrong please try again :(')
|
|
|
107 |
|
108 |
elif food_cat == 'not food':
|
109 |
with col1:
|
|
|
3 |
import time
|
4 |
|
5 |
import tensorflow as tf
|
6 |
+
from utils import load_prepare_image_tf, model_pred_tf, fetch_recipe, load_prepare_image_pt, model_pred_pt
|
7 |
from FoodNoFood import food_not_food
|
8 |
from PIL import Image
|
9 |
|
10 |
import sys
|
11 |
+
sys.path.insert(1, 'Api Data')
|
12 |
from RecipeData import fetchRecipeData
|
13 |
|
14 |
IMG_SIZE = (224, 224)
|
15 |
model_V1 = 'Seefood_model_v1.tflite'
|
16 |
model_V2 = 'Seefood_model_V2.tflite'
|
17 |
+
ViT_model = 'ViT-101-1.pt'
|
18 |
+
|
19 |
+
@st.cache(show_spinner=False)
|
20 |
+
def model_prediction(model, img_file, rescale, model_tensor_type):
|
21 |
+
if model_tensor_type == 'TF':
|
22 |
+
img = load_prepare_image_tf(img_file, IMG_SIZE, rescale=rescale)
|
23 |
+
prediction = model_pred_tf(model, img)
|
24 |
+
sorceCode, recipe_data = fetchRecipeData(prediction)
|
25 |
+
elif model_tensor_type == 'Pt':
|
26 |
+
img = load_prepare_image_pt(img_file)
|
27 |
+
prediction = model_pred_pt(img, model)
|
28 |
+
print(prediction)
|
29 |
+
sorceCode, recipe_data = fetchRecipeData(prediction)
|
30 |
+
|
31 |
return prediction, sorceCode, recipe_data
|
32 |
|
33 |
|
|
|
42 |
st.title('SeeFood🍔')
|
43 |
st.write('Upload a food image and get the recipe for that food and other details of that food')
|
44 |
|
45 |
+
col1, col2 = st.columns(2, gap='large')
|
46 |
|
47 |
with col1:
|
48 |
# image uploading button
|
49 |
uploaded_file = st.file_uploader("Choose a file")
|
50 |
+
selected_model = st.selectbox('Select Model',( 'ViT Model', 'model 1', 'model 2'), index=0)
|
51 |
+
|
52 |
if uploaded_file is not None:
|
53 |
uploaded_img = uploaded_file.read()
|
54 |
pil_img = Image.open(uploaded_file)
|
55 |
|
56 |
+
col2.image(uploaded_file, width=700)
|
57 |
|
58 |
# butoon to make predictions
|
59 |
predict = st.button('Get Recipe!')
|
|
|
67 |
with st.spinner('Please Wait 👩🍳'):
|
68 |
|
69 |
# setting model and rescalling
|
70 |
+
if selected_model in ['model 1', 'model 2']:
|
71 |
+
|
72 |
+
if selected_model == 'model 2':
|
73 |
+
pred_model = model_V2
|
74 |
+
pred_rescale = True
|
75 |
+
elif selected_model == 'model 1':
|
76 |
+
pred_model = model_V1
|
77 |
+
pred_rescale = False
|
78 |
+
|
79 |
+
# makeing prediction and fetching food recipe form api
|
80 |
+
food, source_code, recipe_data = model_prediction(pred_model, uploaded_img, pred_rescale, 'TF')
|
81 |
+
|
82 |
+
elif selected_model == 'ViT Model':
|
83 |
+
pred_model = ViT_model
|
84 |
pred_rescale = True
|
85 |
+
# makeing prediction and fetching food recipe form api
|
86 |
+
food, source_code, recipe_data = model_prediction(pred_model, pil_img, pred_rescale, 'Pt')
|
87 |
+
|
|
|
|
|
|
|
|
|
88 |
# asssigning caleoric breakdown data
|
89 |
percent_Protein = recipe_data['percentProtein']
|
90 |
percent_fat = recipe_data['percentFat']
|
91 |
percent_carbs = recipe_data['percentCarbs']
|
92 |
+
|
93 |
# food name message
|
94 |
col1.success(f"It's an {food}")
|
95 |
+
|
96 |
if source_code == 200:
|
97 |
# desplay food recipe
|
98 |
st.header(recipe_data['title']+" Recipe")
|
99 |
+
|
100 |
+
col3, col4 = st.columns(2, gap='medium')
|
101 |
|
102 |
with col3:
|
103 |
# Ingridents of recipie
|
|
|
117 |
* Protien: {percent_Protein}%
|
118 |
* Fat: {percent_fat}%
|
119 |
* Carbohydrates: {percent_carbs}%
|
120 |
+
''')
|
121 |
+
|
122 |
|
|
|
123 |
else:
|
124 |
st.error('Something went wrong please try again :(')
|
125 |
+
|
126 |
|
127 |
elif food_cat == 'not food':
|
128 |
with col1:
|