Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from ultralytics import YOLO
|
| 2 |
import pandas as pd
|
| 3 |
-
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# تحميل الموديل
|
| 7 |
model = YOLO("best.pt")
|
| 8 |
|
| 9 |
-
# تحميل بيانات التغذية
|
| 10 |
food_df = pd.read_csv("food_cleaned.csv")
|
| 11 |
|
| 12 |
# جلب القيم الغذائية
|
| 13 |
def get_nutrition(label):
|
| 14 |
row = food_df[food_df["Food_Name"].str.lower() == label.lower()]
|
| 15 |
if row.empty:
|
| 16 |
-
return "No data"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
def detect(image):
|
| 25 |
results = model.predict(image)
|
| 26 |
result = results[0]
|
| 27 |
boxes = result.boxes
|
| 28 |
names = model.names
|
| 29 |
|
| 30 |
-
|
| 31 |
-
draw = ImageDraw.Draw(img)
|
| 32 |
|
| 33 |
for box in boxes:
|
| 34 |
cls_id = int(box.cls[0])
|
| 35 |
label = names[cls_id]
|
| 36 |
nutrition = get_nutrition(label)
|
| 37 |
-
|
| 38 |
-
draw.text((xy[0], xy[1] - 10), nutrition, fill=(255, 0, 0))
|
| 39 |
|
| 40 |
-
return
|
| 41 |
|
| 42 |
# Gradio app
|
| 43 |
demo = gr.Interface(
|
| 44 |
fn=detect,
|
| 45 |
inputs=gr.Image(type="pil"),
|
| 46 |
-
outputs=gr.Image(type="pil"),
|
| 47 |
title="YOLOv8 Food Detector + Nutrition Info",
|
| 48 |
-
description="Upload an image of food and
|
| 49 |
)
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 1 |
+
# from ultralytics import YOLO
|
| 2 |
+
# import pandas as pd
|
| 3 |
+
# from PIL import Image, ImageDraw
|
| 4 |
+
# import gradio as gr
|
| 5 |
+
|
| 6 |
+
# # تحميل الموديل
|
| 7 |
+
# model = YOLO("best.pt")
|
| 8 |
+
|
| 9 |
+
# # تحميل بيانات التغذية
|
| 10 |
+
# food_df = pd.read_csv("food_cleaned.csv")
|
| 11 |
+
|
| 12 |
+
# # جلب القيم الغذائية
|
| 13 |
+
# def get_nutrition(label):
|
| 14 |
+
# row = food_df[food_df["Food_Name"].str.lower() == label.lower()]
|
| 15 |
+
# if row.empty:
|
| 16 |
+
# return "No data"
|
| 17 |
+
# cals = row["Calories_per_100g"].values[0]
|
| 18 |
+
# fat = row["Fat_g"].values[0]
|
| 19 |
+
# protein = row["Protein_g"].values[0]
|
| 20 |
+
# carbs = row["Carbs_g"].values[0]
|
| 21 |
+
# return f"{label}: {cals} kcal, {fat}g fat, {protein}g protein, {carbs}g carbs"
|
| 22 |
+
|
| 23 |
+
# # دالة الكشف والرسم
|
| 24 |
+
# def detect(image):
|
| 25 |
+
# results = model.predict(image)
|
| 26 |
+
# result = results[0]
|
| 27 |
+
# boxes = result.boxes
|
| 28 |
+
# names = model.names
|
| 29 |
+
|
| 30 |
+
# img = Image.fromarray(result.plot()) # الصورة عليها البوكسات
|
| 31 |
+
# draw = ImageDraw.Draw(img)
|
| 32 |
+
|
| 33 |
+
# for box in boxes:
|
| 34 |
+
# cls_id = int(box.cls[0])
|
| 35 |
+
# label = names[cls_id]
|
| 36 |
+
# nutrition = get_nutrition(label)
|
| 37 |
+
# xy = box.xyxy[0].tolist()
|
| 38 |
+
# draw.text((xy[0], xy[1] - 10), nutrition, fill=(255, 0, 0))
|
| 39 |
+
|
| 40 |
+
# return img
|
| 41 |
+
|
| 42 |
+
# # Gradio app
|
| 43 |
+
# demo = gr.Interface(
|
| 44 |
+
# fn=detect,
|
| 45 |
+
# inputs=gr.Image(type="pil"),
|
| 46 |
+
# outputs=gr.Image(type="pil"),
|
| 47 |
+
# title="YOLOv8 Food Detector + Nutrition Info",
|
| 48 |
+
# description="Upload an image of food and see calories and nutrients!"
|
| 49 |
+
# )
|
| 50 |
+
|
| 51 |
+
# demo.launch()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
from ultralytics import YOLO
|
| 55 |
import pandas as pd
|
| 56 |
+
from PIL import Image
|
| 57 |
import gradio as gr
|
| 58 |
|
| 59 |
# تحميل الموديل
|
| 60 |
model = YOLO("best.pt")
|
| 61 |
|
| 62 |
+
# تحميل بيانات التغذية
|
| 63 |
food_df = pd.read_csv("food_cleaned.csv")
|
| 64 |
|
| 65 |
# جلب القيم الغذائية
|
| 66 |
def get_nutrition(label):
|
| 67 |
row = food_df[food_df["Food_Name"].str.lower() == label.lower()]
|
| 68 |
if row.empty:
|
| 69 |
+
return {"label": label, "info": "No data"}
|
| 70 |
+
return {
|
| 71 |
+
"label": label,
|
| 72 |
+
"calories": float(row["Calories_per_100g"].values[0]),
|
| 73 |
+
"fat": float(row["Fat_g"].values[0]),
|
| 74 |
+
"protein": float(row["Protein_g"].values[0]),
|
| 75 |
+
"carbs": float(row["Carbs_g"].values[0])
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
# دالة الكشف
|
| 79 |
def detect(image):
|
| 80 |
results = model.predict(image)
|
| 81 |
result = results[0]
|
| 82 |
boxes = result.boxes
|
| 83 |
names = model.names
|
| 84 |
|
| 85 |
+
detected_info = []
|
|
|
|
| 86 |
|
| 87 |
for box in boxes:
|
| 88 |
cls_id = int(box.cls[0])
|
| 89 |
label = names[cls_id]
|
| 90 |
nutrition = get_nutrition(label)
|
| 91 |
+
detected_info.append(nutrition)
|
|
|
|
| 92 |
|
| 93 |
+
return Image.fromarray(result.plot()), detected_info
|
| 94 |
|
| 95 |
# Gradio app
|
| 96 |
demo = gr.Interface(
|
| 97 |
fn=detect,
|
| 98 |
inputs=gr.Image(type="pil"),
|
| 99 |
+
outputs=[gr.Image(type="pil", label="Detected Image"), gr.JSON(label="Nutrition Info")],
|
| 100 |
title="YOLOv8 Food Detector + Nutrition Info",
|
| 101 |
+
description="Upload an image of food and get calories and nutrients without overlaying on the image."
|
| 102 |
)
|
| 103 |
|
| 104 |
demo.launch()
|