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("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("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("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("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.forehead = None self.foreheadLast = None self.foreheadOrig = None self.foreheadDiff = None self.stopMove = None self.startMove = None self.track_object = 1 ############################## # ACTION POINTS # ############################## self.mpPointer = None ############################## # CLICK RELATED VARIABLES # ############################## self.isMoving = False self.prepare_config() debug.info("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("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, "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() """ if not hasattr(self.cap, "forehead"): self.get_forehead() #return self.cap.resize(200, 160, True) return self.cap def get_pointer(self): """ Returns the new MousePosition Arguments: - self: The main object pointer """ if hasattr(self.cap, "forehead"): return self.cap.forehead def get_forehead(self): eyes = False #self.cap.add_message("Getting Forehead!!!") face = self.cap.get_area(commons.haar_cds['Face']) if face: 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] # Shows the face rectangle #self.cap.add( Graphic("rect", "Face", ( startF.x, startF.y ), (endF.x, endF.y), parent=self.cap) ) eyes = self.cap.get_area( commons.haar_cds['Eyes'], {"start" : startF.x, "end" : startF.y, "width" : endF.x - startF.x, "height" : endF.y - startF.y}, (startF.x, startF.y) ) if eyes: areas = [ (pt[1].x - pt[0].x)*(pt[1].y - pt[0].y) for pt in eyes ] point1, point2 = eyes[areas.index(max(areas))][0], eyes[areas.index(max(areas))][1] # Shows the eyes rectangle #self.cap.add(Graphic("rect", "Face", ( point1.x, point1.y ), (point2.x, point2.y), parent=self.cap)) X, Y = ( (point1.x + point2.x) / 2 ), ( point1.y + ( (point1.y + point2.y) / 2 ) ) / 2 self.cap.add( Point("point", "forehead", ( X, Y ), parent=self.cap, follow=True) ) return True self.foreheadOrig = None return False
class Module(object): def __init__(self, controller, stgs = {}): Camera.init() self.img = None self.ctr = controller self.cap = None self.stgs = stgs ############################## # MOTION RELATED VARIABLES # ############################## #self.step = self.settings.getint( "mouse", "stepSpeed" ) self.forehead = None self.foreheadLast = None self.foreheadOrig = None self.foreheadDiff = None self.stopMove = None self.startMove = None ############################## # ACTION POINTS # ############################## self.mpPointer = None ############################## # CLICK RELATED VARIABLES # ############################## self.isMoving = False self.prepare_config() def prepare_config(self): global a_settings for key in self.stgs: pass def set_capture(self, cam): self.cap = Capture(async=True, idx=cam) self.cap.change(color="rgb") def calc_motion(self): if not hasattr(self.cap, "forehead"): self.get_forehead() def get_image(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, "forehead"): self.get_forehead() return self.cap.image() def get_pointer(self): """ Returns the new MousePosition Arguments: - self: The main object pointer """ if hasattr(self.cap, "forehead"): return self.cap.forehead def get_forehead(self): eyes = False #self.cap.add_message("Getting Forehead!!!") face = self.cap.get_area(commons.haar_cds['Face']) if face: 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] # Shows the face rectangle #self.cap.add( Graphic("rect", "Face", ( startF.x, startF.y ), (endF.x, endF.y), parent=self.cap) ) eyes = self.cap.get_area( commons.haar_cds['Eyes'], {"start" : startF.x, "end" : startF.y, "width" : endF.x - startF.x, "height" : endF.y - startF.y}, (startF.x, startF.y) ) if eyes: areas = [ (pt[1].x - pt[0].x)*(pt[1].y - pt[0].y) for pt in eyes ] point1, point2 = eyes[areas.index(max(areas))][0], eyes[areas.index(max(areas))][1] # Shows the eyes rectangle #self.cap.add(Graphic("rect", "Face", ( point1.x, point1.y ), (point2.x, point2.y), parent=self.cap)) X, Y = ( (point1.x + point2.x) / 2 ), ( point1.y + ( (point1.y + point2.y) / 2 ) ) / 2 self.cap.add( Point("point", "forehead", ( X, Y ), parent=self.cap, follow=True) ) return True self.foreheadOrig = None return False