Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,37 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
from ultralytics.nn.tasks import DetectionModel
|
| 6 |
+
from ultralytics.nn.modules.conv import Conv
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# ---- FIX for PyTorch 2.6+ ----
|
| 10 |
+
torch.serialization.add_safe_globals([DetectionModel, nn.Sequential, Conv])
|
| 11 |
+
|
| 12 |
+
# ---- Load trained YOLO model ----
|
| 13 |
+
model = YOLO("best.pt") # your junk food model
|
| 14 |
+
model.to("cpu") # Required for Hugging Face Spaces
|
| 15 |
+
|
| 16 |
+
# ---- Prediction function ----
|
| 17 |
+
def predict(image):
|
| 18 |
+
results = model.predict(image, conf=0.25)
|
| 19 |
+
annotated = results[0].plot() # BGR numpy image
|
| 20 |
+
|
| 21 |
+
# Convert BGR → RGB
|
| 22 |
+
annotated = annotated[:, :, ::-1]
|
| 23 |
+
|
| 24 |
+
return Image.fromarray(annotated)
|
| 25 |
+
|
| 26 |
+
# ---- Gradio Interface ----
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=predict,
|
| 29 |
+
inputs=gr.Image(type="pil", label="Upload Food Image"),
|
| 30 |
+
outputs=gr.Image(type="pil", label="Detection Result"),
|
| 31 |
+
title="🍔 Junk Food Detection (YOLO)",
|
| 32 |
+
description="Upload an image to detect junk food items like Pizza, Burger, Ice Cream, Fries, etc.",
|
| 33 |
+
allow_flagging="never"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|