code update
Browse files
app.py
CHANGED
@@ -1,109 +1,119 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import numpy as np
|
3 |
-
import time
|
4 |
-
|
5 |
-
import tensorflow as tf
|
6 |
-
from utils import load_prepare_image, model_pred, fetch_recipe
|
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 |
-
st.
|
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 |
-
st.
|
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 |
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import time
|
4 |
+
|
5 |
+
import tensorflow as tf
|
6 |
+
from utils import load_prepare_image, model_pred, fetch_recipe
|
7 |
+
from FoodNoFood import food_not_food
|
8 |
+
|
9 |
+
import sys
|
10 |
+
from RecipeData import fetchRecipeData
|
11 |
+
|
12 |
+
IMG_SIZE = (224, 224)
|
13 |
+
model_V1 = 'Seefood_model_v1.tflite'
|
14 |
+
model_V2 = 'Seefood_model_V2.tflite'
|
15 |
+
|
16 |
+
@st.cache()
|
17 |
+
def model_prediction(model, img_file, rescale):
|
18 |
+
img = load_prepare_image(img_file, IMG_SIZE, rescale=rescale)
|
19 |
+
prediction = model_pred(model, img)
|
20 |
+
sorceCode, recipe_data = fetchRecipeData(prediction)
|
21 |
+
return prediction, sorceCode, recipe_data
|
22 |
+
|
23 |
+
|
24 |
+
def main():
|
25 |
+
st.set_page_config(
|
26 |
+
page_title="SeeFood",
|
27 |
+
page_icon="๐",
|
28 |
+
layout="wide",
|
29 |
+
initial_sidebar_state="expanded"
|
30 |
+
)
|
31 |
+
|
32 |
+
st.title('SeeFood๐')
|
33 |
+
st.write('Upload a food image and get the recipe for that food and other details of that food')
|
34 |
+
|
35 |
+
col1, col2 = st.columns(2)
|
36 |
+
|
37 |
+
with col1:
|
38 |
+
# image uploading button
|
39 |
+
uploaded_file = st.file_uploader("Choose a file")
|
40 |
+
selected_model = st.selectbox('Select Model',('model 1', 'model 2'), index=1)
|
41 |
+
if uploaded_file is not None:
|
42 |
+
uploaded_img = uploaded_file.read()
|
43 |
+
pil_img = Image.open(uploaded_file)
|
44 |
+
|
45 |
+
col2.image(uploaded_file, width=500)
|
46 |
+
|
47 |
+
# butoon to make predictions
|
48 |
+
predict = st.button('Get Recipe!')
|
49 |
+
|
50 |
+
if predict:
|
51 |
+
with st.spinner("Analyzing Image ๐ต๏ธโโ๏ธ"):
|
52 |
+
food_cat = food_not_food(pil_img)
|
53 |
+
|
54 |
+
if food_cat == 'food':
|
55 |
+
if uploaded_file is not None:
|
56 |
+
with st.spinner('Please Wait ๐ฉโ๐ณ'):
|
57 |
+
|
58 |
+
# setting model and rescalling
|
59 |
+
if selected_model == 'model 2':
|
60 |
+
pred_model = model_V2
|
61 |
+
pred_rescale = True
|
62 |
+
else:
|
63 |
+
pred_model = model_V1
|
64 |
+
pred_rescale = False
|
65 |
+
|
66 |
+
# makeing prediction and fetching food recipe form api
|
67 |
+
food, source_code, recipe_data = model_prediction(pred_model, uploaded_img, pred_rescale)
|
68 |
+
|
69 |
+
# asssigning caleoric breakdown data
|
70 |
+
percent_Protein = recipe_data['percentProtein']
|
71 |
+
percent_fat = recipe_data['percentFat']
|
72 |
+
percent_carbs = recipe_data['percentCarbs']
|
73 |
+
|
74 |
+
# food name message
|
75 |
+
col1.success(f"It's an {food}")
|
76 |
+
|
77 |
+
if source_code == 200:
|
78 |
+
# desplay food recipe
|
79 |
+
st.header(recipe_data['title']+" Recipe")
|
80 |
+
|
81 |
+
col3, col4 = st.columns(2)
|
82 |
+
|
83 |
+
with col3:
|
84 |
+
# Ingridents of recipie
|
85 |
+
st.subheader('Ingredients')
|
86 |
+
# st.info(recipe_data['ingridents'])
|
87 |
+
for i in recipe_data['ingridents']:
|
88 |
+
st.info(f"{i}")
|
89 |
+
# Inctuction for recipe
|
90 |
+
with col4:
|
91 |
+
st.subheader('Instructions')
|
92 |
+
st.info(recipe_data['instructions'])
|
93 |
+
# st.subheader('Caloric Breakdown')
|
94 |
+
'''
|
95 |
+
## Caloric Breakdown
|
96 |
+
'''
|
97 |
+
st.success(f'''
|
98 |
+
* Protien: {percent_Protein}%
|
99 |
+
* Fat: {percent_fat}%
|
100 |
+
* Carbohydrates: {percent_carbs}%
|
101 |
+
''')
|
102 |
+
|
103 |
+
|
104 |
+
else:
|
105 |
+
st.error('Something went wrong please try again :(')
|
106 |
+
|
107 |
+
elif food_cat == 'not food':
|
108 |
+
with col1:
|
109 |
+
st.warning('Invalid Image Please Add Food Image ๐จโ๐ง')
|
110 |
+
|
111 |
+
|
112 |
+
else:
|
113 |
+
st.warning('Please Upload Image')
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
if __name__=='__main__':
|
119 |
main()
|