Vrk commited on
Commit
d565bc8
β€’
1 Parent(s): c79b178

code update

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