ma4389 commited on
Commit
435df87
·
verified ·
1 Parent(s): 3b21053

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -1,22 +1,37 @@
1
- import gradio as gr
2
- from ultralytics import YOLO
3
- from PIL import Image
4
- import numpy as np
5
-
6
- # Load model
7
- model = YOLO("best.pt")
8
-
9
- def detect(image):
10
- results = model(image)
11
- annotated_image = results[0].plot()
12
- return Image.fromarray(annotated_image)
13
-
14
- interface = gr.Interface(
15
- fn=detect,
16
- inputs=gr.Image(type="pil"),
17
- outputs=gr.Image(type="pil"),
18
- title="Food & Object Detection",
19
- description="Upload an image to detect objects."
20
- )
21
-
22
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()