Spaces:
Running
Running
Upload 4 files
Browse files- firedetection.py +76 -0
- last.pt +3 -0
- main.py +43 -0
- requirements.txt +18 -0
firedetection.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import defaultdict
|
2 |
+
import cv2
|
3 |
+
from ultralytics import YOLO
|
4 |
+
from ultralytics.utils.plotting import Annotator, colors
|
5 |
+
|
6 |
+
def detect_fire_in_video(
|
7 |
+
input_video_path: str,
|
8 |
+
output_video_path: str,
|
9 |
+
model_path: str,
|
10 |
+
device: str = "cpu"
|
11 |
+
):
|
12 |
+
"""
|
13 |
+
Belirtilen videoda YOLO modelini kullanarak yangın tespiti yapar ve
|
14 |
+
etiketlenmiş çıktıyı output_video_path'e kaydeder.
|
15 |
+
|
16 |
+
:param input_video_path: Girdi videosunun dosya yolu
|
17 |
+
:param output_video_path: İşlenen (annotate edilmiş) videonun kaydedileceği dosya yolu
|
18 |
+
:param model_path: YOLO model dosyasının (ör. .pt uzantılı) dosya yolu
|
19 |
+
:param device: İşlem için kullanılacak donanım ('cpu', 'cuda', 'mps' vb.)
|
20 |
+
:return: İşlenen video yolunu döndürür.
|
21 |
+
"""
|
22 |
+
|
23 |
+
# Takip geçmişi için defaultdict oluştur
|
24 |
+
track_history = defaultdict(lambda: [])
|
25 |
+
|
26 |
+
# YOLO modelini yükle
|
27 |
+
model = YOLO(model_path, device=device)
|
28 |
+
|
29 |
+
# Video dosyasını aç
|
30 |
+
cap = cv2.VideoCapture(input_video_path)
|
31 |
+
|
32 |
+
# Video özelliklerini al
|
33 |
+
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
|
34 |
+
cv2.CAP_PROP_FRAME_HEIGHT,
|
35 |
+
cv2.CAP_PROP_FPS))
|
36 |
+
|
37 |
+
# Çıktı videosu kaydı için VideoWriter ayarla
|
38 |
+
out = cv2.VideoWriter(
|
39 |
+
output_video_path,
|
40 |
+
cv2.VideoWriter_fourcc(*"MJPG"),
|
41 |
+
fps,
|
42 |
+
(w, h)
|
43 |
+
)
|
44 |
+
|
45 |
+
while True:
|
46 |
+
ret, im0 = cap.read()
|
47 |
+
if not ret:
|
48 |
+
print("Video bitti veya çerçeve okunamadı.")
|
49 |
+
break
|
50 |
+
|
51 |
+
# Annotator objesi ile çizim yapma
|
52 |
+
annotator = Annotator(im0, line_width=2)
|
53 |
+
|
54 |
+
# model.track ile takip (tracking) sonuçlarını al
|
55 |
+
results = model.track(im0, persist=True)
|
56 |
+
|
57 |
+
# Track ID'ler ve maskeler varsa işleme al
|
58 |
+
if results[0].boxes.id is not None and results[0].masks is not None:
|
59 |
+
masks = results[0].masks.xy
|
60 |
+
track_ids = results[0].boxes.id.int().cpu().tolist()
|
61 |
+
|
62 |
+
for mask, track_id in zip(masks, track_ids):
|
63 |
+
annotator.seg_bbox(
|
64 |
+
mask=mask,
|
65 |
+
mask_color=colors(int(track_id), True),
|
66 |
+
label=str(track_id)
|
67 |
+
)
|
68 |
+
|
69 |
+
# Annotate edilmiş frame'i kaydet
|
70 |
+
out.write(im0)
|
71 |
+
|
72 |
+
out.release()
|
73 |
+
cap.release()
|
74 |
+
cv2.destroyAllWindows()
|
75 |
+
|
76 |
+
return output_video_path
|
last.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:23fbcbdb3822f64f2a4a0114cd587c606830abfb2b32e191b21b2d8e3bd51a8c
|
3 |
+
size 5543699
|
main.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from firedetection import detect_fire_in_video
|
4 |
+
|
5 |
+
TEMP_DIR = "temp_outputs"
|
6 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
7 |
+
|
8 |
+
MODEL_PATH = "/Users/bahakizil/Desktop/firedetection/last.pt" # Fire detection model .pt dosyası (örnek)
|
9 |
+
DEVICE = "cpu" # İsterseniz 'cpu' veya 'cuda' vb. olarak değiştirebilirsiniz.
|
10 |
+
|
11 |
+
def fire_detection_interface(video_file):
|
12 |
+
|
13 |
+
if not video_file:
|
14 |
+
return None
|
15 |
+
|
16 |
+
input_video_path = video_file
|
17 |
+
|
18 |
+
base_name = os.path.basename(input_video_path)
|
19 |
+
name_no_ext, ext = os.path.splitext(base_name)
|
20 |
+
output_video_path = os.path.join(TEMP_DIR, f"{name_no_ext}_fire_detection{ext}")
|
21 |
+
|
22 |
+
processed_path = detect_fire_in_video(
|
23 |
+
input_video_path=input_video_path,
|
24 |
+
output_video_path=output_video_path,
|
25 |
+
model_path=MODEL_PATH,
|
26 |
+
device=DEVICE
|
27 |
+
)
|
28 |
+
|
29 |
+
return processed_path
|
30 |
+
|
31 |
+
|
32 |
+
demo = gr.Interface(
|
33 |
+
fn=fire_detection_interface,
|
34 |
+
inputs=gr.Video(label="Video Yükleyin"),
|
35 |
+
outputs=gr.Video(label="İşlenmiş Video"),
|
36 |
+
title="Fire Detection",
|
37 |
+
description=(
|
38 |
+
|
39 |
+
),
|
40 |
+
)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.2.2
|
2 |
+
torchvision==0.17.2
|
3 |
+
timm==1.0.14
|
4 |
+
albumentations==2.0.4
|
5 |
+
onnx==1.14.0
|
6 |
+
onnxruntime==1.15.1
|
7 |
+
pycocotools==2.0.7
|
8 |
+
PyYAML==6.0.1
|
9 |
+
scipy==1.13.0
|
10 |
+
onnxslim==0.1.31
|
11 |
+
gradio==4.44.1
|
12 |
+
opencv-python==4.9.0.80
|
13 |
+
psutil==5.9.8
|
14 |
+
py-cpuinfo==9.0.0
|
15 |
+
huggingface-hub==0.23.2
|
16 |
+
safetensors==0.4.3
|
17 |
+
numpy==1.26.4
|
18 |
+
|