class Module(object): """ This is the IDM's Main class, called by mousetrap.py in the load process. """ def __init__(self, controller, stgs = {}): """ IDM's init function. Arguments: - self: The main object pointer. - controller: mousetrap main class pointer. This is passed by MouseTrap's controller (mousetrap.py) when loaded. - stgs: Possible settings loaded from the user's settings file. If there aren't settings the IDM will use the a_settings dict. """ debug.debug("mousetrap.ocvfw.idm", "Starting %s idm" % a_name) self.img = None self.ctr = controller self.cap = None self.stgs = stgs ############################## # MOTION RELATED VARIABLES # ############################## self.fel = eye_locator(FEL_NAME) ############################## # ACTION POINTS # ############################## self.mpPointer = None ############################## # CLICK RELATED VARIABLES # ############################## self.isMoving = False self.prepare_config() debug.info("mousetrap.ocvfw.idm", "Forhead Algorithm loaded") def prepare_config(self): """ Prepares the IDM using the settings Arguments: - self: The main object pointer """ global a_settings for key in self.stgs: pass def set_capture(self, cam): """ Initialize the capture and sets the main settings. Arguments: - self: The main object pointer - cam: The camera device index. For Example: 0 = /dev/video0, 1 = /dev/video1 """ debug.debug("mousetrap.ocvfw.idm", "Setting Capture") self.cap = Capture(async=False, idx=cam, backend="OcvfwPython") self.cap.change(color="rgb") self.cap.set_camera("lk_swap", True) def calc_motion(self): if not hasattr(self.cap, "forehead"): self.get_forehead() def get_capture(self): """ Sets the forehead point if needed and returns the formated image. Arguments: - self: The main object pointer returns self.cap.image() """ self.cap.sync() if not hasattr(self.cap, "leye") or not hasattr(self.cap, "reye"): self.get_eye() return self.cap def get_pointer(self): """ Returns the new MousePosition Arguments: - self: The main object pointer """ return True def get_eye(self): eyes = False face = self.cap.get_area(commons.haar_cds['Face']) if face: cvtile = cv.cvCreateMat(128,128,cv.CV_8UC3) bwtile = cv.cvCreateMat(128,128,cv.CV_8U) areas = [ (pt[1].x - pt[0].x)*(pt[1].y - pt[0].y) for pt in face ] startF = face[areas.index(max(areas))][0] endF = face[areas.index(max(areas))][1] facerect = self.cap.rect(startF.x, startF.y, endF.x - startF.x, endF.y - startF.y) if not facerect: return cv.cvResize(facerect, cvtile) cv.cvCvtColor( cvtile, bwtile, cv.CV_BGR2GRAY ) leye,reye,lcp,rcp = self.fel.locateEyes(bwtile) leye = pv.Point(leye) reye = pv.Point(reye) leye_x = int((float(leye.X())*facerect.width/cvtile.width) + startF.x) leye_y = int((float(leye.Y())*facerect.height/cvtile.height) + startF.y) reye_x = int((float(reye.X())*facerect.width/cvtile.width) + startF.x) reye_y = int((float(reye.Y())*facerect.height/cvtile.height) + startF.y) eye_rect = { "startX" : leye_x - 5, "startY" : leye_y - 5, "endX" : leye_x + 5, "endY" : leye_y + 5} #self.cap.image(self.cap.rect(leye_x - 5, leye_y - 5, 20, 20)) if not hasattr(self.cap, "leye"): self.cap.add( Point("point", "leye", [int(leye_x), int(leye_y)], parent=self.cap, follow=True) ) else: self.cap.add( Point("point", "reye", [int(reye_x), int(reye_y)], parent=self.cap, follow=True) ) # Shows the face rectangle #self.cap.add( Graphic("rect", "Face", ( startF.x, startF.y ), (endF.x, endF.y), parent=self.cap) ) self.foreheadOrig = None return False
class Module(object): """ This is the IDM's Main class, called by mousetrap.py in the load process. """ def __init__(self, controller, stgs = {}): """ IDM's init function. Arguments: - self: The main object pointer. - controller: mousetrap main class pointer. This is passed by MouseTrap's controller (mousetrap.py) when loaded. - stgs: Possible settings loaded from the user's settings file. If there aren't settings the IDM will use the a_settings dict. """ debug.debug("mousetrap.ocvfw.idm", "Starting %s idm" % a_name) self.ctr = controller self.cap = None self.stgs = stgs ############################## # MOTION RELATED VARIABLES # ############################## #self.step = self.settings.getint( "mouse", "stepSpeed" ) self.tmpl = None self.timer = None self.prepare_config() debug.info("mousetrap.ocvfw.idm", "Finger Algorithm loaded") def prepare_config(self): """ Prepares the IDM using the settings Arguments: - self: The main object pointer """ global a_settings for key in self.stgs: a_settings[key] = self.stgs[key] self.stgs = a_settings def set_capture(self, cam): """ Initialize the capture and sets the main settings. Arguments: - self: The main object pointer - cam: The camera device index. For Example: 0 = /dev/video0, 1 = /dev/video1 """ debug.debug("mousetrap.ocvfw.idm", "Setting Capture") self.cap = Capture(async=True, idx=cam, backend="OcvfwPython") self.cap.change(color="rgb") self.cap.set_camera("lk_swap", True) def calc_motion(self): if not hasattr(self.cap, "finger"): self.follow_finger() def get_capture(self): """ Sets the forehead point if needed and returns the formated image. Arguments: - self: The main object pointer returns self.cap.image() """ if not hasattr(self.cap, "finger") and not hasattr(self.cap, "finger"): self.get_template() return self.cap def get_template(self): """ Sets the template capture rectangle. Arguments: - self: The main object pointer """ self.cap.add(Graphic("rect", "tpl_rect", ( 325, 325 ), (425, 425), parent=self.cap)) self.timer = Timer(10.0, self.follow_finger) self.timer.start() def cap_template(self): """ Captures the template Arguments: - self: The main object pointer. """ debug.debug("finger", "Trying to save capture template") self.timer.cancel() self.cap.save(os.path.abspath("%s/tmpl.jpg" % self.stgs["conf_path"]), self.cap.rect(100, 100, 150, 150)) def load_template(self): """ Loads the finger template if exists Arguments: - self: The main object pointer. """ try: self.tmpl = co.hg.cvLoadImage("%s/tmpl.jpg" % self.stgs["conf_path"], 3) debug.debug("finger", "Loading template") except: pass def get_pointer(self): """ Returns the new MousePosition Arguments: - self: The main object pointer """ if hasattr(self.cap, "finger"): return self.cap.finger def follow_finger(self): self.cap.add( Point("point", "finger", ( 375, 375 ), parent=self.cap, follow=True) )