Spaces:
Sleeping
Sleeping
Update app.py
Browse filesShow Bounding Boxes
app.py
CHANGED
|
@@ -3,41 +3,40 @@ from ultralytics import YOLO
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
|
| 6 |
-
|
| 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 |
-
|
| 17 |
-
return "β SAFE β No Fire or Smoke Detected"
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
cls_id = int(box.cls[0]) # YOLOv10 classes
|
| 23 |
-
conf = float(box.conf[0])
|
| 24 |
|
| 25 |
-
|
| 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 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
return "\n".join(output)
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
inputs=gr.Image(type="pil"),
|
| 38 |
-
outputs=
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 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()
|