Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,59 @@
|
|
| 1 |
import torch
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
model_path = "best.pt"
|
| 9 |
-
|
| 10 |
-
# رابط التحميل من الريبو بتاعك على Hugging Face
|
| 11 |
model_url = "https://huggingface.co/AlaaElsayed/yolospace/resolve/main/best.pt"
|
| 12 |
-
|
| 13 |
-
# حمل الموديل لو مش موجود
|
| 14 |
if not os.path.exists(model_path):
|
| 15 |
r = requests.get(model_url)
|
| 16 |
with open(model_path, "wb") as f:
|
| 17 |
f.write(r.content)
|
| 18 |
|
| 19 |
-
# تحميل
|
| 20 |
-
model = torch.hub.load('ultralytics/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# دالة الكشف
|
| 23 |
def detect(image):
|
| 24 |
results = model(image)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
demo = gr.Interface(
|
| 29 |
fn=detect,
|
| 30 |
inputs=gr.Image(type="pil"),
|
| 31 |
outputs=gr.Image(type="pil"),
|
| 32 |
-
title="
|
| 33 |
-
description="Upload
|
| 34 |
)
|
| 35 |
|
| 36 |
demo.launch()
|
|
|
|
| 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.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()
|