Commit
·
8ada8ab
1
Parent(s):
abd6949
API for nutrition values
Browse files- __pycache__/app.cpython-311.pyc +0 -0
- app.py +18 -14
- index.py +25 -6
__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
|
app.py
CHANGED
|
@@ -44,16 +44,18 @@ def analyze_nutrition(ingredients):
|
|
| 44 |
# Edamam API application ID and key
|
| 45 |
app_id = "26722303"
|
| 46 |
app_key = "44f19a04e17d83e91706e4047804e690"
|
|
|
|
|
|
|
| 47 |
|
| 48 |
for ingredient in ingredients:
|
|
|
|
|
|
|
| 49 |
# Parameters for the API request
|
| 50 |
params = {
|
| 51 |
"app_id": app_id,
|
| 52 |
"app_key": app_key,
|
| 53 |
"ingr": ingredient
|
| 54 |
}
|
| 55 |
-
print(ingredient)
|
| 56 |
-
|
| 57 |
try:
|
| 58 |
# Send a GET request to the API
|
| 59 |
response = requests.get(endpoint, params=params)
|
|
@@ -63,24 +65,26 @@ def analyze_nutrition(ingredients):
|
|
| 63 |
# Parse the JSON response
|
| 64 |
data = response.json()
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
print() # Add a newline for separation
|
| 75 |
|
|
|
|
| 76 |
else:
|
| 77 |
print("Error for", ingredient, ":", response.status_code)
|
| 78 |
|
| 79 |
except requests.exceptions.RequestException as e:
|
| 80 |
print("Error for", ingredient, ":", e)
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Example ingredients list
|
| 83 |
-
ingredients = ["Orange per 100 grams", "Apple per 100 grams", "Banana per 100 grams"]
|
| 84 |
|
| 85 |
-
# Analyze nutrition for all ingredients
|
| 86 |
-
analyze_nutrition(ingredients)
|
|
|
|
| 44 |
# Edamam API application ID and key
|
| 45 |
app_id = "26722303"
|
| 46 |
app_key = "44f19a04e17d83e91706e4047804e690"
|
| 47 |
+
processed_ingredients = set()
|
| 48 |
+
food_dict= {}
|
| 49 |
|
| 50 |
for ingredient in ingredients:
|
| 51 |
+
if ingredient in processed_ingredients:
|
| 52 |
+
continue
|
| 53 |
# Parameters for the API request
|
| 54 |
params = {
|
| 55 |
"app_id": app_id,
|
| 56 |
"app_key": app_key,
|
| 57 |
"ingr": ingredient
|
| 58 |
}
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
# Send a GET request to the API
|
| 61 |
response = requests.get(endpoint, params=params)
|
|
|
|
| 65 |
# Parse the JSON response
|
| 66 |
data = response.json()
|
| 67 |
|
| 68 |
+
food_dict[ingredient] = {
|
| 69 |
+
'Calories': str(data['calories']) + "kcal",
|
| 70 |
+
'Calories from Protein': str(data['totalNutrientsKCal']['PROCNT_KCAL']['quantity']) + "kcal",
|
| 71 |
+
'Calories from Fat': str(data['totalNutrientsKCal']['FAT_KCAL']['quantity']) + "kcal",
|
| 72 |
+
'Calories from Carbohydrates': str(data['totalNutrientsKCal']['CHOCDF_KCAL']['quantity']) + "kcal",
|
| 73 |
+
'Grams in Protein': str(data['totalNutrients']['PROCNT']['quantity']) + "g",
|
| 74 |
+
'Grams in Carbohydrates': str(data['totalNutrients']['CHOCDF']['quantity']) +"g"
|
| 75 |
+
}
|
|
|
|
| 76 |
|
| 77 |
+
processed_ingredients.add(ingredient)
|
| 78 |
else:
|
| 79 |
print("Error for", ingredient, ":", response.status_code)
|
| 80 |
|
| 81 |
except requests.exceptions.RequestException as e:
|
| 82 |
print("Error for", ingredient, ":", e)
|
| 83 |
+
return food_dict
|
| 84 |
+
|
| 85 |
|
| 86 |
# Example ingredients list
|
| 87 |
+
# ingredients = ["Orange per 100 grams", "Apple per 100 grams", "Banana per 100 grams"]
|
| 88 |
|
| 89 |
+
# # # Analyze nutrition for all ingredients
|
| 90 |
+
# print(analyze_nutrition(ingredients))
|
index.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import cv2
|
| 3 |
import os
|
| 4 |
from app import *
|
|
|
|
| 5 |
|
| 6 |
# Create a folder to save captured images
|
| 7 |
if not os.path.exists("captured_images"):
|
|
@@ -19,12 +20,12 @@ background_style = """
|
|
| 19 |
"""
|
| 20 |
st.markdown(background_style, unsafe_allow_html=True)
|
| 21 |
|
| 22 |
-
|
| 23 |
# Initialize the session state
|
| 24 |
session_state = st.session_state
|
| 25 |
if 'ingredientsList' not in session_state:
|
| 26 |
session_state['ingredientsList'] = []
|
| 27 |
#["apple", "banana", "orange", "strawberries"]
|
|
|
|
| 28 |
|
| 29 |
def main():
|
| 30 |
# Create two columns
|
|
@@ -103,6 +104,7 @@ def main():
|
|
| 103 |
# Display the frame in the Streamlit app
|
| 104 |
video_placeholder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB", use_column_width=True)
|
| 105 |
if button_clicked:
|
|
|
|
| 106 |
cap.release()
|
| 107 |
if session_state['ingredientsList']:
|
| 108 |
session_state['ingredientsList'].pop()
|
|
@@ -111,14 +113,23 @@ def main():
|
|
| 111 |
displayRecipes(session_state['ingredientsList'])
|
| 112 |
# Define content for each item
|
| 113 |
content = {}
|
| 114 |
-
for ingredient in
|
| 115 |
-
content[ingredient] = askGPT(f"Give me your estimate the calories, grams of protein, grams of sugar, grams of fat, and grams of carbohydrates per 100g of {ingredient} as a list")
|
|
|
|
| 116 |
|
| 117 |
# Display expanders for each item
|
| 118 |
-
for ingredient in session_state['ingredientsList']:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
with st.sidebar.expander(ingredient):
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
def displayRecipes(ingredientsList):
|
| 124 |
items = []
|
|
@@ -179,5 +190,13 @@ def capture_image():
|
|
| 179 |
return image_path
|
| 180 |
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
if __name__ == '__main__':
|
| 183 |
main()
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import os
|
| 4 |
from app import *
|
| 5 |
+
import pprint
|
| 6 |
|
| 7 |
# Create a folder to save captured images
|
| 8 |
if not os.path.exists("captured_images"):
|
|
|
|
| 20 |
"""
|
| 21 |
st.markdown(background_style, unsafe_allow_html=True)
|
| 22 |
|
|
|
|
| 23 |
# Initialize the session state
|
| 24 |
session_state = st.session_state
|
| 25 |
if 'ingredientsList' not in session_state:
|
| 26 |
session_state['ingredientsList'] = []
|
| 27 |
#["apple", "banana", "orange", "strawberries"]
|
| 28 |
+
xyz = ["apple", "banana", "orange", "strawberries"]
|
| 29 |
|
| 30 |
def main():
|
| 31 |
# Create two columns
|
|
|
|
| 104 |
# Display the frame in the Streamlit app
|
| 105 |
video_placeholder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB", use_column_width=True)
|
| 106 |
if button_clicked:
|
| 107 |
+
nutrition_values = analyze_nutrition(nutrients(xyz))
|
| 108 |
cap.release()
|
| 109 |
if session_state['ingredientsList']:
|
| 110 |
session_state['ingredientsList'].pop()
|
|
|
|
| 113 |
displayRecipes(session_state['ingredientsList'])
|
| 114 |
# Define content for each item
|
| 115 |
content = {}
|
| 116 |
+
for ingredient in nutrition_values:
|
| 117 |
+
# content[ingredient] = askGPT(f"Give me your estimate the calories, grams of protein, grams of sugar, grams of fat, and grams of carbohydrates per 100g of {ingredient} as a list")
|
| 118 |
+
content[ingredient] = nutrition_values[ingredient]
|
| 119 |
|
| 120 |
# Display expanders for each item
|
| 121 |
+
# for ingredient in session_state['ingredientsList']:
|
| 122 |
+
# with st.sidebar.expander(ingredient):
|
| 123 |
+
# st.write(content[ingredient])
|
| 124 |
+
# displayRecipes(session_state['ingredientsList'])
|
| 125 |
+
|
| 126 |
+
for ingredient in content:
|
| 127 |
with st.sidebar.expander(ingredient):
|
| 128 |
+
ingred = str(content[ingredient])
|
| 129 |
+
st.write(ingred[1:len(ingred)-1].replace("'", "").replace(',', '\n\n'))
|
| 130 |
+
#displayRecipes(session_state['ingredientsList'])
|
| 131 |
+
|
| 132 |
+
|
| 133 |
|
| 134 |
def displayRecipes(ingredientsList):
|
| 135 |
items = []
|
|
|
|
| 190 |
return image_path
|
| 191 |
|
| 192 |
|
| 193 |
+
def nutrients(ingredients):
|
| 194 |
+
formatted_list = []
|
| 195 |
+
for ingredient in ingredients:
|
| 196 |
+
formatted_list.append(ingredient + " per 100 grams")
|
| 197 |
+
return formatted_list
|
| 198 |
+
|
| 199 |
+
|
| 200 |
if __name__ == '__main__':
|
| 201 |
main()
|
| 202 |
+
|