Upload 8 files
Browse files- .env +0 -0
- 01.jpeg +0 -0
- 02.jpeg +0 -0
- 03.jpeg +0 -0
- app.py +103 -0
- food_identification.ipynb +69 -0
- predict_food.ipynb +88 -0
- requirements.txt +6 -0
.env
ADDED
File without changes
|
01.jpeg
ADDED
![]() |
02.jpeg
ADDED
![]() |
03.jpeg
ADDED
![]() |
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
3 |
+
import warnings
|
4 |
+
import requests
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
warnings.filterwarnings('ignore')
|
8 |
+
|
9 |
+
# Load the pre-trained Vision Transformer model and feature extractor
|
10 |
+
model_name = "google/vit-base-patch16-224"
|
11 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
12 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
13 |
+
|
14 |
+
# API key for the nutrition information
|
15 |
+
api_key = 'Place API key here'
|
16 |
+
|
17 |
+
def identify_image(image_path):
|
18 |
+
"""Identify the food item in the image."""
|
19 |
+
image = Image.open(image_path)
|
20 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
21 |
+
outputs = model(**inputs)
|
22 |
+
logits = outputs.logits
|
23 |
+
predicted_class_idx = logits.argmax(-1).item()
|
24 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
25 |
+
food_name = predicted_label.split(',')[0]
|
26 |
+
return food_name
|
27 |
+
|
28 |
+
def get_calories(food_name):
|
29 |
+
"""Get the calorie information of the identified food item."""
|
30 |
+
api_url = 'https://api.api-ninjas.com/v1/nutrition?query={}'.format(food_name)
|
31 |
+
response = requests.get(api_url, headers={'X-Api-Key': api_key})
|
32 |
+
if response.status_code == requests.codes.ok:
|
33 |
+
nutrition_info = response.json()
|
34 |
+
else:
|
35 |
+
nutrition_info = {"Error": response.status_code, "Message": response.text}
|
36 |
+
return nutrition_info
|
37 |
+
|
38 |
+
def format_nutrition_info(nutrition_info):
|
39 |
+
"""Format the nutritional information into an HTML table."""
|
40 |
+
if "Error" in nutrition_info:
|
41 |
+
return f"Error: {nutrition_info['Error']} - {nutrition_info['Message']}"
|
42 |
+
|
43 |
+
if len(nutrition_info) == 0:
|
44 |
+
return "No nutritional information found."
|
45 |
+
|
46 |
+
nutrition_data = nutrition_info[0]
|
47 |
+
table = f"""
|
48 |
+
<table border="1" style="width: 100%; border-collapse: collapse;">
|
49 |
+
<tr><th colspan="4" style="text-align: center;"><b>Nutrition Facts</b></th></tr>
|
50 |
+
<tr><td colspan="4" style="text-align: center;"><b>Food Name: {nutrition_data['name']}</b></td></tr>
|
51 |
+
<tr>
|
52 |
+
<td style="text-align: left;"><b>Calories</b></td><td style="text-align: right;">{nutrition_data['calories']}</td>
|
53 |
+
<td style="text-align: left;"><b>Serving Size (g)</b></td><td style="text-align: right;">{nutrition_data['serving_size_g']}</td>
|
54 |
+
</tr>
|
55 |
+
<tr>
|
56 |
+
<td style="text-align: left;"><b>Total Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_total_g']}</td>
|
57 |
+
<td style="text-align: left;"><b>Saturated Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_saturated_g']}</td>
|
58 |
+
</tr>
|
59 |
+
<tr>
|
60 |
+
<td style="text-align: left;"><b>Protein (g)</b></td><td style="text-align: right;">{nutrition_data['protein_g']}</td>
|
61 |
+
<td style="text-align: left;"><b>Sodium (mg)</b></td><td style="text-align: right;">{nutrition_data['sodium_mg']}</td>
|
62 |
+
</tr>
|
63 |
+
<tr>
|
64 |
+
<td style="text-align: left;"><b>Potassium (mg)</b></td><td style="text-align: right;">{nutrition_data['potassium_mg']}</td>
|
65 |
+
<td style="text-align: left;"><b>Cholesterol (mg)</b></td><td style="text-align: right;">{nutrition_data['cholesterol_mg']}</td>
|
66 |
+
</tr>
|
67 |
+
<tr>
|
68 |
+
<td style="text-align: left;"><b>Total Carbohydrates (g)</b></td><td style="text-align: right;">{nutrition_data['carbohydrates_total_g']}</td>
|
69 |
+
<td style="text-align: left;"><b>Fiber (g)</b></td><td style="text-align: right;">{nutrition_data['fiber_g']}</td>
|
70 |
+
</tr>
|
71 |
+
<tr>
|
72 |
+
<td style="text-align: left;"><b>Sugar (g)</b></td><td style="text-align: right;">{nutrition_data['sugar_g']}</td>
|
73 |
+
<td></td><td></td>
|
74 |
+
</tr>
|
75 |
+
</table>
|
76 |
+
"""
|
77 |
+
return table
|
78 |
+
|
79 |
+
def main_process(image_path):
|
80 |
+
"""Identify the food item and fetch its calorie information."""
|
81 |
+
food_name = identify_image(image_path)
|
82 |
+
nutrition_info = get_calories(food_name)
|
83 |
+
formatted_nutrition_info = format_nutrition_info(nutrition_info)
|
84 |
+
return formatted_nutrition_info
|
85 |
+
|
86 |
+
# Define the Gradio interface
|
87 |
+
def gradio_interface(image):
|
88 |
+
formatted_nutrition_info = main_process(image)
|
89 |
+
return formatted_nutrition_info
|
90 |
+
|
91 |
+
# Create the Gradio UI
|
92 |
+
iface = gr.Interface(
|
93 |
+
fn=gradio_interface,
|
94 |
+
inputs=gr.Image(type="filepath"),
|
95 |
+
outputs="html",
|
96 |
+
title="Food Identification and Nutrition Info",
|
97 |
+
description="Upload an image of food to get nutritional information.",
|
98 |
+
allow_flagging="never" # Disable flagging
|
99 |
+
)
|
100 |
+
|
101 |
+
# Launch the Gradio app
|
102 |
+
if __name__ == "__main__":
|
103 |
+
iface.launch()
|
food_identification.ipynb
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Identified food: Granny Smith\n"
|
13 |
+
]
|
14 |
+
}
|
15 |
+
],
|
16 |
+
"source": [
|
17 |
+
"import torch\n",
|
18 |
+
"from PIL import Image\n",
|
19 |
+
"from transformers import ViTFeatureExtractor, ViTForImageClassification\n",
|
20 |
+
"import warnings\n",
|
21 |
+
"\n",
|
22 |
+
"warnings.filterwarnings('ignore')\n",
|
23 |
+
"\n",
|
24 |
+
"model_name = \"google/vit-base-patch16-224\"\n",
|
25 |
+
"feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)\n",
|
26 |
+
"model = ViTForImageClassification.from_pretrained(model_name)\n",
|
27 |
+
"\n",
|
28 |
+
"def identify_food(image_path):\n",
|
29 |
+
" \"\"\"Identify the food item in the image.\"\"\"\n",
|
30 |
+
" image = Image.open(image_path)\n",
|
31 |
+
" inputs = feature_extractor(images=image, return_tensors=\"pt\")\n",
|
32 |
+
" outputs = model(**inputs)\n",
|
33 |
+
" logits = outputs.logits\n",
|
34 |
+
" predicted_class_idx = logits.argmax(-1).item()\n",
|
35 |
+
" predicted_label = model.config.id2label[predicted_class_idx]\n",
|
36 |
+
" food_name = predicted_label.split(',')[0]\n",
|
37 |
+
" return food_name\n"
|
38 |
+
]
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "code",
|
42 |
+
"execution_count": null,
|
43 |
+
"metadata": {},
|
44 |
+
"outputs": [],
|
45 |
+
"source": []
|
46 |
+
}
|
47 |
+
],
|
48 |
+
"metadata": {
|
49 |
+
"kernelspec": {
|
50 |
+
"display_name": "myenv",
|
51 |
+
"language": "python",
|
52 |
+
"name": "python3"
|
53 |
+
},
|
54 |
+
"language_info": {
|
55 |
+
"codemirror_mode": {
|
56 |
+
"name": "ipython",
|
57 |
+
"version": 3
|
58 |
+
},
|
59 |
+
"file_extension": ".py",
|
60 |
+
"mimetype": "text/x-python",
|
61 |
+
"name": "python",
|
62 |
+
"nbconvert_exporter": "python",
|
63 |
+
"pygments_lexer": "ipython3",
|
64 |
+
"version": "3.12.4"
|
65 |
+
}
|
66 |
+
},
|
67 |
+
"nbformat": 4,
|
68 |
+
"nbformat_minor": 2
|
69 |
+
}
|
predict_food.ipynb
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"ename": "TypeError",
|
10 |
+
"evalue": "'>' not supported between instances of 'str' and 'int'",
|
11 |
+
"output_type": "error",
|
12 |
+
"traceback": [
|
13 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
14 |
+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
15 |
+
"Cell \u001b[1;32mIn[5], line 56\u001b[0m\n\u001b[0;32m 54\u001b[0m test_food \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGranny Smith apple\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 55\u001b[0m nutrition_info \u001b[38;5;241m=\u001b[39m get_nutrition_info(test_food)\n\u001b[1;32m---> 56\u001b[0m analysis \u001b[38;5;241m=\u001b[39m \u001b[43manalyze_nutrition\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnutrition_info\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 57\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAnalysis for \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtest_food\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 58\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mNutritional Data:\u001b[39m\u001b[38;5;124m\"\u001b[39m, analysis[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnutrition_data\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
|
16 |
+
"Cell \u001b[1;32mIn[5], line 34\u001b[0m, in \u001b[0;36manalyze_nutrition\u001b[1;34m(nutrition_info)\u001b[0m\n\u001b[0;32m 31\u001b[0m suggestions \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 33\u001b[0m \u001b[38;5;66;03m# Analyze nutritional values and provide suggestions\u001b[39;00m\n\u001b[1;32m---> 34\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mcalories\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m300\u001b[39;49m:\n\u001b[0;32m 35\u001b[0m suggestions\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHigh in calories - Consider portion control\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 36\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m fat_total_g \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m10\u001b[39m:\n",
|
17 |
+
"\u001b[1;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'"
|
18 |
+
]
|
19 |
+
}
|
20 |
+
],
|
21 |
+
"source": [
|
22 |
+
"import requests\n",
|
23 |
+
"\n",
|
24 |
+
"api_key = 'jVjdskzcX1Fv3o5OFJPxbw==EHXvm2HiyPYoOm2p'\n",
|
25 |
+
"\n",
|
26 |
+
"def get_nutrition_info(food_name):\n",
|
27 |
+
" \"\"\"Get the nutritional information for the identified food item.\"\"\"\n",
|
28 |
+
" api_url = f'https://api.api-ninjas.com/v1/nutrition?query={food_name}'\n",
|
29 |
+
" response = requests.get(api_url, headers={'X-Api-Key': api_key})\n",
|
30 |
+
" if response.status_code == requests.codes.ok:\n",
|
31 |
+
" return response.json()\n",
|
32 |
+
" else:\n",
|
33 |
+
" return {\"Error\": response.status_code, \"Message\": response.text}\n",
|
34 |
+
"\n",
|
35 |
+
"def analyze_nutrition(nutrition_info):\n",
|
36 |
+
" \"\"\"Analyze the nutritional information and provide suggestions.\"\"\"\n",
|
37 |
+
" if \"Error\" in nutrition_info:\n",
|
38 |
+
" return f\"Error: {nutrition_info['Error']} - {nutrition_info['Message']}\"\n",
|
39 |
+
"\n",
|
40 |
+
" if len(nutrition_info) == 0:\n",
|
41 |
+
" return \"No nutritional information found for the given food item.\"\n",
|
42 |
+
"\n",
|
43 |
+
" nutrition_data = nutrition_info[0]\n",
|
44 |
+
" suggestions = []\n",
|
45 |
+
"\n",
|
46 |
+
" if nutrition_data['calories'] > 500:\n",
|
47 |
+
" suggestions.append(\"This food is high in calories. Consider portion control.\")\n",
|
48 |
+
" if nutrition_data['fat_total_g'] > 20:\n",
|
49 |
+
" suggestions.append(\"High in fat. Consider low-fat alternatives.\")\n",
|
50 |
+
" if nutrition_data['sugar_g'] > 20:\n",
|
51 |
+
" suggestions.append(\"High in sugar. Consider limiting sugar intake.\")\n",
|
52 |
+
"\n",
|
53 |
+
" return {\n",
|
54 |
+
" \"nutrition_data\": nutrition_data,\n",
|
55 |
+
" \"suggestions\": suggestions\n",
|
56 |
+
" }\n"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": null,
|
62 |
+
"metadata": {},
|
63 |
+
"outputs": [],
|
64 |
+
"source": []
|
65 |
+
}
|
66 |
+
],
|
67 |
+
"metadata": {
|
68 |
+
"kernelspec": {
|
69 |
+
"display_name": "myenv",
|
70 |
+
"language": "python",
|
71 |
+
"name": "python3"
|
72 |
+
},
|
73 |
+
"language_info": {
|
74 |
+
"codemirror_mode": {
|
75 |
+
"name": "ipython",
|
76 |
+
"version": 3
|
77 |
+
},
|
78 |
+
"file_extension": ".py",
|
79 |
+
"mimetype": "text/x-python",
|
80 |
+
"name": "python",
|
81 |
+
"nbconvert_exporter": "python",
|
82 |
+
"pygments_lexer": "ipython3",
|
83 |
+
"version": "3.12.4"
|
84 |
+
}
|
85 |
+
},
|
86 |
+
"nbformat": 4,
|
87 |
+
"nbformat_minor": 2
|
88 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=3.39
|
2 |
+
transformers>=4.34.0
|
3 |
+
torch>=2.1.0
|
4 |
+
torchvision>=0.16.0
|
5 |
+
requests>=2.31.0
|
6 |
+
python-dotenv>=1.0.0
|