Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,32 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
from ultralytics import YOLO
|
| 4 |
|
| 5 |
-
# 1. Load
|
| 6 |
model = YOLO("best.pt")
|
| 7 |
|
| 8 |
-
# 2. CORRECT WAY TO OVERRIDE NAMES (Accessing the underlying model dictionary)
|
| 9 |
-
# This bypasses the read-only property and fixes the cross-wiring safely.
|
| 10 |
-
model.model.names = {0: "Smoke", 1: "Fire"}
|
| 11 |
-
|
| 12 |
def predict_image(img):
|
| 13 |
if img is None:
|
| 14 |
return None, "No image uploaded."
|
| 15 |
|
| 16 |
-
# Convert Gradio's RGB
|
| 17 |
bgr_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 18 |
|
| 19 |
-
# Run prediction (
|
| 20 |
-
results = model.predict(source=bgr_img, conf=0.
|
| 21 |
|
| 22 |
-
# Get the
|
| 23 |
annotated_img_bgr = results[0].plot()
|
| 24 |
annotated_img_rgb = cv2.cvtColor(annotated_img_bgr, cv2.COLOR_BGR2RGB)
|
| 25 |
|
| 26 |
-
# Extract
|
| 27 |
detected_classes = []
|
| 28 |
if results[0].boxes is not None:
|
| 29 |
for box in results[0].boxes:
|
| 30 |
cls_id = int(box.cls[0])
|
| 31 |
-
class_name = model.
|
| 32 |
detected_classes.append(class_name)
|
| 33 |
|
| 34 |
-
# Generate the
|
| 35 |
if len(detected_classes) == 0:
|
| 36 |
status_warning = "✅ SYSTEM STATUS: Safe (No Fire or Smoke detected)"
|
| 37 |
else:
|
|
@@ -41,10 +37,10 @@ def predict_image(img):
|
|
| 41 |
|
| 42 |
return annotated_img_rgb, status_warning
|
| 43 |
|
| 44 |
-
# Build the Gradio UI Layout
|
| 45 |
-
with gr.Blocks(title="🔥 AI Fire & Smoke Detection System") as demo:
|
| 46 |
-
gr.Markdown("# 🔥 AI Fire & Smoke Detection System")
|
| 47 |
-
gr.Markdown("
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
|
|
|
| 2 |
import cv2
|
| 3 |
from ultralytics import YOLO
|
| 4 |
|
| 5 |
+
# 1. Load your new masterpiece model
|
| 6 |
model = YOLO("best.pt")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def predict_image(img):
|
| 9 |
if img is None:
|
| 10 |
return None, "No image uploaded."
|
| 11 |
|
| 12 |
+
# Convert Gradio's RGB format to BGR for YOLO
|
| 13 |
bgr_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 14 |
|
| 15 |
+
# Run prediction (Balanced threshold at 0.25 confidence)
|
| 16 |
+
results = model.predict(source=bgr_img, conf=0.25)
|
| 17 |
|
| 18 |
+
# Get the visually annotated BGR image and map back to RGB for Gradio
|
| 19 |
annotated_img_bgr = results[0].plot()
|
| 20 |
annotated_img_rgb = cv2.cvtColor(annotated_img_bgr, cv2.COLOR_BGR2RGB)
|
| 21 |
|
| 22 |
+
# Extract detected classes safely using native, pre-mapped indices
|
| 23 |
detected_classes = []
|
| 24 |
if results[0].boxes is not None:
|
| 25 |
for box in results[0].boxes:
|
| 26 |
cls_id = int(box.cls[0])
|
| 27 |
+
class_name = model.names[cls_id] # Natively tracks 'Smoke' or 'Fire' perfectly
|
| 28 |
detected_classes.append(class_name)
|
| 29 |
|
| 30 |
+
# Generate the warning message
|
| 31 |
if len(detected_classes) == 0:
|
| 32 |
status_warning = "✅ SYSTEM STATUS: Safe (No Fire or Smoke detected)"
|
| 33 |
else:
|
|
|
|
| 37 |
|
| 38 |
return annotated_img_rgb, status_warning
|
| 39 |
|
| 40 |
+
# Build the Masterpiece Gradio UI Layout
|
| 41 |
+
with gr.Blocks(title="🔥 AI Fire & Smoke Detection System v2.0") as demo:
|
| 42 |
+
gr.Markdown("# 🔥 AI Fire & Smoke Detection System v2.0 (Masterpiece Edition)")
|
| 43 |
+
gr.Markdown("An advanced custom-trained YOLOv8 system optimized against glare, ambient lighting, and complex vapor patterns.")
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Column():
|