Ejemplo n.º 1
0
	def startThreads( self ):
		self.grabFrameOK = False
		
		self.listenerThread = SocketListener( self.requestQ, self.messageQ )
		error = self.listenerThread.test()
		if error:
			wx.MessageBox('Socket Error:\n\n{}\n\nIs another CrossMgrVideo or CrossMgrCamera running on this computer?'.format(error),
				"Socket Error",
				wx.OK | wx.ICON_ERROR
			)
			wx.Exit()
				
		self.writerThread = threading.Thread( target=PhotoWriter, args=(self.writerQ, self.messageQ, self.ftpQ) )
		self.writerThread.daemon = True
		
		self.renamerThread = threading.Thread( target=PhotoRenamer, args=(self.renamerQ, self.writerQ, self.messageQ) )
		self.renamerThread.daemon = True
		
		self.ftpThread = threading.Thread( target=FTPWriter, args=(self.ftpQ, self.messageQ) )
		self.ftpThread.daemon = True
		
		self.fcb = FrameCircBuf( int(self.bufferSecs * self.fps) )
		
		self.listenerThread.start()
		self.writerThread.start()
		self.renamerThread.start()
		self.ftpThread.start()
		
		self.grabFrameOK = True
		self.messageQ.put( ('threads', 'Successfully Launched') )
		return True
Ejemplo n.º 2
0
	def startThreads( self ):
		self.grabFrameOK = False
		
		self.listenerThread = threading.Thread( target=SocketListener, args=(self.listenerSocket, self.requestQ, self.messageQ, self.renamerQ) )
		self.listenerThread.daemon = True
		
		self.writerThread = threading.Thread( target=PhotoWriter, args=(self.writerQ, self.messageQ, self.ftpQ) )
		self.writerThread.daemon = True
		
		self.renamerThread = threading.Thread( target=PhotoRenamer, args=(self.renamerQ, self.writerQ, self.messageQ) )
		self.renamerThread.daemon = True
		
		self.ftpThread = threading.Thread( target=FTPWriter, args=(self.ftpQ, self.messageQ) )
		self.ftpThread.daemon = True
		
		self.fcb = FrameCircBuf( int(self.bufferSecs * self.fps) )
		
		self.listenerThread.start()
		self.writerThread.start()
		self.renamerThread.start()
		self.ftpThread.start()
		
		self.grabFrameOK = True
		self.messageQ.put( ('threads', 'Successfully Launched') )
		return True
Ejemplo n.º 3
0
	def __init__( self, camera, refTime = None, dirName = '.', fps = 25, bufferSeconds = 4.0 ):
		threading.Thread.__init__( self )
		self.daemon = True
		self.name = 'VideoBuffer'
		self.camera = camera
		self.refTime = refTime if refTime is not None else now()
		self.dirName = dirName
		self.fps = fps
		self.frameMax = int(fps * bufferSeconds)
		self.frameDelay = 1.0 / fps
		self.frameSaver = None
		self.fcb = FrameCircBuf()
		self.reset()
Ejemplo n.º 4
0
	def __init__( self, camera, refTime=None, dirName='.', fps=25, bufferSeconds=4.0, owner=None, burstMode=True ):
		self.camera = camera
		self.refTime = refTime if refTime is not None else now()
		self.dirName = dirName
		self.fps = fps
		self.frameMax = int(fps * bufferSeconds)
		
		self.frameDelay = 1.0 / fps
		self.frameDelayTimeDelta = timedelta(seconds=self.frameDelay)
		
		self.frameSaver = None
		self.fcb = FrameCircBuf()
		self.owner = owner			# Destination to send photos after they are taken.
		self.burstMode = burstMode
		
		self.timer = CallbackTimer( self.recordVideoFrame )
		self.reset()
Ejemplo n.º 5
0
def CamServer( qIn, pWriter, camInfo=None ):
	sendUpdates = {}
	tsSeen = set()
	camInfo = camInfo or {}
	backlog = []
	
	def pWriterSend( msg ):
		try:
			pWriter.send( msg )
		except MemoryError as e:
			six.print_( 'pWriterSend: ', e )
	
	while 1:
		with VideoCaptureManager(**camInfo) as cap:
			time.sleep( 0.25 )
			frameCount = 0
			inCapture = False
			doSnapshot = False
			tsSeen.clear()
			fcb = FrameCircBuf( int(camInfo.get('fps', 30) * bufferSeconds) )
			tsQuery = tsMax = now()
			keepCapturing = 1
			
			while keepCapturing:
				# Capture frame-by-frame
				try:
					ret, frame = cap.read()
				except KeyboardInterrupt:
					return
				
				ts = now()
				if not ret:
					break
				fcb.append( ts, frame )
				
				try:
					m = qIn.get_nowait()
					while 1:
						cmd = m['cmd']
						if cmd == 'query':
							if m['tStart'] > ts:
								# Reschedule future requests for later.
								Timer( (m['tStart'] - ts).total_seconds(), qIn.put, m ).start()
								continue
							
							if (ts - tsQuery).total_seconds() > bufferSeconds or len(tsSeen) > 5000:
								tsSeen.clear()
							tsQuery = ts

							times, frames = fcb.getTimeFrames( m['tStart'], m['tEnd'], tsSeen )
							backlog.extend( (t, f) for t, f in zip(times, frames) )
							
							if m['tEnd'] > tsMax:
								tsMax = m['tEnd']
						elif cmd == 'start_capture':
							if 'tStart' in m:
								times, frames = fcb.getTimeFrames( m['tStart'], ts, tsSeen )
								backlog.extend( (t, f) for t, f in zip(times, frames) )
								inCapture = True
						elif cmd == 'stop_capture':
							inCapture = False
						elif cmd == 'snapshot':
							doSnapshot = True
						elif cmd == 'send_update':
							sendUpdates[m['name']] = m['freq']
						elif cmd == 'cancel_update':
							if 'name' not in m:
								sendUpdates.clear()
							else:
								sendUpdates.pop(m['name'], None)
						elif cmd == 'cam_info':
							camInfo = m['info'] or {}
							keepCapturing = 0
							break
						elif cmd == 'terminate':
							pWriterSend( {'cmd':'terminate'} )
							return
						else:
							assert False, 'Unknown Command'
						m = qIn.get_nowait()						
				except Empty:
					pass
				
				if tsMax > ts or inCapture:
					backlog.append( (ts, frame) )
					tsSeen.add( ts )

				# Don't send too many frames at a time.  We don't want to overwhelm the pipe and lose frames.
				# Always ensure that the most recent frame is sent so any update requests can be satisfied with the last frame.
				if backlog:
					pWriterSend( { 'cmd':'response', 'ts_frames': backlog[-transmitFramesMax:] } )
						
				# Send update messages.  If there was a backlog, don't send the frame as we can use the last frame sent.
				updateFrame = None if backlog and backlog[-1][0] == ts else frame
				for name, f in six.iteritems(sendUpdates):
					#if frameCount % (f if backlog else 8) == 0:
					if frameCount % f == 0:
						pWriterSend( {'cmd':'update', 'name':name, 'frame':updateFrame} )
						updateFrame = None
				if doSnapshot:
					pWriterSend( {'cmd':'snapshot', 'ts':ts, 'frame':updateFrame} )
					doSnapshot = False
						
				del backlog[-transmitFramesMax:]
				frameCount += 1
Ejemplo n.º 6
0
class VideoBuffer( object ):
	
	def __init__( self, camera, refTime=None, dirName='.', fps=25, bufferSeconds=4.0, owner=None, burstMode=True ):
		self.camera = camera
		self.refTime = refTime if refTime is not None else now()
		self.dirName = dirName
		self.fps = fps
		self.frameMax = int(fps * bufferSeconds)
		
		self.frameDelay = 1.0 / fps
		self.frameDelayTimeDelta = timedelta(seconds=self.frameDelay)
		
		self.frameSaver = None
		self.fcb = FrameCircBuf()
		self.owner = owner			# Destination to send photos after they are taken.
		self.burstMode = burstMode
		
		self.timer = CallbackTimer( self.recordVideoFrame )
		self.reset()
	
	def setOwner( self, owner=None ):
		self.owner = owner
	
	def reset( self ):
		if self.frameSaver and self.frameSaver.is_alive():
			self.frameSaver.stop()
		
		if self.timer.IsRunning():
			self.timer.Stop()
		
		self.fcb.reset( self.frameMax )
		self.frameCount = 0
		
		self.frameSaver = FrameSaver()
		self.frameSaver.start()
		
		self.tFindLast = now() - timedelta( days=1 )
		
		self.lastSampleTime = now()
		self.sampleCount = 0
	
	def recordVideoFrame( self ):
		tNow = now()
		self.sampleCount += 1
		if not self.camera:
			return
			
		try:
			image = self.camera.getImage()
			self.fcb.append( tNow, image )
			if self.owner is not None:
				wx.PostEvent( self.owner, PhotoEvent(t=tNew, photo=image) )
		except Exception as e:
			logException( e, sys.exc_info() )

	def getFrameRate( self ):
		tNow = now()
		rate = self.sampleCount / (tNow -self.lastSampleTime).total_seconds()
		self.lastSampleTime = tNow
		self.sampleCount = 0
		return rate
	
	def start( self ):
		self.reset()
		milliseconds = int(self.frameDelay * 1000.0)
		self.timer.Start( milliseconds, oneShot=False )
	
	def stop( self ):
		if self.timer.IsRunning():
			self.timer.Stop()
		if self.frameSaver and self.frameSaver.is_alive():
			self.frameSaver.stop()
		self.frameSaver = None
		self.lastSampleTime = now()
			
	def takePhoto( self, bib, t ):
		tNow = now()

		tFind = self.refTime + timedelta( seconds = t + Model.race.advancePhotoMilliseconds / 1000.0 )
		if tFind > tNow:
			wx.CallLater( int(((tFind - tNow).total_seconds() + 0.1) * 1000.0), self.takePhoto, bib, t )
			return
		
		# If burst mode, check if there was another rider before within frameDelay seconds.
		# If so, also save the frame just before the given time.
		# Always save the closest frame after the given time.
		times, frames = self.fcb.findBeforeAfter(
							tFind,
							1, # 1 if tFind - self.tFindLast < self.frameDelayTimeDelta and self.burstMode else 0,
							1 )
		for i, frame in enumerate( frames ):
			t = (times[i]-self.refTime).total_seconds()
			self.frameSaver.save( GetFilename(bib, t, self.dirName, i), bib, t, frame )
		self.frameCount += len(frames)
		self.tFindLast = tFind
	
	def getT( self, i ):
		return self.fcb.getT(i)
		
	def getFrame( self, i ):
		return self.fcb.getFrame(i)
	
	def getTimeFrame( self, i ):
		return (self.fcb.getT(i), self.fcb.getFrame(i))
		
	def findBeforeAfter( self, t, before=0, after=1 ):
		''' Call the frame cyclic buffer from race time, not clock time. '''
		tFind = self.refTime + timedelta( seconds=t )
		times, frames = self.fcb.findBeforeAfter( tFind, before, after )
		return zip( times, frames )
		
	def __len__( self ):
		return self.frameMax
Ejemplo n.º 7
0
	def __init__( self, parent, id = wx.ID_ANY, title='', size=(1000,800) ):
		wx.Frame.__init__(self, parent, id, title, size=size)
		
		self.fps = 25
		self.frameDelay = 1.0 / self.fps
		self.bufferSecs = 10
		
		self.tFrameCount = self.tLaunch = self.tLast = now()
		self.frameCount = 0
		self.frameCountUpdate = self.fps * 2
		self.fpsActual = 0.0
		self.fpt = timedelta(seconds=0)
		
		self.fcb = FrameCircBuf( self.bufferSecs * self.fps )
		
		self.config = wx.Config(appName="CrossMgrCamera",
						vendorName="SmartCyclingSolutions",
						#style=wx.CONFIG_USE_LOCAL_FILE
		)
		
		self.requestQ = Queue()			# Select photos from photobuf.
		self.writerQ = Queue( 400 )		# Selected photos waiting to be written out.
		self.ftpQ = Queue()				# Photos waiting to be ftp'd.
		self.renamerQ = Queue()			# Photos waiting to be renamed and possibly ftp'd.
		self.messageQ = Queue()			# Collection point for all status/failure messages.
		
		self.SetBackgroundColour( wx.Colour(232,232,232) )
		
		mainSizer = wx.BoxSizer( wx.VERTICAL )
		
		self.primaryImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.beforeImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.afterImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.beforeAfterImages = [self.beforeImage, self.afterImage]
		
		#------------------------------------------------------------------------------------------------
		headerSizer = wx.BoxSizer( wx.HORIZONTAL )
		
		self.logo = Utils.GetPngBitmap('CrossMgrHeader.png')
		headerSizer.Add( wx.StaticBitmap(self, wx.ID_ANY, self.logo) )
		
		self.title = wx.StaticText(self, label='CrossMgr Camera\nVersion {}'.format(AppVerName.split()[1]), style=wx.ALIGN_RIGHT )
		self.title.SetFont( wx.Font( (0,28), wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL ) )
		headerSizer.Add( self.title, flag=wx.ALL, border=10 )
		
		#------------------------------------------------------------------------------
		self.cameraDeviceLabel = wx.StaticText(self, label='Camera Device:')
		self.cameraDevice = wx.StaticText( self )
		boldFont = self.cameraDevice.GetFont()
		boldFont.SetWeight( wx.BOLD )
		self.cameraDevice.SetFont( boldFont )
		self.cameraResolution = wx.StaticText( self )
		self.cameraResolution.SetFont( boldFont )
		bitmap = wx.Bitmap( clipboard_xpm )
		self.copyLogToClipboard = wx.BitmapButton( self, bitmap=bitmap )
		self.copyLogToClipboard.Bind( wx.EVT_BUTTON, self.onCopyLogToClipboard )
		self.reset = wx.Button( self, label="Reset Camera" )
		self.reset.Bind( wx.EVT_BUTTON, self.resetCamera )
		cameraDeviceSizer = wx.BoxSizer( wx.HORIZONTAL )
		cameraDeviceSizer.Add( self.cameraDeviceLabel, flag=wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT )
		cameraDeviceSizer.Add( self.cameraDevice, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.cameraResolution, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.copyLogToClipboard, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.reset, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=24 )

		#------------------------------------------------------------------------------
		self.targetProcessingTimeLabel = wx.StaticText(self, label='Target Frames:')
		self.targetProcessingTime = wx.StaticText(self, label=u'{:.3f}'.format(self.fps))
		self.targetProcessingTime.SetFont( boldFont )
		self.targetProcessingTimeUnit = wx.StaticText(self, label='per sec')
		
		self.framesPerSecondLabel = wx.StaticText(self, label='Actual Frames:')
		self.framesPerSecond = wx.StaticText(self, label='25.000')
		self.framesPerSecond.SetFont( boldFont )
		self.framesPerSecondUnit = wx.StaticText(self, label='per sec')
		
		self.availableMsPerFrameLabel = wx.StaticText(self, label='Available Time Per Frame:')
		self.availableMsPerFrame = wx.StaticText(self, label=u'{:.0f}'.format(1000.0*self.frameDelay))
		self.availableMsPerFrame.SetFont( boldFont )
		self.availableMsPerFrameUnit = wx.StaticText(self, label='ms')
		
		self.frameProcessingTimeLabel = wx.StaticText(self, label='Actual Frame Processing:')
		self.frameProcessingTime = wx.StaticText(self, label='20')
		self.frameProcessingTime.SetFont( boldFont )
		self.frameProcessingTimeUnit = wx.StaticText(self, label='ms')
		
		pfgs = wx.FlexGridSizer( rows=0, cols=6, vgap=4, hgap=8 )
		fRight = wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT
		fLeft = wx.ALIGN_CENTRE_VERTICAL
		
		#------------------- Row 1 ------------------------------
		pfgs.Add( self.targetProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.targetProcessingTime, flag=fRight )
		pfgs.Add( self.targetProcessingTimeUnit, flag=fLeft )
		pfgs.Add( self.availableMsPerFrameLabel, flag=fRight )
		pfgs.Add( self.availableMsPerFrame, flag=fRight )
		pfgs.Add( self.availableMsPerFrameUnit, flag=fLeft )
		
		#------------------- Row 2 ------------------------------
		pfgs.Add( self.framesPerSecondLabel, flag=fRight )
		pfgs.Add( self.framesPerSecond, flag=fRight )
		pfgs.Add( self.framesPerSecondUnit, flag=fLeft )
		pfgs.Add( self.frameProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.frameProcessingTime, flag=fRight )
		pfgs.Add( self.frameProcessingTimeUnit, flag=fLeft )

		statsSizer = wx.BoxSizer( wx.VERTICAL )
		statsSizer.Add( cameraDeviceSizer )
		statsSizer.Add( pfgs, flag=wx.TOP, border=8 )
		headerSizer.Add( statsSizer, flag=wx.ALL, border=4 )
		mainSizer.Add( headerSizer )
		
		self.messagesText = wx.TextCtrl( self, style=wx.TE_READONLY|wx.TE_MULTILINE|wx.HSCROLL, size=(350,imageHeight) )
		self.messageManager = MessageManager( self.messagesText )
		
		border = 2
		row1Sizer = wx.BoxSizer( wx.HORIZONTAL )
		row1Sizer.Add( self.primaryImage, flag=wx.ALL, border=border )
		row1Sizer.Add( self.messagesText, 1, flag=wx.TOP|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=border )
		mainSizer.Add( row1Sizer, 1, flag=wx.EXPAND )
		
		row2Sizer = wx.BoxSizer( wx.HORIZONTAL )
		row2Sizer.Add( self.beforeImage, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM, border=border )
		row2Sizer.Add( self.afterImage, flag=wx.RIGHT|wx.BOTTOM, border=border )
		mainSizer.Add( row2Sizer )
		
		#------------------------------------------------------------------------------------------------
		# Create a timer to update the frame loop.
		#
		self.timer = wx.Timer()
		self.timer.Bind( wx.EVT_TIMER, self.frameLoop )
		
		self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

		self.readOptions()
		self.SetSizerAndFit( mainSizer )
		
		# Start the message reporting thread so we can see what is going on.
		self.messageThread = threading.Thread( target=self.showMessages )
		self.messageThread.daemon = True
		self.messageThread.start()
		
		self.grabFrameOK = False
		
		for i in [self.primaryImage, self.beforeImage, self.afterImage]:
			i.SetTestImage()
		
		# Start the frame loop.
		delayAdjustment = 0.80 if 'win' in sys.platform else 0.98
		ms = int(1000 * self.frameDelay * delayAdjustment)
		self.timer.Start( ms, False )
