-
Notifications
You must be signed in to change notification settings - Fork 10
/
pvg_acquire.py
439 lines (329 loc) · 15.7 KB
/
pvg_acquire.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# pvg_acquire.py
#
# Copyright 2011 Giorgio Gilestro <[email protected]>
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
__author__ = "Giorgio Gilestro <[email protected]>"
__version__ = "$Revision: 1.0 $"
__date__ = "$Date: 2011/08/16 21:57:19 $"
__copyright__ = "Copyright (c) 2011 Giorgio Gilestro"
__license__ = "Python"
import os, optparse
from pvg_common import pvg_config, DEFAULT_CONFIG, options
from accessories.sleepdeprivator import sleepdeprivator
import pysolovideo
import cv2
import threading
from time import sleep
import wx
from wx.lib.filebrowsebutton import FileBrowseButton
import wx.grid as gridlib
class partial: #AKA curry
'''
This functions allows calling another function upon event trigger and pass arguments to it
ex buttonA.Bind (wx.EVT_BUTTON, partial(self.Print, 'Hello World!'))
'''
def __init__(self, fun, *args, **kwargs):
self.fun = fun
self.pending = args[:]
self.kwargs = kwargs.copy()
def __call__(self, *args, **kwargs):
if kwargs and self.kwargs:
kw = self.kwargs.copy()
kw.update(kwargs)
else:
kw = kwargs or self.kwargs
return self.fun(*(self.pending + args), **kw)
class comboFileBrowser(wx.ComboBox):
def __init__(self, parent, id=-1, pos=(-1,-1), size=(-1,-1), value="", choices=[], style=0, dialogTitle = "Choose a File", startDirectory = ".", fileMask = "*.*", browsevalue="Browse for file", changeCallback= None):
choices = list(set([value] + choices ))
choices.sort()
self.fileMask = fileMask
self.dialogTitle = dialogTitle
self.startDirectory = startDirectory
self.defaultFile = value
self.browsevalue=browsevalue
self.changeCallback = changeCallback
wx.ComboBox.__init__(self, parent, id, value, pos, size, choices + [browsevalue], style=wx.CB_DROPDOWN | wx.CB_READONLY | wx.CB_SORT)
self.Bind(wx.EVT_COMBOBOX, self.onItemChanged)
def onItemChanged(self, event):
"""
"""
if event.GetString() == self.browsevalue:
dlg = wx.FileDialog(
self, message=self.dialogTitle,
defaultDir=self.startDirectory,
defaultFile=self.defaultFile,
wildcard=self.fileMask,
style=wx.OPEN | wx.CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
__, filename = os.path.split(path)
self.Append(filename)
self.SetValue(filename)
event.SetString(path)
#self.Command(event)
dlg.Destroy()
#event.SetValue(filename)
self.changeCallback(event=event)
class pvg_AcquirePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, wx.ID_ANY)
self.loadMonitors()
self.drawPanel()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.updateTimes, self.timer)
self.dopreview = False
cv2.namedWindow("preview")
def drawPanel(self):
"""
"""
###################################################
for child in self.GetChildren():
child.Destroy()
mon_num = options.GetOption("Monitors")
num_cams = options.GetOption("Webcams")
monitorsData = options.getMonitorsData()
WebcamsList = [ 'Camera %02d' % (int(w) +1) for w in range( num_cams ) ]
colLabels = ['Status', 'Monitor', 'Source', 'Mask', 'Output', 'Track type', 'Track', 'uptime', 'preview']
tracktypes = ['DISTANCE','VBS','XY_COORDS']
mainSizer = wx.BoxSizer(wx.VERTICAL)
gridSizer = wx.FlexGridSizer (cols=len(colLabels), vgap=5, hgap=5) #wx.BoxSizer(wx.VERTICAL)
#FIRST ROW
for key in colLabels:
text = wx.StaticText(self, -1, key )
text.SetFont( wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD) )
gridSizer.Add(text, 0, wx.ALL|wx.ALIGN_CENTER, 5)
self.status = []
self.recordBTNS = []
self.uptimeTXT = []
#LOOP THROUGH
for mn in range(1, mon_num+1):
if not options.HasMonitor(mn):
options.SetMonitor(mn) #If monitor does not exist in options we create it
md = options.GetMonitor(mn)
try:
_, source = os.path.split( md['source'] )
except:
source = 'Camera %02d' % ( md['source'] )
_, mf = os.path.split(md['mask_file'])
df = 'Monitor%02d.txt' % (mn)
#ICON
self.status.append( wx.StaticBitmap(self, -1, wx.EmptyBitmap(16,16)) )
gridSizer.Add(self.status[-1], 0, wx.ALL|wx.ALIGN_CENTER, 5)
self.changeIcon(mn)
#TEXT
gridSizer.Add(wx.StaticText(self, -1, "Monitor %s" % mn ), 0, wx.ALL|wx.ALIGN_CENTER, 5)
#INPUT SOURCE
gridSizer.Add(comboFileBrowser(self, wx.ID_ANY, size=(-1,-1), dialogTitle = "Choose an Input video file", startDirectory = options.GetOption("Data_Folder"), value = source, choices=WebcamsList, fileMask = "Video File (*.*)|*.*", browsevalue="Browse for video...", changeCallback = partial(self.onChangeDropDown, [mn, "source"])), 0, wx.ALL|wx.ALIGN_CENTER, 5 )
#MASK FILE
gridSizer.Add(comboFileBrowser(self, wx.ID_ANY, size=(-1,-1), dialogTitle = "Choose a Mask file", startDirectory = options.GetOption("Mask_Folder"), value = mf, fileMask = "pySolo mask file (*.msk)|*.msk", browsevalue="Browse for mask...", changeCallback = partial(self.onChangeDropDown, [mn, "mask_file"])), 0, wx.ALL|wx.ALIGN_CENTER, 5 )
#OUTPUT FILE
gridSizer.Add(comboFileBrowser(self, wx.ID_ANY, size=(-1,-1), dialogTitle = "Choose the output file", startDirectory = options.GetOption("Data_Folder"), value = md['outputfile'], fileMask = "Output File (*.txt)|*.txt", browsevalue="Browse for output...", changeCallback = partial(self.onChangeDropDown, [mn, "outputfile"])), 0, wx.ALL|wx.ALIGN_CENTER, 5 )
#TRACKTYPE
ttcb = wx.ComboBox(self, -1, size=(-1,-1), value=md['track_type'], choices=tracktypes, style=wx.CB_DROPDOWN | wx.CB_READONLY | wx.CB_SORT)
ttcb.Bind (wx.EVT_COMBOBOX, partial(self.onChangeDropDown, [mn, "track_type"]))
gridSizer.Add(ttcb , 0, wx.ALL|wx.ALIGN_CENTER, 5)
#RECORD BUTTON
self.recordBTNS.append ( wx.ToggleButton(self, wx.ID_ANY, 'Start') )
self.recordBTNS[-1].Bind (wx.EVT_TOGGLEBUTTON, partial( self.onToggleRecording, mn))
gridSizer.Add(self.recordBTNS[-1], 0, wx.ALL|wx.ALIGN_CENTER, 5)
#UPTIME
self.uptimeTXT.append(wx.TextCtrl(self, value="00:00:00", size=(140,-1)))
gridSizer.Add(self.uptimeTXT[-1], 0, wx.ALL|wx.ALIGN_CENTER, 5)
#VIEW BUTTON
vb = wx.Button(self, wx.ID_ANY, 'View')
vb.Bind(wx.EVT_BUTTON, partial( self.onViewMonitor, mn))
gridSizer.Add(vb, 0, wx.ALL|wx.ALIGN_CENTER, 5)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
conf_btnSizer = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, 'Configuration'), wx.HORIZONTAL)
acq_btnSizer = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, 'Acquisition'), wx.HORIZONTAL)
self.saveOptionsBtn = wx.Button(self, wx.ID_ANY, 'Save')
self.saveOptionsBtn.Enable(False)
self.Bind(wx.EVT_BUTTON, self.onSave, self.saveOptionsBtn)
conf_btnSizer.Add (self.saveOptionsBtn, 0, wx.ALL, 5)
self.startBtn = wx.Button(self, wx.ID_ANY, 'Start All')
self.stopBtn = wx.Button(self, wx.ID_ANY, 'Stop All')
self.startBtn.Enable(True)
self.stopBtn.Enable(False)
self.Bind(wx.EVT_BUTTON, self.onStopAll, self.stopBtn)
self.Bind(wx.EVT_BUTTON, self.onStartAll, self.startBtn)
acq_btnSizer.Add (self.startBtn, 0, wx.ALL, 5)
acq_btnSizer.Add (self.stopBtn, 0, wx.ALL, 5)
btnSizer.Add(conf_btnSizer, 0, wx.ALL, 5)
btnSizer.Add(acq_btnSizer, 0, wx.ALL, 5)
#mainSizer.Add(self.FBconfig, 0, wx.EXPAND|wx.ALL, 5)
mainSizer.Add(gridSizer, 1, wx.EXPAND, 0)
mainSizer.Add(btnSizer, 0, wx.ALL, 5)
mainSizer.Layout()
self.SetSizer(mainSizer)
self.Refresh()
def changeIcon(self, monitor):
"""
"""
if monitor in self.active_monitors and self.active_monitors[monitor].hasSource():
bmp = wx.ArtProvider.GetBitmap(wx.ART_TICK_MARK, wx.ART_MESSAGE_BOX, (16,16))
else:
bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_MESSAGE_BOX, (16,16))
self.status[monitor-1].SetBitmap(bmp)
def onChangeCheckBox(self, target, event=None):
"""
"""
self.saveOptionsBtn.Enable(True)
self.startBtn.Enable(False)
value = event.IsChecked()
section = "Monitor%s" % target[0]
keyname = target[1]
options.setValue(section, keyname, value)
def onChangeDropDown(self, target, event=None):
"""
"""
self.saveOptionsBtn.Enable(True)
self.startBtn.Enable(False)
value = event.GetString()
if "Camera " in value:
value = int(value.split(" ")[1])
section = "Monitor%s" % target[0]
keyname = target[1]
options.setValue(section, keyname, value)
def onChangeValue(self, target, event=None):
"""
"""
self.saveOptionsBtn.Enable(True)
self.startBtn.Enable(False)
if event.GetEventType() == wx.EVT_CHECKBOX.evtType:
value = event.IsChecked()
#if event.GetEventType() == wx.EVT_COMBOBOX.evtType:
else:
value = event.GetString()
if "Camera " in value:
value = int(value.split(" ")[1])
section = "Monitor%s" % target[0]
keyname = target[1]
options.setValue(section, keyname, value)
def loadMonitors(self):
"""
"""
self.active_monitors = {}
resolution = options.GetOption("Resolution")
data_folder = options.GetOption("Data_Folder")
monitorsData = options.getMonitorsData()
for mn in monitorsData:
m = monitorsData[mn]
track_type = ['DISTANCE','VBS','XY_COORDS'].index(m['track_type'])
if type(m['source']) == int:
source = int(m['source']) - 1
else:
source = m['source']
output_file = m['outputfile'] or os.path.join(data_folder, 'Monitor%02d.txt' % mn)
self.active_monitors[mn] = pysolovideo.Monitor()
success = self.active_monitors[mn].setSource(source, resolution)
if success:
self.active_monitors[mn].setTracking(True, track_type, m['mask_file'], output_file)
self.active_monitors[mn].SDserialPort = m['serial_port']
self.active_monitors[mn].inactivity_threshold = m['inactivity_threshold'] or None
#self.changeIcon(mn, 1)
else:
#self.changeIcon(mn, -1)
pass
pysolovideo.MONITORS = self.active_monitors
def onStartAll(self, event=None):
"""
"""
self.stopBtn.Enable(True)
self.startBtn.Enable(False)
for num, btn in enumerate(self.recordBTNS):
recording = btn.GetValue()
if not recording:
self.onToggleRecording(num+1, force="start")
def onStopAll(self, event):
"""
"""
self.stopBtn.Enable(False)
self.startBtn.Enable(True)
for num, btn in enumerate(self.recordBTNS):
recording = btn.GetValue()
if recording: self.onToggleRecording(num+1, force="stop")
def onSave(self, event):
"""
"""
options.Save()
self.drawPanel()
self.saveOptionsBtn.Enable(False)
self.startBtn.Enable(True)
def onToggleRecording(self, monitor, event=None, force=None):
"""
"""
if monitor in self.active_monitors:
recording = self.recordBTNS[monitor-1].GetValue()
if force == "start" or recording:
self.active_monitors[monitor].startTracking()
self.recordBTNS[monitor-1].SetLabelText('Stop')
self.recordBTNS[monitor-1].SetValue(True)
self.timer.Start(1000)
elif force == "stop" or not recording:
self.recordBTNS[monitor-1].SetLabelText('Start')
self.active_monitors[monitor].stopTracking()
self.recordBTNS[monitor-1].SetValue(False)
self.timer.Stop()
def onViewMonitor(self, monitor, event=None):
"""
Called when we hit the "preview" button
"""
self.dopreview = monitor
#self.view_thread = threading.Thread(target=self.displayImage())
def displayImage(self):
"""
Show monitor image on preview window
"""
#while self.dopreview:
if self.dopreview:
frame = self.active_monitors[self.dopreview].getImageFromQueue()
if frame is not None:
cv2.imshow("preview", frame)
def updateTimes(self, event):
"""
"""
for n in range (len(self.active_monitors)):
if self.active_monitors[n+1].isTracking:
t, r = self.active_monitors[n+1].getUptime()
self.uptimeTXT[n].SetValue("%s (%s)" % (t, r))
self.displayImage()
class acquireFrame(wx.Frame):
def __init__(self, *args, **kwargs):
kwargs["size"] = (980, 600)
wx.Frame.__init__(self, *args, **kwargs)
self.sb = wx.StatusBar(self, wx.ID_ANY)
self.SetStatusBar(self.sb)
self.acq_panel = pvg_AcquirePanel(self)
if __name__ == '__main__':
parser = optparse.OptionParser(usage='%prog [options] [argument]', version='%prog version 1.0')
parser.add_option('-c', '--config', dest='config_file', metavar="CONFIG_FILE", help="The full path to the config file to open")
parser.add_option('--acquire', action="store_true", default=False, dest='acquire', help="Start acquisition when the program starts")
parser.add_option('--nogui', action="store_false", default=True, dest='showgui', help="Do not show the graphical interface")
(cmd_opts, args) = parser.parse_args()
app=wx.PySimpleApp(0)
frame_acq = acquireFrame(None, -1, '')
app.SetTopWindow(frame_acq)
frame_acq.Show(cmd_opts.showgui)
configfile = cmd_opts.config_file or DEFAULT_CONFIG
app.MainLoop()