NandanData commited on
Commit
4dc0f57
·
verified ·
1 Parent(s): 06bf58f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -1,20 +1,28 @@
1
  import gradio as gr
 
 
2
  from ultralytics import YOLO
3
  import numpy as np
4
  import cv2
5
  from PIL import Image
 
6
 
7
- # Load your fire+smoke model (best.pt)
8
- model = YOLO("best.pt") # Make sure file exists in Space
 
 
 
 
 
 
 
9
 
10
  def detect_fire_smoke(image):
11
  if image is None:
12
  return "Please upload an image"
13
 
14
- # Convert PIL → CV2
15
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
16
 
17
- # Prediction
18
  results = model(img)[0]
19
 
20
  if len(results.boxes) == 0:
@@ -23,7 +31,7 @@ def detect_fire_smoke(image):
23
  output = []
24
 
25
  for box in results.boxes:
26
- cls = int(box.cls[0]) # 0=fire, 1=smoke (as per model card)
27
  conf = float(box.conf[0])
28
 
29
  if cls == 0:
@@ -41,7 +49,7 @@ demo = gr.Interface(
41
  inputs=gr.Image(type="pil"),
42
  outputs="text",
43
  title="Fire & Smoke Detection",
44
- description="Upload an image to detect fire or smoke using YOLOv10 fire model."
45
  )
46
 
47
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from torch.serialization import add_safe_globals
4
  from ultralytics import YOLO
5
  import numpy as np
6
  import cv2
7
  from PIL import Image
8
+ import ultralytics.nn.tasks as yolov8_tasks
9
 
10
+ # ---------------------------
11
+ # Allow YOLO model class for PyTorch 2.6+
12
+ # ---------------------------
13
+ add_safe_globals([yolov8_tasks.DetectionModel])
14
+
15
+ # ---------------------------
16
+ # Load model safely
17
+ # ---------------------------
18
+ model = YOLO("best.pt") # Works after safe global patch
19
 
20
  def detect_fire_smoke(image):
21
  if image is None:
22
  return "Please upload an image"
23
 
 
24
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
25
 
 
26
  results = model(img)[0]
27
 
28
  if len(results.boxes) == 0:
 
31
  output = []
32
 
33
  for box in results.boxes:
34
+ cls = int(box.cls[0])
35
  conf = float(box.conf[0])
36
 
37
  if cls == 0:
 
49
  inputs=gr.Image(type="pil"),
50
  outputs="text",
51
  title="Fire & Smoke Detection",
52
+ description="Upload an image to detect fire or smoke using YOLOv10 model."
53
  )
54
 
55
  demo.launch()