#!/usr/bin/env python from time import time, sleep import os from threading import Thread from myQueue import * from Queue import * from CVtypes import cv from common import * from string import join import pylab class CERTProcessor(Thread): def __init__(self, name, result_queue): Thread.__init__(self, name=name); self.result_queue = result_queue class CVImageViewer(CERTProcessor): def __init__(self, name, result_queue): CERTProcessor.__init__(self, name, result_queue) self.window = 'win' self.rate = Rate() cv.NamedWindow(self.window); self.flog = open('/tmp/CERTProcessor.log','w'); def run(self): rateQueue = [time()]; outCount = -1; self.nextLoop = True; while self.nextLoop: try: frame=self.result_queue.get_nowait(); if outCount+1 == frame.no or outCount==-1: #count frame rate outCount=frame.no self.rate.updateRate() cv.ShowImage(self.window, frame.img) print >>self.flog, 'FrameNo:',frame.no, \ 'delay:', '%.3f' % (frame.getDelay()), \ 'AU1:', '%.3f' % float(frame.result['(AU 1) Inner Brow Raise']), \ 'Timestamps:', join(map(lambda (t,n):"%s:%3.2f" % (t, n),frame.getTimestamps()), '/'); self.flog.flush() else: #not the immediate next frame self.result_queue.put(frame) if self.result_queue.qsize() > 100: print 'frame missing during processing, skip %d' % (outCount+1) outCount+=1 except Empty: sleep(0.01) def plotInit(self): #matplotlib window self.lastPlot = time(); self.plotDelay = 1e1000 self.processResult = self.myProcessResult self.plotdata = [] self.plotdataLen = 100; #around 5 sec pylab.figure() def plotProcessor(self): #a sliding window for past self.plotdataLen frames self.plotdata.append(float(frame.result['(AU 1) Inner Brow Raise'])) if len(self.plotdata) > self.plotdataLen: self.plotdata.pop(0) if time()-self.lastPlot > self.plotDelay: #dont issue plot command too frequent try: pylab.ioff() pylab.cla() self.processResult(self.plotdata) pylab.ion() pylab.draw() sleep(0.01) except Exception, inst: print inst, 'processResult() restored' self.processResult = self._processResult #fallback self.lastPlot = time() def myProcessResult(self, plotdata): pylab.plot(plotdata) def _processResult(self, plotdata): vec=map(lambda x:float(x['X2']) , plotdata) print vec