| | import numpy as np |
| | import gradio as gr |
| | import requests |
| | import json |
| |
|
| | def list_to_dict(data): |
| | results = {} |
| |
|
| | for i in range(len(data)): |
| | |
| | d = data[i] |
| | |
| | results[d['label']] = d['score'] |
| |
|
| | |
| | return results |
| |
|
| | API_URL = "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2Fnateraw%2Ffood%26quot%3B%3C%2Fspan%3E%3C!-- HTML_TAG_END --> |
| | headers = {"Authorization": "Bearer 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"} |
| |
|
| | def query(filename): |
| | with open(filename, "rb") as f: |
| | data = f.read() |
| | response = requests.request("POST", API_URL, headers=headers, data=data) |
| | output = json.loads(response.content.decode("utf-8")) |
| | return list_to_dict(output),json.dumps(output, indent=2, sort_keys=True) |
| |
|
| | def get_nutrition_info(food_name): |
| | |
| | response = requests.get( |
| | "https://trackapi.nutritionix.com/v2/search/instant", |
| | params={"query": food_name}, |
| | headers={ |
| | "x-app-id": "63a710ef", |
| | "x-app-key": "3ddc7e3feda88e1cf6dd355fb26cb261" |
| | } |
| | ) |
| | |
| | data = response.json() |
| | response = data["branded"][0]["photo"]["thumb"] |
| |
|
| | |
| |
|
| | return { |
| | "food_name": data["branded"][0]["food_name"], |
| | "calories": data["branded"][0]["nf_calories"], |
| | "serving_size": data["branded"][0]["serving_qty"], |
| | "serving_unit": data["branded"][0]["serving_unit"], |
| | |
| | },response |
| |
|
| |
|
| | with gr.Blocks() as demo: |
| | gr.Markdown("Food-Classification-Calorie-Estimation and Volume-Estimation") |
| | with gr.Tab("Food Classification"): |
| | text_input = gr.Image(type="filepath") |
| | text_output = [gr.Label(num_top_classes=6), |
| | gr.Textbox() |
| | ] |
| | text_button = gr.Button("Food Classification") |
| | with gr.Tab("Food Calorie Estimation"): |
| | image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie") |
| | image_output = [gr.Textbox(), |
| | gr.Image(type="filepath") |
| | ] |
| | image_button = gr.Button("Estimate Calories!") |
| | with gr.Tab("Volume Estimation"): |
| | _image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie") |
| | _image_output = [gr.Textbox(), |
| | gr.Image() |
| | ] |
| | _image_button = gr.Button("Volume Calculation") |
| | with gr.Tab("Future Works"): |
| | gr.Markdown("Future work on Food Classification") |
| | gr.Markdown( |
| | "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") |
| | gr.Markdown("Future work on Volume Estimation") |
| | gr.Markdown( |
| | "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") |
| | gr.Markdown("Future work on Calorie Estimation") |
| | gr.Markdown( |
| | "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") |
| | gr.Markdown("https://github.com/Ali-Maq/Food-Classification-Volume-Estimation-and-Calorie-Estimation/blob/main/README.md") |
| |
|
| | text_button.click(query, inputs=text_input, outputs=text_output) |
| | image_button.click(get_nutrition_info, inputs=image_input, outputs=image_output) |
| | _image_button.click(get_nutrition_info, inputs=_image_input, outputs=_image_output) |
| | with gr.Accordion("Open for More!"): |
| | gr.Markdown("π Designed and built by Ali Under the Guidance of Professor Dennis Shasha") |
| | gr.Markdown("Contact me at ali.quidwai@nyu.edu π") |
| |
|
| | demo.launch(share=True, debug=True) |
| |
|