Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
|
6 |
+
def list_to_dict(data):
|
7 |
+
results = {}
|
8 |
+
|
9 |
+
for i in range(len(data)):
|
10 |
+
# Access the i-th dictionary in the list using an integer index
|
11 |
+
d = data[i]
|
12 |
+
# Assign the value of the 'label' key to the 'score' value in the results dictionary
|
13 |
+
results[d['label']] = d['score']
|
14 |
+
|
15 |
+
# The results dictionary will now contain the label-score pairs from the data list
|
16 |
+
return results
|
17 |
+
|
18 |
+
API_URL = "https://api-inference.huggingface.co/models/nateraw/food"
|
19 |
+
headers = {"Authorization": "Bearer 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"}
|
20 |
+
|
21 |
+
def query(filename):
|
22 |
+
with open(filename, "rb") as f:
|
23 |
+
data = f.read()
|
24 |
+
response = requests.request("POST", API_URL, headers=headers, data=data)
|
25 |
+
output = json.loads(response.content.decode("utf-8"))
|
26 |
+
return list_to_dict(output),json.dumps(output, indent=2, sort_keys=True)
|
27 |
+
|
28 |
+
def get_nutrition_info(food_name):
|
29 |
+
#Make request to Nutritionix API
|
30 |
+
response = requests.get(
|
31 |
+
"https://trackapi.nutritionix.com/v2/search/instant",
|
32 |
+
params={"query": food_name},
|
33 |
+
headers={
|
34 |
+
"x-app-id": "63a710ef",
|
35 |
+
"x-app-key": "3ddc7e3feda88e1cf6dd355fb26cb261"
|
36 |
+
}
|
37 |
+
)
|
38 |
+
#Parse response and return relevant information
|
39 |
+
data = response.json()
|
40 |
+
response = data["branded"][0]["photo"]["thumb"]
|
41 |
+
|
42 |
+
# Open the image using PIL
|
43 |
+
|
44 |
+
return {
|
45 |
+
"food_name": data["branded"][0]["food_name"],
|
46 |
+
"calories": data["branded"][0]["nf_calories"],
|
47 |
+
"serving_size": data["branded"][0]["serving_qty"],
|
48 |
+
"serving_unit": data["branded"][0]["serving_unit"],
|
49 |
+
#"images": data["branded"][0]["photo"]
|
50 |
+
},response
|
51 |
+
|
52 |
+
def volume_estimations(ali):
|
53 |
+
return None
|
54 |
+
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("Food-Classification-Calorie-Estimation and Volume-Estimation")
|
57 |
+
with gr.Tab("Food Classification"):
|
58 |
+
text_input = gr.Image(type="filepath")
|
59 |
+
text_output = [gr.Label(num_top_classes=6),
|
60 |
+
gr.Textbox()
|
61 |
+
]
|
62 |
+
text_button = gr.Button("Food Classification")
|
63 |
+
with gr.Tab("Food Calorie Estimation"):
|
64 |
+
image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie")
|
65 |
+
image_output = [gr.Textbox(),
|
66 |
+
gr.Image(type="filepath")
|
67 |
+
]
|
68 |
+
image_button = gr.Button("Estimate Calories!")
|
69 |
+
with gr.Tab("Volume Estimation"):
|
70 |
+
_image_input = gr.Textbox(label="Please Download the Photogrammetry File trained on APPLE AR KIT and follow the instruction mention below to generate the 3D Vortex of the object")
|
71 |
+
_image_output = gr.Image()
|
72 |
+
gr.Markdown("-----------------------------------------------------------------------------")
|
73 |
+
gr.Markdown("Directory where HelloPhotogrammetry app Saved. Example:/Users/ali/Desktop/HelloPhotogrammetry")
|
74 |
+
gr.Markdown("Directory where all the images are saved. Example:: ~/Desktop/Burger_Data_3")
|
75 |
+
gr.Markdown("Directory where the usdz or obj file has to be saved. Example: ~/Desktop/Burger_Data_3/Burger.usdz")
|
76 |
+
gr.Markdown("File Quality that you want your 3D model to be. Example: --detail medium ")
|
77 |
+
gr.Markdown("-----------------------------------------------------------------------------")
|
78 |
+
gr.Markdown("/Users/ali/Desktop/HelloPhotogrammetry ~/Desktop/Burger_Data_3 ~/Desktop/Burger_Data_3/Burger.obj --detail medium")
|
79 |
+
gr.Markdown("You can download the photogrammetry demo and files using this Google drive link")
|
80 |
+
gr.Markdown("-----------------------------------------------------------------------------")
|
81 |
+
gr.Markdown("https://drive.google.com/drive/folders/1QrL0Vhvw5GvIQ8fbHfb9EOsnOlPMmXLG?usp=share_link")
|
82 |
+
gr.Markdown("-----------------------------------------------------------------------------")
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
_image_button = gr.Button("Volume Calculation")
|
87 |
+
with gr.Tab("Future Works"):
|
88 |
+
gr.Markdown("Future work on Food Classification")
|
89 |
+
gr.Markdown(
|
90 |
+
"Currently the Model is trained on food-101 Dataset, which has 100 classes, In the future iteration of the project we would like to train the model on UNIMIB Dataset with 256 Food Classes")
|
91 |
+
gr.Markdown("Future work on Volume Estimation")
|
92 |
+
gr.Markdown(
|
93 |
+
"The volume model has been trained on Apple AR Toolkit and thus can be executred only on Apple devices ie a iOS platform, In futur we would like to train the volume model such that it is Platform independent")
|
94 |
+
gr.Markdown("Future work on Calorie Estimation")
|
95 |
+
gr.Markdown(
|
96 |
+
"The Calorie Estimation currently relies on Nutritionix API , In Future Iteration we would like to build our own Custom Database of Major Food Product across New York Restaurent")
|
97 |
+
gr.Markdown("https://github.com/Ali-Maq/Food-Classification-Volume-Estimation-and-Calorie-Estimation/blob/main/README.md")
|
98 |
+
|
99 |
+
text_button.click(query, inputs=text_input, outputs=text_output)
|
100 |
+
image_button.click(get_nutrition_info, inputs=image_input, outputs=image_output)
|
101 |
+
#_image_button.click(get_nutrition_info, inputs=_image_input, outputs=_image_output)
|
102 |
+
with gr.Accordion("Open for More!"):
|
103 |
+
gr.Markdown("π Designed and built by Ali Under the Guidance of Professor Dennis Shasha")
|
104 |
+
gr.Markdown("Contact me at [email protected] π")
|
105 |
+
|
106 |
+
demo.launch(share=True)
|