[mousetrap] Added idm example and some eye tracking tests



commit f3970c5e85bead0f98e94134120f4a5716c94563
Author: Flavio Percoco Premoli <flaper87 gmail com>
Date:   Fri Jun 19 17:23:33 2009 +0200

    Added idm example and some eye tracking tests

 .gitignore                 |    3 ++
 src/mousetrap/lib/httpd.py |    8 ++++-
 src/mousetrap/mousetrap.py |    4 +-
 src/mousetrap/ui/main.py   |    2 +-
 src/ocvfw/commons.py       |    7 +++++
 src/ocvfw/dev/camera.py    |   59 ++++++++++++++++++++++++++++++++++---------
 src/ocvfw/idm/forehead.py  |   27 ++++++++++++++++++-
 7 files changed, 90 insertions(+), 20 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index a0eebaf..6bc1ff5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,6 @@ prepare.sh
 .svn
 environment.py
 i18n.py
+compile
+*.patch
+*~
diff --git a/src/mousetrap/lib/httpd.py b/src/mousetrap/lib/httpd.py
index 9d98d5a..dc63cac 100644
--- a/src/mousetrap/lib/httpd.py
+++ b/src/mousetrap/lib/httpd.py
@@ -28,9 +28,12 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2008 Flavio Percoco Premoli"
 __license__   = "GPLv2"
 
+import mouse
 import thread
 import BaseHTTPServer
 
+from .. import debug
+from .. import environment as env
 
 class _HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
     """
@@ -59,7 +62,7 @@ class _HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         self.send_response(200)
         self.send_header("Content-type", "text/html")
         self.end_headers()
-        self.wfile.write("<html><body><p>mouseTrap 0.1</p></body></html>")
+        self.wfile.write("<html><body><p>mouseTrap %s</p></body></html>" % (env.version))
 
     def do_POST(self):
         """
@@ -75,7 +78,8 @@ class _HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
             if inputBody.startswith("move:"):
                 X, Y = inputBody[5:].split(",")
-                print X + " " + Y
+                debug.info( "mouseTrap.httpd", "Moving mouse to %s,%s" % (X, Y) )
+                mouse.move(int(X), int(Y))
                 self.send_response(200, 'OK')
         else:
             print( "mal" )
diff --git a/src/mousetrap/mousetrap.py b/src/mousetrap/mousetrap.py
index 5524b70..e36e2e2 100644
--- a/src/mousetrap/mousetrap.py
+++ b/src/mousetrap/mousetrap.py
@@ -83,8 +83,8 @@ class Controller():
             self.idm = idm.Module(self)
             self.idm.set_capture(self.cfg.getint("cam", "inputDevIndex"))
 
-            gobject.timeout_add(150, self.update_frame)
-            gobject.timeout_add(50, self.update_pointers)
+            #gobject.timeout_add(150, self.update_frame)
+            #gobject.timeout_add(50, self.update_pointers)
 
         # Lets build the interface
         self.itf = MainGui(self)
diff --git a/src/mousetrap/ui/main.py b/src/mousetrap/ui/main.py
index 3e45955..05904d9 100644
--- a/src/mousetrap/ui/main.py
+++ b/src/mousetrap/ui/main.py
@@ -161,7 +161,7 @@ class MainGui( gtk.Window ):
         - img: The IPLimage object.
         """
 
-        if img is None:
+        if not img:
             return False
 
         #self.script.update_items(point)
diff --git a/src/ocvfw/commons.py b/src/ocvfw/commons.py
index 63876f0..7a6b5d7 100644
--- a/src/ocvfw/commons.py
+++ b/src/ocvfw/commons.py
@@ -34,3 +34,10 @@ haar_cds = { 'Face'  :  "%s/haars/haarcascade_frontalface_alt.xml" % abs_path,
              'Eyes'  :  "%s/haars/frontalEyes35x16.xml" % abs_path,
              #'Eyes'  :  "../ocvfw/haars/haarcascade_eye_tree_eyeglasses.xml",
              'Mouth' :  "%s/haars/Mouth.xml" % abs_path}
+
+colors = { "gray" : { "ch" : 1 },
+           "rgb"  : { "ch" : 3 },
+           "bgr"  : { "ch" : 3 }}
+
+def get_ch(color):
+    return colors[color]["ch"]
diff --git a/src/ocvfw/dev/camera.py b/src/ocvfw/dev/camera.py
index 0e04a90..eb7cdd1 100644
--- a/src/ocvfw/dev/camera.py
+++ b/src/ocvfw/dev/camera.py
@@ -30,7 +30,7 @@ __license__   = "GPLv2"
 import gobject
 
 from warnings import *
-from .. import debug
+from .. import debug, commons
 from opencv import cv
 from opencv import highgui as hg
 from .._ocv import Ocvfw as ocv
@@ -75,6 +75,7 @@ class Capture(object):
         self.__graphics    = { "rect"  : [],
                                "point" : []}
 
+        self.__ch          = 3
         self.__image       = image
         self.__image_log   = []
         self.__image_orig  = None
@@ -124,7 +125,7 @@ class Capture(object):
         self.__image_orig  = self.__image = self.__camera.img
 
         if self.__color != self.__color_set:
-            self.__image = self.color("rgb")
+            self.__image = self.color(self.__color_set)
 
         # TODO: Workaround, I've to fix it
         if len(self.__camera.img_lkpoints["last"]) > 0:
@@ -134,12 +135,10 @@ class Capture(object):
 
         self.show_rectangles(self.rectangles())
 
-        self.__image = self.resize(200, 160)
-
         return self.async
 
     # property
-    def image(self):
+    def image(self, new_img = None):
         """
         Returns the image ready to use
 
