|
|
import gradio as gr |
|
|
from PIL import Image |
|
|
from food_identification import identify_food |
|
|
from nutritional_analysis import get_nutritional_info |
|
|
from portion_size_analysis import estimate_portion_size |
|
|
|
|
|
def analyze_food(image): |
|
|
""" |
|
|
Analyze the uploaded image for food items and provide dynamic nutritional and portion size information. |
|
|
""" |
|
|
|
|
|
food_items = identify_food(image) |
|
|
|
|
|
|
|
|
nutrition_data = get_nutritional_info(food_items) |
|
|
|
|
|
|
|
|
portion_size = estimate_portion_size(image) |
|
|
|
|
|
|
|
|
description = f"Detected food items: {', '.join(food_items)}.\n\n" |
|
|
description += f"Portion size: {portion_size}.\n\n" |
|
|
description += "Nutritional Information:\n" |
|
|
|
|
|
for food, nutrients in nutrition_data.items(): |
|
|
if "Error" in nutrients: |
|
|
description += f"- {food}: {nutrients['Error']}\n" |
|
|
else: |
|
|
description += ( |
|
|
f"- {food}:\n" |
|
|
f" - Energy: {nutrients['Energy (kcal)']} kcal\n" |
|
|
f" - Protein: {nutrients['Protein (g)']} g\n" |
|
|
f" - Carbs: {nutrients['Carbs (g)']} g\n" |
|
|
f" - Fiber: {nutrients['Fiber (g)']} g\n" |
|
|
f" - Fat: {nutrients['Fat (g)']} g\n" |
|
|
f" - Sugar: {nutrients['Sugar (g)']} g\n" |
|
|
) |
|
|
return description |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=analyze_food, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs="text", |
|
|
title="Diet Nutrition Analyzer", |
|
|
description="Upload an image of your food plate to analyze nutrition and portion size dynamically." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch() |
|
|
|