-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideoPlayer.py
More file actions
198 lines (162 loc) · 8.21 KB
/
videoPlayer.py
File metadata and controls
198 lines (162 loc) · 8.21 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from PySide6.QtCore import Qt, Slot, QMutex, QMutexLocker, QFile, QCoreApplication, QThreadPool, QTimer
from PySide6.QtUiTools import QUiLoader
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtWidgets import QMainWindow, QLabel
import numpy as np
import cv2
import os
from VideoCaptureThread import VideoCaptureThread
from VideoWriterThread import VideoWriterThread
from VideoRendererThread import VideoRendererThread
from DialogSettings import DialogSettings
# Set required attributes before creating QGuiApplication
QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
class VideoPlayer(QMainWindow):
def __init__(self):
super().__init__()
#Loading the Main Window and its components from the .ui file
ui_file_path = QFile("VAR_System_ui\MainWindow.ui")
loader = QUiLoader()
self.main_window = loader.load(ui_file_path)
# Member variables
self.buffer_size = 250
self.encoding = "DIVX"
self.camera_index = 1
self.clip_index = 0
self.number_of_threads = 2
self.capture_threads = []
self.renderer_threads = []
self.writer_threads = []
self.dialog_settings = None
self.output_path = "output\\"
if not os.path.exists(self.output_path):
try:
os.makedirs(self.output_path)
except OSError as err:
print(f"Error creating folder {self.output_path} : {err}")
# Timer used to sync capture threads
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_threads)
# Function used to connect signals and start threads
self.prepare_application()
def prepare_application(self):
'''Setting UI parameters. Connecting signals and slots. Showing the MainWindow.
'''
self.main_window.start_button.clicked.connect(self.start)
self.main_window.realtime_button.clicked.connect(self.resume_realtime)
self.main_window.playback_button.clicked.connect(self.restart_playback)
self.main_window.save_buffer_button.clicked.connect(self.save_video_buffer)
self.main_window.playback_slider.sliderPressed.connect(self.playback_cursor_pressed)
self.main_window.playback_slider.sliderMoved.connect(self.playback_cursor_dragged)
self.main_window.playback_slider.sliderReleased.connect(self.playback_cursor_released)
self.main_window.actionSettings.triggered.connect(self.open_dialog_settings)
self.setWindowTitle("VAR System Test")
self.setCentralWidget(self.main_window)
self.show()
@Slot()
def restart_playback(self):
'''Set the tail position to the beginning of the buffer'''
for thread in self.capture_threads:
thread.set_buffer_playback(True)
@Slot()
def start(self):
for thread_index in range(self.number_of_threads):
# Capture Thread
c_thread = VideoCaptureThread(buffer_size=self.buffer_size,
capture_index=thread_index,
parent=self)
# Renderer thread
label_string = "video_label_" + str(thread_index + 1)
video_label = self.main_window.findChild(QLabel, label_string)
r_thread = VideoRendererThread(video_label)
# Connecting signals
c_thread.tail_position_updated.connect(self.update_playback_cursor_position)
c_thread.head_position_updated.connect(self.update_write_cursor_position)
c_thread.display_frame.connect(r_thread.video_label_update)
# Adding threads to their respective list
self.capture_threads.append(c_thread)
self.renderer_threads.append(r_thread)
self.main_window.playback_slider.setRange(0, self.buffer_size)
self.main_window.write_slider.setRange(0, self.buffer_size)
self.main_window.menuOptions.setEnabled(False)
self.main_window.realtime_button.setEnabled(True)
self.main_window.playback_button.setEnabled(True)
self.main_window.save_buffer_button.setEnabled(True)
self.main_window.start_button.setEnabled(False)
self.main_window.playback_slider.setEnabled(True)
self.main_window.write_slider.setEnabled(True)
self.timer.start(self.capture_threads[0].frame_interval)
for c_thread, r_thread in zip(self.capture_threads, self.renderer_threads):
c_thread.start()
r_thread.start()
@Slot()
def update_threads(self):
for thread in self.capture_threads:
thread.synchronize_threads()
@Slot(int)
def update_playback_cursor_position(self, position:int):
'''The playback cursor is connected to the tail pointer of the buffer. Its position is updated
through a signal emitted by the WorkerThread.'''
#print(f"Buffer TAIL position {position}")
self.main_window.playback_slider.setValue(position)
@Slot(int)
def update_write_cursor_position(self, position:int):
#print(f"Buffer HEAD position {position}")
self.main_window.write_slider.setValue(position)
@Slot(bool)
def playback_cursor_pressed(self):
'''When the playback cursor is pressed the thread stops updating the cursor position'''
for thread in self.capture_threads:
thread.tail_position_updated.disconnect(self.update_playback_cursor_position)
@Slot(int)
def playback_cursor_dragged(self, slider_value: int):
'''When the playback cursor is dragged the thread starts peeking at the buffer in the selected position
Arguments:
-slider_value (int): The slider value is passed through a signal and determines the index to peek at
'''
for thread in self.capture_threads:
thread.set_buffer_peeking(is_peeking=True, new_peek_position=slider_value)
@Slot(int)
def playback_cursor_released(self):
'''When the playback cursor is released the thread stops peeking at the buffer and resumes playback from the selected position'''
new_peek_position = self.main_window.playback_slider.value()
for thread in self.capture_threads:
thread.set_buffer_peeking(is_peeking=False, new_peek_position=new_peek_position)
thread.tail_position_updated.connect(self.update_playback_cursor_position)
@Slot()
def resume_realtime(self):
'''Set the tail position to the head position to resume the real-time playback'''
for thread in self.capture_threads:
thread.set_buffer_playback(False)
@Slot()
def open_dialog_settings(self):
if not self.dialog_settings:
self.dialog_settings = DialogSettings()
self.dialog_settings.updateSettings.connect(self.update_settings)
self.dialog_settings.show()
else:
self.dialog_settings.show()
@Slot()
def save_video_buffer(self):
self.main_window.save_buffer_button.setEnabled(False)
for index, thread in enumerate(self.capture_threads):
capture_data = thread.get_capture_data()
filename = self.output_path + "capture_" + str(index) + "_clip_" + str(self.clip_index) + ".avi"
self.clip_index += 1
w_thread = VideoWriterThread(buffer = capture_data[0],
width = int(capture_data[1]),
height = int(capture_data[2]),
fps = capture_data[3],
filename = filename,
fourcc = cv2.VideoWriter_fourcc(*self.encoding),
parent = self)
self.writer_threads.append(w_thread)
for thread in self.writer_threads:
thread.start()
self.main_window.save_buffer_button.setEnabled(True)
@Slot(int, int, str, str)
def update_settings(self, number_of_cameras, buffer_size, encoding, output_path):
self.buffer_size = buffer_size
self.number_of_threads = number_of_cameras
self.encoding = encoding
self.output_path = output_path