Ejemplo n.º 8
0
class MainWin( wx.Frame ):
	def __init__( self, parent, id = wx.ID_ANY, title='', size=(1000,800) ):
		wx.Frame.__init__(self, parent, id, title, size=size)
		
		self.fps = 25
		self.frameDelay = 1.0 / self.fps
		self.bufferSecs = 10
		
		self.tFrameCount = self.tLaunch = self.tLast = now()
		self.frameCount = 0
		self.frameCountUpdate = self.fps * 2
		self.fpsActual = 0.0
		self.fpt = timedelta(seconds=0)
		
		self.fcb = FrameCircBuf( self.bufferSecs * self.fps )
		
		self.config = wx.Config(appName="CrossMgrCamera",
						vendorName="SmartCyclingSolutions",
						#style=wx.CONFIG_USE_LOCAL_FILE
		)
		
		self.requestQ = Queue()			# Select photos from photobuf.
		self.writerQ = Queue( 400 )		# Selected photos waiting to be written out.
		self.ftpQ = Queue()				# Photos waiting to be ftp'd.
		self.renamerQ = Queue()			# Photos waiting to be renamed and possibly ftp'd.
		self.messageQ = Queue()			# Collection point for all status/failure messages.
		
		self.SetBackgroundColour( wx.Colour(232,232,232) )
		
		mainSizer = wx.BoxSizer( wx.VERTICAL )
		
		self.primaryImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.beforeImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.afterImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.beforeAfterImages = [self.beforeImage, self.afterImage]
		
		#------------------------------------------------------------------------------------------------
		headerSizer = wx.BoxSizer( wx.HORIZONTAL )
		
		self.logo = Utils.GetPngBitmap('CrossMgrHeader.png')
		headerSizer.Add( wx.StaticBitmap(self, wx.ID_ANY, self.logo) )
		
		self.title = wx.StaticText(self, label='CrossMgr Camera\nVersion {}'.format(AppVerName.split()[1]), style=wx.ALIGN_RIGHT )
		self.title.SetFont( wx.Font( (0,28), wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL ) )
		headerSizer.Add( self.title, flag=wx.ALL, border=10 )
		
		#------------------------------------------------------------------------------
		self.cameraDeviceLabel = wx.StaticText(self, label='Camera Device:')
		self.cameraDevice = wx.StaticText( self )
		boldFont = self.cameraDevice.GetFont()
		boldFont.SetWeight( wx.BOLD )
		self.cameraDevice.SetFont( boldFont )
		self.cameraResolution = wx.StaticText( self )
		self.cameraResolution.SetFont( boldFont )
		bitmap = wx.Bitmap( clipboard_xpm )
		self.copyLogToClipboard = wx.BitmapButton( self, bitmap=bitmap )
		self.copyLogToClipboard.Bind( wx.EVT_BUTTON, self.onCopyLogToClipboard )
		self.reset = wx.Button( self, label="Reset Camera" )
		self.reset.Bind( wx.EVT_BUTTON, self.resetCamera )
		cameraDeviceSizer = wx.BoxSizer( wx.HORIZONTAL )
		cameraDeviceSizer.Add( self.cameraDeviceLabel, flag=wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT )
		cameraDeviceSizer.Add( self.cameraDevice, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.cameraResolution, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.copyLogToClipboard, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.reset, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=24 )

		#------------------------------------------------------------------------------
		self.targetProcessingTimeLabel = wx.StaticText(self, label='Target Frames:')
		self.targetProcessingTime = wx.StaticText(self, label=u'{:.3f}'.format(self.fps))
		self.targetProcessingTime.SetFont( boldFont )
		self.targetProcessingTimeUnit = wx.StaticText(self, label='per sec')
		
		self.framesPerSecondLabel = wx.StaticText(self, label='Actual Frames:')
		self.framesPerSecond = wx.StaticText(self, label='25.000')
		self.framesPerSecond.SetFont( boldFont )
		self.framesPerSecondUnit = wx.StaticText(self, label='per sec')
		
		self.availableMsPerFrameLabel = wx.StaticText(self, label='Available Time Per Frame:')
		self.availableMsPerFrame = wx.StaticText(self, label=u'{:.0f}'.format(1000.0*self.frameDelay))
		self.availableMsPerFrame.SetFont( boldFont )
		self.availableMsPerFrameUnit = wx.StaticText(self, label='ms')
		
		self.frameProcessingTimeLabel = wx.StaticText(self, label='Actual Frame Processing:')
		self.frameProcessingTime = wx.StaticText(self, label='20')
		self.frameProcessingTime.SetFont( boldFont )
		self.frameProcessingTimeUnit = wx.StaticText(self, label='ms')
		
		pfgs = wx.FlexGridSizer( rows=0, cols=6, vgap=4, hgap=8 )
		fRight = wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT
		fLeft = wx.ALIGN_CENTRE_VERTICAL
		
		#------------------- Row 1 ------------------------------
		pfgs.Add( self.targetProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.targetProcessingTime, flag=fRight )
		pfgs.Add( self.targetProcessingTimeUnit, flag=fLeft )
		pfgs.Add( self.availableMsPerFrameLabel, flag=fRight )
		pfgs.Add( self.availableMsPerFrame, flag=fRight )
		pfgs.Add( self.availableMsPerFrameUnit, flag=fLeft )
		
		#------------------- Row 2 ------------------------------
		pfgs.Add( self.framesPerSecondLabel, flag=fRight )
		pfgs.Add( self.framesPerSecond, flag=fRight )
		pfgs.Add( self.framesPerSecondUnit, flag=fLeft )
		pfgs.Add( self.frameProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.frameProcessingTime, flag=fRight )
		pfgs.Add( self.frameProcessingTimeUnit, flag=fLeft )

		statsSizer = wx.BoxSizer( wx.VERTICAL )
		statsSizer.Add( cameraDeviceSizer )
		statsSizer.Add( pfgs, flag=wx.TOP, border=8 )
		headerSizer.Add( statsSizer, flag=wx.ALL, border=4 )
		mainSizer.Add( headerSizer )
		
		self.messagesText = wx.TextCtrl( self, style=wx.TE_READONLY|wx.TE_MULTILINE|wx.HSCROLL, size=(350,imageHeight) )
		self.messageManager = MessageManager( self.messagesText )
		
		border = 2
		row1Sizer = wx.BoxSizer( wx.HORIZONTAL )
		row1Sizer.Add( self.primaryImage, flag=wx.ALL, border=border )
		row1Sizer.Add( self.messagesText, 1, flag=wx.TOP|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=border )
		mainSizer.Add( row1Sizer, 1, flag=wx.EXPAND )
		
		row2Sizer = wx.BoxSizer( wx.HORIZONTAL )
		row2Sizer.Add( self.beforeImage, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM, border=border )
		row2Sizer.Add( self.afterImage, flag=wx.RIGHT|wx.BOTTOM, border=border )
		mainSizer.Add( row2Sizer )
		
		#------------------------------------------------------------------------------------------------
		# Create a timer to update the frame loop.
		#
		self.timer = wx.Timer()
		self.timer.Bind( wx.EVT_TIMER, self.frameLoop )
		
		self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

		self.readOptions()
		self.SetSizerAndFit( mainSizer )
		
		# Start the message reporting thread so we can see what is going on.
		self.messageThread = threading.Thread( target=self.showMessages )
		self.messageThread.daemon = True
		self.messageThread.start()
		
		self.grabFrameOK = False
		
		for i in [self.primaryImage, self.beforeImage, self.afterImage]:
			i.SetTestImage()
		
		# Start the frame loop.
		delayAdjustment = 0.80 if 'win' in sys.platform else 0.98
		ms = int(1000 * self.frameDelay * delayAdjustment)
		self.timer.Start( ms, False )
		
	def Start( self ):
		self.messageQ.put( ('', '************************************************') )
		self.messageQ.put( ('started', now().strftime('%Y/%m/%d %H:%M:%S')) )
		self.startThreads()
		self.startCamera()
	
	def onCopyLogToClipboard( self, event ):
		with open( redirectFileName, 'r' ) as fp:
			logData = fp.read()
		dataObj = wx.TextDataObject()
		dataObj.SetText(logData)
		if wx.TheClipboard.Open():
			wx.TheClipboard.SetData( dataObj )
			wx.TheClipboard.Close()
			Utils.MessageOK(self, u'\n\n'.join( [_("Log file copied to clipboard."), _("You can now paste it into an email.")] ), _("Success") )
		else:
			Utils.MessageOK(self, _("Unable to open the clipboard."), _("Error"), wx.ICON_ERROR )
	
	def showMessages( self ):
		while 1:
			message = self.messageQ.get()
			assert len(message) == 2, 'Incorrect message length'
			cmd, info = message
			wx.CallAfter( self.messageManager.write, '{}:  {}'.format(cmd, info) if cmd else info )
		
	def startCamera( self ):
		self.camera = None
		try:
			self.camera = Device( max(self.getCameraDeviceNum(), 0) )
		except Exception as e:
			self.messageQ.put( ('camera', 'Error: {}'.format(e)) )
			return False
		
		try:
			self.camera.set_resolution( *self.getCameraResolution() )
			self.messageQ.put( ('camera', '{}x{} Supported'.format(*self.getCameraResolution())) )
		except Exception as e:
			self.messageQ.put( ('camera', '{}x{} Unsupported Resolution'.format(*self.getCameraResolution())) )
			
		self.messageQ.put( ('camera', 'Successfully Connected: Device: {}'.format(self.getCameraDeviceNum()) ) )
		for i in self.beforeAfterImages:
			i.SetTestImage()
		return True
	
	def startThreads( self ):
		self.grabFrameOK = False
		
		self.listenerThread = SocketListener( self.requestQ, self.messageQ )
		error = self.listenerThread.test()
		if error:
			wx.MessageBox('Socket Error:\n\n{}\n\nIs another CrossMgrVideo or CrossMgrCamera running on this computer?'.format(error),
				"Socket Error",
				wx.OK | wx.ICON_ERROR
			)
			wx.Exit()
				
		self.writerThread = threading.Thread( target=PhotoWriter, args=(self.writerQ, self.messageQ, self.ftpQ) )
		self.writerThread.daemon = True
		
		self.renamerThread = threading.Thread( target=PhotoRenamer, args=(self.renamerQ, self.writerQ, self.messageQ) )
		self.renamerThread.daemon = True
		
		self.ftpThread = threading.Thread( target=FTPWriter, args=(self.ftpQ, self.messageQ) )
		self.ftpThread.daemon = True
		
		self.fcb = FrameCircBuf( int(self.bufferSecs * self.fps) )
		
		self.listenerThread.start()
		self.writerThread.start()
		self.renamerThread.start()
		self.ftpThread.start()
		
		self.grabFrameOK = True
		self.messageQ.put( ('threads', 'Successfully Launched') )
		return True
	
	def frameLoop( self, event=None ):
		if not self.grabFrameOK:
			return
			
		self.frameCount += 1
		tNow = now()
		
		# Compute frame rate statistics.
		if self.frameCount == self.frameCountUpdate:
			self.fpsActual = self.frameCount / (tNow - self.tFrameCount).total_seconds()
			self.frameCount = 0
			self.tFrameCount = tNow
			self.framesPerSecond.SetLabel( u'{:.3f}'.format(self.fpsActual) )
			self.availableMsPerFrame.SetLabel( u'{:.0f}'.format(1000.0/self.fpsActual) )
			self.frameProcessingTime.SetLabel( u'{:.0f}'.format(1000.0*self.fpt.total_seconds()) )
		
		try:
			image = self.camera.getImage()
		except Exception as e:
			self.messageQ.put( ('error', 'Webcam Failure: {}'.format(e) ) )
			self.grabFrameOK = False
			return
		
		if not image:
			return
		
		image = AddPhotoHeader( PilImageToWxImage(image),
			time=tNow, raceSeconds=(tNow - self.tLaunch).total_seconds(),
			bib=999,
			firstNameTxt=u'Firstname', lastNameTxt=u'LASTNAME', teamTxt=u'Team',
			raceNameTxt='Racename'
		)
		self.fcb.append( tNow, image )
		wx.CallAfter( self.primaryImage.SetImage, image )
		
		# Process any save messages
		while 1:
			try:
				message = self.requestQ.get(False)
			except Empty:
				break
			
			tSearch = message['time']
			advanceSeconds = message.get('advanceSeconds', 0.0)
			if advanceSeconds:
				tSearch += timedelta(seconds=advanceSeconds)
			times, frames = self.fcb.findBeforeAfter( tSearch, 1, 1 )
			if not frames:
				self.messageQ.put( ('error', 'No photos for {} at {}'.format(message.get('bib', None), message['time'].isoformat()) ) )
				
			lastImage = None
			for i, f in enumerate(frames):
				
				fname = GetPhotoFName( message['dirName'], message.get('bib',None), message.get('raceSeconds',None), i, times[i] )
				image = AddPhotoHeader(
					f,
					message.get('bib', None),
					message.get('time', None),
					message.get('raceSeconds', None),
					message.get('firstName',u''),
					message.get('lastName',u''),
					message.get('team',u''),
					message.get('raceName',u'')
				)
				
				#if lastImage is not None and image.GetData() == lastImage.GetData():
				#	self.messageQ.put( ('duplicate', '"{}"'.format(os.path.basename(fname))) )
				#	continue
				
				wx.CallAfter( self.beforeAfterImages[i].SetImage, image )
				
				SaveImage( fname, image, message.get('ftpInfo', None), self.messageQ, self.writerQ )
				lastImage = image
				
			if (now() - tNow).total_seconds() > self.frameDelay / 2.0:
				break
				
		self.fpt = now() - tNow
		self.tLast = tNow
	
	def shutdown( self ):
		# Ensure that all images in the queue are saved.
		self.grabFrameOK = False
		if hasattr(self, 'writerThread'):
			self.writerQ.put( ('terminate', ) )
			self.writerThread.join()
	
	def resetCamera( self, event ):
		dlg = ConfigDialog( self, self.getCameraDeviceNum(), self.getCameraResolution() )
		ret = dlg.ShowModal()
		cameraDeviceNum = dlg.GetCameraDeviceNum()
		cameraResolution = dlg.GetCameraResolution()
		dlg.Destroy()
		if ret != wx.ID_OK:
			return False
		
		self.setCameraDeviceNum( cameraDeviceNum )
		self.setCameraResolution( *cameraResolution )
		self.writeOptions()

		self.grabFrameOK = self.startCamera()
		return True
		
	def setCameraDeviceNum( self, num ):
		self.cameraDevice.SetLabel( unicode(num) )
		
	def setCameraResolution( self, width, height ):
		self.cameraResolution.SetLabel( u'{}x{}'.format(width, height) )
			
	def getCameraDeviceNum( self ):
		return int(self.cameraDevice.GetLabel())
		
	def getCameraResolution( self ):
		try:
			resolution = [int(v) for v in self.cameraResolution.GetLabel().split('x')]
			return resolution[0], resolution[1]
		except:
			return 640, 400
		
	def onCloseWindow( self, event ):
		self.shutdown()
		wx.Exit()
		
	def writeOptions( self ):
		self.config.Write( 'CameraDevice', self.cameraDevice.GetLabel() )
		self.config.Write( 'CameraResolution', self.cameraResolution.GetLabel() )
		self.config.Flush()
	
	def readOptions( self ):
		self.cameraDevice.SetLabel( self.config.Read('CameraDevice', u'0') )
		self.cameraResolution.SetLabel( self.config.Read('CameraResolution', u'640x480') )
