-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
151 lines (109 loc) · 4.25 KB
/
app.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
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QSizePolicy
from PySide6.QtGui import QPalette, QColor, QPixmap, QImage, QShortcut
from PySide6.QtCore import QThreadPool, QSize, Signal
import cv2
from videoworker import VideoWorker
from audioworker import AudioWorker
from settings import SettingsControls
from filesaver import Filesaver
from config import video_device
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("diafilm")
self.resize(900, 700)
self.filesaver = Filesaver()
main_layout = QVBoxLayout()
preview = QHBoxLayout()
self.last_shot = LiveImageView('lime')
self.last_shot.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
self.video = ToggleImageView('purple')
self.video.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
preview.addWidget(self.last_shot)
preview.addWidget(self.video)
main_layout.addLayout(preview)
self.controls = SettingsControls()
self.controls.switch_tabs.connect(self.video.show_view)
main_layout.addWidget(self.controls)
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
# initialize ThreadPool
self.threadpool = QThreadPool()
# start VideoWorker
self.video_worker = VideoWorker(video_device, self.width()//2-50)
self.video_worker.signals.update_preview.connect(self.update_preview)
self.video_worker.signals.update_analysis.connect(self.update_analysis)
self.video_worker.analyzer.new_image.connect(self.new_image)
self.video_worker.analyzer.update_progress.connect(self.update_progress)
self.controls.threshold_changed.connect(self.video_worker.set_threshold)
self.controls.interval_changed.connect(self.video_worker.set_interval)
self.threadpool.start(self.video_worker)
self.audio_worker = AudioWorker()
self.mute = False
self.redo_shortcut = QShortcut(self)
self.redo_shortcut.setKey('r')
self.redo_shortcut.activated.connect(lambda: self.new_image(redo=True, manual=True))
self.take_shortcut = QShortcut(self)
self.take_shortcut.setKey('t')
self.take_shortcut.activated.connect(lambda: self.new_image(manual=True))
self.mute_shortcut = QShortcut(self)
self.mute_shortcut.setKey('m')
self.mute_shortcut.activated.connect(self.toggle_mute)
self.run_shortcut = QShortcut(self)
self.run_shortcut.setKey('s')
self.run_shortcut.activated.connect(self.controls.run_button.toggle_shortcut)
def toggle_mute(self):
self.mute = not self.mute
def closeEvent(self, event):
self.video_worker.stop()
def new_image(self, redo=False, manual=False):
if not self.mute:
self.audio_worker.start()
self.last_shot.update(self.video_worker.get_last_preview())
if not self.controls.is_paused() or manual:
if not redo:
self.controls.no += 1
self.filesaver.save(self.controls.get_output_dir(), self.controls.get_prefix(), self.controls.no, self.video_worker.get_last_frame())
def update_progress(self, progress, max_progress, status):
self.controls.update_progress(progress, max_progress)
pass
def update_preview(self, frame):
self.video.update_main(frame)
def update_analysis(self, frame, percent):
self.video.update_secondary(frame)
self.controls.update_percentage(percent)
class LiveImageView(QLabel):
def __init__(self, color):
super().__init__()
self.setAutoFillBackground(True)
self.setScaledContents(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
def update(self, frame):
img = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)
self.setPixmap(QPixmap.fromImage(img))
def flash(self):
white_frame = QPixmap()
white_frame.fill()
self.setPixmap(white_frame)
class ToggleImageView(LiveImageView):
def __init__(self, color):
super().__init__(color)
self._main_view = True
def get_pixmap(self):
return super().pixmap
def update_secondary(self, frame):
if not self._main_view:
super().update(frame)
def update_main(self, frame):
if self._main_view:
super().update(frame)
def show_view(self, show_main_view):
self._main_view = show_main_view
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()