@@ -147,9 +146,12 @@ class Capture(object):
         - self: The main object pointer.
         """
 
+        if new_img:
+            self.__image = new_img
+
         return self.__image
 
-    def resize(self, width, height):
+    def resize(self, width, height, copy=False):
         """
         Image resizing function.
 
@@ -159,8 +161,15 @@ class Capture(object):
         - height: The new image height.
         """
 
-        tmp = cv.cvCreateImage( cv.cvSize( width, height ), 8, 3 )
+        if self.__image is None:
+            return False
+
+        tmp = cv.cvCreateImage( cv.cvSize( width, height ), 8, self.__ch )
         cv.cvResize( self.__image, tmp, cv.CV_INTER_AREA )
+
+        if not copy:
+            self.__image = tmp
+
         return tmp
 
 
@@ -206,6 +215,26 @@ class Capture(object):
         """
         return Capture(self.__image_orig)
 
+    def rect(self, *args):
+        """
+        Returns a Rectangle of the image.
+
+        Arguments:
+        - self: The main object pointer.
+        - args: Could be the CVRect (at index 0) or the 4 values needed (X, Y, Width, Height)
+        """
+
+        if not self.__image:
+            return
+
+        rect = args[0]
+
+        if len(args) > 1:
+            rect = cv.cvRect( args[0], args[1], args[2], args[3] )
+
+        return cv.cvGetSubRect(self.__image, rect)
+
+
     def flip(self, flip):
         """
         Flips the image
@@ -223,7 +252,7 @@ class Capture(object):
 
         return self.__image
 
-    def color(self, new_color, channel=3):
+    def color(self, new_color, channel=None, copy=False):
         """
         Changes the image's color.
 
@@ -234,14 +263,18 @@ class Capture(object):
         returns self.color if color == None
         """
 
+        channel = channel if channel != None else commons.get_ch(new_color)
+
         if new_color:
-            #img = cv.cvCreateImage ( cv.cvGetSize(self.__image), 8, channel )
-            img = self.__images_cn[channel]
-            cv.cvCvtColor( self.__image, img, self.__color_int['cv_%s2%s' % (self.__color, new_color) ])
-            #cv.cvCvtColor( self.__image, img, self.__color_int['cv_%s2%s' % (self.__color, new_color) ])
+            tmp = self.__images_cn[channel]
+            cv.cvCvtColor( self.__image, tmp, self.__color_int['cv_%s2%s' % (self.__color, new_color) ])
             self.__color = new_color
+            self.__ch = channel
 
-        return img
+        if not copy:
+            self.__image = tmp
+
+        return tmp
 
     def change(self, size=None, color=None, flip=None):
         """
diff --git a/src/ocvfw/idm/forehead.py b/src/ocvfw/idm/forehead.py
index 76ccbe4..d3a8288 100644
--- a/src/ocvfw/idm/forehead.py
+++ b/src/ocvfw/idm/forehead.py
@@ -35,11 +35,21 @@ a_description = "Forehead point tracker based on LK Algorithm"
 a_settings = { 'speed' : {"value":2}}
 
 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.
+        """
         Camera.init()
 
-        self.img          = None
         self.ctr          = controller
         self.cap          = None
         self.stgs         = stgs
@@ -70,12 +80,25 @@ class Module(object):
         self.prepare_config()
 
     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
+        """
         self.cap = Capture(async=True, idx=cam)
         self.cap.change(color="rgb")
 
@@ -96,7 +119,7 @@ class Module(object):
         if not hasattr(self.cap, "forehead"):
             self.get_forehead()
 
-        return self.cap.image()
+        return self.cap.resize(200, 160, True)
 
     def get_pointer(self):
         """



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