Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,103 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import requests
|
| 3 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
-
from PIL import Image, ImageDraw
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
# روابط وتحميل الموديل
|
| 9 |
-
model_path = "best.pt"
|
| 10 |
-
model_url = "https://huggingface.co/AlaaElsayed/yolospace/resolve/main/best.pt"
|
| 11 |
-
if not os.path.exists(model_path):
|
| 12 |
-
r = requests.get(model_url)
|
| 13 |
-
with open(model_path, "wb") as f:
|
| 14 |
-
f.write(r.content)
|
| 15 |
-
|
| 16 |
# تحميل الموديل
|
| 17 |
-
model =
|
| 18 |
|
| 19 |
-
# تحميل
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def get_nutrition(label):
|
| 24 |
-
row =
|
| 25 |
if row.empty:
|
| 26 |
-
return "No
|
| 27 |
cals = row["Calories_per_100g"].values[0]
|
| 28 |
fat = row["Fat_g"].values[0]
|
| 29 |
protein = row["Protein_g"].values[0]
|
| 30 |
carbs = row["Carbs_g"].values[0]
|
| 31 |
-
return f"{cals} kcal
|
| 32 |
|
| 33 |
-
# دالة الكشف
|
| 34 |
def detect(image):
|
| 35 |
-
results = model(image)
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
img = Image.fromarray(
|
| 40 |
draw = ImageDraw.Draw(img)
|
| 41 |
|
| 42 |
-
for
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
return img
|
| 49 |
|
|
@@ -52,7 +106,7 @@ demo = gr.Interface(
|
|
| 52 |
fn=detect,
|
| 53 |
inputs=gr.Image(type="pil"),
|
| 54 |
outputs=gr.Image(type="pil"),
|
| 55 |
-
title="
|
| 56 |
description="Upload an image of food and see calories and nutrients!"
|
| 57 |
)
|
| 58 |
|
|
|
|
| 1 |
+
# import torch
|
| 2 |
+
# import requests
|
| 3 |
+
# import os
|
| 4 |
+
# import pandas as pd
|
| 5 |
+
# from PIL import Image, ImageDraw, ImageFont
|
| 6 |
+
# import gradio as gr
|
| 7 |
+
|
| 8 |
+
# # روابط وتحميل الموديل
|
| 9 |
+
# model_path = "best.pt"
|
| 10 |
+
# model_url = "https://huggingface.co/AlaaElsayed/yolospace/resolve/main/best.pt"
|
| 11 |
+
# if not os.path.exists(model_path):
|
| 12 |
+
# r = requests.get(model_url)
|
| 13 |
+
# with open(model_path, "wb") as f:
|
| 14 |
+
# f.write(r.content)
|
| 15 |
+
|
| 16 |
+
# # تحميل الموديل
|
| 17 |
+
# model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, source='local')
|
| 18 |
+
|
| 19 |
+
# # تحميل معلومات الطعام
|
| 20 |
+
# food_info = pd.read_csv("food_cleaned.csv")
|
| 21 |
+
|
| 22 |
+
# # دالة لإحضار البيانات الغذائية
|
| 23 |
+
# def get_nutrition(label):
|
| 24 |
+
# row = food_info[food_info["Food_Name"].str.lower() == label.lower()]
|
| 25 |
+
# if row.empty:
|
| 26 |
+
# return "No info", "?", "?", "?"
|
| 27 |
+
# cals = row["Calories_per_100g"].values[0]
|
| 28 |
+
# fat = row["Fat_g"].values[0]
|
| 29 |
+
# protein = row["Protein_g"].values[0]
|
| 30 |
+
# carbs = row["Carbs_g"].values[0]
|
| 31 |
+
# return f"{cals} kcal", f"{fat}g fat", f"{protein}g protein", f"{carbs}g carbs"
|
| 32 |
+
|
| 33 |
+
# # دالة الكشف
|
| 34 |
+
# def detect(image):
|
| 35 |
+
# results = model(image)
|
| 36 |
+
# labels = results.names
|
| 37 |
+
# df = results.pandas().xyxy[0]
|
| 38 |
+
|
| 39 |
+
# img = Image.fromarray(results.render()[0])
|
| 40 |
+
# draw = ImageDraw.Draw(img)
|
| 41 |
+
|
| 42 |
+
# for _, row in df.iterrows():
|
| 43 |
+
# label = row["name"]
|
| 44 |
+
# cal, fat, pro, carb = get_nutrition(label)
|
| 45 |
+
# text = f"{label}: {cal}, {fat}, {pro}, {carb}"
|
| 46 |
+
# draw.text((row["xmin"], row["ymin"] - 10), text, fill=(255, 0, 0))
|
| 47 |
+
|
| 48 |
+
# return img
|
| 49 |
+
|
| 50 |
+
# # Gradio app
|
| 51 |
+
# demo = gr.Interface(
|
| 52 |
+
# fn=detect,
|
| 53 |
+
# inputs=gr.Image(type="pil"),
|
| 54 |
+
# outputs=gr.Image(type="pil"),
|
| 55 |
+
# title="YOLOv5 Food Detector + Nutrition Info",
|
| 56 |
+
# description="Upload an image of food and see calories and nutrients!"
|
| 57 |
+
# )
|
| 58 |
+
|
| 59 |
+
# demo.launch()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
from ultralytics import YOLO
|
| 64 |
import pandas as pd
|
| 65 |
+
from PIL import Image, ImageDraw
|
| 66 |
import gradio as gr
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# تحميل الموديل
|
| 69 |
+
model = YOLO("best.pt")
|
| 70 |
|
| 71 |
+
# تحميل بيانات التغذية
|
| 72 |
+
food_df = pd.read_csv("food_cleaned.csv")
|
| 73 |
|
| 74 |
+
# جلب القيم الغذائية
|
| 75 |
def get_nutrition(label):
|
| 76 |
+
row = food_df[food_df["Food_Name"].str.lower() == label.lower()]
|
| 77 |
if row.empty:
|
| 78 |
+
return "No data"
|
| 79 |
cals = row["Calories_per_100g"].values[0]
|
| 80 |
fat = row["Fat_g"].values[0]
|
| 81 |
protein = row["Protein_g"].values[0]
|
| 82 |
carbs = row["Carbs_g"].values[0]
|
| 83 |
+
return f"{label}: {cals} kcal, {fat}g fat, {protein}g protein, {carbs}g carbs"
|
| 84 |
|
| 85 |
+
# دالة الكشف والرسم
|
| 86 |
def detect(image):
|
| 87 |
+
results = model.predict(image)
|
| 88 |
+
result = results[0]
|
| 89 |
+
boxes = result.boxes
|
| 90 |
+
names = model.names
|
| 91 |
|
| 92 |
+
img = Image.fromarray(result.plot()) # الصورة عليها البوكسات
|
| 93 |
draw = ImageDraw.Draw(img)
|
| 94 |
|
| 95 |
+
for box in boxes:
|
| 96 |
+
cls_id = int(box.cls[0])
|
| 97 |
+
label = names[cls_id]
|
| 98 |
+
nutrition = get_nutrition(label)
|
| 99 |
+
xy = box.xyxy[0].tolist()
|
| 100 |
+
draw.text((xy[0], xy[1] - 10), nutrition, fill=(255, 0, 0))
|
| 101 |
|
| 102 |
return img
|
| 103 |
|
|
|
|
| 106 |
fn=detect,
|
| 107 |
inputs=gr.Image(type="pil"),
|
| 108 |
outputs=gr.Image(type="pil"),
|
| 109 |
+
title="YOLOv8 Food Detector + Nutrition Info",
|
| 110 |
description="Upload an image of food and see calories and nutrients!"
|
| 111 |
)
|
| 112 |
|