AlaaElsayed commited on
Commit
f2cee83
·
verified ·
1 Parent(s): 11b476f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -13
app.py CHANGED
@@ -50,22 +50,24 @@
50
 
51
  # demo.launch()
52
 
 
53
  from ultralytics import YOLO
54
  import pandas as pd
55
  from PIL import Image
56
  import gradio as gr
 
57
 
58
- # تحميل الموديل
59
  model = YOLO("best.pt")
60
 
61
- # تحميل بيانات التغذية
62
  food_df = pd.read_csv("food_cleaned.csv")
63
 
64
- # جلب القيم الغذائية
65
  def get_nutrition(label):
66
  row = food_df[food_df["Food_Name"].str.lower() == label.lower()]
67
  if row.empty:
68
- return {"label": label, "info": "No data"}
69
  return {
70
  "label": label,
71
  "calories": float(row["Calories_per_100g"].values[0]),
@@ -74,32 +76,66 @@ def get_nutrition(label):
74
  "carbs": float(row["Carbs_g"].values[0])
75
  }
76
 
77
- # دالة الكشف
78
  def detect(image):
79
  results = model.predict(image)
80
  result = results[0]
81
  boxes = result.boxes
82
  names = model.names
83
 
84
- detected_info = []
85
-
86
  for box in boxes:
87
  cls_id = int(box.cls[0])
88
  label = names[cls_id]
 
 
 
 
 
 
 
 
 
 
 
89
  nutrition = get_nutrition(label)
90
- detected_info.append(nutrition)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- return Image.fromarray(result.plot()), detected_info
93
 
94
- # Gradio app
95
  demo = gr.Interface(
96
  fn=detect,
97
  inputs=gr.Image(type="pil"),
98
- outputs=[gr.Image(type="pil", label="Detected Image"), gr.JSON(label="Nutrition Info")],
99
- title="YOLOv8 Food Detector + Nutrition Info",
100
- description="Upload an image of food and get calories and nutrients without overlaying on the image."
 
 
 
101
  )
102
 
103
  demo.launch()
104
 
105
 
 
 
50
 
51
  # demo.launch()
52
 
53
+
54
  from ultralytics import YOLO
55
  import pandas as pd
56
  from PIL import Image
57
  import gradio as gr
58
+ from collections import Counter
59
 
60
+ # Load YOLO model
61
  model = YOLO("best.pt")
62
 
63
+ # Load food nutrition data
64
  food_df = pd.read_csv("food_cleaned.csv")
65
 
66
+ # Retrieve nutrition info for a given label
67
  def get_nutrition(label):
68
  row = food_df[food_df["Food_Name"].str.lower() == label.lower()]
69
  if row.empty:
70
+ return {"label": label, "info": "No data found"}
71
  return {
72
  "label": label,
73
  "calories": float(row["Calories_per_100g"].values[0]),
 
76
  "carbs": float(row["Carbs_g"].values[0])
77
  }
78
 
79
+ # Detection and nutrition calculation
80
  def detect(image):
81
  results = model.predict(image)
82
  result = results[0]
83
  boxes = result.boxes
84
  names = model.names
85
 
86
+ detected_labels = []
 
87
  for box in boxes:
88
  cls_id = int(box.cls[0])
89
  label = names[cls_id]
90
+ detected_labels.append(label)
91
+
92
+ label_counts = Counter(detected_labels)
93
+
94
+ total_calories = 0
95
+ total_fat = 0
96
+ total_protein = 0
97
+ total_carbs = 0
98
+ detailed_items = []
99
+
100
+ for label, count in label_counts.items():
101
  nutrition = get_nutrition(label)
102
+ if "info" in nutrition:
103
+ continue
104
+ nutrition["count"] = count
105
+ nutrition["total_calories"] = round(nutrition["calories"] * count, 2)
106
+ nutrition["total_fat"] = round(nutrition["fat"] * count, 2)
107
+ nutrition["total_protein"] = round(nutrition["protein"] * count, 2)
108
+ nutrition["total_carbs"] = round(nutrition["carbs"] * count, 2)
109
+
110
+ total_calories += nutrition["total_calories"]
111
+ total_fat += nutrition["total_fat"]
112
+ total_protein += nutrition["total_protein"]
113
+ total_carbs += nutrition["total_carbs"]
114
+
115
+ detailed_items.append(nutrition)
116
+
117
+ overall_summary = {
118
+ "Total Calories": round(total_calories, 2),
119
+ "Total Fat": round(total_fat, 2),
120
+ "Total Protein": round(total_protein, 2),
121
+ "Total Carbs": round(total_carbs, 2)
122
+ }
123
 
124
+ return Image.fromarray(result.plot()), {"summary": overall_summary, "details": detailed_items}
125
 
126
+ # Gradio web app
127
  demo = gr.Interface(
128
  fn=detect,
129
  inputs=gr.Image(type="pil"),
130
+ outputs=[
131
+ gr.Image(type="pil", label="Detected Image"),
132
+ gr.JSON(label="Nutrition Info")
133
+ ],
134
+ title="Smart Food Detector - Nutrition Calculator",
135
+ description="Upload a food image to get total calories, fat, protein, and carbs."
136
  )
137
 
138
  demo.launch()
139
 
140
 
141
+