Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,12 @@ from PIL import Image
|
|
7 |
from autogen import AssistantAgent, GroupChat, GroupChatManager
|
8 |
import os
|
9 |
import openai
|
|
|
|
|
|
|
10 |
|
11 |
# Multi-label recognition model (placeholder - swap for fine-tuned multi-label later)
|
12 |
-
recognizer = pipeline("image-classification", model=
|
13 |
|
14 |
# Agent Definitions
|
15 |
food_recognizer = AssistantAgent(
|
@@ -46,17 +49,31 @@ manager = GroupChatManager(groupchat=group_chat)
|
|
46 |
|
47 |
# Agent Functions
|
48 |
def recognize_foods(image):
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
def estimate_sizes(image, foods):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
return sizes
|
61 |
|
62 |
def fetch_nutrition(foods_with_sizes, nutritionix_key):
|
|
|
7 |
from autogen import AssistantAgent, GroupChat, GroupChatManager
|
8 |
import os
|
9 |
import openai
|
10 |
+
from ultralytics import YOLO
|
11 |
+
|
12 |
+
model = YOLO("yolov8n.pt") # Nano model for speed, fine-tune on food data later
|
13 |
|
14 |
# Multi-label recognition model (placeholder - swap for fine-tuned multi-label later)
|
15 |
+
recognizer = pipeline("image-classification", model=model)
|
16 |
|
17 |
# Agent Definitions
|
18 |
food_recognizer = AssistantAgent(
|
|
|
49 |
|
50 |
# Agent Functions
|
51 |
def recognize_foods(image):
|
52 |
+
start = time.time()
|
53 |
+
# Resize to 640x640 (YOLO default)
|
54 |
+
pil_image = Image.fromarray(image).resize((640, 640))
|
55 |
+
results = model(pil_image)
|
56 |
+
foods = []
|
57 |
+
for result in results:
|
58 |
+
for cls in result.boxes.cls:
|
59 |
+
label = model.names[int(cls)]
|
60 |
+
if "food" in label.lower() or label in ["pasta", "rice", "tomato", "potato", "bread"]: # Expand this list
|
61 |
+
conf = result.boxes.conf[result.boxes.cls == cls].item()
|
62 |
+
foods.append((label, conf))
|
63 |
+
print(f"Recognition took {time.time() - start:.2f}s")
|
64 |
+
return list(set(foods)) # Remove duplicates
|
65 |
|
66 |
def estimate_sizes(image, foods):
|
67 |
+
start = time.time()
|
68 |
+
img_cv = cv2.cvtColor(image, cv2.COLOR_RGB2BGR).resize((640, 640)) # Match YOLO size
|
69 |
+
sizes = {}
|
70 |
+
total_area = img_cv.shape[0] * img_cv.shape[1]
|
71 |
+
for food, _ in foods:
|
72 |
+
# Dummy: assume area proportion (refine with food-specific weights later)
|
73 |
+
area = total_area / len(foods) # Even split for now
|
74 |
+
grams = min(500, int(area / (640 * 640) * 100)) # 100g per ~640k pixels
|
75 |
+
sizes[food] = grams
|
76 |
+
print(f"Size estimation took {time.time() - start:.2f}s")
|
77 |
return sizes
|
78 |
|
79 |
def fetch_nutrition(foods_with_sizes, nutritionix_key):
|