dschandra commited on
Commit
3ca8a50
·
verified ·
1 Parent(s): 9873322

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
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
- # Format output
24
- if not food_items:
25
- return "No valid food items detected. Please try again with a clearer food image."
26
 
27
- description = f"Detected {len(food_items)} food items: {', '.join(food_items)}.\n\n"
28
- description += f"Portion size: {portion_size}.\n\n"
 
 
 
29
  description += "Nutritional Information:\n"
30
 
31
- for food, nutrients in nutrition_data.items():
32
- if "Error" in nutrients:
33
- description += f"- {food}: {nutrients['Error']}\n"
34
- else:
35
- description += (
36
- f"- {food}:\n"
37
- f" - Energy: {nutrients['Energy (kcal)']} kcal\n"
38
- f" - Protein: {nutrients['Protein (g)']} g\n"
39
- f" - Carbs: {nutrients['Carbs (g)']} g\n"
40
- f" - Fiber: {nutrients['Fiber (g)']} g\n"
41
- f" - Fat: {nutrients['Fat (g)']} g\n"
42
- f" - Sugar: {nutrients['Sugar (g)']} g\n"
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 and portion size."
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__":