File size: 4,146 Bytes
06b1c65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
import openpyxl

#Function to predict the food from the image using the pre-trained model "nateraw/food"
def predict(image):
    extractor = AutoFeatureExtractor.from_pretrained("nateraw/food")
    model = AutoModelForImageClassification.from_pretrained("nateraw/food")
    
    input = extractor(images=image, return_tensors='pt')
    output = model(**input)
    logits = output.logits
    
    pred_class = logits.argmax(-1).item()
    return(model.config.id2label[pred_class])

#Function to retrieve the Nutritional Value from database.xlsx which is downloaded from USDA
def check_food(food, counter):
    path = './database.xlsx'
    wb_obj = openpyxl.load_workbook(path)
    sheet_obj = wb_obj.active
    
    foodPred, cal, carb, prot, fat = None, None, None, None, None
    
    #Filter to prioritize the most probable match between the prediction and the entries in the database
    for i in range(3, sheet_obj.max_row+1):
        cell_obj = sheet_obj.cell(row = i, column = 2)
        if counter == 0:
            if len(food) >= 3:
                foodName = food[0].capitalize() + " " + food[1] + " " + food[2] + ","
            elif len(food) == 2:
                foodName = food[0].capitalize() + " " + food[1] + ","
            elif len(food) == 1:
                foodName = food[0].capitalize() + ","
            condition = foodName == cell_obj.value[0:len(foodName):]
        elif counter == 1:
            if len(food) >= 3:
                foodName = food[0].capitalize() + " " + food[1] + " " + food[2]
            elif len(food) == 2:
                foodName = food[0].capitalize() + " " + food[1]
            elif len(food) == 1:
                foodName = food[0].capitalize()
            condition = foodName == cell_obj.value[0:len(foodName):]
        elif counter == 2:
            if len(food) >= 3:
                foodName = food[0] + " " + food[1] + " " + food[2]
            elif len(food) == 2:
                foodName = food[0] + " " + food[1]
            elif len(food) == 1:
                foodName = food[0]
            condition = foodName in cell_obj.value
        elif (counter == 3) & (len(food) > 1):
            condition = food[0] in cell_obj.value
        else:
            break
        
        #Update values if conditions are met
        if condition:
            foodPred = cell_obj.value
            cal = sheet_obj.cell(row = i, column = 5).value
            carb = sheet_obj.cell(row = i, column = 7).value
            prot = sheet_obj.cell(row = i, column = 6).value
            fat = sheet_obj.cell(row = i, column = 10).value
            break
    
    return foodPred, cal, carb, prot, fat

#Function to prepare the output
def get_cc(food, weight):

    #Configure the food string to match the entries in the database
    food = food.split("_")
    if food[-1][-1] == "s":
        food[-1] = food[-1][:-1]
    
    foodPred, cal, carb, prot, fat = None, None, None, None, None
    counter = 0
    
    #Try for the most probable match between the prediction and the entries in the database
    while (not foodPred) &  (counter <= 3):
        foodPred, cal, carb, prot, fat = check_food(food,counter)
        counter += 1

    #Check if there is a match
    if food:        
        output = foodPred + "\nCalories: " + str(round(cal * weight)/100) + " kJ\nCarbohydrate: " + str(round(carb * weight)/100) + " g\nProtein: " + str(round(prot * weight)/100) + " g\nTotal Fat: " + str(round(fat * weight)/100) + " g"      
    elif not food:
        output = "No data for food"

    return(output)

#Main function
def CC(image, weight):
    pred = predict(image)
    cc = get_cc(pred, weight)
    return(pred, cc)

interface = gr.Interface(
    fn = CC,
    inputs = [gr.inputs.Image(shape=(224,224)), gr.inputs.Number(default = 100, label = "Weight in grams (g):")],
    outputs = [gr.outputs.Textbox(label='Food Prediction:'), gr.outputs.Textbox(label='Nutritional Value:')],
    examples = [["pizza.jpg", 107], ["spaghetti.jpg",205]])

interface.launch()