Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
from ultralytics import YOLO
|
4 |
+
|
5 |
+
# Load the YOLO model (update the path to your fire detection model weights)
|
6 |
+
model = YOLO('path/to/your/best.pt') # Replace 'path/to/your/best.pt' with the actual path to your model file
|
7 |
+
|
8 |
+
def detect_fire(frame):
|
9 |
+
# Convert the frame to RGB format (YOLO expects this format)
|
10 |
+
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
11 |
+
|
12 |
+
# Perform fire detection using the YOLO model
|
13 |
+
results = model(img)
|
14 |
+
|
15 |
+
# Draw bounding boxes and labels on the detected fire areas
|
16 |
+
for bbox in results[0].boxes:
|
17 |
+
xyxy = bbox.xyxy[0] # Bounding box coordinates
|
18 |
+
conf = bbox.conf[0] # Confidence score
|
19 |
+
cls = int(bbox.cls[0]) # Class ID
|
20 |
+
label = model.names[cls] # Class name
|
21 |
+
|
22 |
+
if label == "fire": # Make sure this matches the label in your trained model
|
23 |
+
# Draw a rectangle around the detected fire
|
24 |
+
cv2.rectangle(img, (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3])), (255, 0, 0), 2)
|
25 |
+
# Put the label text above the rectangle
|
26 |
+
cv2.putText(img, f"{label} {conf:.2f}", (int(xyxy[0]), int(xyxy[1]) - 10),
|
27 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
28 |
+
|
29 |
+
# Convert the image back to BGR format for display
|
30 |
+
return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
31 |
+
|
32 |
+
# Create a Gradio interface for fire detection
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=detect_fire,
|
35 |
+
inputs=gr.Image(source="webcam", tool="editor", streaming=True), # Use webcam as the input source
|
36 |
+
outputs="image",
|
37 |
+
title="Fire Detection using YOLO",
|
38 |
+
description="This application detects fire in real-time using a YOLO model."
|
39 |
+
)
|
40 |
+
|
41 |
+
# Launch the Gradio app
|
42 |
+
iface.launch()
|