-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_detector_editor.py
359 lines (277 loc) · 12.8 KB
/
motion_detector_editor.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Filename: motion_detector_editor.py
"""The main purpose of this program is to take Tesla Sentinal Clips and Edit them down to a highlights reel"""
import sys
import os
import cv2, time
import glob
# Import QApplication and the required widgets from PySide2.QtWidgets
from PySide2 import QtWidgets
from PySide2 import QtCore
from PySide2.QtCore import Qt
from PySide2.QtCore import QTimer
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QMainWindow
from PySide2.QtWidgets import QWidget
from PySide2.QtWidgets import QGridLayout
from PySide2.QtWidgets import QLineEdit
from PySide2.QtWidgets import QPushButton
from PySide2.QtWidgets import QHBoxLayout
from PySide2.QtWidgets import QVBoxLayout
from PySide2.QtWidgets import QComboBox
from PySide2.QtWidgets import QGroupBox
from PySide2.QtWidgets import QFrame
from PySide2.QtWidgets import QLabel
from PySide2.QtWidgets import QDialog
from PySide2.QtWidgets import QDialogButtonBox
from PySide2.QtWidgets import QFormLayout
from PySide2.QtWidgets import QLineEdit
from PySide2.QtWidgets import QFileDialog
from PySide2.QtWidgets import QProgressBar
from PySide2.QtWidgets import QMessageBox
from style import styles
class ProgramUI(QtWidgets.QDialog):
"""Generic (GUI)."""
def __init__(self, parent=None):
super(ProgramUI, self).__init__(parent)
# loads an exteranl styesheet as python
newstyle = styles()
self.setStyleSheet(newstyle.dark_orange)
self.setWindowTitle("Motion Detector Editor")
self.setFixedSize(375, 250)
# create the main layout
self.main_layout = QVBoxLayout()
# create the top layout and add it
self.createtoplayout()
self.main_layout.addWidget(self.topGroupBox)
# create the save botton and add it
self.save_location_button = QtWidgets.QPushButton("Save Location")
self.save_location_button.setAutoDefault(False)
self.main_layout.addWidget(self.save_location_button)
# create the bottom layout and add it
self.createbottomlayout()
self.main_layout.addLayout(self.proggresslayout)
# resize window to match the width of what has been added
self.setLayout(self.main_layout)
layout_size = self.main_layout.sizeHint()
self.setFixedSize(self.width(),layout_size.height())
def createtoplayout(self):
self.topGroupBox = QGroupBox("")
self.open_location_button = QtWidgets.QPushButton("Full Path Location")
self.open_location_button.setAutoDefault(False)
self.textboxlabel = QLabel("Wildcard")
self.textboxlabel.setMinimumWidth(100)
self.lineEdit = QLineEdit("")
text_box_layout = QHBoxLayout()
text_box_layout.addWidget(self.textboxlabel)
text_box_layout.addWidget(self.lineEdit )
self.infoboxlabel = QLabel("Number of Videos to Process")
self.infoboxlabel.setMinimumWidth(100)
self.numberbox = QLabel("0")
self.numberbox.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken )
self.numberbox.setStyleSheet("""
QWidget {
padding: 1px;
border-style: solid;
border: 1px solid #1e1e1e;
border-radius: 5;
min-height: 15px;
font-size: 12px;
}
""")
info_box_layout = QHBoxLayout()
info_box_layout.addWidget(self.infoboxlabel)
info_box_layout.addWidget(self.numberbox)
layout = QVBoxLayout()
layout.addWidget(self.open_location_button)
layout.addLayout(text_box_layout)
layout.addLayout(info_box_layout)
layout.addStretch(1)
self.topGroupBox.setLayout(layout)
def advanceProgressBar(self, percentage_complete):
if percentage_complete < self.progressBar.maximum():
self.progressBar.setValue(percentage_complete)
else:
self.progressBar.setValue(self.progressBar.maximum())
def createbottomlayout(self):
self.proggresslayout = QHBoxLayout()
self.runprogram_button = QtWidgets.QPushButton("Run the Program")
self.runprogram_button.setAutoDefault(False)
#self.runprogram_button.setDisabled(True)
self.progressBar = QProgressBar()
self.progressBar.setRange(0, 100)
self.progressBar.setValue(0)
self.timer = QTimer(self)
self.proggresslayout.addWidget(self.runprogram_button)
self.proggresslayout.addWidget(self.progressBar)
def showdialog(self, message, title = "Error", details = "An error has occured"):
self.msg = QMessageBox()
self.msg.setIcon(QMessageBox.Information)
self.msg.setText(message)
#self.msg.setInformativeText("This is additional information")
self.msg.setWindowTitle(title)
self.msg.setDetailedText(details)
self.msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
#self.msg.buttonClicked.connect(msgbtn)
retval = self.msg.exec_()
# Create a Controller class to connect the GUI and the model
class ProgramCtrl:
"""Program Controller class."""
def __init__(self, view, model):
"""Controller initializer."""
self._model = model
self._view = view
# Connect signals and slots
self._connectSignals()
# this needs to change
def _getfolder(self, message, button_name):
dialog = QFileDialog()
#basefoldername = dialog.getExistingDirectory()
basefoldername = dialog.getExistingDirectory(caption=message,options = QFileDialog.DontUseNativeDialog)
button_name.setText(message + basefoldername)
common_prefix = basefoldername
self._model.setfolder(basefoldername)
if basefoldername != "":
self._model._getvideolist()
self._wildcardchange()
def _getsavefilename(self, message, button_name):
dialog = QFileDialog()
save_file_name = dialog.getSaveFileName(filter="*.avi")
button_name.setText(message + save_file_name[0])
self._model.setsavefilename(save_file_name[0])
def _runtheprogram(self):
if self._model.oktoruncheck(self._view.showdialog):
self._model.theprogram(self._view.showdialog)
def _wildcardchange(self):
self._model._sortvideolist(self._view.lineEdit.text())
self._view.numberbox.setText(str(len(self._model.wildcard_video_list)))
self._view.runprogram_button.setFocus()
# my_text_cursor = self._view.lineEdit.cursor()
# my_text_cursor = my_text_edit.textCursor()
def _connectSignals(self):
"""Connect signals and slots."""
self.fullpath = self._view.open_location_button.clicked.connect(lambda: self._getfolder("Full Path Location: ", self._view.open_location_button))
self.relativepath = self._view.save_location_button.clicked.connect(lambda: self._getsavefilename("Relative Path Location: ", self._view.save_location_button))
self._view.runprogram_button.clicked.connect(lambda: self._runtheprogram())
self._view.lineEdit.editingFinished.connect(self._wildcardchange)
self._view.lineEdit.returnPressed.connect(self._wildcardchange)
self._view.timer.timeout.connect(lambda: self._view.advanceProgressBar(self._model.progress))
self._view.timer.start(1000)
class ProgramModel:
def __init__(self):
"""Model initializer."""
self.folderlocation = ""
self.savefilelocation = ""
#self.path = "C:\\test\\motion"
self.fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
#self.out = cv2.VideoWriter((self.path + "out_test.avi"), self.fourcc, 24,(1280,960))
self.video_list = []
self.progress = 0
def setfolder(self, folderlocation):
self.folderlocation = folderlocation
def setsavefilename(self, savefilelocation):
self.savefilelocation = savefilelocation
def oktoruncheck(self, popup):
if self.folderlocation == "" or self.savefilelocation == "":
if self.folderlocation == "" and self.savefilelocation == "":
popup("Please select a Folder and a Save File")
elif self.folderlocation == "":
popup("Please select a Folder")
elif self.savefilelocation == "":
popup("Please select a File to save to")
return False
return True
def _capture_motion(self, video_file):
self.video=cv2.VideoCapture(video_file)
self._check = True
self._first_frame = None
self._frame_count = 0
#
while self._check:
self._check, frame = self.video.read()
self._status=0
if self._check:
self._frame_count = self._frame_count + 1
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if self._first_frame is None:
self._first_frame = gray
continue
delta_frame=cv2.absdiff(self._first_frame,gray)
thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame= cv2.dilate(thresh_frame, None, iterations=2)
#cnts = None
(cnts,_)=cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 1000:
continue
if self._frame_count < 30:
print("action on first frame")
self._errored_videos.append(tuple((video_file,"action on first frame")))
return
#print(self._frame_count)
self._status = 1
# (x,y,w,h)=cv2.boundingRect(contour)
# cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
#cv2.imshow("Capturing", gray)
# cv2.imshow("Delta Frame",delta_frame)
# cv2.imshow("Threshold Frame",thresh_frame)
previewframe = cv2.resize(frame, (480,270 ))
cv2.imshow("Color Frame",previewframe )
if self._status == 1:
self.out.write(frame)
key = cv2.waitKey(1)
if key ==ord('q'):
break
def _trim_videos(self, video_list):
progress_amount = int(100/(len(video_list)+1))+1
self.out = cv2.VideoWriter((self.savefilelocation), self.fourcc, 24,(1280,960))
for video_file in video_list:
self.progress = self.progress + progress_amount
self._capture_motion(video_file)
def _close_video_tools(self):
self.progress =100
self.out.release()
self.video.release()
cv2.destroyAllWindows()
def run_the_edit(self,video_file_list):
self._trim_videos(video_file_list)
self._close_video_tools()
def _getvideolist(self):
videos = []
types = ("**\**\*.mp4", "**\**\*.mp3") # the tuple of file types
# grab all the files in selected directory recursively
for files in types:
videos.extend(glob.glob(self.folderlocation + files, recursive=True))
self.video_list = videos
#return(videos)
#video_file_list=["test_videos\\front.mp4", "test_videos\\front2.mp4"]
def _sortvideolist(self, wildcard):
newvideos = [i for i in self.video_list if wildcard in i]
self.wildcard_video_list = newvideos
def _format_error_message(self, errored_videos):
error_message = ""
for video in errored_videos:
error_message = error_message + ("\n video "+ video[0] +" \n error message " + video[1])
return error_message
def theprogram(self,popup):
self.progress = 0
self._errored_videos =[]
self.run_the_edit(self.wildcard_video_list)
print(self._errored_videos)
print(len(self._errored_videos) )
if len(self._errored_videos) > 0:
print("popup fired")
popup("Some Videos Errored while processing", title = "Not all Videos Completes", details = (self._format_error_message(self._errored_videos)))
def main():
"""Main function"""
#Create and instance of QApplication
pyqt_interface = QApplication(sys.argv)
# Show the Programs's GUI
view = ProgramUI()
view.show()
model = ProgramModel()
ProgramCtrl(view=view, model=model)
# Execute the Programs's main loop
sys.exit(pyqt_interface.exec_())
if __name__ == '__main__':
main()