-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
176 lines (147 loc) · 6.23 KB
/
ui.py
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
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import torch
from models.models import NutriFusionNet
from torchvision import transforms
import os
import pandas as pd
import numpy as np
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#============== Load the model ==============#
embeddings = torch.load(f'./embeddings/ingredient_embeddings_gat_512.pt', map_location=device, weights_only=True)
model = NutriFusionNet(num_ingr=199, backbone='vit', ingredient_embedding=embeddings, hidden_dim=512, lstm_layers=2, num_layers=2, extraction_mode='global_pooling').to(device)
model.load_state_dict(torch.load("./models/checkpoints/NutriFusionNet_vit_global_pooling_2lstm_2attn_gat_512_pretrained_da_16_75_25.pth", map_location=device, weights_only=True))
# Preprocess the image
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
def preprocess_image(image_path):
image = Image.open(image_path)
image = transform(image).unsqueeze(0)
return image.to(device)
#============== Load the ingredient metadata ==============#
dataset_path = '../data/nutrition5k_reconstructed/'
image_path = os.path.join(dataset_path, 'images')
ingr_mata = os.path.join(dataset_path, 'metadata/ingredients_metadata.csv')
# Load the ingredient metadata
ingr_dataset_path = './utils/data/test_labels_ingr_log.csv'
ingr_df = pd.read_csv(ingr_mata)
ingr_index = {}
ingr_indx_df = pd.read_csv(ingr_dataset_path)
colnames = ingr_indx_df.columns[1:-1]
for i in range(len(colnames)):
ingr_index[i] = colnames[i]
# ingr,id,cal/g,fat(g),carb(g),protein(g)
ingr_dict = {}
for i in range(len(ingr_df)):
ingr = ingr_df.iloc[i]['ingr']
cal = ingr_df.iloc[i]['cal/g']
fat = ingr_df.iloc[i]['fat(g)']
carb = ingr_df.iloc[i]['carb(g)']
protein = ingr_df.iloc[i]['protein(g)']
ingr_dict[ingr] = (cal, fat, carb, protein)
#============== Inference function ==============#
def calculate_ingrdients_and_nutritional_facts(outputs, ingr_dict, ingr_index, k = 3):
# outputs will always be (1, 199) since we are processing one image at a time
outputs = outputs.squeeze(0).cpu().detach().numpy()
sample_calories = 0
sample_mass = 0
sample_fat = 0
sample_carbs = 0
sample_protein = 0
top_k_ingr = []
idx = np.where(outputs > 0.15)[0]
if len(idx) != 0:
# Dictionary to store ingredient mass for top k selection
ingredient_masses = {}
for ingr_idx in idx:
mass = outputs[ingr_idx]
# Convert back from log scale
mass = np.exp(mass) - 1
ingr_name = ingr_index[ingr_idx]
cal, fat, carb, protein = ingr_dict[ingr_name]
sample_calories += mass * cal
sample_fat += mass * fat
sample_carbs += mass * carb
sample_protein += mass * protein
sample_mass += mass
# Store ingredient name and mass
ingredient_masses[ingr_name] = mass
# Sort ingredients by mass and select the top 3
sorted_ingredients = sorted(ingredient_masses.items(), key=lambda x: x[1], reverse=True)
top_k_ingr = [ingr for ingr, _ in sorted_ingredients[:k]]
else:
top_k_ingr = ["No ingredients detected"]
return {
"calories": sample_calories,
"mass": sample_mass,
"fat": sample_fat,
"carbs": sample_carbs,
"protein": sample_protein,
"top_k_ingr": top_k_ingr
}
#============== Process the image ==============#
# Function to process the image and predict
def process_image():
# Get the file path
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp")])
if not file_path:
# No file selected, output a message
ingredients_text.set("No image selected. Please try again.")
stats_text.set("")
return
# Load and display the image
image = Image.open(file_path)
image.thumbnail((300, 300)) # Resize for display
img_display = ImageTk.PhotoImage(image)
img_label.config(image=img_display)
img_label.image = img_display
# Preprocess the image
image_tensor = preprocess_image(file_path)
outputs = model(image_tensor)
results = calculate_ingrdients_and_nutritional_facts(outputs, ingr_dict, ingr_index)
ingredients = results["top_k_ingr"]
nutritional_facts = {
"Calories": results["calories"],
"Total Mass (g)": results["mass"],
"Fat (g)": results["fat"],
"Carbs (g)": results["carbs"],
"Protein (g)": results["protein"],
}
# Update the UI with the predictions
ingredients_text.set("\n".join(ingredients))
stats_text.set("\n".join([f"{k}: {v:.2f}" for k, v in nutritional_facts.items()]))
return ingredients, nutritional_facts
# Create the UI
root = tk.Tk()
root.title("Image to Ingredients & Nutrition Facts")
# File selection and display frame
frame_top = tk.Frame(root)
frame_top.pack(pady=10)
select_button = tk.Button(frame_top, text="Select Image", command=process_image)
select_button.pack(side=tk.LEFT, padx=10)
img_label = tk.Label(frame_top)
img_label.pack(side=tk.RIGHT, padx=10)
# Results frame
frame_results = tk.Frame(root)
frame_results.pack(pady=10)
# Ingredients
ingredients_label = tk.Label(frame_results, text="Ingredients:", font=("Arial", 14))
ingredients_label.grid(row=0, column=0, sticky=tk.W, padx=10, pady=5)
ingredients_text = tk.StringVar()
ingredients_display = tk.Label(frame_results, textvariable=ingredients_text, justify=tk.LEFT, font=("Arial", 12))
ingredients_display.grid(row=1, column=0, sticky=tk.W, padx=10)
# Nutritional Facts
stats_label = tk.Label(frame_results, text="Nutritional Facts:", font=("Arial", 14))
stats_label.grid(row=0, column=1, sticky=tk.W, padx=10, pady=5)
stats_text = tk.StringVar()
stats_display = tk.Label(frame_results, textvariable=stats_text, justify=tk.LEFT, font=("Arial", 12))
stats_display.grid(row=1, column=1, sticky=tk.W, padx=10)
if __name__ == '__main__':
root.mainloop()
# # test the model
# image_file = '../data/nutrition5k_reconstructed/images/dish_1568048053.jpeg'
# process_image(image_file)