Spaces:
Running
Running
Upload 3 files
Browse files- app.py +94 -31
- requirements.txt +0 -21
app.py
CHANGED
@@ -1,43 +1,106 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
-
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
"""
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
"""
|
16 |
-
|
17 |
-
return None
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
device="cpu"
|
31 |
)
|
32 |
-
return processed_path
|
33 |
-
|
34 |
-
demo = gr.Interface(
|
35 |
-
fn=fire_detection_interface,
|
36 |
-
inputs=gr.Video(label="Upload a Video"),
|
37 |
-
outputs=gr.Video(label="Annotated Video"),
|
38 |
-
title="Fire Detection with YOLO",
|
39 |
-
description="This interface detects fire in an uploaded video using YOLO."
|
40 |
-
)
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
demo.launch()
|
|
|
|
|
1 |
import os
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
from ultralytics import YOLO
|
6 |
+
from ultralytics.utils.plotting import Annotator, colors
|
7 |
|
8 |
+
def detect_fire_in_video(input_video_path: str, output_video_path: str) -> str:
|
9 |
+
"""
|
10 |
+
Verilen video üzerinde YOLO modeli kullanarak yangın tespiti yapar.
|
11 |
+
Her kareye anotasyon ekler ve çıktıyı bir video dosyasına kaydeder.
|
12 |
|
13 |
+
Args:
|
14 |
+
input_video_path (str): Giriş video dosyasının yolu.
|
15 |
+
output_video_path (str): Anotasyonlu çıktı videosunun kaydedileceği yol.
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
str: Anotasyonlu çıktı videosunun yolu.
|
19 |
"""
|
20 |
+
|
21 |
+
# Kullanılacak cihazı belirle
|
22 |
+
device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
|
23 |
+
print(f"Seçilen cihaz: {device}")
|
24 |
+
|
25 |
+
# Aynı dizindeki 'last.pt' modelini yükle
|
26 |
+
model_path = os.path.join(os.path.dirname(__file__), 'last.pt')
|
27 |
+
if not os.path.isfile(model_path):
|
28 |
+
raise FileNotFoundError(f"Model dosyası bulunamadı: {model_path}")
|
29 |
+
model = YOLO(model_path)
|
30 |
+
model.to(device)
|
31 |
+
|
32 |
+
# Giriş videosunu aç
|
33 |
+
cap = cv2.VideoCapture(input_video_path)
|
34 |
+
if not cap.isOpened():
|
35 |
+
raise ValueError(f"Video dosyası açılamadı: {input_video_path}")
|
36 |
+
|
37 |
+
# Video özelliklerini al
|
38 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
39 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
40 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
41 |
+
|
42 |
+
# Çıktı videosu için VideoWriter oluştur
|
43 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
44 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
45 |
+
|
46 |
+
while cap.isOpened():
|
47 |
+
ret, frame = cap.read()
|
48 |
+
if not ret:
|
49 |
+
break
|
50 |
+
|
51 |
+
# Nesne tespiti yap
|
52 |
+
results = model(frame, device=device)
|
53 |
+
|
54 |
+
# Kare üzerinde anotasyon yapmak için Annotator oluştur
|
55 |
+
annotator = Annotator(frame)
|
56 |
+
|
57 |
+
# Tespit edilen nesneler üzerinde iterasyon yap ve anotasyon ekle
|
58 |
+
for result in results:
|
59 |
+
boxes = result.boxes
|
60 |
+
for box in boxes:
|
61 |
+
b = box.xyxy[0].cpu().numpy()
|
62 |
+
c = int(box.cls[0])
|
63 |
+
label = f'{model.names[c]} {box.conf[0]:.2f}'
|
64 |
+
annotator.box_label(b, label, color=colors(c, True))
|
65 |
+
|
66 |
+
# Anotasyonlu kareyi çıktı videosuna yaz
|
67 |
+
out.write(frame)
|
68 |
+
|
69 |
+
# Kaynakları serbest bırak
|
70 |
+
cap.release()
|
71 |
+
out.release()
|
72 |
+
cv2.destroyAllWindows()
|
73 |
+
|
74 |
+
return output_video_path
|
75 |
+
|
76 |
+
def process_video(input_video) -> str:
|
77 |
"""
|
78 |
+
Yüklenen videoyu yangın tespiti için işler.
|
|
|
79 |
|
80 |
+
Args:
|
81 |
+
input_video (str): Yüklenen video dosyasının yolu.
|
82 |
+
|
83 |
+
Returns:
|
84 |
+
str: Anotasyonlu çıktı videosunun yolu.
|
85 |
+
"""
|
86 |
+
output_video_path = "annotated_output.mp4"
|
87 |
+
return detect_fire_in_video(input_video, output_video_path)
|
88 |
|
89 |
+
# Gradio arayüzünü tanımla
|
90 |
+
with gr.Blocks() as demo:
|
91 |
+
gr.Markdown("## Yangın Tespiti Video İşleme")
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column():
|
94 |
+
video_input = gr.Video(label="Video Yükle")
|
95 |
+
process_button = gr.Button("Videoyu İşle")
|
96 |
+
with gr.Column():
|
97 |
+
video_output = gr.Video(label="Anotasyonlu Video")
|
98 |
|
99 |
+
process_button.click(
|
100 |
+
fn=process_video,
|
101 |
+
inputs=video_input,
|
102 |
+
outputs=video_output
|
|
|
103 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,21 +0,0 @@
|
|
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 |
-
psutil==5.9.8
|
13 |
-
py-cpuinfo==9.0.0
|
14 |
-
huggingface-hub==0.23.2
|
15 |
-
safetensors==0.4.3
|
16 |
-
numpy==1.26.4
|
17 |
-
ultralytics==8.0.20
|
18 |
-
opencv-python==4.7.0.72
|
19 |
-
langchain==0.3.0
|
20 |
-
pydantic==2.9.2
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|