-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
334 lines (251 loc) · 9.38 KB
/
main.py
File metadata and controls
334 lines (251 loc) · 9.38 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
"""
/******************************************************************************************
Flight Separator
A Standalone Desktop Application
This tool detects and separates drone photos in a folder taken in
different flights based on timestamps.
-------------------
begin : 2020-09-01
copyright : (C) 2019-2021 by Chubu University and
National Research Institute for Earth Science and Disaster Resilience (NIED)
email : chuc92man@gmail.com
******************************************************************************************/
/******************************************************************************************
* This file is part of Flight Separator. *
* *
* 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, version 3 of the License. *
* *
* Flight Separator 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 *
* Flight Separator. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
"""
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QObject, pyqtSignal, QRunnable, pyqtSlot, QThreadPool, QDateTime, Qt
from PyQt5.uic import loadUiType
import traceback, sys
from os.path import abspath, join
import resources_rc
import folder_edit
from flight_separator import flightSeparator
MAX_THREADS = 2
def resourcePath(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = abspath(".")
return join(base_path, relative_path)
FORM_CLASS,_ = loadUiType(resourcePath('main.ui'))
class WorkerSignals(QObject):
'''
Defines the signals available from a running worker thread.
Supported signals are:
finished
No data
error
tuple (exctype, value, traceback.format_exc() )
result
object data returned from processing, anything
progress
int indicating % progress
'''
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
progress = pyqtSignal(int)
class Worker(QRunnable):
'''
Worker thread
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
:param callback: The function callback to run on this worker thread. Supplied args and
kwargs will be passed through to the runner.
:type callback: function
:param args: Arguments to pass to the callback function
:param kwargs: Keywords to pass to the callback function
'''
def __init__(self, func, *args, **kwargs):
super(Worker, self).__init__()
# Store constructor arguments (re-used for processing)
self.func = func
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
# Add the callback to our kwargs
self.kwargs['progress_callback'] = self.signals.progress
@pyqtSlot()
def run(self):
'''
Initialise the runner function with passed args, kwargs.
'''
# Retrieve args/kwargs here; and fire processing using them
try:
result = self.func(self.args[0], self.args[1], self.args[2], self.args[3], **self.kwargs)
except:
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
self.signals.result.emit(result) # Return the result of the processing
finally:
self.signals.finished.emit() # Done
class Main(QMainWindow, FORM_CLASS):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
QMainWindow.__init__(self)
self.setWindowIcon(QIcon(join(resourcePath('icon'), 'app.png')))
self.setupUi(self)
self.Handel_Buttons()
# initialize input variables
self.fs_folder_name = None
self.fs_stime = 1
self.threadpool = QThreadPool()
self.threadpool.setMaxThreadCount(MAX_THREADS)
def Handel_Buttons(self):
self.fs_intext.textChanged.connect(self.onIntextChanged)
self.fs_inbutton.clicked.connect(self.onSelectPhotoFolder)
self.fs_timesbox.valueChanged.connect(self.onSetFStime)
self.fs_button_box.accepted.connect(self.onAccept)
self.fs_button_box.rejected.connect(self.onClosePlugin)
self.fs_clearlog.clicked.connect(self.onClearLog)
self.fs_copylog.clicked.connect(self.onCopyLog)
self.fs_savelog.clicked.connect(self.onSaveLog)
self.fs_clearlog.setIcon(QIcon(join(resourcePath('icon'), 'erase.png')))
self.fs_copylog.setIcon(QIcon(join(resourcePath('icon'), 'copypaste.png')))
self.fs_savelog.setIcon(QIcon(join(resourcePath('icon'), 'save2file.png')))
def onIntextChanged(self):
"""
Callback for LineEdit change, set content to self.fs_folder_name.
Returns
-------
None.
"""
self.fs_folder_name = self.fs_intext.text()
self.fs_progress.setValue(0)
def onSelectPhotoFolder(self):
"""
Set content for self.fs_intext when user choose folder via button.
Returns
-------
None.
"""
folder = QFileDialog.getExistingDirectory(self, "Select folder ")
# if user do not select any folder, then don't change folder_name
if len(folder) > 1:
self.fs_intext.setText(folder)
def onSetFStime(self, new_value):
"""
Set flight separation time.
Parameters
----------
new_value : TYPE
DESCRIPTION.
Returns
-------
None.
"""
self.fs_stime = int(new_value)
def onAccept(self):
"""
Flight separator processing.
Returns
-------
None.
"""
if self.fs_folder_name is not None:
c_fs_stime = self.fs_stime * 60
iskeep = self.fs_checkbox.isChecked()
worker = Worker(flightSeparator, self.fs_folder_name, (".jpg"), c_fs_stime, iskeep)
worker.signals.result.connect(self.onWriteLog)
worker.signals.progress.connect(self.onProgressUpdate)
worker.signals.error.connect(self.onError)
self.threadpool.start(worker)
def onProgressUpdate(self, n):
"""
Update processing progress.
Parameters
----------
n : float
Percentage of work done.
Returns
-------
None.
"""
self.fs_progress.setValue(int(n))
def onError(self, e):
"""
Processing with exception.
Parameters
----------
e : Exception
Exception encounted during processing.
Returns
-------
None.
"""
self.fs_log.appendPlainText("{0}: Task completed!\n {1}\n".format(QDateTime.currentDateTime().toString(Qt.ISODate), e[1]))
def onWriteLog(self, result):
"""
Write results to log widget.
Parameters
----------
result : string
Log string.
Returns
-------
None.
"""
self.fs_log.appendPlainText("{0}: Task completed!\n {1}\n".format(QDateTime.currentDateTime().toString(Qt.ISODate), result["msg"]))
def onClearLog(self):
"""
Clear log widget.
Returns
-------
None.
"""
self.fs_log.clear()
def onCopyLog(self):
"""
Copy log content.
Returns
-------
None.
"""
self.fs_log.selectAll()
self.fs_log.copy()
def onSaveLog(self):
"""
Save log content to a file.
Returns
-------
None.
"""
try:
name = QFileDialog.getSaveFileName(self, "Save File", '/', '.txt')[0]
with open(name, 'w') as f:
f.write(str(self.fs_log.toPlainText()))
except:
return
def onClosePlugin(self):
"""
Close the application.
Returns
-------
None.
"""
self.close()
def main():
app = QApplication(sys.argv)
window = Main()
window.show()
app.exec_()
if __name__=='__main__':
main()