Spaces:
Running
Running
Update app.py
Browse files
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
|
| 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 β
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
for box in results.boxes:
|
| 24 |
-
|
| 25 |
conf = float(box.conf[0])
|
| 26 |
|
| 27 |
-
if
|
| 28 |
-
|
| 29 |
-
elif
|
| 30 |
-
|
| 31 |
|
| 32 |
-
if not
|
| 33 |
-
return "β SAFE β
|
| 34 |
|
| 35 |
-
return "\n".join(
|
| 36 |
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=detect_fire_smoke,
|
| 39 |
inputs=gr.Image(type="pil"),
|
| 40 |
outputs="text",
|
| 41 |
-
title="Fire & Smoke Detection
|
| 42 |
-
description="Upload an image to
|
| 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()
|