Update app.py
Browse files
app.py
CHANGED
|
@@ -2,55 +2,45 @@ import gradio as gr
|
|
| 2 |
from image_processing import preprocess_image
|
| 3 |
from food_identification import identify_food
|
| 4 |
from nutritional_analysis import get_nutritional_info
|
| 5 |
-
from portion_size_analysis import estimate_portion_size
|
| 6 |
|
| 7 |
def analyze_food(image):
|
|
|
|
|
|
|
|
|
|
| 8 |
# Step 1: Preprocess the image
|
| 9 |
preprocessed_image = preprocess_image(image)
|
| 10 |
-
|
| 11 |
-
# Step 2: Identify food items
|
| 12 |
-
food_items = identify_food(preprocessed_image)
|
| 13 |
-
|
| 14 |
-
# Filter non-food items (manually exclude invalid detections)
|
| 15 |
-
food_items = [item for item in food_items if item not in ["packet", "spoon", "bowl", "plate"]]
|
| 16 |
-
|
| 17 |
-
# Step 3: Fetch nutritional information
|
| 18 |
-
nutrition_data = get_nutritional_info(food_items)
|
| 19 |
-
|
| 20 |
-
# Step 4: Estimate portion size
|
| 21 |
-
portion_size = estimate_portion_size(image)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
return "No valid food items detected. Please try again with a clearer food image."
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
description += "Nutritional Information:\n"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
return description
|
| 45 |
|
| 46 |
-
|
| 47 |
# Gradio Interface
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=analyze_food,
|
| 50 |
inputs=gr.Image(type="pil"),
|
| 51 |
outputs="text",
|
| 52 |
title="Diet Nutrition Analyzer",
|
| 53 |
-
description="Upload an image of your food plate to analyze nutrition
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from image_processing import preprocess_image
|
| 3 |
from food_identification import identify_food
|
| 4 |
from nutritional_analysis import get_nutritional_info
|
|
|
|
| 5 |
|
| 6 |
def analyze_food(image):
|
| 7 |
+
"""
|
| 8 |
+
Analyze the uploaded image for food items and provide dynamic nutritional information.
|
| 9 |
+
"""
|
| 10 |
# Step 1: Preprocess the image
|
| 11 |
preprocessed_image = preprocess_image(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Step 2: Detect food items dynamically
|
| 14 |
+
food_item = identify_food(preprocessed_image)
|
|
|
|
| 15 |
|
| 16 |
+
# Step 3: Fetch dynamic nutritional data
|
| 17 |
+
nutrition_data = get_nutritional_info(food_item)
|
| 18 |
+
|
| 19 |
+
# Format the output
|
| 20 |
+
description = f"Detected food item: {food_item}.\n\n"
|
| 21 |
description += "Nutritional Information:\n"
|
| 22 |
|
| 23 |
+
if "Error" in nutrition_data:
|
| 24 |
+
description += f"- {food_item}: {nutrition_data['Error']}\n"
|
| 25 |
+
else:
|
| 26 |
+
description += (
|
| 27 |
+
f"- {food_item}:\n"
|
| 28 |
+
f" - Energy: {nutrition_data['Energy (kcal)']} kcal\n"
|
| 29 |
+
f" - Protein: {nutrition_data['Protein (g)']} g\n"
|
| 30 |
+
f" - Carbs: {nutrition_data['Carbs (g)']} g\n"
|
| 31 |
+
f" - Fiber: {nutrition_data['Fiber (g)']} g\n"
|
| 32 |
+
f" - Fat: {nutrition_data['Fat (g)']} g\n"
|
| 33 |
+
f" - Sugar: {nutrition_data['Sugar (g)']} g\n"
|
| 34 |
+
)
|
|
|
|
| 35 |
return description
|
| 36 |
|
|
|
|
| 37 |
# Gradio Interface
|
| 38 |
iface = gr.Interface(
|
| 39 |
fn=analyze_food,
|
| 40 |
inputs=gr.Image(type="pil"),
|
| 41 |
outputs="text",
|
| 42 |
title="Diet Nutrition Analyzer",
|
| 43 |
+
description="Upload an image of your food plate to analyze nutrition dynamically."
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|