[mousetrap] Fixed Makefiles and Installation process



commit 1b7de5a019dfe94a5e70a1ef92ec231d545e23a0
Author: Flavio Percoco Premoli <flaper87 gmail com>
Date:   Sun Oct 18 18:56:11 2009 +0200

    Fixed Makefiles and Installation process

 {dev_tools => samples}/idm_example.py              |    0
 samples/userSettings.cfg                           |   41 ++++++
 src/mousetrap/commons.py                           |   53 ++++++++
 src/mousetrap/ui/scripts/color.py                  |  140 ++++++++++++++++++++
 src/ocvfw/backends/Makefile.am                     |    9 ++
 .../idm_example.py => src/ocvfw/idm/finger.py      |  130 +++++++++++-------
 6 files changed, 323 insertions(+), 50 deletions(-)
---
diff --git a/dev_tools/idm_example.py b/samples/idm_example.py
similarity index 100%
copy from dev_tools/idm_example.py
copy to samples/idm_example.py
diff --git a/samples/userSettings.cfg b/samples/userSettings.cfg
new file mode 100644
index 0000000..1a75f3c
--- /dev/null
+++ b/samples/userSettings.cfg
@@ -0,0 +1,41 @@
+[forehead]
+speed = 2
+script = screen
+
+[color]
+blue = 1.5
+script = screen
+green = 1.5
+red = 1.5
+hrange = 15
+selection_size = 30
+vscale = 10
+
+[gui]
+showCapture = True
+showMainGui = True
+showPointMapper = True
+
+[access]
+reqMovement = 10
+
+[finger]
+speed = 2
+script = screen
+
+[cam]
+inputDevIndex = 0
+flipImage = False
+
+[main]
+debugLevel = 10
+addon = cpu
+algorithm = forehead
+startCam = True
+
+[mouse]
+defClick = b1c
+stepSpeed = 5
+
+[cpu]
+
diff --git a/src/mousetrap/commons.py b/src/mousetrap/commons.py
new file mode 100644
index 0000000..943bbc3
--- /dev/null
+++ b/src/mousetrap/commons.py
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+
+# MouseTrap
+#
+# Copyright 2009 Flavio Percoco Premoli
+#
+# This file is part of mouseTrap.
+#
+# MouseTrap is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License v2 as published
+# by the Free Software Foundation.
+#
+# mouseTrap is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with mouseTrap.  If not, see <http://www.gnu.org/licenses/>.
+
+""" Common MouseTrap Functions. """
+
+__id__        = "$Id$"
+__version__   = "$Revision$"
+__date__      = "$Date$"
+__copyright__ = "Copyright (c) 2008 Flavio Percoco Premoli."
+__license__   = "GPLv2"
+
+import os
+import re
+
+def get_py_list(dirlist):
+    """
+    Checks for .py files on directories in dirlist 
+    and removes the extensions. 
+        
+    Arguments:
+    - dirlist: The directories list.
+    """
+
+    if not type(dirlist) is list:
+        dirlist = [dirlist]
+        
+    reg = re.compile(r'([A-Za-z0-9]+)\.py$', re.DOTALL)
+
+    group = []
+    for dir in dirlist:
+        if not os.path.isdir(dir):
+            continue
+        group.append([ mod[0] for mod in [ reg.findall(f) for f in os.listdir("%s/" % dir) if "handler" not in f] if mod ])
+
+    return [] + [x for l in group for x in l]
+
diff --git a/src/mousetrap/ui/scripts/color.py b/src/mousetrap/ui/scripts/color.py
new file mode 100644
index 0000000..9179442
--- /dev/null
+++ b/src/mousetrap/ui/scripts/color.py
@@ -0,0 +1,140 @@
+# -*- coding: utf-8 -*-
+
+# MouseTrap
+#
+# Copyright 2009 Flavio Percoco Premoli
+#
+# This file is part of mouseTrap.
+#
+# MouseTrap is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License v2 as published
+# by the Free Software Foundation.
+#
+# mouseTrap is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with mouseTrap.  If not, see <http://www.gnu.org/licenses/>.
+
+
+"""The Screen Mode script."""
+
+__id__        = "$Id$"
+__version__   = "$Revision$"
+__date__      = "$Date$"
+__copyright__ = "Copyright (c) 2008 Flavio Percoco Premoli"
+__license__   = "GPLv2"
+
+import mousetrap.environment as env
+import mousetrap.lib.mouse as mouse
+import mousetrap.lib.settings as settings
+
+# pylint: disable-msg=F0401
+# Unable to import 'widgets' (No module named widgets)
+# Widgets is in the parent folder
+from ..widgets import Mapper
+
+from ocvfw import pocv
+
+# The name given for the config file
+setName = "screen"
+
+## Internal Modes
+modes = { "screen|abs"  :  "Mouse Absolute Movements",
+          "screen|rel"  :  "Mouse Relative Movements"}
+
+
+
+# We get too many E1101 messages, but We know what we're doing.
+# Mapper does have those methods.
+# pylint: disable-msg=E1101
+class ScriptClass(Mapper):
+
+    def __init__(self):
+        Mapper.__init__(self, 200, 160)
+
+        self.point       = None
+        self.border_with = 0
+        """
+        self.threshold = 1 # Threshold for mouse movement.  If any mouse movement is 
+                           # smaller than self.threshold pixels (I guess) then the mouse is not moved.
+                           # Trying to reduce jitteriness. Not working.
+        self.same_location_count = 0 # Yeah this variable name is bad.  It means the number of times
+                                     # that the cursor has been in the same location under the threshold.
+        """
+        self.connect("expose_event", self.expose_event)
+        
+        self.cfg = settings.load()
+    
+        algorithm_info = pocv.get_idm_inf("color")
+        
+        self.scale = self.cfg.get("color", "vscale") 
+
+    def update_items(self, point):
+        self.point = point
+        #self.point.set_opencv(self.point)
+        self.calc_move()
+        self.queue_draw()
+
+    def expose_event(self, widget, event):
+        self.width, self.height = self.allocation[2], self.allocation[3]
+
+        self.draw_rectangle(self.border_with,
+                            self.border_with,
+                            self.width - 2*self.border_with,
+                            self.height - 2*self.border_with,
+                            self.style.fg[self.state],
+                            5.0)
+
+        self.center = { "x" : self.width / 2,
+                        "y" : self.height / 2 }
+
+        #we need to figure out how to get access to the cfg file from here
+        self.vscreen = { "x" : self.center["x"] - int(self.scale) * 4 / 2,
+                         "y" : self.center["y"] - int(self.scale) * 3 / 2,
+                         "width"  : int(self.scale) * 4,
+                         "height" : int(self.scale) * 3}
+
+        self.draw_rectangle( self.vscreen["x"], self.vscreen["y"],
+                             self.vscreen["width"], self.vscreen["height"],
+                             self.style.fg[self.state], 5.0)
+
+        if hasattr(self.point, "abs_diff"):
+            self.vpoint = { "x" : self.center["x"] - self.point.abs_diff.x,
+                            "y" : self.center["y"] + self.point.abs_diff.y }
+
+            self.draw_point( self.vpoint["x"], self.vpoint["y"], 2)
+
+    def calc_move(self):
+        if not hasattr(self, "vpoint"):
+            return False
+
+        x, y = mouse.position()
+        """
+        # If the difference between the new point and the last point is less than the threshold:
+        if -self.threshold < self.point.rel_diff.x < self.threshold or -self.threshold < self.point.rel_diff.y < self.threshold:
+            self.same_location_count+=1
+            # If the point has been in the same location for more than 5 frames:
+            if self.same_location_count > 5:
+                mouse.move(x,y) # Leave the mouse in the same location
+                return False
+        """
+        par = ["width", "height"]
+
+        new_x, new_y = [ (float(poss)/self.vscreen[par[i]])*env.screen[par[i]]
+                      for i,poss in enumerate([ (self.vscreen["width"]/2) - ( self.center["x"] - self.vpoint["x"]),
+                                                (self.vscreen["height"]/2) - ( self.center["y"] - self.vpoint["y"] ) ])]
+
+        mouse.move( new_x, new_y)
+        #mouse.move(self.point.x, self.point.y)
+
+    def prefferences(self):
+        """
+        This function contains the screen's script prefferences dialog tab.
+
+        Arguments:
+        - self: the main object pointer.
+        """
+        pass
diff --git a/src/ocvfw/backends/Makefile.am b/src/ocvfw/backends/Makefile.am
new file mode 100644
index 0000000..cc69bba
--- /dev/null
+++ b/src/ocvfw/backends/Makefile.am
@@ -0,0 +1,9 @@
+ocvfw_pathdir=$(pyexecdir)
+
+ocvfw_python_PYTHON = \
+	OcvfwBase.py \
+	OcvfwCtypes.py \
+	OcvfwPython.py \
+	__init__.py
+
+ocvfw_pythondir=$(pyexecdir)/ocvfw/backends
diff --git a/dev_tools/idm_example.py b/src/ocvfw/idm/finger.py
similarity index 52%
rename from dev_tools/idm_example.py
rename to src/ocvfw/idm/finger.py
index 549d290..5b47c4d 100644
--- a/dev_tools/idm_example.py
+++ b/src/ocvfw/idm/finger.py
@@ -27,18 +27,16 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2008 Flavio Percoco Premoli"
 __license__   = "GPLv2"
 
