Spaces:
Running
Running
Update cal.py
Browse files
cal.py
CHANGED
@@ -132,12 +132,38 @@ def predict_image(image_path, model, conf_threshold=0.03):
|
|
132 |
|
133 |
# Function to calculate detected items and their calories
|
134 |
def calculate_calories(detection_details):
|
135 |
-
|
|
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
for det in detection_details:
|
138 |
item = det["class"]
|
139 |
-
calories = Config.CALORIES_DICT[item]
|
140 |
confidence = det["top_confidence"]
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
return detected_items
|
|
|
132 |
|
133 |
# Function to calculate detected items and their calories
|
134 |
def calculate_calories(detection_details):
|
135 |
+
"""
|
136 |
+
Calculate calories for detected items, keeping only the highest confidence detection for each unique food item.
|
137 |
|
138 |
+
Args:
|
139 |
+
detection_details: List of dictionaries containing detection information
|
140 |
+
Each dict has keys: "class" (food name), "top_confidence" (detection confidence), "bbox"
|
141 |
+
|
142 |
+
Returns:
|
143 |
+
List of tuples: (food_item, calories, confidence) for unique items with highest confidence
|
144 |
+
"""
|
145 |
+
# Dictionary to keep track of highest confidence detection for each food item
|
146 |
+
unique_items = {}
|
147 |
+
|
148 |
+
# Process each detection
|
149 |
for det in detection_details:
|
150 |
item = det["class"]
|
|
|
151 |
confidence = det["top_confidence"]
|
152 |
+
|
153 |
+
# Only update if this is the first instance or has higher confidence
|
154 |
+
if item not in unique_items or confidence > unique_items[item]["confidence"]:
|
155 |
+
unique_items[item] = {
|
156 |
+
"calories": Config.CALORIES_DICT[item],
|
157 |
+
"confidence": confidence
|
158 |
+
}
|
159 |
+
|
160 |
+
# Convert to list of tuples format
|
161 |
+
detected_items = [
|
162 |
+
(item, data["calories"], data["confidence"])
|
163 |
+
for item, data in unique_items.items()
|
164 |
+
]
|
165 |
+
|
166 |
+
# Sort by confidence (optional)
|
167 |
+
detected_items.sort(key=lambda x: x[2], reverse=True)
|
168 |
|
169 |
return detected_items
|