File size: 1,732 Bytes
3808896
1100370
3808896
 
1100370
3808896
 
3ca8a50
1100370
3ca8a50
1100370
 
8ed0611
1100370
 
31897a9
1100370
 
3ca8a50
1100370
 
 
8ed0611
 
1100370
 
 
 
 
 
 
 
 
 
 
 
 
8ed0611
3808896
 
 
 
 
8ed0611
3808896
1100370
3808896
 
 
 
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.
    """
    # Step 1: Identify food items
    food_items = identify_food(image)

    # Step 2: Fetch nutritional data dynamically
    nutrition_data = get_nutritional_info(food_items)

    # Step 3: Estimate portion size
    portion_size = estimate_portion_size(image)

    # Format output
    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

# Gradio Interface
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()