Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,30 @@
|
|
| 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 |
-
|
| 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:
|
| 29 |
return "β SAFE β No Fire/Smoke Detected"
|
| 30 |
|
| 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:
|
| 38 |
-
output.append(f"π₯ FIRE DETECTED β Confidence
|
| 39 |
elif cls == 1:
|
| 40 |
-
output.append(f"π¨ SMOKE DETECTED β Confidence
|
| 41 |
|
| 42 |
if not output:
|
| 43 |
return "β SAFE β No Fire/Smoke Detected"
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
+
model = YOLO("best.pt") # Fire + Smoke YOLOv10 model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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/Smoke Detected"
|
| 18 |
|
| 19 |
output = []
|
|
|
|
| 20 |
for box in results.boxes:
|
| 21 |
+
cls = int(box.cls[0]) # 0=fire, 1=smoke
|
| 22 |
conf = float(box.conf[0])
|
| 23 |
|
| 24 |
if cls == 0:
|
| 25 |
+
output.append(f"π₯ FIRE DETECTED β Confidence {conf:.2f}")
|
| 26 |
elif cls == 1:
|
| 27 |
+
output.append(f"π¨ SMOKE DETECTED β Confidence {conf:.2f}")
|
| 28 |
|
| 29 |
if not output:
|
| 30 |
return "β SAFE β No Fire/Smoke Detected"
|