File size: 3,343 Bytes
1afa0e4
d8dc06c
 
 
 
 
1afa0e4
d8dc06c
 
 
 
1afa0e4
d8dc06c
 
 
 
 
 
11ce781
d8dc06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11ce781
d8dc06c
1afa0e4
d8dc06c
 
 
 
 
 
 
 
11ce781
d8dc06c
 
 
 
 
 
 
 
 
1afa0e4
d8dc06c
 
 
 
1afa0e4
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import cv2
import torch
import gradio as gr
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

def detect_fire_in_video(input_video_path: str, output_video_path: str) -> str:
    """
    Verilen video üzerinde YOLO modeli kullanarak yangın tespiti yapar.
    Her kareye anotasyon ekler ve çıktıyı bir video dosyasına kaydeder.

    Args:
        input_video_path (str): Giriş video dosyasının yolu.
        output_video_path (str): Anotasyonlu çıktı videosunun kaydedileceği yol.

    Returns:
        str: Anotasyonlu çıktı videosunun yolu.
    """

    # Kullanılacak cihazı belirle
    device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
    print(f"Seçilen cihaz: {device}")

    # Aynı dizindeki 'last.pt' modelini yükle
    model_path = os.path.join(os.path.dirname(__file__), 'last.pt')
    if not os.path.isfile(model_path):
        raise FileNotFoundError(f"Model dosyası bulunamadı: {model_path}")
    model = YOLO(model_path)
    model.to(device)

    # Giriş videosunu aç
    cap = cv2.VideoCapture(input_video_path)
    if not cap.isOpened():
        raise ValueError(f"Video dosyası açılamadı: {input_video_path}")

    # Video özelliklerini al
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Çıktı videosu için VideoWriter oluştur
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        # Nesne tespiti yap
        results = model(frame, device=device)

        # Kare üzerinde anotasyon yapmak için Annotator oluştur
        annotator = Annotator(frame)

        # Tespit edilen nesneler üzerinde iterasyon yap ve anotasyon ekle
        for result in results:
            boxes = result.boxes
            for box in boxes:
                b = box.xyxy[0].cpu().numpy()
                c = int(box.cls[0])
                label = f'{model.names[c]} {box.conf[0]:.2f}'
                annotator.box_label(b, label, color=colors(c, True))

        # Anotasyonlu kareyi çıktı videosuna yaz
        out.write(frame)

    # Kaynakları serbest bırak
    cap.release()
    out.release()
    cv2.destroyAllWindows()

    return output_video_path

def process_video(input_video) -> str:
    """
    Yüklenen videoyu yangın tespiti için işler.

    Args:
        input_video (str): Yüklenen video dosyasının yolu.

    Returns:
        str: Anotasyonlu çıktı videosunun yolu.
    """
    output_video_path = "annotated_output.mp4"
    return detect_fire_in_video(input_video, output_video_path)

# Gradio arayüzünü tanımla
with gr.Blocks() as demo:
    gr.Markdown("## Yangın Tespiti Video İşleme")
    with gr.Row():
        with gr.Column():
            video_input = gr.Video(label="Video Yükle")
            process_button = gr.Button("Videoyu İşle")
        with gr.Column():
            video_output = gr.Video(label="Anotasyonlu Video")

    process_button.click(
        fn=process_video,
        inputs=video_input,
        outputs=video_output
    )

if __name__ == "__main__":
    demo.launch()