File size: 6,297 Bytes
6a86b46
 
 
 
 
 
 
c2cd6cd
6a86b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2cd6cd
 
 
 
 
 
 
 
 
 
 
 
 
 
6a86b46
 
 
 
 
c2cd6cd
 
6a86b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2cd6cd
6a86b46
 
 
 
 
 
 
 
 
 
 
 
c2cd6cd
6a86b46
 
 
 
 
 
 
71ce8c5
6a86b46
 
 
 
 
c2cd6cd
6a86b46
 
 
 
 
 
 
 
 
 
 
 
c2cd6cd
6a86b46
c2cd6cd
6a86b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2cd6cd
 
 
 
 
6a86b46
 
 
c2cd6cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a86b46
c2cd6cd
 
6a86b46
c2cd6cd
 
 
6a86b46
 
 
 
 
 
 
 
 
c2cd6cd
6a86b46
 
 
 
 
 
 
 
 
 
 
 
 
c2cd6cd
6a86b46
 
 
 
 
 
 
 
 
b04d195
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import gradio as gr
import os
import json
import requests
from PIL import Image
from difflib import get_close_matches
from functools import lru_cache
from collections import Counter

# =====================
# ENV
# =====================
ROBOFLOW_API_KEY = os.getenv("ROBOFLOW_API_KEY")

# =====================
# LOAD DB
# =====================
with open("nutrition_db.json", "r") as f:
    NUTRITION_DB = json.load(f)

print("βœ… Loaded DB:", len(NUTRITION_DB))

# =====================
# COUNTABLE FOODS (NEW)
# =====================
COUNTABLE_FOODS = {
    "roti": {"calories": 120, "protein": 3, "carbs": 20, "fat": 2},
    "bread": {"calories": 80, "protein": 3, "carbs": 15, "fat": 1},
    "samosa": {"calories": 260, "protein": 5, "carbs": 30, "fat": 14},
    "gulab jamun": {"calories": 150, "protein": 2, "carbs": 30, "fat": 5},
    "laddu": {"calories": 180, "protein": 3, "carbs": 25, "fat": 8},
    "idli": {"calories": 60, "protein": 2, "carbs": 12, "fat": 1},
    "vada": {"calories": 150, "protein": 4, "carbs": 20, "fat": 8}
}

# =====================
# NORMALIZATION (FIXED)
# =====================
def normalize_food_name(name):
    name = name.lower()

    mapping = {
        "chapati": "roti",
        "omellete": "omelette",
        "fried rice": "rice",
        "plain rice": "rice"
    }

    return mapping.get(name, name)

# =====================
# FIND MATCH
# =====================
def find_food(name):
    if name in NUTRITION_DB:
        return name

    matches = get_close_matches(name, NUTRITION_DB.keys(), n=1, cutoff=0.6)
    return matches[0] if matches else None

# =====================
# FALLBACK
# =====================
def estimate_unknown_food(grams):
    return {
        "calories": round(1.5 * grams, 2),
        "protein": round(0.05 * grams, 2),
        "carbs": round(0.2 * grams, 2),
        "fat": round(0.05 * grams, 2),
    }

# =====================
# CACHE
# =====================
@lru_cache(maxsize=1000)
def compute_nutrition_cached(food_key, grams):
    base = NUTRITION_DB[food_key]
    factor = grams / 100

    return {
        "calories": round(base["calories"] * factor, 2),
        "protein": round(base["protein"] * factor, 2),
        "carbs": round(base["carbs"] * factor, 2),
        "fat": round(base["fat"] * factor, 2),
    }

# =====================
# GET NUTRITION
# =====================
def get_nutrition(dish, grams):
    dish = normalize_food_name(dish)
    food_key = find_food(dish)

    if not food_key:
        return estimate_unknown_food(grams)

    return compute_nutrition_cached(food_key, grams)

# =====================
# QUANTITY ESTIMATION
# =====================
def estimate_quantity(pred):
    width = pred.get("width", 0)
    height = pred.get("height", 0)

    area = width * height
    ratio = area / (640 * 640)

    grams = 150 + (ratio * 300)
    return round(grams, 1)

# =====================
# ROBOFLOW DETECTION
# =====================
def detect(image_path):
    api_key = os.getenv("ROBOFLOW_API_KEY")

    if not api_key:
        return "❌ API KEY NOT FOUND"

    url = "https://detect.roboflow.com/almost-final/2"

    try:
        with open(image_path, "rb") as f:
            response = requests.post(
                url,
                files={"file": f},
                params={"api_key": api_key},
                timeout=15
            )

        if response.status_code != 200:
            return f"❌ Roboflow Error: {response.text}"

        data = response.json()
        return data.get("predictions", [])

    except Exception as e:
        return f"❌ Request Failed: {str(e)}"

# =====================
# MAIN FUNCTION (HYBRID)
# =====================
def analyze_image(image):
    if image is None:
        return "Please upload an image"

    path = "temp.jpg"
    image.save(path)

    preds = detect(path)

    if isinstance(preds, str):
        return preds

    if len(preds) == 0:
        return "❌ No food detected"

    # GROUP ITEMS
    dish_counts = Counter([
        normalize_food_name(p.get("class", "unknown")) for p in preds
    ])

    output = ""
    total = {"calories": 0, "protein": 0, "carbs": 0, "fat": 0}

    for dish, count in dish_counts.items():

        if dish == "unknown":
            continue

        # =========================
        # COUNTABLE FOODS
        # =========================
        if dish in COUNTABLE_FOODS:
            base = COUNTABLE_FOODS[dish]

            nutrition = {
                "calories": base["calories"] * count,
                "protein": base["protein"] * count,
                "carbs": base["carbs"] * count,
                "fat": base["fat"] * count,
            }

            output += f"🍽️ {dish} ({count} pcs)\n"

        # =========================
        # WEIGHT-BASED FOODS
        # =========================
        else:
            relevant_preds = [
                p for p in preds if normalize_food_name(p.get("class")) == dish
            ]

            grams_list = [estimate_quantity(p) for p in relevant_preds]
            grams = sum(grams_list)

            nutrition = get_nutrition(dish, grams)

            output += f"🍽️ {dish}\n"
            output += f"πŸ“ {grams:.1f} g\n"

        # =========================
        # OUTPUT
        # =========================
        output += f"πŸ”₯ {nutrition['calories']} kcal\n"
        output += f"πŸ’ͺ Protein: {nutrition['protein']} g\n"
        output += f"🍞 Carbs: {nutrition['carbs']} g\n"
        output += f"🧈 Fat: {nutrition['fat']} g\n"
        output += "-"*30 + "\n"

        for k in total:
            total[k] += nutrition[k]

    # TOTAL
    output += "\n🧾 TOTAL:\n"
    output += f"πŸ”₯ Calories: {round(total['calories'],2)}\n"
    output += f"πŸ’ͺ Protein: {round(total['protein'],2)} g\n"
    output += f"🍞 Carbs: {round(total['carbs'],2)} g\n"
    output += f"🧈 Fat: {round(total['fat'],2)} g\n"

    return output

# =====================
# UI
# =====================
demo = gr.Interface(
    fn=analyze_image,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="🍽️ AI Nutritionist",
    description="Upload food image to get calories & macros"
)

# =====================
# RUN
# =====================
if __name__ == "__main__":
    demo.launch(ssr_mode=False)