-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvg_common.py
626 lines (503 loc) · 21.8 KB
/
pvg_common.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# -*- coding: utf-8 -*-
#
# pvg_common.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.
import inspect
import wx, cv, os
import pysolovideo as pv
import ConfigParser, threading
DEFAULT_CONFIG = 'pysolo_video'
class myConfig():
"""
Handles program configuration
Uses ConfigParser to store and retrieve
From gg's toolbox
"""
def __init__(self, filename=None, temporary=False, defaultOptions=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
filename the name of the configuration file
temporary whether we are reading and storing values temporarily
defaultOptions a dict containing the defaultOptions
"""
filename = filename or DEFAULT_CONFIG
pDir = os.getcwd()
if not os.access(pDir, os.W_OK): pDir = os.environ['HOME']
print(pDir + filename)
self.filename = os.path.join (pDir, filename)
self.filename_temp = '%s~' % self.filename
self.config = None
if defaultOptions != None:
self.defaultOptions = defaultOptions
else:
self.defaultOptions = { "option_1" : [0, "Description"],
}
self.Read(temporary)
def New(self, filename):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.filename = filename
self.Read()
def Read(self, temporary=False):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
read the configuration file. Initiate one if does not exist
temporary True Read the temporary file instead
False (Default) Read the actual file
"""
if temporary: filename = self.filename_temp
else: filename = self.filename
if os.path.exists(filename):
self.config = ConfigParser.RawConfigParser()
self.config.read(filename)
else:
self.Save(temporary, newfile=True)
def Save(self, temporary=False, newfile=False, filename=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if temporary and not filename: filename = self.filename_temp
elif not temporary and not filename: filename = self.filename
if newfile:
self.config = ConfigParser.RawConfigParser()
self.config.add_section('Options')
for key in self.defaultOptions:
self.config.set('Options', key, self.defaultOptions[key][0])
with open(filename, 'wb') as configfile:
self.config.write(configfile)
if not temporary: self.Save(temporary=True)
def SetValue(self, section, key, value):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if not self.config.has_section(section):
self.config.add_section(section)
self.config.set(section, key, value)
def GetValue(self, section, key):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
get value from config file
Does some sanity checking to return tuple, integer and strings
as required.
"""
r = self.config.get(section, key)
if type(r) == type(0) or type(r) == type(1.0): #native int and float
return r
elif type(r) == type(True): #native boolean
return r
elif type(r) == type(''):
r = r.split(',')
if len(r) == 2: #tuple
r = tuple([int(i) for i in r]) # tuple
elif len(r) < 2: #string or integer
try:
r = int(r[0]) #int as text
except:
if len(r) > 0:
r = r[0] #string
else:
r = ""
if r == 'False' or r == 'True':
r = (r == 'True') #bool
return r
def GetOption(self, key):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
return self.GetValue('Options', key)
class acquireObject():
def __init__(self, monitor, source, resolution, mask_file, track, track_type, dataFolder):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.monitor = monitor
self.keepGoing = False
self.verbose = True # false turns off debug
self.track = track
dataFolder = options.GetOption("Data_Folder")
outputFile = os.path.join(dataFolder, 'Monitor%02d.txt' % monitor)
self.mon = pv.Monitor()
self.mon.setSource(source, resolution)
self.mon.setTracking(True, track_type, mask_file, outputFile)
if self.verbose: print ( "Verbose - Setting monitor %s with source %s and mask %s. Output to %s " % (monitor, source, os.path.split(mask_file)[1], os.path.split(outputFile)[1] ) )
def run(self, kbdint=False):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
while self.keepGoing:
self.mon.GetImage()
def start(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.keepGoing = True
self.run()
def halt(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.keepGoing = False
if self.verbose: print ( "Verbose: Stopping capture" )
class acquireThread(threading.Thread):
def __init__(self, monitor, source, resolution, mask_file, track, track_type, dataFolder):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
threading.Thread.__init__(self)
self.monitor = monitor
self.keepGoing = False
self.verbose = True
self.track = track
outputFile = os.path.join(dataFolder, 'Monitor%02d.txt' % monitor)
self.mon = pv.Monitor()
self.mon.setSource(source, resolution)
self.mon.setTracking(True, track_type, mask_file, outputFile)
if self.verbose: print ( "Verbose2: Setting monitor %s with source %s and mask %s. Output to %s " % (monitor, source, os.path.split(mask_file)[1], os.path.split(outputFile)[1] ) )
def run(self, kbdint=False):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if kbdint:
while self.keepGoing:
try:
self.mon.GetImage()
except KeyboardInterrupt:
self.halt()
else:
while self.keepGoing:
self.mon.GetImage()
def doTrack(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.keepGoing = True
self.start()
def halt(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.keepGoing = False
if self.verbose: print ( "Verbose3: Stopping capture" )
class pvg_config(myConfig):
"""
Inheriting from myConfig
"""
def __init__(self, filename=None, temporary=False):
# print inspect.currentframe().f_back.f_locals['self'] # debug
# print(' inspect not working in pvg_config __init__') # debug
defaultOptions = { "Monitors" : [9, "Select the number of monitors connected to this machine"],
"Webcams" : [1, "Select the number of webcams connected to this machine"],
"ThumbnailSize" : ['320, 240', "Specify the size for the thumbnail previews"],
"FullSize" : ['640, 480', "Specify the size for the actual acquisition from the webcams.\nMake sure your webcam supports this definition"],
"FPS_preview" : [5, "Refresh frequency (FPS) of the thumbnails during preview.\nSelect a low rate for slow computers"],
"FPS_recording" : [5, "Actual refresh rate (FPS) during acquisition and processing"],
"Data_Folder" : ['', "Folder where the final data are saved"]
}
self.monitorProperties = ['sourceType', 'source', 'track', 'maskfile', 'trackType', 'isSDMonitor']
# print(self.monitorProperties)
myConfig.__init__(self, filename, temporary, defaultOptions)
def SetMonitor(self, monitor, *args):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
mn = 'Monitor%s' % monitor
for v, vn in zip( args, self.monitorProperties ):
self.SetValue(mn, vn, v)
def GetMonitor(self, monitor):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
mn = 'Monitor%s' % monitor
md = []
if self.config.has_section(mn):
for vn in self.monitorProperties:
md.append ( self.GetValue(mn, vn) )
return md
def HasMonitor(self, monitor):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
mn = 'Monitor%s' % monitor
return self.config.has_section(mn)
def getMonitorsData(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
return a list containing the monitors that we need to track
based on info found in configfile
"""
monitors = {}
ms = self.GetOption('Monitors')
resolution = self.GetOption('FullSize')
dataFolder = self.GetOption('Data_Folder')
for mon in range(0,ms):
if self.HasMonitor(mon):
_,source,track,mask_file,track_type,isSDMonitor = self.GetMonitor(mon)
monitors[mon] = {}
monitors[mon]['source'] = source
monitors[mon]['resolution'] = resolution
monitors[mon]['mask_file'] = mask_file
monitors[mon]['track_type'] = track_type
monitors[mon]['dataFolder'] = dataFolder
monitors[mon]['track'] = track
monitors[mon]['isSDMonitor'] = isSDMonitor
return monitors
class previewPanel(wx.Panel):
"""
A panel showing the video images.
Used for thumbnails
"""
def __init__(self, parent, size, keymode=True):
print inspect.currentframe().f_back.f_locals['self'] # debug
wx.Panel.__init__(self, parent, wx.ID_ANY, style=wx.WANTS_CHARS)
self.parent = parent
self.size = size
self.SetMinSize(self.size)
fps = options.GetOption('FPS_preview') or 25
self.interval = 1000/fps # fps determines refresh interval in ms
self.SetBackgroundColour('#A9A9A9')
self.sourceType = 0
self.source = ''
self.mon = None
self.track = False
self.isSDMonitor = False
self.trackType = 1
self.drawROI = True
self.timestamp = False
self.camera = None
self.resolution = None
self.recording = False
self.isPlaying = False
self.allowEditing = True
self.dragging = None # Set to True while dragging
self.startpoints = None # Set to (x,y) when mouse starts drag
self.track_window = None # Set to rect when the mouse drag finishes
self.selection = None
self.selROI = -1
self.polyPoints = []
self.keymode = keymode
self.ACTIONS = {
"a": [self.AutoMask, "Automatically create the mask"],
"c": [self.ClearLast, "Clear last selected area of interest"],
"t": [self.Calibrate, "Calibrate the mask after selecting two points distant 1cm from each other"],
"x": [self.ClearAll, "Clear all marked region of interest"],
"j": [self.SaveCurrentSelection, "Save last marked area of interest"],
"s": [self.SaveMask, "Save mask to file"],
"q": [self.Stop, "Close connection to camera"]
}
self.Bind( wx.EVT_LEFT_DOWN, self.onLeftDown )
self.Bind( wx.EVT_LEFT_UP, self.onLeftUp )
#self.Bind( wx.EVT_LEFT_DCLICK, self.AddPoint )
self.Bind( wx.EVT_LEFT_DCLICK, self.SaveCurrentSelection )
self.Bind( wx.EVT_MOTION, self.onMotion )
self.Bind( wx.EVT_RIGHT_DOWN, self.ClearLast )
#self.Bind( wx.EVT_MIDDLE_DOWN, self.SaveCurrentSelection )
if keymode:
self.Bind( wx.EVT_CHAR, self.onKeyPressed )
self.SetFocus()
def ClearAll(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
Clear all ROIs
"""
self.mon.delROI(-1)
def ClearLast(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
Cancel current drawing
"""
if self.allowEditing:
self.selection = None
self.polyPoints = []
if self.selROI >= 0:
self.mon.delROI(self.selROI)
self.selROI = -1
def SaveCurrentSelection(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
save current selection
"""
if self.allowEditing and self.selection:
self.mon.addROI(self.selection, 1)
self.selection = None
self.polyPoints = []
def AddPoint(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
Add point
"""
if self.allowEditing:
if len(self.polyPoints) == 4:
self.polyPoints = []
#This is to avoid selecting a neigh. area when drawing point
self.selection = None
self.selROI = -1
x = event.GetX()
y = event.GetY()
self.polyPoints.append( (x,y) )
def onLeftDown(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if self.allowEditing and self.mon:
x = event.GetX()
y = event.GetY()
r = self.mon.isPointInROI ( (x,y) )
if r < 0:
self.startpoints = (x, y)
else:
self.selection = self.mon.getROI(r)
self.selROI = r
def onLeftUp(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if self.allowEditing:
self.dragging = None
self.track_window = self.selection
if len(self.polyPoints) == 4:
self.selection = self.polyPoints
self.polyPoints = []
def onMotion(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if self.allowEditing:
x = event.GetX()
y = event.GetY()
self.dragging = event.Dragging()
if self.dragging:
xmin = min(x, self.startpoints[0])
ymin = min(y, self.startpoints[1])
xmax = max(x, self.startpoints[0])
ymax = max(y, self.startpoints[1])
x1, y1, x2, y2 = (xmin, ymin, xmax, ymax)
self.selection = (x1,y1), (x2,y1), (x2,y2), (x1, y2)
def prinKeyEventsHelp(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
for key in self.ACTIONS:
print "%s\t%s" % (key, self.ACTIONS[key][1])
def onKeyPressed(self, event):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
Regulates key pressing responses:
"""
key = chr(event.GetKeyCode())
if key == "g" and self.mon.writer: self.mon.grabMovie = not self.mon.grabMovie
if self.ACTIONS.has_key(key):
self.ACTIONS[key][0]()
def Calibrate(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if len(self.polyPoints) > 2:
print "You need only two points for calibration. I am going to use the first two"
if len(self.polyPoints) > 1:
pt1, pt2 = self.polyPoints[0], self.polyPoints[1]
r = self.mon.calibrate(pt1, pt2)
self.polyPoints = []
else:
print "You need at least two points for calibration."
print "%spixels = 1cm" % r
def AutoMask(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if len(self.polyPoints > 1):
pt1, pt2 = self.polyPoints[0], self.polyPoints[1]
self.mon.autoMask(pt1, pt2)
else:
print "Too few points to automask"
self.polyPoints = []
def SaveMask(self, event=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.mon.saveROIS()
def setMonitor(self, camera, resolution=None):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if not resolution: resolution = self.size
self.camera = camera
self.resolution = resolution
self.mon = pv.Monitor()
frame = cv.CreateMat(self.size[1], self.size[0], cv.CV_8UC3)
self.bmp = wx.BitmapFromBuffer(self.size[0], self.size[1], frame.tostring())
self.Bind(wx.EVT_PAINT, self.onPaint)
self.playTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onNextFrame)
def paintImg(self, img):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if img:
depth, channels = img.depth, img.nChannels
# print('common, ln 527, cv.cv_maketype')
datatype = cv.CV_MAKETYPE(depth, channels)
frame = cv.CreateMat(self.size[1], self.size[0], datatype)
cv.Resize(img, frame)
cv.CvtColor(frame, frame, cv.CV_BGR2RGB)
#cv.CvtColor(frame, frame, cv.CV_GRAY2RGB)
self.bmp.CopyFromBuffer(frame.tostring())
self.Refresh()
def onPaint(self, evt):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if self.bmp:
dc = wx.BufferedPaintDC(self)
#self.PrepareDC(dc)
dc.DrawBitmap(self.bmp, 0, 0, True)
evt.Skip()
def onNextFrame(self, evt):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
img = self.mon.GetImage(drawROIs = self.drawROI, selection=self.selection, crosses=self.polyPoints, timestamp=self.timestamp)
self.paintImg( img )
if evt: evt.Skip()
def Play(self, status=True, showROIs=True):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
if self.camera != None and self.resolution != None and not self.mon.hasSource():
self.mon.setSource(self.camera, self.resolution)
if self.mon:
self.drawROI = showROIs
self.isPlaying = status
if status:
self.playTimer.Start(self.interval)
else:
self.playTimer.Stop()
def Stop(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
self.Play(False)
self.mon.close()
def hasMonitor(self):
# print inspect.currentframe().f_back.f_locals['self'] # debug
"""
"""
return (self.mon != None)
#################
options = pvg_config(DEFAULT_CONFIG)