forked from lukefi/FishTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_manager.py
298 lines (240 loc) · 13 KB
/
ui_manager.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
"""
This file is part of Fish Tracker.
Copyright 2021, VTT Technical research centre of Finland Ltd.
Developed by: Mikael Uimonen.
Fish Tracker 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.
Fish Tracker 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 Fish Tracker. If not, see <https://www.gnu.org/licenses/>.
"""
import sys, os, cv2
import numpy
from datetime import datetime
from PyQt5 import QtGui, QtCore, QtWidgets
import file_handler as fh
from main_window import MainWindow
from sonar_widget import SonarViewer
from echogram_widget import EchogramViewer
from fish_manager import FishManager
from fish_list import FishList
from parameter_list import ParameterList
from detector import Detector
from tracker import Tracker
from detector_parameters_view import DetectorParametersView
from detection_list import DetectionList, DetectionDataModel
from tracker_parameters_view import TrackerParametersView
from playback_manager import PlaybackManager
from sonar_view3 import Ui_MainWindow
from output_widget import OutputViewer, LogToFile
from log_object import LogObject
from batch_dialog import BatchDialog
from save_manager import SaveManager
from user_preferences import UserPreferencesDialog
class UIManager():
def __init__(self, main_window, playback_manager, detector, tracker, fish_manager, save_manager):
self.widgets_initialized = False
self.main_window = main_window
self.playback = playback_manager
self.detector = detector
self.tracker = tracker
self.fish_manager = fish_manager
self.save_manager = save_manager
self.ui = Ui_MainWindow()
self.ui.setupUi(main_window)
self.main_window.setupStatusBar()
self.setUpFunctions()
self.main_window.show()
self.setupWidgets()
self.playback.setTitle()
def setupWidgets(self):
_translate = QtCore.QCoreApplication.translate
echo = EchogramViewer(self.playback, self.detector, self.fish_manager)
self.ui.splitter_2.replaceWidget(0, echo)
self.ui.echogram_widget = echo
echo.setMaximumHeight(400)
self.sonar_viewer = SonarViewer(self.main_window, self.playback, self.detector, self.tracker, self.fish_manager)
self.ui.splitter.replaceWidget(0, self.sonar_viewer)
self.ui.sonar_widget = self.sonar_viewer
self.tool_bar = ParameterList(self.playback, self.sonar_viewer.image_processor, self.sonar_viewer, self.fish_manager, self.detector, self.tracker, echo)
self.ui.horizontalLayout_2.replaceWidget(self.ui.tool_bar, self.tool_bar)
self.tool_bar.setMaximumWidth(40)
self.ui.tool_bar = self.tool_bar
self.fish_list = FishList(self.fish_manager, self.playback, self.sonar_viewer)
self.sonar_viewer.measure_event.append(self.fish_list.setMeasurementResult)
self.detector_parameters = DetectorParametersView(self.playback, self.detector, self.sonar_viewer.image_processor)
detection_model = DetectionDataModel(self.detector)
self.detection_list = DetectionList(detection_model)
self.tracker_parameters = TrackerParametersView(self.playback, self.tracker, self.detector, self.fish_manager)
self.save_manager.file_loaded_event.connect(self.tracker_parameters.refreshValues)
self.output = OutputViewer()
self.output.connectToLogObject()
self.output.updateLogSignal.connect(self.main_window.updateStatusLog)
self.logToFile = LogToFile()
LogObject().connect(self.logToFile.writeLine)
# Tabs for the side panel.
self.ui.info_widget.removeTab(0)
self.ui.info_widget.addTab(self.detector_parameters, "")
self.ui.info_widget.setTabText(self.ui.info_widget.indexOf(self.detector_parameters), _translate("MainWindow", "Detector"))
self.ui.info_widget.addTab(self.detection_list, "")
self.ui.info_widget.setTabText(self.ui.info_widget.indexOf(self.detection_list), _translate("MainWindow", "Detections"))
self.ui.info_widget.addTab(self.tracker_parameters, "")
self.ui.info_widget.setTabText(self.ui.info_widget.indexOf(self.tracker_parameters), _translate("MainWindow", "Tracker"))
self.ui.info_widget.addTab(self.fish_list, "")
self.ui.info_widget.setTabText(self.ui.info_widget.indexOf(self.fish_list), _translate("MainWindow", "Tracks"))
self.ui.info_widget.addTab(self.output, "")
self.ui.info_widget.setTabText(self.ui.info_widget.indexOf(self.output), _translate("MainWindow", "Log"))
def setUpFunctions(self):
self.ui.menu_File.aboutToShow.connect(self.menuFileAboutToShow)
self.ui.action_Open.setShortcut('Ctrl+O')
self.ui.action_Open.triggered.connect(self.openFile)
self.ui.action_Batch.setShortcut('Ctrl+B')
self.ui.action_Batch.triggered.connect(self.runBatch)
self.ui.action_FlowDir.setShortcut('Ctrl+D')
self.ui.action_FlowDir.triggered.connect(self.changeFlowDirection)
self.ui.action_UserPref.setShortcut('Ctrl+U')
self.ui.action_UserPref.triggered.connect(self.openUserPreferences)
if fh.getTestFilePath() is not None:
self.ui.action_OpenTest = QtWidgets.QAction(self.main_window)
self.ui.action_OpenTest.setObjectName("action_OpenTest")
self.ui.menu_File.addAction(self.ui.action_OpenTest)
self.ui.action_OpenTest.setShortcut('Ctrl+T')
self.ui.action_OpenTest.triggered.connect(self.openTestFile)
self.ui.action_OpenTest.setText(QtCore.QCoreApplication.translate("MainWindow", "&Open test file"))
self.ui.action_save_as = QtWidgets.QAction(self.main_window)
self.ui.action_save_as.setObjectName("action_save_as")
self.ui.menu_File.addAction(self.ui.action_save_as)
self.ui.action_save_as.triggered.connect(self.saveAs)
self.ui.action_save_as.setText(QtCore.QCoreApplication.translate("MainWindow", "&Save as..."))
self.ui.action_save_as.setShortcut('Ctrl+Shift+S')
self.ui.action_save = QtWidgets.QAction(self.main_window)
self.ui.action_save.setObjectName("action_save")
self.ui.menu_File.addAction(self.ui.action_save)
self.ui.action_save.triggered.connect(self.save)
self.ui.action_save.setText(QtCore.QCoreApplication.translate("MainWindow", "&Save..."))
self.ui.action_save.setShortcut('Ctrl+S')
self.ui.action_load = QtWidgets.QAction(self.main_window)
self.ui.action_load.setObjectName("action_load")
self.ui.menu_File.addAction(self.ui.action_load)
self.ui.action_load.triggered.connect(self.load)
self.ui.action_load.setText(QtCore.QCoreApplication.translate("MainWindow", "&Load..."))
self.ui.action_export_detections = QtWidgets.QAction(self.main_window)
self.ui.action_export_detections.setObjectName("action_export_detections")
self.ui.menu_File.addAction(self.ui.action_export_detections)
self.ui.action_export_detections.triggered.connect(self.exportDetections)
self.ui.action_export_detections.setText(QtCore.QCoreApplication.translate("MainWindow", "&Export detections..."))
self.ui.action_export_tracks = QtWidgets.QAction(self.main_window)
self.ui.action_export_tracks.setObjectName("action_export_tracks")
self.ui.menu_File.addAction(self.ui.action_export_tracks)
self.ui.action_export_tracks.triggered.connect(self.exportTracks)
self.ui.action_export_tracks.setText(QtCore.QCoreApplication.translate("MainWindow", "&Export tracks..."))
self.ui.action_import_detections = QtWidgets.QAction(self.main_window)
self.ui.action_import_detections.setObjectName("action_import_detections")
self.ui.menu_File.addAction(self.ui.action_import_detections)
self.ui.action_import_detections.triggered.connect(self.importDetections)
self.ui.action_import_detections.setText(QtCore.QCoreApplication.translate("MainWindow", "&Import detections..."))
self.ui.action_import_tracks = QtWidgets.QAction(self.main_window)
self.ui.action_import_tracks.setObjectName("action_import_tracks")
self.ui.menu_File.addAction(self.ui.action_import_tracks)
self.ui.action_import_tracks.triggered.connect(self.importTracks)
self.ui.action_import_tracks.setText(QtCore.QCoreApplication.translate("MainWindow", "&Import tracks..."))
self.ui.action_close_file = QtWidgets.QAction(self.main_window)
self.ui.action_close_file.setObjectName("action_close_file")
self.ui.menu_File.addAction(self.ui.action_close_file)
self.ui.action_close_file.setShortcut('Ctrl+Q')
self.ui.action_close_file.triggered.connect(self.closeFile)
self.ui.action_close_file.setText(QtCore.QCoreApplication.translate("MainWindow", "&Close file"))
def openFile(self):
try:
self.playback.openFile()
except FileNotFoundError as e:
if e.filename and e.filename != "":
LogObject().print(e)
def openTestFile(self):
try:
self.playback.openTestFile()
except FileNotFoundError as e:
if e.filename and e.filename != "":
LogObject().print(e)
def closeFile(self):
self.playback.closeFile()
def saveAs(self):
path = self.playback.selectSaveFile(None, "FishTracker Files (*.fish)")
if path != "" :
self.save_manager.saveFile(path, fh.getConfValue(fh.ConfKeys.save_as_binary))
def save(self):
path = None
if self.save_manager.fast_save_enabled:
path = self.save_manager.previous_path
self.save_manager.saveFile(path, fh.getConfValue(fh.ConfKeys.save_as_binary))
else:
self.saveAs()
def load(self):
path = self.playback.selectLoadFile(None, "FishTracker Files (*.fish)")
if path != "" :
self.save_manager.loadFile(path)
def exportDetections(self):
path = self.playback.selectSaveFile()
if path != "" :
self.detector.saveDetectionsToFile(path)
def exportTracks(self):
path = self.playback.selectSaveFile()
if path != "" :
self.fish_manager.saveToFile(path)
def importDetections(self):
path = self.playback.selectLoadFile()
if path != "":
self.detector.loadDetectionsFromFile(path)
def importTracks(self):
path = self.playback.selectLoadFile()
if path != "":
self.fish_manager.loadFromFile(path)
def runBatch(self):
dparams = self.detector.parameters.copy()
tparams = self.tracker.getAllParameters()
dialog = BatchDialog(self.playback, dparams, tparams)
dialog.exec_()
def openUserPreferences(self):
dialog = UserPreferencesDialog(self.playback)
dialog.exec_()
def changeFlowDirection(self):
self.fish_manager.toggleUpDownInversion()
if self.playback.isMappingDone():
self.playback.refreshFrame()
else:
self.sonar_viewer.displayImage(None)
def menuFileAboutToShow(self):
file_open = self.playback.sonar is not None
self.ui.action_save_as.setEnabled(file_open)
self.ui.action_save.setEnabled(file_open)
self.ui.action_export_detections.setEnabled(file_open)
self.ui.action_export_tracks.setEnabled(file_open)
self.ui.action_import_detections.setEnabled(file_open)
self.ui.action_import_tracks.setEnabled(file_open)
self.ui.action_close_file.setEnabled(file_open)
def launch_ui():
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
fh.checkConfFile()
playback_manager = PlaybackManager(app, main_window)
detector = Detector(playback_manager)
tracker = Tracker(detector)
fish_manager = FishManager(playback_manager, tracker)
save_manager = SaveManager(playback_manager, detector, tracker, fish_manager)
detector.all_computed_signal.connect(playback_manager.refreshFrame)
tracker.all_computed_signal.connect(lambda x: playback_manager.refreshFrame)
playback_manager.mapping_done.connect(lambda: playback_manager.runInThread(lambda: detector.initMOG(False)))
playback_manager.frame_available_immediate.append(detector.compute_from_event)
playback_manager.file_opened.connect(detector.clearDetections)
playback_manager.file_closed.connect(detector.clearDetections)
playback_manager.file_opened.connect(tracker.clear)
playback_manager.file_closed.connect(tracker.clear)
ui_manager = UIManager(main_window, playback_manager, detector, tracker, fish_manager, save_manager)
sys.exit(app.exec_())
if __name__ == "__main__":
launch_ui()