-
Notifications
You must be signed in to change notification settings - Fork 0
/
video2vr.py
279 lines (228 loc) · 9.31 KB
/
video2vr.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'''
video2vr - Convert 2d videos into side-by-side vr videos
Copyright (C) 2024 xXChampionsXx
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import sys
import numpy as np
from PyQt6.QtWidgets import (
QApplication,
QMainWindow,
QWidget,
QPushButton,
QFileDialog,
QVBoxLayout,
QLabel,
QHBoxLayout,
QDialog,
QSpinBox,
QComboBox,
)
from PyQt6.QtGui import QPixmap, QImage, QAction
from PyQt6.QtCore import QThread, pyqtSignal, Qt
from libs.video_utils import generate_vr_video
from libs.settings import settings_manager
class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray, np.ndarray)
finished_signal = pyqtSignal()
def __init__(self, video_path):
super().__init__()
self.video_path = video_path
self.running = True # Control the thread running
def run(self):
generate_vr_video(self.video_path, self.emit_frame)
self.finished_signal.emit()
def stop(self):
self.running = False
def emit_frame(self, frame: np.ndarray, new_frame: np.ndarray):
if self.running:
self.change_pixmap_signal.emit(frame, new_frame)
class ConverterWidget(QWidget):
def __init__(self):
super().__init__()
# Layout
self.layout = QVBoxLayout()
# Horizontal layout for the QLabels
self.h_layout = QHBoxLayout() # Create a QHBoxLayout
# Original Image label
self.original_image_label = QLabel()
self.original_image_label.setMaximumSize(1280, 720)
self.h_layout.addWidget(self.original_image_label)
# Depth Map label
self.new_frame_label = QLabel()
self.new_frame_label.setMaximumSize(1280, 720)
self.h_layout.addWidget(self.new_frame_label)
# Add the QHBoxLayout to the main QVBoxLayout
self.layout.addLayout(self.h_layout)
# Start button
self.start_button = QPushButton("Select Video and Start")
self.start_button.clicked.connect(self.open_file_dialog)
self.layout.addWidget(self.start_button)
# Set the layout to the QWidget
self.setLayout(self.layout)
# Video thread
self.video_thread = None
def open_file_dialog(self):
filename, _ = QFileDialog.getOpenFileName(
self, "Open Video File", "", "Video Files (*.mp4 *.flv *.ts *.mts *.avi)"
)
if filename:
self.start_button.setEnabled(False)
if (
self.video_thread is not None
): # Stop existing video thread if it's running
self.video_thread.stop()
self.video_thread.wait()
self.video_thread = VideoThread(filename)
self.video_thread.change_pixmap_signal.connect(self.update_frame_labels)
self.video_thread.finished_signal.connect(self.unlock_start_button)
self.video_thread.start()
def unlock_start_button(self):
self.start_button.setEnabled(True)
def update_frame_labels(self, frame: np.ndarray, new_frame: np.ndarray):
video_pixmap = self.convert_cv_to_pixmap(frame, self.original_image_label)
new_frame_pixmap = self.convert_cv_to_pixmap(new_frame, self.new_frame_label)
self.original_image_label.setPixmap(video_pixmap)
self.new_frame_label.setPixmap(new_frame_pixmap)
def convert_cv_to_pixmap(self, cv_img: np.ndarray, label: QImage):
"""Converts an opencv image to QPixmap"""
if len(cv_img.shape) == 2: # Grayscale image
height, width = cv_img.shape
q_img = QImage(
cv_img.data, width, height, width, QImage.Format.Format_Grayscale8
)
else: # Assume BGR color image
height, width, channels = cv_img.shape
bytes_per_line = channels * width
q_img = QImage(
cv_img.data, width, height, bytes_per_line, QImage.Format.Format_RGB888
).rgbSwapped()
pixmap = QPixmap.fromImage(q_img)
return pixmap.scaled(
label.size(),
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
def closeEvent(self, event):
if self.video_thread is not None:
self.video_thread.stop()
self.video_thread.wait()
super().closeEvent(event)
class SettingsDialog(QDialog):
"""
A simple settings dialog.
"""
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.available_ai_models = [
"Intel/dpt-swinv2-large-384",
"Intel/dpt-beit-large-512",
"LiheYoung/depth-anything-large-hf",
"LiheYoung/depth-anything-base-hf",
"LiheYoung/depth-anything-small-hf",
"LiheYoung/depth_anything_vitl14",
"LiheYoung/depth_anything_vitb14",
"LiheYoung/depth_anything_vits14",
]
settings_manager.load_settings()
print(settings_manager.settings)
self.initUI()
def initUI(self):
self.setWindowTitle("Settings")
layout = QVBoxLayout(self)
# Batch Size Setting
batch_size_layout = QHBoxLayout()
self.batch_size_label = QLabel("Batch Size:")
self.batch_size_input = QSpinBox()
self.batch_size_input.setMinimum(1)
self.batch_size_input.setMaximum(10000)
self.batch_size_input.setValue(settings_manager.settings.batch_size)
batch_size_layout.addWidget(self.batch_size_label)
batch_size_layout.addWidget(self.batch_size_input)
layout.addLayout(batch_size_layout)
# AI Model Setting
ai_model_layout = QHBoxLayout()
self.ai_model_label = QLabel("AI Model:")
self.ai_model_input = QComboBox()
self.ai_model_input.addItems(self.available_ai_models)
# Select the current ai_model in the dropdown
current_ai_model_index = self.ai_model_input.findText(
settings_manager.settings.ai_model
)
if current_ai_model_index >= 0:
self.ai_model_input.setCurrentIndex(current_ai_model_index)
ai_model_layout.addWidget(self.ai_model_label)
ai_model_layout.addWidget(self.ai_model_input)
layout.addLayout(ai_model_layout)
# Depth Offset Setting
depth_offset_layout = QHBoxLayout()
self.depth_offset_label = QLabel("Depth Offset:")
self.depth_offset_input = QSpinBox()
self.depth_offset_input.setMinimum(0)
self.depth_offset_input.setMaximum(1000)
self.depth_offset_input.setValue(settings_manager.settings.depth_offset)
depth_offset_layout.addWidget(self.depth_offset_label)
depth_offset_layout.addWidget(self.depth_offset_input)
layout.addLayout(depth_offset_layout)
# Save Button
self.save_button = QPushButton("Save")
self.save_button.clicked.connect(self.save_settings)
layout.addWidget(self.save_button)
self.setLayout(layout)
def save_settings(self):
# Retrieve values from input widgets and save them using the settings manager
settings_manager.set("batch_size", self.batch_size_input.value())
selected_model = self.ai_model_input.currentText()
settings_manager.set("ai_model", selected_model)
settings_manager.set("depth_offset", self.depth_offset_input.value())
# Close the dialog after saving
self.accept()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Window Configurations
self.setWindowTitle("Video To VR (V1.0)")
self.setGeometry(200, 100, 1280, 720)
# Main Widget
self.player = ConverterWidget()
self.setCentralWidget(self.player)
# Status bar
self.statusBar().showMessage("Ready")
# Menu bar
menubar = self.menuBar()
file_menu = menubar.addMenu("File")
# Add File Menu Actions
exit_action = QAction("Exit", self)
exit_action.setShortcut("Ctrl+Q")
exit_action.triggered.connect(self.close)
# Create the settings action.
settings_action = QAction("Settings", self)
settings_action.setStatusTip("Open settings")
settings_action.setShortcut("Ctrl+Alt+S")
settings_action.triggered.connect(self.openSettings)
file_menu.addAction(settings_action)
file_menu.addAction(exit_action)
def openSettings(self):
"""
Opens the settings dialog.
"""
settings_dialog = SettingsDialog(self)
settings_dialog.exec() # Show the dialog as a modal window.
def main():
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()