NandanData commited on
Commit
74e16c9
Β·
verified Β·
1 Parent(s): d031018

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -2,44 +2,46 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  import numpy as np
4
  import cv2
 
5
 
6
- # Load the fire+smoke model
7
- model = YOLO("best.pt")
8
 
9
  def detect_fire_smoke(image):
10
  if image is None:
11
  return "Please upload an image"
12
 
 
13
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
14
 
 
15
  results = model(img)[0]
16
 
17
- # No detection
18
  if len(results.boxes) == 0:
19
- return "βœ” SAFE β€” NO FIRE/SMOKE DETECTED"
20
 
21
- final = []
22
 
23
  for box in results.boxes:
24
- cls_id = int(box.cls[0])
25
  conf = float(box.conf[0])
26
 
27
- if cls_id == 0:
28
- final.append(f"πŸ”₯ FIRE DETECTED (confidence {conf:.2f})")
29
- elif cls_id == 1:
30
- final.append(f"πŸ’¨ SMOKE DETECTED (confidence {conf:.2f})")
31
 
32
- if not final:
33
- return "βœ” SAFE β€” NO FIRE/SMOKE DETECTED"
34
 
35
- return "\n".join(final)
36
 
37
  demo = gr.Interface(
38
  fn=detect_fire_smoke,
39
  inputs=gr.Image(type="pil"),
40
  outputs="text",
41
- title="Fire & Smoke Detection (DEMO)",
42
- description="Upload an image to test detection using YOLOv10 Fire & Smoke Model."
43
  )
44
 
45
  demo.launch()
 
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:
21
+ return "βœ” SAFE β€” No Fire/Smoke Detected"
22
 
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:
30
+ output.append(f"πŸ”₯ FIRE DETECTED β€” Confidence: {conf:.2f}")
31
+ elif cls == 1:
32
+ output.append(f"πŸ’¨ SMOKE DETECTED β€” Confidence: {conf:.2f}")
33
 
34
+ if not output:
35
+ return "βœ” SAFE β€” No Fire/Smoke Detected"
36
 
37
+ return "\n".join(output)
38
 
39
  demo = gr.Interface(
40
  fn=detect_fire_smoke,
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()