Ejemplo n.º 9
0
	def __init__( self, parent, id = wx.ID_ANY, title='', size=(1000,800) ):
		wx.Frame.__init__(self, parent, id, title, size=size)
		
		self.db = Database()
		
		self.fps = 25
		self.frameDelay = 1.0 / self.fps
		self.bufferSecs = 10
		self.xFinish = None
		
		self.tFrameCount = self.tLaunch = self.tLast = now()
		self.frameCount = 0
		self.frameCountUpdate = int(self.fps * 2)
		self.fpsActual = 0.0
		self.fpt = timedelta(seconds=0)
		self.iTriggerSelect = None
		self.triggerInfo = None
		
		self.captureTimer = wx.CallLater( 10, self.stopCapture )
		
		self.fcb = FrameCircBuf( self.bufferSecs * self.fps )
		
		self.config = wx.Config(appName="CrossMgrVideo",
						vendorName="SmartCyclingSolutions",
						style=wx.CONFIG_USE_LOCAL_FILE)
		
		self.requestQ = Queue()		# Select photos from photobuf.
		self.dbWriterQ = Queue()	# Photos waiting to be renamed and possibly ftp'd.
		self.dbReaderQ = Queue()	# Photos read as requested from user.
		self.messageQ = Queue()		# Collection point for all status/failure messages.
		
		self.SetBackgroundColour( wx.Colour(232,232,232) )
		
		mainSizer = wx.BoxSizer( wx.VERTICAL )
		
		#------------------------------------------------------------------------------------------------
		headerSizer = wx.BoxSizer( wx.HORIZONTAL )
		
		self.logo = Utils.GetPngBitmap('CrossMgrHeader.png')
		headerSizer.Add( wx.StaticBitmap(self, bitmap=self.logo) )
		
		self.title = wx.StaticText(self, label='CrossMgr Video\nVersion {}'.format(AppVerName.split()[1]), style=wx.ALIGN_RIGHT )
		self.title.SetFont( wx.FontFromPixelSize( wx.Size(0,28), wx.FONTFAMILY_SWISS, wx.NORMAL, wx.FONTWEIGHT_NORMAL ) )
		headerSizer.Add( self.title, flag=wx.ALL, border=10 )
		
		#------------------------------------------------------------------------------
		self.cameraDeviceLabel = wx.StaticText(self, label='Camera Device:')
		self.cameraDevice = wx.StaticText( self )
		boldFont = self.cameraDevice.GetFont()
		boldFont.SetWeight( wx.BOLD )
		self.cameraDevice.SetFont( boldFont )
		self.reset = wx.Button( self, label="Reset Camera" )
		self.reset.Bind( wx.EVT_BUTTON, self.resetCamera )
		
		self.manage = wx.Button( self, label="Manage Database" )
		self.manage.Bind( wx.EVT_BUTTON, self.manageDatabase )
		
		self.test = wx.Button( self, label="Test" )
		self.test.Bind( wx.EVT_BUTTON, self.onTest )
		
		cameraDeviceSizer = wx.BoxSizer( wx.HORIZONTAL )
		cameraDeviceSizer.Add( self.cameraDeviceLabel, flag=wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT )
		cameraDeviceSizer.Add( self.cameraDevice, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.reset, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.manage, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=16 )
		cameraDeviceSizer.Add( self.test, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=16 )

		#------------------------------------------------------------------------------
		self.targetProcessingTimeLabel = wx.StaticText(self, label='Target Frames:')
		self.targetProcessingTime = wx.StaticText(self, label=u'{:.3f}'.format(self.fps))
		self.targetProcessingTime.SetFont( boldFont )
		self.targetProcessingTimeUnit = wx.StaticText(self, label='/ sec')
		
		self.framesPerSecondLabel = wx.StaticText(self, label='Actual Frames:')
		self.framesPerSecond = wx.StaticText(self, label='25.000')
		self.framesPerSecond.SetFont( boldFont )
		self.framesPerSecondUnit = wx.StaticText(self, label='/ sec')
		
		self.availableMsPerFrameLabel = wx.StaticText(self, label='Available Time Per Frame:')
		self.availableMsPerFrame = wx.StaticText(self, label=u'{:.0f}'.format(1000.0*self.frameDelay))
		self.availableMsPerFrame.SetFont( boldFont )
		self.availableMsPerFrameUnit = wx.StaticText(self, label='ms')
		
		self.frameProcessingTimeLabel = wx.StaticText(self, label='Frame Processing Time:')
		self.frameProcessingTime = wx.StaticText(self, label='20')
		self.frameProcessingTime.SetFont( boldFont )
		self.frameProcessingTimeUnit = wx.StaticText(self, label='ms')
		
		pfgs = wx.FlexGridSizer( rows=0, cols=6, vgap=4, hgap=8 )
		fRight = wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT
		fLeft = wx.ALIGN_CENTRE_VERTICAL
		
		#------------------- Row 1 ------------------------------
		pfgs.Add( self.targetProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.targetProcessingTime, flag=fRight )
		pfgs.Add( self.targetProcessingTimeUnit, flag=fLeft )
		pfgs.Add( self.availableMsPerFrameLabel, flag=fRight )
		pfgs.Add( self.availableMsPerFrame, flag=fRight )
		pfgs.Add( self.availableMsPerFrameUnit, flag=fLeft )
		
		#------------------- Row 2 ------------------------------
		pfgs.Add( self.framesPerSecondLabel, flag=fRight )
		pfgs.Add( self.framesPerSecond, flag=fRight )
		pfgs.Add( self.framesPerSecondUnit, flag=fLeft )
		pfgs.Add( self.frameProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.frameProcessingTime, flag=fRight )
		pfgs.Add( self.frameProcessingTimeUnit, flag=fLeft )

		statsSizer = wx.BoxSizer( wx.VERTICAL )
		statsSizer.Add( cameraDeviceSizer )
		statsSizer.Add( pfgs, flag=wx.TOP, border=8 )
		headerSizer.Add( statsSizer, flag=wx.ALL, border=4 )
		
		mainSizer.Add( headerSizer )
		
		#------------------------------------------------------------------------------------------------
		self.finishStrip = FinishStripPanel( self, size=(-1,wx.GetDisplaySize()[1]//2) )
		self.finishStrip.finish.Bind( wx.EVT_RIGHT_DOWN, self.onRightClick )
		
		self.primaryImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.primaryImage.SetTestImage()
		
		hsDate = wx.BoxSizer( wx.HORIZONTAL )
		hsDate.Add( wx.StaticText(self, label='Show Triggers for'), flag=wx.ALIGN_CENTER_VERTICAL )
		tQuery = now()
		self.date = wx.DatePickerCtrl(
			self,
			dt=wx.DateTimeFromDMY( tQuery.day, tQuery.month-1, tQuery.year ),
			style=wx.DP_DROPDOWN|wx.DP_SHOWCENTURY
		)
		self.date.Bind( wx.EVT_DATE_CHANGED, self.onQueryDateChanged )
		hsDate.Add( self.date, flag=wx.LEFT, border=2 )
		
		hsDate.Add( wx.StaticText(self, label='Filter by Bib'), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT, border=12 )
		self.bib = wx.lib.intctrl.IntCtrl( self, style=wx.TE_PROCESS_ENTER, size=(64,-1), min=1, allow_none=True, value=None )
		self.bib.Bind( wx.EVT_TEXT_ENTER, self.onQueryBibChanged )
		hsDate.Add( self.bib, flag=wx.LEFT, border=2 )
		
		self.tsQueryLower = datetime(tQuery.year, tQuery.month, tQuery.day)
		self.tsQueryUpper = self.tsQueryLower + timedelta(days=1)
		self.bibQuery = None
		
		self.triggerList = AutoWidthListCtrl( self, style=wx.LC_REPORT|wx.BORDER_SUNKEN|wx.LC_SORT_ASCENDING )
		
		self.il = wx.ImageList(16, 16)
		self.sm_check = self.il.Add( Utils.GetPngBitmap('check_icon.png'))
		self.sm_close = self.il.Add( Utils.GetPngBitmap('flame_icon.png'))
		self.sm_up = self.il.Add( Utils.GetPngBitmap('SmallUpArrow.png'))
		self.sm_dn = self.il.Add( Utils.GetPngBitmap('SmallDownArrow.png'))
		self.triggerList.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
		
		headers = ['Time', 'Bib', 'Name', 'Team', 'Wave']
		for i, h in enumerate(headers):
			self.triggerList.InsertColumn(i, h, wx.LIST_FORMAT_RIGHT if h == 'Bib' else wx.LIST_FORMAT_LEFT)
		self.itemDataMap = {}
		
		self.triggerList.Bind( wx.EVT_LIST_ITEM_SELECTED, self.onTriggerSelected )
		
		self.messagesText = wx.TextCtrl( self, style=wx.TE_READONLY|wx.TE_MULTILINE|wx.HSCROLL, size=(250,-1) )
		self.messageManager = MessageManager( self.messagesText )
		
		vsTriggers = wx.BoxSizer( wx.VERTICAL )
		vsTriggers.Add( hsDate )
		vsTriggers.Add( self.triggerList, 1, flag=wx.EXPAND|wx.TOP, border=2)
		
		#------------------------------------------------------------------------------------------------
		mainSizer.Add( self.finishStrip, 1, flag=wx.EXPAND )
		
		border = 2
		row1Sizer = wx.BoxSizer( wx.HORIZONTAL )
		row1Sizer.Add( self.primaryImage, flag=wx.ALL, border=border )
		row1Sizer.Add( vsTriggers, 1, flag=wx.TOP|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=border )
		row1Sizer.Add( self.messagesText, flag=wx.TOP|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=border )
		mainSizer.Add( row1Sizer, flag=wx.EXPAND )
		
		#------------------------------------------------------------------------------------------------
		# Create a timer to update the frame loop.
		#
		self.timer = wx.Timer()
		self.timer.Bind( wx.EVT_TIMER, self.frameLoop )
		
		self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

		self.readOptions()
		self.SetSizerAndFit( mainSizer )
		
		# Start the message reporting thread so we can see what is going on.
		self.messageThread = threading.Thread( target=self.showMessages )
		self.messageThread.daemon = True
		self.messageThread.start()
		
		self.grabFrameOK = False
		self.tsMax = None
		
		# Start the frame loop.
		delayAdjustment = 0.80 if 'win' in sys.platform else 0.98
		ms = int(1000 * self.frameDelay * delayAdjustment)
		self.timer.Start( ms, False )
		
		wx.CallLater( 300, self.refreshTriggers )
Ejemplo n.º 10
0
class MainWin( wx.Frame ):
	def __init__( self, parent, id = wx.ID_ANY, title='', size=(1000,800) ):
		wx.Frame.__init__(self, parent, id, title, size=size)
		
		self.db = Database()
		
		self.fps = 25
		self.frameDelay = 1.0 / self.fps
		self.bufferSecs = 10
		self.xFinish = None
		
		self.tFrameCount = self.tLaunch = self.tLast = now()
		self.frameCount = 0
		self.frameCountUpdate = int(self.fps * 2)
		self.fpsActual = 0.0
		self.fpt = timedelta(seconds=0)
		self.iTriggerSelect = None
		self.triggerInfo = None
		
		self.captureTimer = wx.CallLater( 10, self.stopCapture )
		
		self.fcb = FrameCircBuf( self.bufferSecs * self.fps )
		
		self.config = wx.Config(appName="CrossMgrVideo",
						vendorName="SmartCyclingSolutions",
						style=wx.CONFIG_USE_LOCAL_FILE)
		
		self.requestQ = Queue()		# Select photos from photobuf.
		self.dbWriterQ = Queue()	# Photos waiting to be renamed and possibly ftp'd.
		self.dbReaderQ = Queue()	# Photos read as requested from user.
		self.messageQ = Queue()		# Collection point for all status/failure messages.
		
		self.SetBackgroundColour( wx.Colour(232,232,232) )
		
		mainSizer = wx.BoxSizer( wx.VERTICAL )
		
		#------------------------------------------------------------------------------------------------
		headerSizer = wx.BoxSizer( wx.HORIZONTAL )
		
		self.logo = Utils.GetPngBitmap('CrossMgrHeader.png')
		headerSizer.Add( wx.StaticBitmap(self, bitmap=self.logo) )
		
		self.title = wx.StaticText(self, label='CrossMgr Video\nVersion {}'.format(AppVerName.split()[1]), style=wx.ALIGN_RIGHT )
		self.title.SetFont( wx.FontFromPixelSize( wx.Size(0,28), wx.FONTFAMILY_SWISS, wx.NORMAL, wx.FONTWEIGHT_NORMAL ) )
		headerSizer.Add( self.title, flag=wx.ALL, border=10 )
		
		#------------------------------------------------------------------------------
		self.cameraDeviceLabel = wx.StaticText(self, label='Camera Device:')
		self.cameraDevice = wx.StaticText( self )
		boldFont = self.cameraDevice.GetFont()
		boldFont.SetWeight( wx.BOLD )
		self.cameraDevice.SetFont( boldFont )
		self.reset = wx.Button( self, label="Reset Camera" )
		self.reset.Bind( wx.EVT_BUTTON, self.resetCamera )
		
		self.manage = wx.Button( self, label="Manage Database" )
		self.manage.Bind( wx.EVT_BUTTON, self.manageDatabase )
		
		self.test = wx.Button( self, label="Test" )
		self.test.Bind( wx.EVT_BUTTON, self.onTest )
		
		cameraDeviceSizer = wx.BoxSizer( wx.HORIZONTAL )
		cameraDeviceSizer.Add( self.cameraDeviceLabel, flag=wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT )
		cameraDeviceSizer.Add( self.cameraDevice, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.reset, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=8 )
		cameraDeviceSizer.Add( self.manage, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=16 )
		cameraDeviceSizer.Add( self.test, flag=wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border=16 )

		#------------------------------------------------------------------------------
		self.targetProcessingTimeLabel = wx.StaticText(self, label='Target Frames:')
		self.targetProcessingTime = wx.StaticText(self, label=u'{:.3f}'.format(self.fps))
		self.targetProcessingTime.SetFont( boldFont )
		self.targetProcessingTimeUnit = wx.StaticText(self, label='/ sec')
		
		self.framesPerSecondLabel = wx.StaticText(self, label='Actual Frames:')
		self.framesPerSecond = wx.StaticText(self, label='25.000')
		self.framesPerSecond.SetFont( boldFont )
		self.framesPerSecondUnit = wx.StaticText(self, label='/ sec')
		
		self.availableMsPerFrameLabel = wx.StaticText(self, label='Available Time Per Frame:')
		self.availableMsPerFrame = wx.StaticText(self, label=u'{:.0f}'.format(1000.0*self.frameDelay))
		self.availableMsPerFrame.SetFont( boldFont )
		self.availableMsPerFrameUnit = wx.StaticText(self, label='ms')
		
		self.frameProcessingTimeLabel = wx.StaticText(self, label='Frame Processing Time:')
		self.frameProcessingTime = wx.StaticText(self, label='20')
		self.frameProcessingTime.SetFont( boldFont )
		self.frameProcessingTimeUnit = wx.StaticText(self, label='ms')
		
		pfgs = wx.FlexGridSizer( rows=0, cols=6, vgap=4, hgap=8 )
		fRight = wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_RIGHT
		fLeft = wx.ALIGN_CENTRE_VERTICAL
		
		#------------------- Row 1 ------------------------------
		pfgs.Add( self.targetProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.targetProcessingTime, flag=fRight )
		pfgs.Add( self.targetProcessingTimeUnit, flag=fLeft )
		pfgs.Add( self.availableMsPerFrameLabel, flag=fRight )
		pfgs.Add( self.availableMsPerFrame, flag=fRight )
		pfgs.Add( self.availableMsPerFrameUnit, flag=fLeft )
		
		#------------------- Row 2 ------------------------------
		pfgs.Add( self.framesPerSecondLabel, flag=fRight )
		pfgs.Add( self.framesPerSecond, flag=fRight )
		pfgs.Add( self.framesPerSecondUnit, flag=fLeft )
		pfgs.Add( self.frameProcessingTimeLabel, flag=fRight )
		pfgs.Add( self.frameProcessingTime, flag=fRight )
		pfgs.Add( self.frameProcessingTimeUnit, flag=fLeft )

		statsSizer = wx.BoxSizer( wx.VERTICAL )
		statsSizer.Add( cameraDeviceSizer )
		statsSizer.Add( pfgs, flag=wx.TOP, border=8 )
		headerSizer.Add( statsSizer, flag=wx.ALL, border=4 )
		
		mainSizer.Add( headerSizer )
		
		#------------------------------------------------------------------------------------------------
		self.finishStrip = FinishStripPanel( self, size=(-1,wx.GetDisplaySize()[1]//2) )
		self.finishStrip.finish.Bind( wx.EVT_RIGHT_DOWN, self.onRightClick )
		
		self.primaryImage = ScaledImage( self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight) )
		self.primaryImage.SetTestImage()
		
		hsDate = wx.BoxSizer( wx.HORIZONTAL )
		hsDate.Add( wx.StaticText(self, label='Show Triggers for'), flag=wx.ALIGN_CENTER_VERTICAL )
		tQuery = now()
		self.date = wx.DatePickerCtrl(
			self,
			dt=wx.DateTimeFromDMY( tQuery.day, tQuery.month-1, tQuery.year ),
			style=wx.DP_DROPDOWN|wx.DP_SHOWCENTURY
		)
		self.date.Bind( wx.EVT_DATE_CHANGED, self.onQueryDateChanged )
		hsDate.Add( self.date, flag=wx.LEFT, border=2 )
		
		hsDate.Add( wx.StaticText(self, label='Filter by Bib'), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT, border=12 )
		self.bib = wx.lib.intctrl.IntCtrl( self, style=wx.TE_PROCESS_ENTER, size=(64,-1), min=1, allow_none=True, value=None )
		self.bib.Bind( wx.EVT_TEXT_ENTER, self.onQueryBibChanged )
		hsDate.Add( self.bib, flag=wx.LEFT, border=2 )
		
		self.tsQueryLower = datetime(tQuery.year, tQuery.month, tQuery.day)
		self.tsQueryUpper = self.tsQueryLower + timedelta(days=1)
		self.bibQuery = None
		
		self.triggerList = AutoWidthListCtrl( self, style=wx.LC_REPORT|wx.BORDER_SUNKEN|wx.LC_SORT_ASCENDING )
		
		self.il = wx.ImageList(16, 16)
		self.sm_check = self.il.Add( Utils.GetPngBitmap('check_icon.png'))
		self.sm_close = self.il.Add( Utils.GetPngBitmap('flame_icon.png'))
		self.sm_up = self.il.Add( Utils.GetPngBitmap('SmallUpArrow.png'))
		self.sm_dn = self.il.Add( Utils.GetPngBitmap('SmallDownArrow.png'))
		self.triggerList.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
		
		headers = ['Time', 'Bib', 'Name', 'Team', 'Wave']
		for i, h in enumerate(headers):
			self.triggerList.InsertColumn(i, h, wx.LIST_FORMAT_RIGHT if h == 'Bib' else wx.LIST_FORMAT_LEFT)
		self.itemDataMap = {}
		
		self.triggerList.Bind( wx.EVT_LIST_ITEM_SELECTED, self.onTriggerSelected )
		
		self.messagesText = wx.TextCtrl( self, style=wx.TE_READONLY|wx.TE_MULTILINE|wx.HSCROLL, size=(250,-1) )
		self.messageManager = MessageManager( self.messagesText )
		
		vsTriggers = wx.BoxSizer( wx.VERTICAL )
		vsTriggers.Add( hsDate )
		vsTriggers.Add( self.triggerList, 1, flag=wx.EXPAND|wx.TOP, border=2)
		
		#------------------------------------------------------------------------------------------------
		mainSizer.Add( self.finishStrip, 1, flag=wx.EXPAND )
		
		border = 2
		row1Sizer = wx.BoxSizer( wx.HORIZONTAL )
		row1Sizer.Add( self.primaryImage, flag=wx.ALL, border=border )
		row1Sizer.Add( vsTriggers, 1, flag=wx.TOP|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=border )
		row1Sizer.Add( self.messagesText, flag=wx.TOP|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=border )
		mainSizer.Add( row1Sizer, flag=wx.EXPAND )
		
		#------------------------------------------------------------------------------------------------
		# Create a timer to update the frame loop.
		#
		self.timer = wx.Timer()
		self.timer.Bind( wx.EVT_TIMER, self.frameLoop )
		
		self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

		self.readOptions()
		self.SetSizerAndFit( mainSizer )
		
		# Start the message reporting thread so we can see what is going on.
		self.messageThread = threading.Thread( target=self.showMessages )
		self.messageThread.daemon = True
		self.messageThread.start()
		
		self.grabFrameOK = False
		self.tsMax = None
		
		# Start the frame loop.
		delayAdjustment = 0.80 if 'win' in sys.platform else 0.98
		ms = int(1000 * self.frameDelay * delayAdjustment)
		self.timer.Start( ms, False )
		
		wx.CallLater( 300, self.refreshTriggers )
	
	def onQueryDateChanged( self, event ):
		v = self.date.GetValue()
		self.tsQueryLower = datetime( v.GetYear(), v.GetMonth() + 1, v.GetDay() )
		self.tsQueryUpper = self.tsQueryLower + timedelta( days=1 )
		self.refreshTriggers( True )
	
	def onQueryBibChanged( self, event ):
		self.bibQuery = self.bib.GetValue()
		self.refreshTriggers( True )
	
	def GetListCtrl( self ):
		return self.triggerList
	
	def GetSortImages(self):
		return (self.sm_dn, self.sm_up)
	
	def getItemData( self, i ):
		data = self.triggerList.GetItemData( i )
		return self.itemDataMap[data]
	
	def refreshTriggers( self, replace=False ):
		tNow = now()
		self.lastTriggerRefresh = tNow
		
		if replace:
			tsLower = self.tsQueryLower
			tsUpper = self.tsQueryUpper
			self.triggerList.DeleteAllItems()
			self.itemDataMap = {}
			self.tsMax = None
			self.iTriggerSelect = None
			self.triggerInfo = {}
		else:
			tsLower = (self.tsMax or datetime(tNow.year, tNow.month, tNow.day)) + timedelta(seconds=0.00001)
			tsUpper = tsLower + timedelta(days=1)

		triggers = self.db.getTriggers( tsLower, tsUpper, self.bibQuery )
		if not triggers:
			return
			
		tsPrev = (self.tsMax or datetime(2000,1,1))
		self.tsMax = triggers[-1][0]
		
		for i, (ts,bib,first_name,last_name,team,wave,race_name) in enumerate(triggers):
			closeFinish = ((ts-tsPrev).total_seconds() < 0.3)
			row = self.triggerList.InsertImageStringItem( sys.maxint, ts.strftime('%H:%M:%S.%f')[:-3], self.sm_close if closeFinish else self.sm_check )
			if closeFinish and row > 0:
				self.triggerList.SetItemImage( row-1, self.sm_close )
			self.triggerList.SetStringItem( row, 1, u'{:>6}'.format(bib) )
			name = u', '.join( n for n in (last_name, first_name) if n )
			self.triggerList.SetStringItem( row, 2, name )
			self.triggerList.SetStringItem( row, 3, team )
			self.triggerList.SetStringItem( row, 4, wave )
			
			self.triggerList.SetItemData( row, row )
			self.itemDataMap[row] = (ts,bib,name,team,wave,race_name,first_name,last_name)
			tsPrev = ts
			
		for i in xrange(5):
			self.triggerList.SetColumnWidth(i, wx.LIST_AUTOSIZE)

		self.triggerList.EnsureVisible( self.triggerList.GetItemCount() - 1 )

	def Start( self ):
		self.messageQ.put( ('', '************************************************') )
		self.messageQ.put( ('started', now().strftime('%Y/%m/%d %H:%M:%S')) )
		self.startThreads()
		self.startCamera()

	def onTest( self, event ):
		self.testCount = getattr(self, 'testCount', 0) + 1
		self.requestQ.put( {
				'time':now(),
				'bib':self.testCount,
				'firstName':u'Test',
				'lastName':u'Test',
				'team':u'Test',
				'wave':u'Test',
				'raceName':u'Test',				
			}
		)
		wx.CallLater( 500, self.dbWriterQ.put, ('flush',) )
		wx.CallLater( int(100+1000*int(tdCaptureBefore.total_seconds())), self.refreshTriggers )
	
	def onRightClick( self, event ):
		if not self.triggerInfo:
			return
		self.xFinish = event.GetX()
		pd = PhotoDialog( self, self.finishStrip.finish.getJpg(self.xFinish), self.triggerInfo, self.finishStrip.GetTsJpgs(), self.fps )
		pd.ShowModal()
		pd.Destroy()

	def setFinishStripJpgs( self, jpgs ):
		self.tsJpg = jpgs
		wx.CallAfter( self.finishStrip.SetTsJpgs, self.tsJpg, self.ts, self.triggerInfo )

	def onTriggerSelected( self, event ):
		self.iTriggerSelect = event.m_itemIndex
		data = self.itemDataMap[self.triggerList.GetItemData(self.iTriggerSelect)]
		self.triggerInfo = {
			a:data[i] for i, a in enumerate(('ts','bib','name','team','wave','raceName','firstName','lastName'))
		}
		self.ts = data[0]
		self.dbReaderQ.put( ('getphotos', self.ts-tdCaptureBefore, self.ts+tdCaptureAfter) )
		
	def showMessages( self ):
		while 1:
			message = self.messageQ.get()
			assert len(message) == 2, 'Incorrect message length'
			cmd, info = message
			wx.CallAfter( self.messageManager.write, '{}:  {}'.format(cmd, info) if cmd else info )
		
	def startCamera( self ):
		self.camera = None
		try:
			self.camera = Device( max(self.getCameraDeviceNum(), 0) )
		except Exception as e:
			self.messageQ.put( ('camera', 'Error: {}'.format(e)) )
			return False
		
		#self.camera.set_resolution( 640, 480 )
		self.messageQ.put( ('camera', 'Successfully Connected: Device: {}'.format(self.getCameraDeviceNum()) ) )
		return True
	
	def startThreads( self ):
		self.grabFrameOK = False
		
		self.listenerThread = SocketListener( self.requestQ, self.messageQ )		
		error = self.listenerThread.test()
		if error:
			wx.MessageBox('Socket Error:\n\n{}\n\nIs another CrossMgrVideo or CrossMgrCamera running on this computer?'.format(error),
				"Socket Error",
				wx.OK | wx.ICON_ERROR
			)
			wx.Exit()
		
		self.dbWriterThread = threading.Thread( target=DBWriter, args=(self.dbWriterQ,) )
		self.dbWriterThread.daemon = True
		
		self.dbReaderThread = threading.Thread( target=DBReader, args=(self.dbReaderQ, self.setFinishStripJpgs) )
		self.dbReaderThread.daemon = True
		
		self.fcb = FrameCircBuf( int(self.bufferSecs * self.fps) )
		
		self.listenerThread.start()
		self.dbWriterThread.start()
		self.dbReaderThread.start()
		
		self.grabFrameOK = True
		self.messageQ.put( ('threads', 'Successfully Launched') )
		return True
	
	def stopCapture( self ):
		self.dbWriterQ.put( ('flush',) )
	
	def frameLoop( self, event=None ):
		if not self.grabFrameOK:
			return
			
		self.frameCount += 1
		tNow = now()
		
		# Compute frame rate statistics.
		if self.frameCount == self.frameCountUpdate:
			self.fpsActual = self.frameCount / (tNow - self.tFrameCount).total_seconds()
			self.frameCount = 0
			self.tFrameCount = tNow
			self.framesPerSecond.SetLabel( u'{:.3f}'.format(self.fpsActual) )
			self.availableMsPerFrame.SetLabel( u'{:.0f}'.format(1000.0/self.fpsActual) )
			self.frameProcessingTime.SetLabel( u'{:.0f}'.format(1000.0*self.fpt.total_seconds()) )
		
		try:
			image = self.camera.getImage()
		except Exception as e:
			self.messageQ.put( ('error', 'Webcam Failure: {}'.format(e) ) )
			self.grabFrameOK = False
			return
		
		image = PilImageToWxImage( image )
		
		# Add the image to the circular buffer.
		self.fcb.append( tNow, image )
		
		# Update the monitor screen.
		if self.frameCount & 3 == 0:
			wx.CallAfter( self.primaryImage.SetImage, image )
		
		# Record images if the timer is running.
		if self.captureTimer.IsRunning():
			self.dbWriterQ.put( ('photo', tNow, image) )
		
		# Periodically update events.
		if (tNow - self.lastTriggerRefresh).total_seconds() > 5.0:
			self.refreshTriggers()
			self.lastTriggerRefresh = tNow
			return
		
		# Process event messages
		while 1:
			try:
				message = self.requestQ.get(False)
			except Empty:
				break
			
			tSearch = message['time']
			advanceSeconds = message.get('advanceSeconds', 0.0)
			tSearch += timedelta(seconds=advanceSeconds)
			
			# Record this trigger.
			self.dbWriterQ.put( (
				'trigger',
				tSearch - timedelta(seconds=advanceSeconds),
				message.get('bib', 99999),
				message.get('firstName',u''),
				message.get('lastName',u''),
				message.get('team',u''),
				message.get('wave',u''),
				message.get('raceName',u'')
			) )

			# If we are not currently capturing, make sure we record past frames.
			if not self.captureTimer.IsRunning():
				times, frames = self.fcb.getBackFrames( tSearch - tdCaptureBefore )
				for t, f in zip(times, frames):
					if f:
						self.dbWriterQ.put( ('photo', t, f) )
			else:
				self.captureTimer.Stop()
			
			# Set a timer to stop recording after the capture window.
			millis = int((tSearch - now() + tdCaptureAfter).total_seconds() * 1000.0)
			if millis > 0:
				self.captureTimer.Start( millis )
			
			if (now() - tNow).total_seconds() > self.frameDelay / 2.0:
				break
				
		self.fpt = now() - tNow
		self.tLast = tNow
	
	def shutdown( self ):
		# Ensure that all images in the queue are saved.
		self.grabFrameOK = False
		if hasattr(self, 'dbWriterThread'):
			self.dbWriterQ.put( ('terminate', ) )
			self.dbWriterThread.join()
			self.dbReaderQ.put( ('terminate', ) )
			self.dbReaderThread.join()
	
	def resetCamera( self, event ):
		self.writeOptions()
		
		dlg = ConfigDialog( self, self.getCameraDeviceNum() )
		ret = dlg.ShowModal()
		cameraDeviceNum = dlg.GetCameraDeviceNum()
		dlg.Destroy()
		if ret != wx.ID_OK:
			return
		
		self.setCameraDeviceNum( cameraDeviceNum )
		self.grabFrameOK = self.startCamera()
	
	def manageDatabase( self, event ):
		dlg = ManageDatabase( self, self.db.getsize(), self.db.fname, title='Manage Database' )
		if dlg.ShowModal() == wx.ID_OK:
			tsLower, tsUpper = dlg.GetDates()
			self.db.cleanBetween( tsLower, tsUpper )
			wx.CallAfter( self.finishStrip.Clear )
			wx.CallAfter( self.refreshTriggers, True )
		dlg.Destroy()
	
	def setCameraDeviceNum( self, num ):
		self.cameraDevice.SetLabel( unicode(num) )
		
	def getCameraDeviceNum( self ):
		return int(self.cameraDevice.GetLabel())
		
	def onCloseWindow( self, event ):
		self.shutdown()
		wx.Exit()
		
	def writeOptions( self ):
		self.config.Write( 'CameraDevice', self.cameraDevice.GetLabel() )
		self.config.Flush()
	
	def readOptions( self ):
		self.cameraDevice.SetLabel( self.config.Read('CameraDevice', u'0') )
Ejemplo n.º 11
0
class VideoBuffer( object ):
	
	def __init__( self, camera, refTime=None, dirName='.', fps=25, bufferSeconds=4.0, owner=None, burstMode=True ):
		self.camera = camera
		self.refTime = refTime if refTime is not None else now()
		self.dirName = dirName
		self.fps = fps
		self.frameMax = int(fps * bufferSeconds)
		
		self.frameDelay = 1.0 / fps
		self.frameDelayTimeDelta = timedelta(seconds=self.frameDelay)
		
		self.frameSaver = None
		self.fcb = FrameCircBuf()
		self.owner = owner			# Destination to send photos after they are taken.
		self.burstMode = burstMode
		
		self.timer = CallbackTimer( self.recordVideoFrame )
		self.reset()
	
	def setOwner( self, owner=None ):
		self.owner = owner
	
	def reset( self ):
		if self.frameSaver and self.frameSaver.is_alive():
			self.frameSaver.stop()
		
		if self.timer.IsRunning():
			self.timer.Stop()
		
		self.fcb.reset( self.frameMax )
		self.frameCount = 0
		
		self.frameSaver = FrameSaver()
		self.frameSaver.start()
		
		self.tFindLast = now() - timedelta( days=1 )
		
		self.lastSampleTime = now()
		self.sampleCount = 0
	
	def recordVideoFrame( self ):
		tNow = now()
		self.sampleCount += 1
		if not self.camera:
			return
			
		try:
			image = self.camera.getImage()
			self.fcb.append( tNow, image )
			if self.owner is not None:
				wx.PostEvent( self.owner, PhotoEvent(t=tNew, photo=image) )
		except Exception as e:
			logException( e, sys.exc_info() )

	def getFrameRate( self ):
		tNow = now()
		rate = self.sampleCount / (tNow -self.lastSampleTime).total_seconds()
		self.lastSampleTime = tNow
		self.sampleCount = 0
		return rate
	
	def start( self ):
		self.reset()
		milliseconds = int(self.frameDelay * 1000.0)
		self.timer.Start( milliseconds, oneShot=False )
	
	def stop( self ):
		if self.timer.IsRunning():
			self.timer.Stop()
		if self.frameSaver and self.frameSaver.is_alive():
			self.frameSaver.stop()
		self.frameSaver = None
		self.lastSampleTime = now()
			
	def takePhoto( self, bib, t ):
		tNow = now()

		tFind = self.refTime + timedelta( seconds = t + Model.race.advancePhotoMilliseconds / 1000.0 )
		if tFind > tNow:
			wx.CallLater( max(1,int(((tFind - tNow).total_seconds() + 0.1) * 1000.0)), self.takePhoto, bib, t )
			return
		
		# If burst mode, check if there was another rider before within frameDelay seconds.
		# If so, also save the frame just before the given time.
		# Always save the closest frame after the given time.
		times, frames = self.fcb.findBeforeAfter(
							tFind,
							1, # 1 if tFind - self.tFindLast < self.frameDelayTimeDelta and self.burstMode else 0,
							1 )
		for i, frame in enumerate( frames ):
			t = (times[i]-self.refTime).total_seconds()
			self.frameSaver.save( GetFilename(bib, t, self.dirName, i), bib, t, frame )
		self.frameCount += len(frames)
		self.tFindLast = tFind
	
	def getT( self, i ):
		return self.fcb.getT(i)
		
	def getFrame( self, i ):
		return self.fcb.getFrame(i)
	
	def getTimeFrame( self, i ):
		return (self.fcb.getT(i), self.fcb.getFrame(i))
		
	def findBeforeAfter( self, t, before=0, after=1 ):
		''' Call the frame cyclic buffer from race time, not clock time. '''
		tFind = self.refTime + timedelta( seconds=t )
		times, frames = self.fcb.findBeforeAfter( tFind, before, after )
		return zip( times, frames )
		
	def __len__( self ):
		return self.frameMax
Ejemplo n.º 12
0
    def __init__(self, parent, id=wx.ID_ANY, title='', size=(1000, 800)):
        wx.Frame.__init__(self, parent, id, title, size=size)

        self.db = Database()

        self.fps = 25
        self.frameDelay = 1.0 / self.fps
        self.bufferSecs = 10
        self.xFinish = None

        self.tFrameCount = self.tLaunch = self.tLast = now()
        self.frameCount = 0
        self.frameCountUpdate = int(self.fps * 2)
        self.fpsActual = 0.0
        self.fpt = timedelta(seconds=0)
        self.iTriggerSelect = None
        self.triggerInfo = None

        self.captureTimer = wx.CallLater(10, self.stopCapture)

        self.fcb = FrameCircBuf(self.bufferSecs * self.fps)

        self.config = wx.Config(
            appName="CrossMgrVideo",
            vendorName="SmartCyclingSolutions",
            #style=wx.CONFIG_USE_LOCAL_FILE
        )

        self.requestQ = Queue()  # Select photos from photobuf.
        self.dbWriterQ = Queue()  # Photos waiting to be written
        self.dbReaderQ = Queue()  # Photos read as requested from user.
        self.messageQ = Queue(
        )  # Collection point for all status/failure messages.

        self.SetBackgroundColour(wx.Colour(232, 232, 232))

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #------------------------------------------------------------------------------------------------
        headerSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.logo = Utils.GetPngBitmap('CrossMgrHeader.png')
        headerSizer.Add(wx.StaticBitmap(self, wx.ID_ANY, self.logo))

        self.title = wx.StaticText(self,
                                   label='CrossMgr Video\nVersion {}'.format(
                                       AppVerName.split()[1]),
                                   style=wx.ALIGN_RIGHT)
        self.title.SetFont(
            wx.Font((0, 28), wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))
        headerSizer.Add(self.title, flag=wx.ALL, border=10)

        #------------------------------------------------------------------------------
        self.cameraDeviceLabel = wx.StaticText(self, label='Camera Device:')
        self.cameraDevice = wx.StaticText(self)
        boldFont = self.cameraDevice.GetFont()
        boldFont.SetWeight(wx.BOLD)
        self.cameraDevice.SetFont(boldFont)
        self.cameraResolution = wx.StaticText(self)
        self.cameraResolution.SetFont(boldFont)
        self.reset = wx.Button(self, label="Reset Camera")
        self.reset.Bind(wx.EVT_BUTTON, self.resetCamera)

        self.manage = wx.Button(self, label="Manage Database")
        self.manage.Bind(wx.EVT_BUTTON, self.manageDatabase)

        self.test = wx.Button(self, label="Test")
        self.test.Bind(wx.EVT_BUTTON, self.onTest)

        self.focus = wx.Button(self, label="Focus...")
        self.focus.Bind(wx.EVT_BUTTON, self.onFocus)

        cameraDeviceSizer = wx.BoxSizer(wx.HORIZONTAL)
        cameraDeviceSizer.Add(self.cameraDeviceLabel,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_RIGHT)
        cameraDeviceSizer.Add(self.cameraDevice,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=8)
        cameraDeviceSizer.Add(self.cameraResolution,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=8)
        cameraDeviceSizer.Add(self.reset,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=32)
        cameraDeviceSizer.Add(self.manage,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=16)
        cameraDeviceSizer.Add(self.test,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=16)
        cameraDeviceSizer.Add(self.focus,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=16)

        #------------------------------------------------------------------------------
        self.targetProcessingTimeLabel = wx.StaticText(self,
                                                       label='Target Frames:')
        self.targetProcessingTime = wx.StaticText(self,
                                                  label=u'{:.3f}'.format(
                                                      self.fps))
        self.targetProcessingTime.SetFont(boldFont)
        self.targetProcessingTimeUnit = wx.StaticText(self, label='/ sec')

        self.framesPerSecondLabel = wx.StaticText(self, label='Actual Frames:')
        self.framesPerSecond = wx.StaticText(self, label='25.000')
        self.framesPerSecond.SetFont(boldFont)
        self.framesPerSecondUnit = wx.StaticText(self, label='/ sec')

        self.availableMsPerFrameLabel = wx.StaticText(
            self, label='Available Time Per Frame:')
        self.availableMsPerFrame = wx.StaticText(self,
                                                 label=u'{:.0f}'.format(
                                                     1000.0 * self.frameDelay))
        self.availableMsPerFrame.SetFont(boldFont)
        self.availableMsPerFrameUnit = wx.StaticText(self, label='ms')

        self.frameProcessingTimeLabel = wx.StaticText(
            self, label='Frame Processing Time:')
        self.frameProcessingTime = wx.StaticText(self, label='20')
        self.frameProcessingTime.SetFont(boldFont)
        self.frameProcessingTimeUnit = wx.StaticText(self, label='ms')

        pfgs = wx.FlexGridSizer(rows=0, cols=6, vgap=4, hgap=8)
        fRight = wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_RIGHT
        fLeft = wx.ALIGN_CENTRE_VERTICAL

        #------------------- Row 1 ------------------------------
        pfgs.Add(self.targetProcessingTimeLabel, flag=fRight)
        pfgs.Add(self.targetProcessingTime, flag=fRight)
        pfgs.Add(self.targetProcessingTimeUnit, flag=fLeft)
        pfgs.Add(self.availableMsPerFrameLabel, flag=fRight)
        pfgs.Add(self.availableMsPerFrame, flag=fRight)
        pfgs.Add(self.availableMsPerFrameUnit, flag=fLeft)

        #------------------- Row 2 ------------------------------
        pfgs.Add(self.framesPerSecondLabel, flag=fRight)
        pfgs.Add(self.framesPerSecond, flag=fRight)
        pfgs.Add(self.framesPerSecondUnit, flag=fLeft)
        pfgs.Add(self.frameProcessingTimeLabel, flag=fRight)
        pfgs.Add(self.frameProcessingTime, flag=fRight)
        pfgs.Add(self.frameProcessingTimeUnit, flag=fLeft)

        statsSizer = wx.BoxSizer(wx.VERTICAL)
        statsSizer.Add(cameraDeviceSizer)
        statsSizer.Add(pfgs, flag=wx.TOP, border=8)
        headerSizer.Add(statsSizer, flag=wx.ALL, border=4)

        mainSizer.Add(headerSizer)

        #------------------------------------------------------------------------------------------------
        self.finishStrip = FinishStripPanel(self,
                                            size=(-1,
                                                  wx.GetDisplaySize()[1] // 2))
        self.finishStrip.finish.Bind(wx.EVT_RIGHT_DOWN, self.onRightClick)

        self.primaryImage = ScaledImage(self,
                                        style=wx.BORDER_SUNKEN,
                                        size=(imageWidth, imageHeight))
        self.primaryImage.SetTestImage()

        self.focusDialog = FocusDialog(self)

        hsDate = wx.BoxSizer(wx.HORIZONTAL)
        hsDate.Add(wx.StaticText(self, label='Show Triggers for'),
                   flag=wx.ALIGN_CENTER_VERTICAL)
        tQuery = now()
        self.date = wx.adv.DatePickerCtrl(
            self,
            dt=wx.DateTime.FromDMY(tQuery.day, tQuery.month - 1, tQuery.year),
            style=wx.adv.DP_DROPDOWN | wx.adv.DP_SHOWCENTURY)
        self.date.Bind(wx.adv.EVT_DATE_CHANGED, self.onQueryDateChanged)
        hsDate.Add(self.date, flag=wx.LEFT, border=2)

        hsDate.Add(wx.StaticText(self, label='Filter by Bib'),
                   flag=wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
                   border=12)
        self.bib = wx.lib.intctrl.IntCtrl(self,
                                          style=wx.TE_PROCESS_ENTER,
                                          size=(64, -1),
                                          min=1,
                                          allow_none=True,
                                          value=None)
        self.bib.Bind(wx.EVT_TEXT_ENTER, self.onQueryBibChanged)
        hsDate.Add(self.bib, flag=wx.LEFT, border=2)

        self.tsQueryLower = datetime(tQuery.year, tQuery.month, tQuery.day)
        self.tsQueryUpper = self.tsQueryLower + timedelta(days=1)
        self.bibQuery = None

        self.triggerList = AutoWidthListCtrl(
            self, style=wx.LC_REPORT | wx.BORDER_SUNKEN | wx.LC_SORT_ASCENDING)

        self.il = wx.ImageList(16, 16)
        self.sm_close = []
        for bm in getCloseFinishBitmaps():
            self.sm_close.append(self.il.Add(bm))
        self.sm_up = self.il.Add(Utils.GetPngBitmap('SmallUpArrow.png'))
        self.sm_up = self.il.Add(Utils.GetPngBitmap('SmallUpArrow.png'))
        self.sm_dn = self.il.Add(Utils.GetPngBitmap('SmallDownArrow.png'))
        self.triggerList.SetImageList(self.il, wx.IMAGE_LIST_SMALL)

        headers = ['Time', 'Bib', 'Name', 'Team', 'Wave', 'km/h', 'mph']
        for i, h in enumerate(headers):
            self.triggerList.InsertColumn(
                i, h,
                wx.LIST_FORMAT_RIGHT if h in ('Bib', 'km/h',
                                              'mph') else wx.LIST_FORMAT_LEFT)
        self.itemDataMap = {}

        self.triggerList.Bind(wx.EVT_LIST_ITEM_SELECTED,
                              self.onTriggerSelected)

        self.messagesText = wx.TextCtrl(self,
                                        style=wx.TE_READONLY | wx.TE_MULTILINE
                                        | wx.HSCROLL,
                                        size=(250, -1))
        self.messageManager = MessageManager(self.messagesText)

        vsTriggers = wx.BoxSizer(wx.VERTICAL)
        vsTriggers.Add(hsDate)
        vsTriggers.Add(self.triggerList, 1, flag=wx.EXPAND | wx.TOP, border=2)

        #------------------------------------------------------------------------------------------------
        mainSizer.Add(self.finishStrip, 1, flag=wx.EXPAND)

        border = 2
        row1Sizer = wx.BoxSizer(wx.HORIZONTAL)
        row1Sizer.Add(self.primaryImage, flag=wx.ALL, border=border)
        row1Sizer.Add(vsTriggers,
                      1,
                      flag=wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND,
                      border=border)
        row1Sizer.Add(self.messagesText,
                      flag=wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND,
                      border=border)
        mainSizer.Add(row1Sizer, flag=wx.EXPAND)

        #------------------------------------------------------------------------------------------------
        # Create a timer to update the frame loop.
        #
        self.timer = wx.Timer()
        self.timer.Bind(wx.EVT_TIMER, self.frameLoop)

        self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

        self.readOptions()
        self.SetSizerAndFit(mainSizer)

        # Start the message reporting thread so we can see what is going on.
        self.messageThread = threading.Thread(target=self.showMessages)
        self.messageThread.daemon = True
        self.messageThread.start()

        self.grabFrameOK = False
        self.tsMax = None

        # Start the frame loop.
        delayAdjustment = 0.80 if 'win' in sys.platform else 0.98
        ms = int(1000 * self.frameDelay * delayAdjustment)
        self.timer.Start(ms, False)

        wx.CallLater(300, self.refreshTriggers)
Ejemplo n.º 13
0
class MainWin(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title='', size=(1000, 800)):
        wx.Frame.__init__(self, parent, id, title, size=size)

        self.db = Database()

        self.fps = 25
        self.frameDelay = 1.0 / self.fps
        self.bufferSecs = 10
        self.xFinish = None

        self.tFrameCount = self.tLaunch = self.tLast = now()
        self.frameCount = 0
        self.frameCountUpdate = int(self.fps * 2)
        self.fpsActual = 0.0
        self.fpt = timedelta(seconds=0)
        self.iTriggerSelect = None
        self.triggerInfo = None

        self.captureTimer = wx.CallLater(10, self.stopCapture)

        self.fcb = FrameCircBuf(self.bufferSecs * self.fps)

        self.config = wx.Config(
            appName="CrossMgrVideo",
            vendorName="SmartCyclingSolutions",
            #style=wx.CONFIG_USE_LOCAL_FILE
        )

        self.requestQ = Queue()  # Select photos from photobuf.
        self.dbWriterQ = Queue()  # Photos waiting to be written
        self.dbReaderQ = Queue()  # Photos read as requested from user.
        self.messageQ = Queue(
        )  # Collection point for all status/failure messages.

        self.SetBackgroundColour(wx.Colour(232, 232, 232))

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #------------------------------------------------------------------------------------------------
        headerSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.logo = Utils.GetPngBitmap('CrossMgrHeader.png')
        headerSizer.Add(wx.StaticBitmap(self, wx.ID_ANY, self.logo))

        self.title = wx.StaticText(self,
                                   label='CrossMgr Video\nVersion {}'.format(
                                       AppVerName.split()[1]),
                                   style=wx.ALIGN_RIGHT)
        self.title.SetFont(
            wx.Font((0, 28), wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))
        headerSizer.Add(self.title, flag=wx.ALL, border=10)

        #------------------------------------------------------------------------------
        self.cameraDeviceLabel = wx.StaticText(self, label='Camera Device:')
        self.cameraDevice = wx.StaticText(self)
        boldFont = self.cameraDevice.GetFont()
        boldFont.SetWeight(wx.BOLD)
        self.cameraDevice.SetFont(boldFont)
        self.cameraResolution = wx.StaticText(self)
        self.cameraResolution.SetFont(boldFont)
        self.reset = wx.Button(self, label="Reset Camera")
        self.reset.Bind(wx.EVT_BUTTON, self.resetCamera)

        self.manage = wx.Button(self, label="Manage Database")
        self.manage.Bind(wx.EVT_BUTTON, self.manageDatabase)

        self.test = wx.Button(self, label="Test")
        self.test.Bind(wx.EVT_BUTTON, self.onTest)

        self.focus = wx.Button(self, label="Focus...")
        self.focus.Bind(wx.EVT_BUTTON, self.onFocus)

        cameraDeviceSizer = wx.BoxSizer(wx.HORIZONTAL)
        cameraDeviceSizer.Add(self.cameraDeviceLabel,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_RIGHT)
        cameraDeviceSizer.Add(self.cameraDevice,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=8)
        cameraDeviceSizer.Add(self.cameraResolution,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=8)
        cameraDeviceSizer.Add(self.reset,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=32)
        cameraDeviceSizer.Add(self.manage,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=16)
        cameraDeviceSizer.Add(self.test,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=16)
        cameraDeviceSizer.Add(self.focus,
                              flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT,
                              border=16)

        #------------------------------------------------------------------------------
        self.targetProcessingTimeLabel = wx.StaticText(self,
                                                       label='Target Frames:')
        self.targetProcessingTime = wx.StaticText(self,
                                                  label=u'{:.3f}'.format(
                                                      self.fps))
        self.targetProcessingTime.SetFont(boldFont)
        self.targetProcessingTimeUnit = wx.StaticText(self, label='/ sec')

        self.framesPerSecondLabel = wx.StaticText(self, label='Actual Frames:')
        self.framesPerSecond = wx.StaticText(self, label='25.000')
        self.framesPerSecond.SetFont(boldFont)
        self.framesPerSecondUnit = wx.StaticText(self, label='/ sec')

        self.availableMsPerFrameLabel = wx.StaticText(
            self, label='Available Time Per Frame:')
        self.availableMsPerFrame = wx.StaticText(self,
                                                 label=u'{:.0f}'.format(
                                                     1000.0 * self.frameDelay))
        self.availableMsPerFrame.SetFont(boldFont)
        self.availableMsPerFrameUnit = wx.StaticText(self, label='ms')

        self.frameProcessingTimeLabel = wx.StaticText(
            self, label='Frame Processing Time:')
        self.frameProcessingTime = wx.StaticText(self, label='20')
        self.frameProcessingTime.SetFont(boldFont)
        self.frameProcessingTimeUnit = wx.StaticText(self, label='ms')

        pfgs = wx.FlexGridSizer(rows=0, cols=6, vgap=4, hgap=8)
        fRight = wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_RIGHT
        fLeft = wx.ALIGN_CENTRE_VERTICAL

        #------------------- Row 1 ------------------------------
        pfgs.Add(self.targetProcessingTimeLabel, flag=fRight)
        pfgs.Add(self.targetProcessingTime, flag=fRight)
        pfgs.Add(self.targetProcessingTimeUnit, flag=fLeft)
        pfgs.Add(self.availableMsPerFrameLabel, flag=fRight)
        pfgs.Add(self.availableMsPerFrame, flag=fRight)
        pfgs.Add(self.availableMsPerFrameUnit, flag=fLeft)

        #------------------- Row 2 ------------------------------
        pfgs.Add(self.framesPerSecondLabel, flag=fRight)
        pfgs.Add(self.framesPerSecond, flag=fRight)
        pfgs.Add(self.framesPerSecondUnit, flag=fLeft)
        pfgs.Add(self.frameProcessingTimeLabel, flag=fRight)
        pfgs.Add(self.frameProcessingTime, flag=fRight)
        pfgs.Add(self.frameProcessingTimeUnit, flag=fLeft)

        statsSizer = wx.BoxSizer(wx.VERTICAL)
        statsSizer.Add(cameraDeviceSizer)
        statsSizer.Add(pfgs, flag=wx.TOP, border=8)
        headerSizer.Add(statsSizer, flag=wx.ALL, border=4)

        mainSizer.Add(headerSizer)

        #------------------------------------------------------------------------------------------------
        self.finishStrip = FinishStripPanel(self,
                                            size=(-1,
                                                  wx.GetDisplaySize()[1] // 2))
        self.finishStrip.finish.Bind(wx.EVT_RIGHT_DOWN, self.onRightClick)

        self.primaryImage = ScaledImage(self,
                                        style=wx.BORDER_SUNKEN,
                                        size=(imageWidth, imageHeight))
        self.primaryImage.SetTestImage()

        self.focusDialog = FocusDialog(self)

        hsDate = wx.BoxSizer(wx.HORIZONTAL)
        hsDate.Add(wx.StaticText(self, label='Show Triggers for'),
                   flag=wx.ALIGN_CENTER_VERTICAL)
        tQuery = now()
        self.date = wx.adv.DatePickerCtrl(
            self,
            dt=wx.DateTime.FromDMY(tQuery.day, tQuery.month - 1, tQuery.year),
            style=wx.adv.DP_DROPDOWN | wx.adv.DP_SHOWCENTURY)
        self.date.Bind(wx.adv.EVT_DATE_CHANGED, self.onQueryDateChanged)
        hsDate.Add(self.date, flag=wx.LEFT, border=2)

        hsDate.Add(wx.StaticText(self, label='Filter by Bib'),
                   flag=wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
                   border=12)
        self.bib = wx.lib.intctrl.IntCtrl(self,
                                          style=wx.TE_PROCESS_ENTER,
                                          size=(64, -1),
                                          min=1,
                                          allow_none=True,
                                          value=None)
        self.bib.Bind(wx.EVT_TEXT_ENTER, self.onQueryBibChanged)
        hsDate.Add(self.bib, flag=wx.LEFT, border=2)

        self.tsQueryLower = datetime(tQuery.year, tQuery.month, tQuery.day)
        self.tsQueryUpper = self.tsQueryLower + timedelta(days=1)
        self.bibQuery = None

        self.triggerList = AutoWidthListCtrl(
            self, style=wx.LC_REPORT | wx.BORDER_SUNKEN | wx.LC_SORT_ASCENDING)

        self.il = wx.ImageList(16, 16)
        self.sm_close = []
        for bm in getCloseFinishBitmaps():
            self.sm_close.append(self.il.Add(bm))
        self.sm_up = self.il.Add(Utils.GetPngBitmap('SmallUpArrow.png'))
        self.sm_up = self.il.Add(Utils.GetPngBitmap('SmallUpArrow.png'))
        self.sm_dn = self.il.Add(Utils.GetPngBitmap('SmallDownArrow.png'))
        self.triggerList.SetImageList(self.il, wx.IMAGE_LIST_SMALL)

        headers = ['Time', 'Bib', 'Name', 'Team', 'Wave', 'km/h', 'mph']
        for i, h in enumerate(headers):
            self.triggerList.InsertColumn(
                i, h,
                wx.LIST_FORMAT_RIGHT if h in ('Bib', 'km/h',
                                              'mph') else wx.LIST_FORMAT_LEFT)
        self.itemDataMap = {}

        self.triggerList.Bind(wx.EVT_LIST_ITEM_SELECTED,
                              self.onTriggerSelected)

        self.messagesText = wx.TextCtrl(self,
                                        style=wx.TE_READONLY | wx.TE_MULTILINE
                                        | wx.HSCROLL,
                                        size=(250, -1))
        self.messageManager = MessageManager(self.messagesText)

        vsTriggers = wx.BoxSizer(wx.VERTICAL)
        vsTriggers.Add(hsDate)
        vsTriggers.Add(self.triggerList, 1, flag=wx.EXPAND | wx.TOP, border=2)

        #------------------------------------------------------------------------------------------------
        mainSizer.Add(self.finishStrip, 1, flag=wx.EXPAND)

        border = 2
        row1Sizer = wx.BoxSizer(wx.HORIZONTAL)
        row1Sizer.Add(self.primaryImage, flag=wx.ALL, border=border)
        row1Sizer.Add(vsTriggers,
                      1,
                      flag=wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND,
                      border=border)
        row1Sizer.Add(self.messagesText,
                      flag=wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND,
                      border=border)
        mainSizer.Add(row1Sizer, flag=wx.EXPAND)

        #------------------------------------------------------------------------------------------------
        # Create a timer to update the frame loop.
        #
        self.timer = wx.Timer()
        self.timer.Bind(wx.EVT_TIMER, self.frameLoop)

        self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

        self.readOptions()
        self.SetSizerAndFit(mainSizer)

        # Start the message reporting thread so we can see what is going on.
        self.messageThread = threading.Thread(target=self.showMessages)
        self.messageThread.daemon = True
        self.messageThread.start()

        self.grabFrameOK = False
        self.tsMax = None

        # Start the frame loop.
        delayAdjustment = 0.80 if 'win' in sys.platform else 0.98
        ms = int(1000 * self.frameDelay * delayAdjustment)
        self.timer.Start(ms, False)

        wx.CallLater(300, self.refreshTriggers)

    def onQueryDateChanged(self, event):
        v = self.date.GetValue()
        self.tsQueryLower = datetime(v.GetYear(), v.GetMonth() + 1, v.GetDay())
        self.tsQueryUpper = self.tsQueryLower + timedelta(days=1)
        self.refreshTriggers(True)

    def onQueryBibChanged(self, event):
        self.bibQuery = self.bib.GetValue()
        self.refreshTriggers(True)

    def GetListCtrl(self):
        return self.triggerList

    def GetSortImages(self):
        return (self.sm_dn, self.sm_up)

    def getItemData(self, i):
        data = self.triggerList.GetItemData(i)
        return self.itemDataMap[data]

    def refreshTriggers(self, replace=False, iTriggerRow=None):
        tNow = now()
        self.lastTriggerRefresh = tNow

        if replace:
            tsLower = self.tsQueryLower
            tsUpper = self.tsQueryUpper
            self.triggerList.DeleteAllItems()
            self.itemDataMap = {}
            self.tsMax = None
            self.iTriggerSelect = None
            self.triggerInfo = {}
        else:
            tsLower = (self.tsMax or datetime(
                tNow.year, tNow.month, tNow.day)) + timedelta(seconds=0.00001)
            tsUpper = tsLower + timedelta(days=1)

        triggers = self.db.getTriggers(tsLower, tsUpper, self.bibQuery)

        if not triggers:
            return

        tsPrev = (self.tsMax or datetime(2000, 1, 1))
        self.tsMax = triggers[-1][
            1]  # id,ts,bib,first_name,last_name,team,wave,race_name,kmh

        for i, (id, ts, bib, first_name, last_name, team, wave, race_name,
                kmh) in enumerate(triggers):
            dtFinish = (ts - tsPrev).total_seconds()
            itemImage = self.sm_close[min(
                len(self.sm_close) - 1,
                int(len(self.sm_close) * dtFinish / closeFinishThreshold))]
            row = self.triggerList.InsertItem(sys.maxint,
                                              ts.strftime('%H:%M:%S.%f')[:-3],
                                              itemImage)
            self.triggerList.SetItem(row, 1, u'{:>6}'.format(bib))
            name = u', '.join(n for n in (last_name, first_name) if n)
            self.triggerList.SetItem(row, 2, name)
            self.triggerList.SetItem(row, 3, team)
            self.triggerList.SetItem(row, 4, wave)
            if kmh:
                kmh_text, mph_text = u'{:.2f}'.format(kmh), u'{:.2f}'.format(
                    kmh * 0.621371)
            else:
                kmh_text = mph_text = u''
            self.triggerList.SetItem(row, 5, kmh_text)
            self.triggerList.SetItem(row, 6, mph_text)

            self.triggerList.SetItemData(row, row)
            self.itemDataMap[row] = (id, ts, bib, name, team, wave, race_name,
                                     first_name, last_name, kmh)
            tsPrev = ts

        for i in xrange(5):
            self.triggerList.SetColumnWidth(i, wx.LIST_AUTOSIZE)

        if iTriggerRow is not None and (0 <= iTriggerRow <
                                        self.triggerList.GetItemCount()):
            self.triggerList.EnsureVisible(iTriggerRow)
            self.triggerList.Select(iTriggerRow)
        else:
            self.triggerList.EnsureVisible(self.triggerList.GetItemCount() - 1)

    def Start(self):
        self.messageQ.put(
            ('', '************************************************'))
        self.messageQ.put(('started', now().strftime('%Y/%m/%d %H:%M:%S')))
        self.startThreads()
        self.startCamera()

    def onTest(self, event):
        self.testCount = getattr(self, 'testCount', 0) + 1
        self.requestQ.put({
            'time': now(),
            'bib': self.testCount,
            'firstName': u'Test',
            'lastName': u'Test',
            'team': u'Test',
            'wave': u'Test',
            'raceName': u'Test',
        })
        wx.CallLater(500, self.dbWriterQ.put, ('flush', ))
        wx.CallLater(int(100 + 1000 * int(tdCaptureBefore.total_seconds())),
                     self.refreshTriggers)

    def onFocus(self, event):
        self.focusDialog.Move((4, 4))
        self.focusDialog.ShowModal()

    def onRightClick(self, event):
        if not self.triggerInfo:
            return
        self.xFinish = event.GetX()

        pd = PhotoDialog(self, self.finishStrip.finish.getJpg(self.xFinish),
                         self.triggerInfo, self.finishStrip.GetTsJpgs(),
                         self.fps)
        pd.ShowModal()
        if self.triggerInfo['kmh'] != (pd.kmh or 0.0):
            self.dbWriterQ.put(('kmh', self.triggerInfo['id'], pd.kmh or 0.0))
            wx.CallLater(300,
                         self.refreshTriggers,
                         replace=True,
                         iTriggerRow=self.iTriggerSelect)
        pd.Destroy()

    def setFinishStripJpgs(self, jpgs):
        self.tsJpg = jpgs
        wx.CallAfter(self.finishStrip.SetTsJpgs, self.tsJpg, self.ts,
                     self.triggerInfo)

    def onTriggerSelected(self, event):
        self.iTriggerSelect = event.Index
        data = self.itemDataMap[self.triggerList.GetItemData(
            self.iTriggerSelect)]
        self.triggerInfo = {
            a: data[i]
            for i, a in enumerate(('id', 'ts', 'bib', 'name', 'team', 'wave',
                                   'raceName', 'firstName', 'lastName', 'kmh'))
        }
        self.ts = self.triggerInfo['ts']
        self.dbReaderQ.put(
            ('getphotos', self.ts - tdCaptureBefore, self.ts + tdCaptureAfter))

    def showMessages(self):
        while 1:
            message = self.messageQ.get()
            assert len(message) == 2, 'Incorrect message length'
            cmd, info = message
            wx.CallAfter(self.messageManager.write,
                         '{}:  {}'.format(cmd, info) if cmd else info)

    def startCamera(self):
        self.camera = None
        try:
            self.camera = Device(max(self.getCameraDeviceNum(), 0))
        except Exception as e:
            self.messageQ.put(('camera', 'Error: {}'.format(e)))
            return False

        try:
            self.camera.set_resolution(*self.getCameraResolution())
            self.messageQ.put(
                ('camera',
                 '{}x{} Supported'.format(*self.getCameraResolution())))
        except Exception as e:
            self.messageQ.put(('camera', '{}x{} Unsupported Resolution'.format(
                *self.getCameraResolution())))

        self.messageQ.put(
            ('camera', 'Successfully Connected: Device: {}'.format(
                self.getCameraDeviceNum())))
        return True

    def startThreads(self):
        self.grabFrameOK = False

        self.listenerThread = SocketListener(self.requestQ, self.messageQ)
        error = self.listenerThread.test()
        if error:
            wx.MessageBox(
                'Socket Error:\n\n{}\n\nIs another CrossMgrVideo or CrossMgrCamera running on this computer?'
                .format(error), "Socket Error", wx.OK | wx.ICON_ERROR)
            wx.Exit()

        self.dbWriterThread = threading.Thread(target=DBWriter,
                                               args=(self.dbWriterQ, ))
        self.dbWriterThread.daemon = True

        self.dbReaderThread = threading.Thread(target=DBReader,
                                               args=(self.dbReaderQ,
                                                     self.setFinishStripJpgs))
        self.dbReaderThread.daemon = True

        self.fcb = FrameCircBuf(int(self.bufferSecs * self.fps))

        self.listenerThread.start()
        self.dbWriterThread.start()
        self.dbReaderThread.start()

        self.grabFrameOK = True
        self.messageQ.put(('threads', 'Successfully Launched'))
        return True

    def stopCapture(self):
        self.dbWriterQ.put(('flush', ))

    def frameLoop(self, event=None):
        if not self.grabFrameOK:
            return

        self.frameCount += 1
        tNow = now()

        # Compute frame rate statistics.
        if self.frameCount == self.frameCountUpdate:
            self.fpsActual = self.frameCount / (
                tNow - self.tFrameCount).total_seconds()
            self.frameCount = 0
            self.tFrameCount = tNow
            self.framesPerSecond.SetLabel(u'{:.3f}'.format(self.fpsActual))
            self.availableMsPerFrame.SetLabel(u'{:.0f}'.format(1000.0 /
                                                               self.fpsActual))
            self.frameProcessingTime.SetLabel(u'{:.0f}'.format(
                1000.0 * self.fpt.total_seconds()))

        try:
            image = self.camera.getImage()
        except Exception as e:
            self.messageQ.put(('error', 'Webcam Failure: {}'.format(e)))
            self.grabFrameOK = False
            return

        if not image:
            return

        image = PilImageToWxImage(image)

        # Add the image to the circular buffer.
        self.fcb.append(tNow, image)

        # Update the monitor screen.
        if self.frameCount & 3 == 0:
            wx.CallAfter(self.primaryImage.SetImage, image)
        if self.focusDialog.IsShown():
            wx.CallAfter(self.focusDialog.SetImage, image)

        # Record images if the timer is running.
        if self.captureTimer.IsRunning():
            self.dbWriterQ.put(('photo', tNow, image))

        # Periodically update events.
        if (tNow - self.lastTriggerRefresh).total_seconds() > 5.0:
            self.refreshTriggers()
            self.lastTriggerRefresh = tNow
            return

        # Process event messages
        while 1:
            try:
                message = self.requestQ.get(False)
            except Empty:
                break

            tSearch = message['time']
            advanceSeconds = message.get('advanceSeconds', 0.0)
            tSearch += timedelta(seconds=advanceSeconds)

            # Record this trigger.
            self.dbWriterQ.put(
                ('trigger', tSearch - timedelta(seconds=advanceSeconds),
                 message.get('bib', 99999), message.get('firstName', u''),
                 message.get('lastName', u''), message.get('team', u''),
                 message.get('wave', u''), message.get('raceName', u'')))

            # If we are not currently capturing, make sure we record past frames.
            if not self.captureTimer.IsRunning():
                times, frames = self.fcb.getBackFrames(tSearch -
                                                       tdCaptureBefore)
                for t, f in zip(times, frames):
                    if f:
                        self.dbWriterQ.put(('photo', t, f))
            else:
                self.captureTimer.Stop()

            # Set a timer to stop recording after the capture window.
            millis = int(
                (tSearch - now() + tdCaptureAfter).total_seconds() * 1000.0)
            if millis > 0:
                self.captureTimer.Start(millis)

            if (now() - tNow).total_seconds() > self.frameDelay / 2.0:
                break

        self.fpt = now() - tNow
        self.tLast = tNow

    def shutdown(self):
        # Ensure that all images in the queue are saved.
        self.grabFrameOK = False
        if hasattr(self, 'dbWriterThread'):
            self.dbWriterQ.put(('terminate', ))
            self.dbWriterThread.join()
            self.dbReaderQ.put(('terminate', ))
            self.dbReaderThread.join()

    def resetCamera(self, event=None):
        dlg = ConfigDialog(self, self.getCameraDeviceNum(),
                           self.getCameraResolution())
        ret = dlg.ShowModal()
        cameraDeviceNum = dlg.GetCameraDeviceNum()
        cameraResolution = dlg.GetCameraResolution()
        dlg.Destroy()
        if ret != wx.ID_OK:
            return False

        self.setCameraDeviceNum(cameraDeviceNum)
        self.setCameraResolution(*cameraResolution)
        self.writeOptions()

        self.grabFrameOK = self.startCamera()
        return True

    def manageDatabase(self, event):
        dlg = ManageDatabase(self,
                             self.db.getsize(),
                             self.db.fname,
                             title='Manage Database')
        if dlg.ShowModal() == wx.ID_OK:
            tsLower, tsUpper = dlg.GetDates()
            self.db.cleanBetween(tsLower, tsUpper)
            wx.CallAfter(self.finishStrip.Clear)
            wx.CallAfter(self.refreshTriggers, True)
        dlg.Destroy()

    def setCameraDeviceNum(self, num):
        self.cameraDevice.SetLabel(unicode(num))

    def setCameraResolution(self, width, height):
        self.cameraResolution.SetLabel(u'{}x{}'.format(width, height))

    def getCameraDeviceNum(self):
        return int(self.cameraDevice.GetLabel())

    def getCameraResolution(self):
        try:
            resolution = [
                int(v) for v in self.cameraResolution.GetLabel().split('x')
            ]
            return resolution[0], resolution[1]
        except:
            return 640, 400

    def onCloseWindow(self, event):
        self.shutdown()
        wx.Exit()

    def writeOptions(self):
        self.config.Write('CameraDevice', self.cameraDevice.GetLabel())
        self.config.Write('CameraResolution', self.cameraResolution.GetLabel())
        self.config.Flush()

    def readOptions(self):
        self.cameraDevice.SetLabel(self.config.Read('CameraDevice', u'0'))
        self.cameraResolution.SetLabel(
            self.config.Read('CameraResolution', u'640x480'))
Ejemplo n.º 14
0
class VideoBuffer( threading.Thread ):
	def __init__( self, camera, refTime = None, dirName = '.', fps = 25, bufferSeconds = 4.0 ):
		threading.Thread.__init__( self )
		self.daemon = True
		self.name = 'VideoBuffer'
		self.camera = camera
		self.refTime = refTime if refTime is not None else now()
		self.dirName = dirName
		self.fps = fps
		self.frameMax = int(fps * bufferSeconds)
		self.frameDelay = 1.0 / fps
		self.frameSaver = None
		self.fcb = FrameCircBuf()
		self.reset()
	
	def reset( self ):
		
		if self.frameSaver and self.frameSaver.is_alive():
			self.frameSaver.stop()
		
		self.fcb.reset( self.frameMax )
		self.frameCount = 0
		
		self.frameSaver = FrameSaver()
		self.frameSaver.start()
		
		self.queue = Queue()
	
	def run( self ):
		self.reset()
		keepGoing = True
		while keepGoing:
			#sys.stderr.write( '.' )
			tNow = now()
			try:
				self.fcb.append( tNow, self.camera.getImage() )
			except Exception as e:
				logException( e, sys.exc_info() )
				break
			
			while 1:
				try:
					message = self.queue.get( False )
				except Empty:
					break
				
				if   message[0] == 'Save':
					cmd, bib, t = message
					tFind = self.refTime + timedelta( seconds = t + getattr(Model.race, 'advancePhotoMilliseconds', Model.Race.advancePhotoMillisecondsDefault) / 1000.0 )
					if tFind > tNow:
						threading.Timer( (tFind - tNow).total_seconds() + 0.1, self.takePhoto, args=[bib, t] ).start()
						continue
						
					times, frames = self.fcb.findBeforeAfter( tFind, 0, 1 )
					for i, frame in enumerate( frames ):
						t = (times[i]-self.refTime).total_seconds()
						self.frameSaver.save( GetFilename(bib, t, self.dirName, i), bib, t, frame )
					self.frameCount += len(frames)
					self.queue.task_done()
					
				elif message[0] == 'Terminate':
					self.queue.task_done()
					self.reset()
					keepGoing = False
					break
				
			# Sleep until we need to grab the next frame.
			frameWait = self.frameDelay - (now() - tNow).total_seconds()
			if frameWait < 0.0:
				frameWait = self.frameDelay		# Give some more time if we are falling behind.
			time.sleep( frameWait )
	
	def stop( self ):
		self.queue.put( ['Terminate'] )
		self.join()
		if self.frameSaver.is_alive():
			self.frameSaver.stop()
		self.frameSaver = None
	
	def takePhoto( self, bib, t ):
		self.queue.put( ['Save', bib, t] )
	
	def getT( self, i ):
		return self.fcb.getT(i)
		
	def getFrame( self, i ):
		return self.fcb.getFrame(i)
	
	def getTimeFrame( self, i ):
		return (self.fcb.getT(i), self.fcb.getFrame(i))
		
	def findBeforeAfter( self, t, before = 0, after = 1 ):
		''' Call the frame cyclic buffer from race time, not clock time. '''
		tFind = self.refTime + timedelta( seconds = t )
		times, frames = self.fcb.findBeforeAfter( tFind, before, after )
		return zip( times, frames )
		
	def __len__( self ):
		return self.frameMax
Ejemplo n.º 15
0
class MainWin(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title="", size=(1000, 800)):
        wx.Frame.__init__(self, parent, id, title, size=size)

        self.fps = 25
        self.frameDelay = 1.0 / self.fps
        self.bufferSecs = 10

        self.tFrameCount = self.tLaunch = self.tLast = now()
        self.frameCount = 0
        self.frameCountUpdate = self.fps * 2
        self.fpsActual = 0.0
        self.fpt = timedelta(seconds=0)

        self.fcb = FrameCircBuf(self.bufferSecs * self.fps)

        self.config = wx.Config(
            appName="CrossMgrCamera", vendorName="SmartCyclingSolutions", style=wx.CONFIG_USE_LOCAL_FILE
        )

        self.requestQ = Queue()  # Select photos from photobuf.
        self.writerQ = Queue(400)  # Selected photos waiting to be written out.
        self.ftpQ = Queue()  # Photos waiting to be ftp'd.
        self.renamerQ = Queue()  # Photos waiting to be renamed and possibly ftp'd.
        self.messageQ = Queue()  # Collection point for all status/failure messages.

        self.SetBackgroundColour(wx.Colour(232, 232, 232))

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.primaryImage = ScaledImage(self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight))
        self.beforeImage = ScaledImage(self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight))
        self.afterImage = ScaledImage(self, style=wx.BORDER_SUNKEN, size=(imageWidth, imageHeight))
        self.beforeAfterImages = [self.beforeImage, self.afterImage]

        # ------------------------------------------------------------------------------------------------
        headerSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.logo = Utils.GetPngBitmap("CrossMgrHeader.png")
        headerSizer.Add(wx.StaticBitmap(self, bitmap=self.logo))

        self.title = wx.StaticText(
            self, label="CrossMgr Camera\nVersion {}".format(AppVerName.split()[1]), style=wx.ALIGN_RIGHT
        )
        self.title.SetFont(wx.FontFromPixelSize(wx.Size(0, 28), wx.FONTFAMILY_SWISS, wx.NORMAL, wx.FONTWEIGHT_NORMAL))
        headerSizer.Add(self.title, flag=wx.ALL, border=10)

        # ------------------------------------------------------------------------------
        self.cameraDeviceLabel = wx.StaticText(self, label="Camera Device:")
        self.cameraDevice = wx.StaticText(self)
        boldFont = self.cameraDevice.GetFont()
        boldFont.SetWeight(wx.BOLD)
        self.cameraDevice.SetFont(boldFont)
        self.reset = wx.Button(self, label="Reset Camera")
        self.reset.Bind(wx.EVT_BUTTON, self.resetCamera)
        cameraDeviceSizer = wx.BoxSizer(wx.HORIZONTAL)
        cameraDeviceSizer.Add(self.cameraDeviceLabel, flag=wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_RIGHT)
        cameraDeviceSizer.Add(self.cameraDevice, flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT, border=8)
        cameraDeviceSizer.Add(self.reset, flag=wx.ALIGN_CENTRE_VERTICAL | wx.LEFT, border=8)

        # ------------------------------------------------------------------------------
        self.targetProcessingTimeLabel = wx.StaticText(self, label="Target Frames:")
        self.targetProcessingTime = wx.StaticText(self, label=u"{:.3f}".format(self.fps))
        self.targetProcessingTime.SetFont(boldFont)
        self.targetProcessingTimeUnit = wx.StaticText(self, label="per sec")

        self.framesPerSecondLabel = wx.StaticText(self, label="Actual Frames:")
        self.framesPerSecond = wx.StaticText(self, label="25.000")
        self.framesPerSecond.SetFont(boldFont)
        self.framesPerSecondUnit = wx.StaticText(self, label="per sec")

        self.availableMsPerFrameLabel = wx.StaticText(self, label="Available Time Per Frame:")
        self.availableMsPerFrame = wx.StaticText(self, label=u"{:.0f}".format(1000.0 * self.frameDelay))
        self.availableMsPerFrame.SetFont(boldFont)
        self.availableMsPerFrameUnit = wx.StaticText(self, label="ms")

        self.frameProcessingTimeLabel = wx.StaticText(self, label="Actual Frame Processing:")
        self.frameProcessingTime = wx.StaticText(self, label="20")
        self.frameProcessingTime.SetFont(boldFont)
        self.frameProcessingTimeUnit = wx.StaticText(self, label="ms")

        pfgs = wx.FlexGridSizer(rows=0, cols=6, vgap=4, hgap=8)
        fRight = wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_RIGHT
        fLeft = wx.ALIGN_CENTRE_VERTICAL

        # ------------------- Row 1 ------------------------------
        pfgs.Add(self.targetProcessingTimeLabel, flag=fRight)
        pfgs.Add(self.targetProcessingTime, flag=fRight)
        pfgs.Add(self.targetProcessingTimeUnit, flag=fLeft)
        pfgs.Add(self.availableMsPerFrameLabel, flag=fRight)
        pfgs.Add(self.availableMsPerFrame, flag=fRight)
        pfgs.Add(self.availableMsPerFrameUnit, flag=fLeft)

        # ------------------- Row 2 ------------------------------
        pfgs.Add(self.framesPerSecondLabel, flag=fRight)
        pfgs.Add(self.framesPerSecond, flag=fRight)
        pfgs.Add(self.framesPerSecondUnit, flag=fLeft)
        pfgs.Add(self.frameProcessingTimeLabel, flag=fRight)
        pfgs.Add(self.frameProcessingTime, flag=fRight)
        pfgs.Add(self.frameProcessingTimeUnit, flag=fLeft)

        statsSizer = wx.BoxSizer(wx.VERTICAL)
        statsSizer.Add(cameraDeviceSizer)
        statsSizer.Add(pfgs, flag=wx.TOP, border=8)
        headerSizer.Add(statsSizer, flag=wx.ALL, border=4)
        mainSizer.Add(headerSizer)

        self.messagesText = wx.TextCtrl(
            self, style=wx.TE_READONLY | wx.TE_MULTILINE | wx.HSCROLL, size=(350, imageHeight)
        )
        self.messageManager = MessageManager(self.messagesText)

        border = 2
        row1Sizer = wx.BoxSizer(wx.HORIZONTAL)
        row1Sizer.Add(self.primaryImage, flag=wx.ALL, border=border)
        row1Sizer.Add(self.messagesText, 1, flag=wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND, border=border)
        mainSizer.Add(row1Sizer, 1, flag=wx.EXPAND)

        row2Sizer = wx.BoxSizer(wx.HORIZONTAL)
        row2Sizer.Add(self.beforeImage, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM, border=border)
        row2Sizer.Add(self.afterImage, flag=wx.RIGHT | wx.BOTTOM, border=border)
        mainSizer.Add(row2Sizer)

        # ------------------------------------------------------------------------------------------------
        # Create a timer to update the frame loop.
        #
        self.timer = wx.Timer()
        self.timer.Bind(wx.EVT_TIMER, self.frameLoop)

        self.Bind(wx.EVT_CLOSE, self.onCloseWindow)

        self.readOptions()
        self.SetSizerAndFit(mainSizer)

        # Start the message reporting thread so we can see what is going on.
        self.messageThread = threading.Thread(target=self.showMessages)
        self.messageThread.daemon = True
        self.messageThread.start()

        self.grabFrameOK = False

        for i in [self.primaryImage, self.beforeImage, self.afterImage]:
            i.SetTestImage()

            # Start the frame loop.
        delayAdjustment = 0.80 if "win" in sys.platform else 0.98
        ms = int(1000 * self.frameDelay * delayAdjustment)
        self.timer.Start(ms, False)

    def Start(self):
        self.messageQ.put(("", "************************************************"))
        self.messageQ.put(("started", now().strftime("%Y/%m/%d %H:%M:%S")))
        self.startThreads()
        self.startCamera()

    def showMessages(self):
        while 1:
            message = self.messageQ.get()
            assert len(message) == 2, "Incorrect message length"
            cmd, info = message
            wx.CallAfter(self.messageManager.write, "{}:  {}".format(cmd, info) if cmd else info)

    def startCamera(self):
        self.camera = None
        try:
            self.camera = Device(max(self.getCameraDeviceNum(), 0))
        except Exception as e:
            self.messageQ.put(("camera", "Error: {}".format(e)))
            return False

            # self.camera.set_resolution( 640, 480 )
        self.messageQ.put(("camera", "Successfully Connected: Device: {}".format(self.getCameraDeviceNum())))
        for i in self.beforeAfterImages:
            i.SetTestImage()
        return True

    def startThreads(self):
        self.grabFrameOK = False

        self.listenerThread = SocketListener(self.requestQ, self.messageQ)
        error = self.listenerThread.test()
        if error:
            wx.MessageBox(
                "Socket Error:\n\n{}\n\nIs another CrossMgrVideo or CrossMgrCamera running on this computer?".format(
                    error
                ),
                "Socket Error",
                wx.OK | wx.ICON_ERROR,
            )
            wx.Exit()

        self.writerThread = threading.Thread(target=PhotoWriter, args=(self.writerQ, self.messageQ, self.ftpQ))
        self.writerThread.daemon = True

        self.renamerThread = threading.Thread(target=PhotoRenamer, args=(self.renamerQ, self.writerQ, self.messageQ))
        self.renamerThread.daemon = True

        self.ftpThread = threading.Thread(target=FTPWriter, args=(self.ftpQ, self.messageQ))
        self.ftpThread.daemon = True

        self.fcb = FrameCircBuf(int(self.bufferSecs * self.fps))

        self.listenerThread.start()
        self.writerThread.start()
        self.renamerThread.start()
        self.ftpThread.start()

        self.grabFrameOK = True
        self.messageQ.put(("threads", "Successfully Launched"))
        return True

    def frameLoop(self, event=None):
        if not self.grabFrameOK:
            return

        self.frameCount += 1
        tNow = now()

        # Compute frame rate statistics.
        if self.frameCount == self.frameCountUpdate:
            self.fpsActual = self.frameCount / (tNow - self.tFrameCount).total_seconds()
            self.frameCount = 0
            self.tFrameCount = tNow
            self.framesPerSecond.SetLabel(u"{:.3f}".format(self.fpsActual))
            self.availableMsPerFrame.SetLabel(u"{:.0f}".format(1000.0 / self.fpsActual))
            self.frameProcessingTime.SetLabel(u"{:.0f}".format(1000.0 * self.fpt.total_seconds()))

        try:
            image = self.camera.getImage()
        except Exception as e:
            self.messageQ.put(("error", "Webcam Failure: {}".format(e)))
            self.grabFrameOK = False
            return

        image = AddPhotoHeader(
            PilImageToWxImage(image),
            time=tNow,
            raceSeconds=(tNow - self.tLaunch).total_seconds(),
            bib=999,
            firstNameTxt=u"Firstname",
            lastNameTxt=u"LASTNAME",
            teamTxt=u"Team",
            raceNameTxt="Racename",
        )
        self.fcb.append(tNow, image)
        wx.CallAfter(self.primaryImage.SetImage, image)

        # Process any save messages
        while 1:
            try:
                message = self.requestQ.get(False)
            except Empty:
                break

            tSearch = message["time"]
            advanceSeconds = message.get("advanceSeconds", 0.0)
            if advanceSeconds:
                tSearch += timedelta(seconds=advanceSeconds)
            times, frames = self.fcb.findBeforeAfter(tSearch, 1, 1)
            if not frames:
                self.messageQ.put(
                    ("error", "No photos for {} at {}".format(message.get("bib", None), message["time"].isoformat()))
                )

            lastImage = None
            for i, f in enumerate(frames):

                fname = GetPhotoFName(
                    message["dirName"], message.get("bib", None), message.get("raceSeconds", None), i, times[i]
                )
                image = AddPhotoHeader(
                    f,
                    message.get("bib", None),
                    message.get("time", None),
                    message.get("raceSeconds", None),
                    message.get("firstName", u""),
                    message.get("lastName", u""),
                    message.get("team", u""),
                    message.get("raceName", u""),
                )

                # if lastImage is not None and image.GetData() == lastImage.GetData():
                # 	self.messageQ.put( ('duplicate', '"{}"'.format(os.path.basename(fname))) )
                # 	continue

                wx.CallAfter(self.beforeAfterImages[i].SetImage, image)

                SaveImage(fname, image, message.get("ftpInfo", None), self.messageQ, self.writerQ)
                lastImage = image

            if (now() - tNow).total_seconds() > self.frameDelay / 2.0:
                break

        self.fpt = now() - tNow
        self.tLast = tNow

    def shutdown(self):
        # Ensure that all images in the queue are saved.
        self.grabFrameOK = False
        if hasattr(self, "writerThread"):
            self.writerQ.put(("terminate",))
            self.writerThread.join()

    def resetCamera(self, event):
        self.writeOptions()

        dlg = ConfigDialog(self, self.getCameraDeviceNum())
        ret = dlg.ShowModal()
        cameraDeviceNum = dlg.GetCameraDeviceNum()
        dlg.Destroy()
        if ret != wx.ID_OK:
            return

        self.setCameraDeviceNum(cameraDeviceNum)
        self.grabFrameOK = self.startCamera()

    def setCameraDeviceNum(self, num):
        self.cameraDevice.SetLabel(unicode(num))

    def getCameraDeviceNum(self):
        return int(self.cameraDevice.GetLabel())

    def onCloseWindow(self, event):
        self.shutdown()
        wx.Exit()

    def writeOptions(self):
        self.config.Write("CameraDevice", self.cameraDevice.GetLabel())
        self.config.Flush()

    def readOptions(self):
        self.cameraDevice.SetLabel(self.config.Read("CameraDevice", u"0"))