-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
executable file
·301 lines (265 loc) · 10.3 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""
DeepLabStream
© J.Schweihoff, M. Loshakov
University Bonn Medical Faculty, Germany
https://github.com/SchwarzNeuroconLab/DeepLabStream
Licensed under GNU General Public License v3.0
"""
import sys
import os
import cv2
from DeepLabStream import DeepLabStream, show_stream
from utils.generic import MissingFrameError
from utils.configloader import MULTI_CAM, STREAMS, RECORD_EXP
from utils.gui_image import QFrame, ImageWindow, emit_qframes
from PySide2.QtCore import QThread
from PySide2.QtWidgets import QPushButton, QApplication, QWidget, QGridLayout
from PySide2.QtGui import QIcon
# creating a complete thread process to work in the background
class AThread(QThread):
"""
QThread is just one of the many PyQt ways to do multitasking
This is, for most intents and purposes, identical to Python multithreading
"""
def start(self, **kwargs):
"""
Setting thread to active, creating a QFrames dictionary
Then just starting parent method
"""
self.threadactive = True
self.qframes = {}
# changePixmap = pyqtSignal(QImage)
for camera in stream_manager.enabled_cameras:
self.qframes[camera] = QFrame()
super().start(**kwargs)
def run(self):
"""
Infinite loop with all the streaming, analysis and recording logic
"""
while self.threadactive:
try:
all_frames = stream_manager.get_frames()
except MissingFrameError as e:
"""catch missing frame, stop Thread and save what can be saved"""
print(*e.args, "\nShutting down DLStream and saving data...")
stream_manager.finish_streaming()
stream_manager.stop_cameras()
self.stop()
break
color_frames, depth_maps, infra_frames = all_frames
# writing the video
if stream_manager.recording_status():
stream_manager.write_video(color_frames, stream_manager.frame_index)
if stream_manager.dlc_status():
# outputting the frames
res_frames, res_time = stream_manager.get_analysed_frames()
# inputting the frames
stream_manager.input_frames_for_analysis(
all_frames, stream_manager.frame_index
)
# streaming the stream
if res_frames:
self._stream_frames(res_frames)
else:
self._stream_frames(color_frames)
stream_manager.frame_index += 1
def stop(self):
"""
Setting thread to active, thus stopping the infinite loop
"""
self.threadactive = False
def _stream_frames(self, frames):
"""
Shows some number of stream frames, depending on cameras quantity
Method of streaming depends on platform
Windows -> through openCV with their window objects
Unix -> thought PyQt with some widget window
:param frames: dictionary of frames in format of {camera:frame}
"""
if os.name == "nt":
show_stream(frames)
# very important line for openCV to work correctly
# actually does nothing, but do NOT delete
cv2.waitKey(1)
else:
emit_qframes(frames, self.qframes)
class ButtonWindow(QWidget):
def __init__(self):
super().__init__()
# setting the icon for window
self.setWindowIcon(QIcon("misc/DLStream_Logo_small.png"))
self.setWindowTitle("DeepLabStream")
self.title = "ButtonWindow"
# next is the complete buttons dictionary with buttons, icons, functions and layouts
self._buttons_dict = {
"Start_Stream": {
"Button": QPushButton("Start Stream"),
"Icon": QIcon("misc/StartStream2.png"),
"Function": self.start_stream,
"Layout": (0, 0, 2, 2),
"State": True,
},
"Start_Analysis": {
"Button": QPushButton("Start Analysis"),
"Icon": QIcon("misc/StartAnalysis2.png"),
"Function": self.start_analysis,
"Layout": (2, 0, 2, 1),
"State": False,
},
"Start_Experiment": {
"Button": QPushButton("Start Experiment"),
"Icon": QIcon("misc/StartExperiment2.png"),
"Function": self.start_experiment,
"Layout": (4, 0, 2, 1),
"State": False,
},
"Start_Recording": {
"Button": QPushButton("Start Recording"),
"Icon": QIcon("misc/StartRecording2.png"),
"Function": self.start_recording,
"Layout": (6, 0, 2, 1),
"State": False,
},
"Stop_Stream": {
"Button": QPushButton("Stop Stream"),
"Icon": QIcon("misc/StopStream2.png"),
"Function": self.stop_stream,
"Layout": (8, 0, 2, 2),
"State": False,
},
"Stop_Analysis": {
"Button": QPushButton("Stop Analysis"),
"Icon": QIcon("misc/StopAnalysis2.png"),
"Function": self.stop_analysis,
"Layout": (2, 1, 2, 1),
"State": False,
},
"Stop_Experiment": {
"Button": QPushButton("Stop Experiment"),
"Icon": QIcon("misc/StopExperiment2.png"),
"Function": self.stop_experiment,
"Layout": (4, 1, 2, 1),
"State": False,
},
"Stop_Recording": {
"Button": QPushButton("Stop Recording"),
"Icon": QIcon("misc/StopRecording2.png"),
"Function": self.stop_recording,
"Layout": (6, 1, 2, 1),
"State": False,
},
}
# creating button layout with icons and functionality
self.initialize_buttons()
self._thread = None
self.image_windows = {}
def start(self):
self._thread.start()
def stop(self):
self._thread.stop()
def initialize_buttons(self):
"""
Function to make button window great again
Sets all buttons with an icon, function and position
"""
layout = QGridLayout()
for func in self._buttons_dict:
# setting icon
self._buttons_dict[func]["Button"].setIcon(self._buttons_dict[func]["Icon"])
# setting function
self._buttons_dict[func]["Button"].clicked.connect(
self._buttons_dict[func]["Function"]
)
# setting position
layout.addWidget(
self._buttons_dict[func]["Button"], *self._buttons_dict[func]["Layout"]
)
# setting default state
self._buttons_dict[func]["Button"].setEnabled(
self._buttons_dict[func]["State"]
)
# setting button size
self._buttons_dict[func]["Button"].setMinimumHeight(100)
# setting window layout for all buttons
self.setLayout(layout)
def buttons_toggle(self, *buttons):
for button in buttons:
self._buttons_dict[button]["Button"].setEnabled(
not self._buttons_dict[button]["Button"].isEnabled()
)
""" Button functions"""
def start_stream(self):
# initializing the stream manager cameras
stream_manager.start_cameras(STREAMS, MULTI_CAM)
# initializing background thread
self._thread = AThread(self)
self._thread.start()
print("Streaming started")
# flipping the state of the buttons
self.buttons_toggle(
"Start_Analysis", "Start_Recording", "Start_Stream", "Stop_Stream"
)
# initializing image windows for Unix systems via PyQt
if os.name != "nt":
for camera in stream_manager.enabled_cameras:
self.image_windows[camera] = ImageWindow(camera)
self._thread.qframes[camera].signal.connect(
self.image_windows[camera].set_image
)
self.image_windows[camera].show()
else:
# for Windows it is taken care by openCV
pass
def stop_stream(self):
# stopping background thread
self._thread.stop()
# flipping the state of the buttons
for func in self._buttons_dict:
self._buttons_dict[func]["Button"].setEnabled(
self._buttons_dict[func]["State"]
)
if os.name != "nt":
for camera in self.image_windows:
self.image_windows[camera].hide()
else:
pass
print("Streaming stopped")
stream_manager.finish_streaming()
stream_manager.stop_cameras()
def start_analysis(self):
print("Analysis starting")
self.buttons_toggle("Stop_Analysis", "Start_Analysis", "Start_Experiment")
stream_manager.set_up_multiprocessing()
stream_manager.start_dlc()
stream_manager.create_output()
def stop_analysis(self):
print("Analysis stopped")
self.buttons_toggle("Stop_Analysis", "Start_Analysis", "Start_Experiment")
stream_manager.stop_dlc()
def start_experiment(self):
print("Experiment started")
self.buttons_toggle("Stop_Experiment", "Start_Experiment")
stream_manager.set_up_experiment()
stream_manager.start_experiment()
if RECORD_EXP:
self.start_recording()
def stop_experiment(self):
print("Experiment stopped")
self.buttons_toggle("Stop_Experiment", "Start_Experiment")
stream_manager.stop_experiment()
if RECORD_EXP:
self.stop_recording()
def start_recording(self):
print("Recording started")
self.buttons_toggle("Stop_Recording", "Start_Recording")
stream_manager.start_recording()
def stop_recording(self):
print("Recording stopped")
self.buttons_toggle("Stop_Recording", "Start_Recording")
stream_manager.stop_recording()
if __name__ == "__main__":
stream_manager = DeepLabStream()
app = QApplication([])
bt = ButtonWindow()
bt.show()
sys.exit(app.exec_())