+import os
 import ocvfw.debug as debug
-import ocvfw.commons as commons
-from ocvfw.dev.camera import Capture, Point
-
-# IDM's Information
-# a_name: IDM's name
-#   This is used by the settings gui to identify the idm
-# a_description: IDM's Description
-# a_settings: Possible settings needed by the idm. For Example: { 'var_name' : { 'value' : default_value}, 'var_name2' : { 'value' : default_value} }
-a_name = "IDM Name"
-a_description = "Forehead point tracker based on LK Algorithm"
-a_settings = { 'speed' : {"value":2}}
+import ocvfw.commons as co
+from ocvfw.dev.camera import Capture, Point, Graphic
+from threading import Timer
+
+a_name = "Finger"
+a_description = "Finger point tracker based on LK Algorithm"
+a_settings = { "speed" : {"value":2 }, 
+               "conf_path" : "%s/.finger" % os.path.expanduser("~")}
 
 class Module(object):
     """
@@ -54,24 +52,24 @@ class Module(object):
         - 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.
+        ##############################
+        #  MOTION RELATED VARIABLES  #
+        ##############################
+
+        #self.step         = self.settings.getint( "mouse", "stepSpeed" )
+        self.tmpl          = None
+
+        self.timer         = None
+
         self.prepare_config()
-        
-        debug.info("ocvfw.idm", "%s Algorithm loaded" % a_name)
+        debug.info("ocvfw.idm", "Finger Algorithm loaded")
 
     def prepare_config(self):
         """
@@ -81,15 +79,11 @@ class Module(object):
         - 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
+            a_settings[key] = self.stgs[key]
+
+        self.stgs = a_settings
 
     def set_capture(self, cam):
         """
@@ -102,44 +96,80 @@ class Module(object):
         
         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")
+        self.cap.set_camera("lk_swap", True)
+
+
+    def calc_motion(self):
+        if not hasattr(self.cap, "finger"):
+            self.follow_finger()
 
     def get_capture(self):
         """
-        Gets the last queried and formated image.
-        Function used by the mousetrap/ui/main.py 
-        to get the output image
+        Sets the forehead point if needed and returns the formated image.
 
         Arguments:
         - self: The main object pointer
 
-        returns self.cap.resize(200, 160, True)
+        returns self.cap.image()
         """
 
-        # We return the self.cap object, the method calling
-        # this method will chose if resize the image or not.
+        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.
-        Function used to pass the Mouse Pointer position
-        to the Scripts.
+        Returns the new MousePosition
 
         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
+        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) )
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]