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 """ # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) self.cap = Capture(async=False, idx=cam, backend="OcvfwCtypes") #for some reason this is necessary! why?!? co.hg.cvNamedWindow( "Histogram", 1 ) if(self.debugLevel >= 30): co.hg.cvNamedWindow( "Mask", 1 ) co.hg.cvCreateTrackbar( "Vmin", "Mask", self.vmin, 256 ) co.hg.cvCreateTrackbar( "Vmax", "Mask", self.vmax, 256 ) co.hg.cvCreateTrackbar( "Smin", "Mask", self.smin, 256 ) co.hg.cvCreateTrackbar( "Smax", "Mask", self.smax, 256 ) co.hg.cvCreateTrackbar( "Hmin", "Mask", self.hmin, 180 ) co.hg.cvCreateTrackbar( "Hmax", "Mask", self.hmax, 180 ) # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb") self.cap.set_camera("lk_swap", True)
def set_capture(self, cam): """ Arguments: cam : Camera device index """ self.cap = Capture(async=True, idx=cam) self.cap.change(color="rgb", flip=True)
class Module(object): def __init__(self): # By Flavio's design, the modules themselves initialize the camera Camera.init() def set_capture(self, cam): """ Arguments: cam : Camera device index """ self.cap = Capture(async=True, idx=cam) self.cap.change(color="rgb", flip=True) def get_image(): """
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 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 """ # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) self.cap = Capture(async=True, idx=cam) # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb")
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") # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) # The default backend used is OcvfwPython but it is possible to chose other backends # that will use other libs to process images. self.cap = Capture(async=True, idx=cam, backend="OcvfwPython") # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb")
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): """ 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. """ # This will init the Camera class. # This class is used to handle the camera device. Camera.init() # Controller instance self.ctr = controller # Capture instance # The capture is the object containing the image # and all the possible methods to modify it. self.cap = None # IDM's Settings dict self.stgs = stgs # Prepares the IDM using the settings. self.prepare_config() def prepare_config(self): """ Prepares the IDM using the settings Arguments: - self: The main object pointer """ global a_settings # If the dict is empty then # use the default settings defined in a_settings if not self.stgs: self.stgs = a_settings # For each key do something if required by the module 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 """ # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) self.cap = Capture(async=True, idx=cam) # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb") def get_image(self): """ Gets the last queried and formated image. Function used by the mousetrap/ui/main.py to get the output image Arguments: - self: The main object pointer returns self.cap.resize(200, 160, True) """ # Calls the resize method passing the new with, height # specifying that the new image has to be a copy of the original # so, self.cap.resize will copy the original instead of modifying it. return self.cap.resize(200, 160, True) def get_pointer(self): """ Returns the new MousePosition. Function used to pass the Mouse Pointer position to the Scripts. Arguments: - self: The main object pointer """ # The return value has to be a Point() type object # Following the forehad IDM, The return is self.cap.forehead # which is created in the get_forehead function as an attribute # of self.cap return self.cap.pointer
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. """ # Debugging is always important debug.debug("ocvfw.idm", "Starting %s idm" % a_name) # Controller instance self.ctr = controller # Capture instance # The capture is the object containing the image # and all the possible methods to modify it. self.cap = None # IDM's Settings dict self.stgs = stgs # Prepares the IDM using the settings. self.prepare_config() debug.info("ocvfw.idm", "%s Algorithm loaded" % a_name) def prepare_config(self): """ Prepares the IDM using the settings Arguments: - self: The main object pointer """ global a_settings # If the dict is empty then # use the default settings defined in a_settings if not self.stgs: self.stgs = a_settings # For each key do something if required by the module 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") # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) # The default backend used is OcvfwPython but it is possible to chose other backends # that will use other libs to process images. self.cap = Capture(async=True, idx=cam, backend="OcvfwPython") # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb") def get_capture(self): """ Gets the last queried and formated image. Function used by the mousetrap/ui/main.py to get the output image Arguments: - self: The main object pointer returns self.cap.resize(200, 160, True) """ # We return the self.cap object, the method calling # this method will chose if resize the image or not. return self.cap def get_pointer(self): """ Returns the new MousePosition. Function used to pass the Mouse Pointer position to the Scripts. Arguments: - self: The main object pointer """ # The return value has to be a Point() type object # Following the forehad IDM, The return is self.cap.forehead # which is created in the get_forehead function as an attribute # of self.cap return self.cap.pointer
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. """ # Controller instance self.ctr = controller self.cfg = self.ctr.cfg if not self.cfg.has_section("color"): self.cfg.add_section("color") # Capture instance # The capture is the object containing the image # and all the possible methods to modify it. self.cap = None # IDM's Settings dict self.stgs = stgs # Prepares the IDM using the settings. self.prepare_config() #TODO: ADD CONDITIONAL TO ENSURE SETTINGS ARE SET EVEN IF YOU HAVE AN OLD CONFIG FILE # Initialization of variables needed for color tracking CAMshift algorithm. self.image = None self.hsv = None self.first_time=True # To keep track of whether or not we have initalized image objects self.select_object = 0 self.track_object = 0 self.origin = None #needed? self.selection = None self.track_window = None self.track_box = None self.track_comp = None self.hdims = 16 #Number of columns in the histogram # Variables to control the ranges for the color mask. self.vmin = c_int(10) # Value self.vmax = c_int(256) self.smin = c_int(80) # Saturation self.smax = c_int(256) self.hmin = c_int(0) # Hue self.hmax = c_int(180) # out of 180, not 256 def prepare_config(self): """ Prepares the IDM using the settings Arguments: - self: The main object pointer """ global a_settings self.debugLevel = self.ctr.cfg.getint("main", "debugLevel") print self.debugLevel # If the dict is empty then # use the default settings defined in a_settings if not self.stgs: self.stgs = a_settings # For each key do something if required by the module for key in self.stgs: pass def hsv2rgb(self, hue): """ Converts an HSV hue to RGB. Arguments: - self: The main object pointer - hue: the hue to convert to RGB. """ rgb=[0,0,0] sector_data= ((0,2,1), (1,2,0), (1,0,2), (2,0,1), (2,1,0), (0,1,2)) hue *= 0.033333333333333333333333333333333 sector = co.cv.cvFloor(hue) p = co.cv.cvRound(255*(hue - sector)) p ^= 255 if bool(sector & 1) else 0 rgb[sector_data[sector][0]] = 255 rgb[sector_data[sector][1]] = 0 rgb[sector_data[sector][2]] = p return co.cv.cvScalar(rgb[2], rgb[1], rgb[0], 0) 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 """ # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) self.cap = Capture(async=False, idx=cam, backend="OcvfwCtypes") #for some reason this is necessary! why?!? co.hg.cvNamedWindow( "Histogram", 1 ) if(self.debugLevel >= 30): co.hg.cvNamedWindow( "Mask", 1 ) co.hg.cvCreateTrackbar( "Vmin", "Mask", self.vmin, 256 ) co.hg.cvCreateTrackbar( "Vmax", "Mask", self.vmax, 256 ) co.hg.cvCreateTrackbar( "Smin", "Mask", self.smin, 256 ) co.hg.cvCreateTrackbar( "Smax", "Mask", self.smax, 256 ) co.hg.cvCreateTrackbar( "Hmin", "Mask", self.hmin, 180 ) co.hg.cvCreateTrackbar( "Hmax", "Mask", self.hmax, 180 ) # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb") self.cap.set_camera("lk_swap", True) def _convertColorDepth(self, color): """ Converts from 16 to 8 bit color depth. Necessary for OpenCV functions and GDK colors to interact as the former expects colors from 0-255 and the latter expects 0-65535. Arguments: - self: The main object pointer - color: The integer color value to convert to 0-255 """ return int((color / 65535.0) * 255) def rgb2hue(self, red, green, blue): """ Converts the rgb values from the config file into the corresponding hue value. This method was stolen from the gtk source! """ hue = 0.0 if (red > green): if (red > blue): cmax = red else: cmax = blue if (green < blue): cmin = green else: cmin = blue; else: if (green > blue): cmax = green else: cmax = blue if (red < blue): cmin = red else: cmin = blue val = cmax if (cmax != 0.0): sat = (cmax - cmin) / cmax else: sat = 0.0 if (sat == 0.0): hue = 0.0 else: delta = cmax - cmin if (red == cmax): hue = (green - blue) / delta elif (green == cmax): hue = 2 + (blue - red) / delta elif (blue == cmax): hue = 4 + (red - green) / delta hue /= 6.0; if (hue < 0.0): hue += 1.0 elif (hue > 1.0): hue -= 1.0 return (hue * 360) / 2 def update_hue_range(self, *args): """ WARNING: HACK by Ryan This method is used as a callback connected to the color picker save button's click event. I had to do this because we need to update the hue min/max in this idm whenever the user saves a new color. However, we can't poll a file's status to see if it's changed, so we routed the event to two callbacks. Suggestion: Maybe use a dictionary for configure settings and serialize it on quit. This would trivialize querying a settings structure and allow us comparatively free use of the data. Right now we're forced to keep in mind how often we query settings because hard drives are SLOW. """ temphue = self.rgb2hue(float(self.cfg.get("color", "red")), float(self.cfg.get("color", "green")), float(self.cfg.get("color", "blue"))) hrange = int(self.cfg.get("color","hrange")) / 2 self.hmin.value = int(max(temphue - hrange, 0)) self.hmax.value = int(min(temphue + hrange, 180)) def histogram_init(self): """ Initializes and displays a color histogram of the selected area Arguments: - self: The main object pointer """ co.cv.cvCalcHist( [self.hue], self.hist, 0, self.mask ) min_val, max_val = co.cv.cvGetMinMaxHistValue(self.hist) hbins = self.hist.bins[0] co.cv.cvConvertScale( hbins, hbins, 255. / max_val if max_val else 0., 0 ) co.cv.cvZero( self.histimg ) bin_w = self.histimg.width / self.hdims for i in xrange(self.hdims): val = co.cv.cvRound( co.cv.cvGetReal1D(hbins,i)*self.histimg.height/255 ) color = self.hsv2rgb(i*180./self.hdims) co.cv.cvRectangle( self.histimg, co.cv.cvPoint(i*bin_w,self.histimg.height), co.cv.cvPoint((i+1)*bin_w,self.histimg.height - val), color, -1, 8, 0 ) if(self.debugLevel >= 30): co.hg.cvShowImage( "Histogram", self.histimg ) def get_capture(self): """ Gets the last queried and formated image. Function used by the mousetrap/ui/main.py to get the output image Arguments: - self: The main object pointer returns self.cap.resize(200, 160, True) """ # Called to update image with latest frame from webcam self.cap.sync() #self.image = self.cap.image().origin needed?? self.image = self.cap.image() if self.first_time: # Initialization of images. Only needs to happen once. # TODO: What is a better way to get the image size? self.hue = co.cv.cvCreateImage( co.cv.cvGetSize(self.image), 8, 1 ) self.mask = co.cv.cvCreateImage( co.cv.cvGetSize(self.image), 8, 1 ) self.backproject = co.cv.cvCreateImage( co.cv.cvGetSize(self.image), 8, 1 ) self.hist = co.cv.cvCreateHist( [self.hdims], co.cv.CV_HIST_ARRAY, [[0, 180]] ) self.histimg = co.cv.cvCreateImage( co.cv.cvSize(320,200), 8, 3 ) self.temp = co.cv.cvCreateImage( co.cv.cvGetSize(self.image), 8, 3) #needed? co.cv.cvZero( self.histimg ) #Initialization of hue range from config file. temphue = self.rgb2hue(float(self.cfg.get("color", "red")), float(self.cfg.get("color", "green")), float(self.cfg.get("color", "blue"))) hrange = int(self.cfg.get("color","hrange")) / 2 self.hmin.value = int(max(temphue - hrange, 0)) self.hmax.value = int(min(temphue + hrange, 180)) #Creates object selection box self.origin = co.cv.cvPoint(self.image.width / 2, self.image.height / 2) self.selection = co.cv.cvRect(self.origin.x-50,self.origin.y-50,100,100) self.first_time=False self.hsv = self.cap.color("hsv", channel=3, copy=True) # Convert to HSV # If tracking if self.track_object != 0: #Masks pixels that fall outside desired range scalar1=co.cv.cvScalar(self.hmin.value,self.smin.value,min(self.vmin.value,self.vmax.value),0) scalar2=co.cv.cvScalar(self.hmax.value,self.smax.value,max(self.vmin.value,self.vmax.value),0) co.cv.cvInRangeS( self.hsv, scalar1, scalar2, self.mask ) co.cv.cvSplit(self.hsv, self.hue) # If tracking, first time if self.track_object < 0: co.cv.cvSetImageROI( self.hue, self.selection) co.cv.cvSetImageROI( self.mask, self.selection) self.histogram_init() co.cv.cvResetImageROI( self.hue ) co.cv.cvResetImageROI( self.mask ) self.track_window = self.selection self.track_object = 1 co.cv.cvCalcBackProject( [self.hue], self.backproject, self.hist ) co.cv.cvAnd(self.backproject, self.mask, self.backproject) #CamShift algorithm is called niter, self.track_comp, self.track_box = co.cv.cvCamShift( self.backproject, self.track_window, co.cv.cvTermCriteria( co.cv.CV_TERMCRIT_EPS | co.cv.CV_TERMCRIT_ITER, 10, 1 )) self.track_window = self.track_comp.rect if not self.origin: self.track_box.angle = -self.track_box.angle # Ensures that track_box size is always at least 0x0 if math.isnan(self.track_box.size.height): self.track_box.size.height = 0 if math.isnan(self.track_box.size.width): self.track_box.size.width = 0 #Creates the ellipse around the tracked object co.cv.cvEllipseBox( self.image, self.track_box, co.cv.CV_RGB(0,255,0), 3, co.cv.CV_AA, 0 ) #Updates cursor location information if (not hasattr(self.cap, "obj_center")): self.cap.add(Point("point", "obj_center", ( int(self.track_box.center.x), int(self.track_box.center.y )), parent=self.cap, follow=False)) else: self.cap.obj_center.set_opencv(co.cv.cvPoint(int(self.track_box.center.x), int(self.track_box.center.y))) #Displays selection box before tracking starts if not self.track_object: co.cv.cvSetImageROI( self.image, self.selection ) co.cv.cvXorS( self.image, co.cv.cvScalarAll(255), self.image ) co.cv.cvResetImageROI( self.image ) if(self.debugLevel >= 30): co.hg.cvShowImage( "Mask", self.mask) self.cap.color("rgb", channel=3, copy=True) # Calls the resize method passing the new with, height # specifying that the new image has to be a copy of the original # so, self.cap.resize will copy the original instead of modifying it. return self.cap def startTracking(self): """ Starts the tracking algorithm. This exists because we set up keyboard input in the main view to start and stop tracking. Maybe generalize this functionality to all idms? Arguments: - self: The main object pointer Raises: - ValueError: If either the selection height or width are less than or equal to zero. """ if (self.selection.width and self.selection.height <= 0): raise ValueError() self.track_object = -1 def stopTracking(self): """ Stops the tracking algorithm. This exists because we set up keyboard input in the main view to start and stop tracking. Maybe generalize this functionality to all idms? Arguments: - self: The main object pointer """ self.track_object = 0 def selSizeUp(self): """ Increases the size of the selection window. Arguments: - self: The main object pointer """ if 6 <= self.selection.x <=self.image.width-self.selection.width-6 and 6 <= self.selection.y <= self.image.height-self.selection.height-6: self.selection = co.cv.cvRect(self.selection.x-5,self.selection.y-5,self.selection.width+10,self.selection.height+10) def selSizeDown(self): """ Decreases the size of the selection window. Arguments: - self: The main object pointer """ if self.selection.width > 10 and self.selection.height > 10: self.selection = co.cv.cvRect(self.selection.x+5,self.selection.y+5,self.selection.width-10,self.selection.height-10) def selPositionLeft(self): """ Changes the location of the selection window. Arguments: - self: The main object pointer """ if 6 <= self.selection.x <= self.image.width: self.selection=co.cv.cvRect(self.selection.x-5,self.selection.y,self.selection.width,self.selection.height) def selPositionRight(self): """ Changes the location of the selection window. Arguments: - self: The main object pointer """ if 0 <= self.selection.x <= self.image.width-self.selection.width-6: self.selection=co.cv.cvRect(self.selection.x+5,self.selection.y,self.selection.width,self.selection.height) def selPositionUp(self): """ Changes the location of the selection window. Arguments: - self: The main object pointer """ if 6 <= self.selection.y <= self.image.height: self.selection=co.cv.cvRect(self.selection.x,self.selection.y-5,self.selection.width,self.selection.height) def selPositionDown(self): """ Changes the location of the selection window. Arguments: - self: The main object pointer """ if 0 <= self.selection.y <= self.image.height-self.selection.height-6: self.selection=co.cv.cvRect(self.selection.x,self.selection.y+5,self.selection.width,self.selection.height) def get_pointer(self): """ Returns the new MousePosition. Function used to pass the Mouse Pointer position to the Scripts. Arguments: - self: The main object pointer """ # The return value has to be a Point() type object # Following the forehad IDM, The return is self.cap.obj_center # which is created in the get_image function as an attribute # of self.cap if hasattr(self.cap, "obj_center"): return self.cap.obj_center else:
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. """ # This will init the Camera class. # This class is used to handle the camera device. Camera.init() # Controller instance self.ctr = controller # Config object self.cfg = self.ctr.cfg if not self.cfg.has_section("color"): self.cfg.add_section("color") # Capture instance # The capture is the object containing the image # and all the possible methods to modify it. self.cap = None # IDM's Settings dict self.stgs = stgs # Prepares the IDM using the settings. self.prepare_config() image = None self.hsv = None self.hue = None self.mask = None self.backproject = None self.histimg = None self.hist = None backproject_mode = 0 select_object = 0 self.track_object = 0 show_hist = 1 origin = None selection = None self.track_window = None self.track_box = None self.track_comp = None self.hdims = 16 # initial settings for value, saturation, and hue self.vmin = c_int(10) self.vmax = c_int(256) self.smin = c_int(80) self.smax = c_int(256) self.hmin = c_int(25) self.hmax = c_int(40) #this may have to be 180 and not 256, for whatever reason self.h2min = c_int(170) self.h2max = c_int(175) def prepare_config(self): """ Prepares the IDM using the settings Arguments: - self: The main object pointer """ global a_settings # If the dict is empty then # use the default settings defined in a_settings if not self.stgs: self.stgs = a_settings # For each key do something if required by the module for key in self.stgs: pass def on_mouse(event, x, y, flags, param): print "ADSFSF" global select_object, image, selection, origin, track_object if image is None: return if image.origin: y = image.height - y if select_object: selection.x = min(x,origin.x) selection.y = min(y,origin.y) selection.width = selection.x + abs(x - origin.x) selection.height = selection.y + abs(y - origin.y) selection.x = max( selection.x, 0 ) selection.y = max( selection.y, 0 ) selection.width = min( selection.width, image.width ) selection.height = min( selection.height, image.height ) selection.width -= selection.x selection.height -= selection.y if event == CV_EVENT_LBUTTONDOWN: origin = cvPoint(x,y) selection = cvRect(x,y,0,0) select_object = 1 elif event == CV_EVENT_LBUTTONUP: select_object = 0 if selection.width > 0 and selection.height > 0: track_object = -1 def hsv2rgb(hue): rgb=[0,0,0] sector_data= ((0,2,1), (1,2,0), (1,0,2), (2,0,1), (2,1,0), (0,1,2)) hue *= 0.033333333333333333333333333333333 sector = cvFloor(hue) p = cvRound(255*(hue - sector)) p ^= 255 if bool(sector & 1) else 0 rgb[sector_data[sector][0]] = 255 rgb[sector_data[sector][1]] = 0 rgb[sector_data[sector][2]] = p return cvScalar(rgb[2], rgb[1], rgb[0], 0) 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 """ # Starts the Capture using the async method. # This means that self.cap.sync() wont be called periodically # by the idm because the Capture syncs the image asynchronously (See dev/camera.py) self.cap = Capture(async=True, idx=cam) # This sets the final image default color to rgb. The default color is bgr. self.cap.change(color="rgb") def _convertColorDepth(self, color): """ Converts from 16 to 8 bit color depth. Necessary for OpenCV functions and GDK colors to interact as the former expects colors from 0-255 and the latter expects 0-65535. Arguments: - self: The main object pointer - color: The integer color value to convert to 0-255 """ return (int)(color / 65535.0) * 255 def get_image(self): """ Gets the last queried and formated image. Function used by the mousetrap/ui/main.py to get the output image Arguments: - self: The main object pointer returns self.cap.resize(200, 160, True) """ """argc = len(argv) if argc == 1 or (argc == 2 and argv[1].isdigit()): capture = cvCaptureFromCAM( int(argv[1]) if argc == 2 else 0 ) elif argc == 2: capture = cvCaptureFromAVI( argv[1] ) else: capture = None if not capture: print "Could not initialize capturing..." exit(-1) #HOTT KEYS!!!!1 print "Hot keys: \n" \ "\tESC - quit the program\n" \ "\tc - stop the tracking\n" \ "\tb - switch to/from backprojection view\n" \ "\th - show/hide object histogram\n" \ "To initialize tracking, select the object with mouse\n" #what windows do we want?""" cvNamedWindow( "Histogram", 1 ) cvNamedWindow( "CamShiftDemo", 1 ) #main image cvNamedWindow( "Mask", 1 ) #cvNamedWindow( "Backproject", 1) #cvNamedWindow( "Hue", 1) #enables mouse event monitoring in the main image window cvSetMouseCallback( "CamShiftDemo", self.on_mouse ) cvCreateTrackbar( "Vmin", "CamShiftDemo", self.vmin, 256 ) cvCreateTrackbar( "Vmax", "CamShiftDemo", self.vmax, 256 ) cvCreateTrackbar( "Smin", "CamShiftDemo", self.smin, 256 ) cvCreateTrackbar( "Smax", "CamShiftDemo", self.smax, 256 ) #select the ranges of hues you want to track cvCreateTrackbar( "Hmin", "CamShiftDemo", self.hmin, 180 ) cvCreateTrackbar( "Hmax", "CamShiftDemo", self.hmax, 180 ) #cvCreateTrackbar( "H2min", "CamShiftDemo", h2min, 180 ) #cvCreateTrackbar( "H2max", "CamShiftDemo", h2max, 180 ) #loop """while True: #get next frame frame = cvQueryFrame( self.cap.image ) if not frame: break if not image: # allocate all the buffers image = cvCreateImage( cvGetSize(frame), 8, 3 ) image.origin = frame.origin hsv = cvCreateImage( cvGetSize(frame), 8, 3 )""" self.hue = cvCreateImage( cvSize(640,480), 8, 1 ) self.mask = cvCreateImage( cvSize(640,480), 8, 1 ) """mask2 = cvCreateImage( cvGetSize(frame), 8, 1 ) maskcombo = cvCreateImage( cvGetSize(frame), 8, 1 )""" self.backproject = cvCreateImage( cvSize(640,480), 8, 1 ) self.hist = cvCreateHist( [self.hdims], CV_HIST_ARRAY, [[0, 180]] ) self.histimg = cvCreateImage( cvSize(320,200), 8, 3 ) cvZero( self.histimg ) self.cap.sync() print(type(self.cap.image())) #cvCopy(frame, image) self.hsv = self.cap.color("hsv", channel=3, copy=True) #make the image hsv #cvCvtColor( self.cap.image, self.hsv, CV_BGR2HSV ) self.track_object=-1 print "ABOUT TO GOT IN" if self.track_object != 0: print "GOT IN" #updates the hsv values to be masked print type(self.hsv) scalar1=cvScalar(self.hmin.value,self.smin.value,min(self.vmin.value,self.vmax.value),0) scalar2=cvScalar(self.hmax.value,self.smax.value,max(self.vmin.value,self.vmax.value),0) cvInRangeS( self.hsv, scalar1, scalar2, self.mask ) #cvInRangeS( self.hsv, cvScalar(h2min.value,smin.value,min(vmin.value,vmax.value),0), # cvScalar(h2max.value,smax.value,max(vmin.value,vmax.value),0), mask2 ) cvSplit(self.hsv, self.hue) #extract hue information? #cvOr(mask, mask2, maskcombo) #combine the masks so that EITHER color is accepted selection=cvRect(10,10,20,20) if track_object < 0: #OH OKAY negative means it's tracking so... make a histogram cvSetImageROI( self.hue, selection ) cvSetImageROI( self.mask, selection ) cvCalcHist( [self.hue], self.hist, 0, self.mask ); min_val, max_val = cvGetMinMaxHistValue(hist) hbins = self.hist.bins[0] cvConvertScale( hbins, hbins, 255. / max_val if max_val else 0., 0 ) cvResetImageROI( self.hue ) # ^ hisogram stuff, v tracking stuff cvResetImageROI( self.mask ) track_window = selection #the original window to track is your mouse selection track_object = 1 #now objects are being tracked #more histogram stuff -- now we're displaying it cvZero( self.histimg ) bin_w = self.histimg.width / self.hdims for i in xrange(self.hdims): val = cvRound( cvGetReal1D(hbins,i)*self.histimg.height/255 ) color = self.hsv2rgb(i*180./self.hdims) cvRectangle( self.histimg, cvPoint(i*bin_w,self.histimg.height), cvPoint((i+1)*bin_w,self.histimg.height - val), color, -1, 8, 0 ) #calculate the back projection (dunno what this is) cvCalcBackProject( [self.hue], self.backproject, self.hist ) #mask the backprojection (why? who knows) cvAnd(self.backproject, self.mask, self.backproject) #CAMSHIFT HAPPENS niter, self.track_comp, self.track_box = cvCamShift( self.backproject, self.track_window, cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 )) track_window = track_comp.rect #no idea if self.backproject_mode: cvCvtColor( self.backproject, self.cap.image, CV_GRAY2BGR ) #why?? if not self.cap.image.origin: track_box.angle = -track_box.angle #why?? # Make sure its a number. if math.isnan(self.track_box.size.height): self.track_box.size.height = 0 if math.isnan(track_box.size.width): self.track_box.size.width = 0 #draws an ellipse around it. the ellipse is GREEN!!!! cvEllipseBox( self.cap.image, self.track_box, CV_RGB(0,255,0), 3, CV_AA, 0 ) #still lost if bool(select_object) and selection.width > 0 and selection.height > 0: cvSetImageROI( self.cap.image, selection ) cvXorS( self.cap.image, cvScalarAll(255), self.cap.image ) cvResetImageROI( self.cap.image ) #hey let's show some stuff in those empty windows!!""" print "Showing the windows" print type(self.hsv) print type(self.histimg) print type(self.mask) cvShowImage( "CamShiftDemo", self.hsv ) cvShowImage( "Histogram", self.histimg ) cvShowImage( "Mask", self.mask ) #cvShowImage( "Backproject", backproject) #cvShowImage( "Hue", hue) #HOTT KEYS!!!!1 """c = '%c' % (cvWaitKey(10) & 255) if c == '\x1b': break elif c == 'b': backproject_mode ^= 1 elif c =='c': track_object = 0 cvZero( histimg ) elif c =='h': show_hist ^= 1 if not show_hist: cvDestroyWindow( "Histogram" ) else: cvNamedWindow( "Histogram", 1 ) print "Hoiahdfoiahs" return image # Calls the resize method passing the new with, height # specifying that the new image has to be a copy of the original # so, self.cap.resize will copy the original instead of modifying it.""" return self.cap.resize(200, 160, True) def get_pointer(self): """ Returns the new MousePosition. Function used to pass the Mouse Pointer position to the Scripts. Arguments: - self: The main object pointer """ # The return value has to be a Point() type object # Following the forehad IDM, The return is self.cap.forehead # which is created in the get_forehead function as an attribute # of self.cap self.cap.pointer=Point("point", "color", ( 1, 2 ), parent=self.cap, follow=True)
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
def set_capture(self, cam): self.cap = Capture(async=True, idx=cam) self.cap.change(color="rgb")
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