NandanData commited on
Commit
178cdf0
Β·
verified Β·
1 Parent(s): 1307b53

Update app.py

Browse files

Show Bounding Boxes

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