[mousetrap/gnome3-wip: 84/240] Add mousetrap.pointers package to hold pointer modules. Each pointer module must define a Pointer cl



commit b705df4d635b75571e9c8c4c92ddb3c221bdaf8b
Author: Stoney Jackson <dr stoney gmail com>
Date:   Thu Jun 12 12:25:51 2014 -0400

    Add mousetrap.pointers package to hold pointer modules.
    Each pointer module must define a Pointer class that inherits
    mousetrap.pointers.interface.Pointer.

 src/mousetrap/gui.py                               |    9 ++-
 src/mousetrap/main.py                              |   42 +++++-----------
 src/mousetrap/pointers/interface.py                |   13 +++++
 src/mousetrap/pointers/nose.py                     |   52 ++++++++++++++++++++
 .../{test_pointer.py => test_mouse_pointer.py}     |    4 +-
 src/mousetrap/vision.py                            |   16 ------
 6 files changed, 87 insertions(+), 49 deletions(-)
---
diff --git a/src/mousetrap/gui.py b/src/mousetrap/gui.py
index 90f9eba..63ae31e 100644
--- a/src/mousetrap/gui.py
+++ b/src/mousetrap/gui.py
@@ -50,7 +50,7 @@ class Gui(object):
         return Gtk.Window().get_screen().get_height()
 
 
-class Pointer(object):
+class MousePointer(object):
     def __init__(self):
         gdk_display = Gdk.Display.get_default()
         device_manager = gdk_display.get_device_manager()
@@ -63,5 +63,8 @@ class Pointer(object):
         position = self._pointer.get_position()
         return (position[x_index], position[y_index])
 
-    def set_position(self, point):
-        self._pointer.warp(self._screen, point[0], point[1])
+    def set_position(self, position=None):
+        '''Move pointer to position (x, y). If position is None,
+        no change is made.'''
+        if position is not None:
+            self._pointer.warp(self._screen, position[0], position[1])
diff --git a/src/mousetrap/main.py b/src/mousetrap/main.py
index 837ac3b..cdd6309 100644
--- a/src/mousetrap/main.py
+++ b/src/mousetrap/main.py
@@ -2,52 +2,38 @@
 Where it all begins.
 '''
 
+from datetime import datetime
 from gi.repository import GObject
-from mousetrap.vision import Camera, NoseLocator
-from mousetrap.gui import Pointer, Gui
+from mousetrap.vision import Camera
+from mousetrap.gui import MousePointer, Gui
+from mousetrap.pointers.nose import Pointer
 
 
-FPS = 5
+FPS = 10
 INTERVAL = int(round(1000.0 / FPS))
 
 
 class Main(object):
     def __init__(self):
-        self.image = None
         self.timeout_id = None
         self.camera = Camera()
         self.camera.set_dimensions(300, 200)
-        self.locator = NoseLocator()
-        self.pointer = Pointer()
         self.gui = Gui()
+        self.pointer = MousePointer()
+        self.nose = Pointer()
 
     def run(self):
         self.timeout_id = GObject.timeout_add(INTERVAL, self.on_timeout, None)
         self.gui.start()
 
     def on_timeout(self, user_data):
-        self.image = self.camera.read_image()
-        try:
-            location = self.locator.locate(self.image)
-            print 'Nose location in image: ' + str(location)
-
-            # Map coordinates from image to screen.
-            image_width = self.image.get_width()
-            image_height = self.image.get_height()
-            x_percent = 1.0 * location['x'] / image_width
-            y_percent = 1.0 * location['y'] / image_height
-            x_screen = x_percent * self.gui.get_screen_width()
-            y_screen = y_percent * self.gui.get_screen_height()
-            half_width = self.gui.get_screen_width() / 2
-            x_screen = (-1 * (x_screen - half_width)) + half_width
-            print 'Pointer location in screen:' + \
-                str({'x':x_screen, 'y':y_screen})
-
-            # move the pointer
-            self.pointer.set_position((x_screen, y_screen))
-        except Exception as exception:
-            print exception.args[0]
-        self.gui.show_image('diff', self.image)
+        image = self.camera.read_image()
+        self.gui.show_image('Raw', image)
+        self.nose.update_image(image)
+        position = self.nose.get_new_position()
+        self.pointer.set_position(position)
+        if position is None:
+            print str(datetime.now()) + ': No change.'
         return True
 
 
diff --git a/src/mousetrap/pointers/__init__.py b/src/mousetrap/pointers/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/mousetrap/pointers/interface.py b/src/mousetrap/pointers/interface.py
new file mode 100644
index 0000000..b463df9
--- /dev/null
+++ b/src/mousetrap/pointers/interface.py
@@ -0,0 +1,13 @@
+'''
+Each module in mousetrap.pointers must define a Pointer class
+that inherits mousetrap.pointers.interface.Pointer.
+'''
+
+
+class Pointer(object):
+    def update_image(self, image):
+        raise Exception('Unimplemented method add_image().')
+
+    def get_new_position(self):
+        '''Returns new location (x, y) or None if no change.'''
+        raise Exception('Unimplemented method get_position().')
diff --git a/src/mousetrap/pointers/nose.py b/src/mousetrap/pointers/nose.py
new file mode 100644
index 0000000..cec1ff7
--- /dev/null
+++ b/src/mousetrap/pointers/nose.py
@@ -0,0 +1,52 @@
+import mousetrap.pointers.interface as interface
+from mousetrap.vision import FeatureDetector
+from mousetrap.gui import Gui
+
+
+class Pointer(interface.Pointer):
+    def __init__(self):
+        self._nose_locator = NoseLocator()
+        self._gui = Gui()
+        self._location = None
+        self._image = None
+
+    def update_image(self, image):
+        self._image = image
+        try:
+            point_image = self._nose_locator.locate(image)
+            point_screen = self._convert_image_to_screen_point(*point_image)
+            self._location = point_screen
+        except Exception as exception:
+            # FIXME: when FeatureDetector is changed to raise custom exceptions
+            # this should be adjusted to match.
+            self._location = None
+
+    def _convert_image_to_screen_point(self, image_x, image_y):
+        image_width = self._image.get_width()
+        image_height = self._image.get_height()
+        percent_x = 1.0 * image_x / image_width
+        percent_y = 1.0 * image_y / image_height
+        screen_x = percent_x * self._gui.get_screen_width()
+        screen_y = percent_y * self._gui.get_screen_height()
+        half_width = self._gui.get_screen_width() / 2
+        screen_x = (-1 * (screen_x - half_width)) + half_width
+        return (screen_x, screen_y)
+
+    def get_new_position(self):
+        return self._location
+
+
+class NoseLocator(object):
+    def __init__(self):
+        self._face_detector = FeatureDetector(
+                'face', scale_factor=1.5, min_neighbors=5)
+        self._nose_detector = FeatureDetector(
+                'nose', scale_factor=1.1, min_neighbors=5)
+
+    def locate(self, image):
+        face = self._face_detector.detect(image)
+        nose = self._nose_detector.detect(face['image'])
+        return (
+                face['x'] + nose['center']['x'],
+                face['y'] + nose['center']['y'],
+                )
diff --git a/src/mousetrap/tests/test_pointer.py b/src/mousetrap/tests/test_mouse_pointer.py
similarity index 88%
rename from src/mousetrap/tests/test_pointer.py
rename to src/mousetrap/tests/test_mouse_pointer.py
index f099dc6..07f76a1 100644
--- a/src/mousetrap/tests/test_pointer.py
+++ b/src/mousetrap/tests/test_mouse_pointer.py
@@ -1,11 +1,11 @@
 import unittest
-from mousetrap.gui import Pointer
+from mousetrap.gui import MousePointer
 
 
 class test_pointer(unittest.TestCase):
 
     def setUp(self):
-        self.pointer = Pointer()
+        self.pointer = MousePointer()
 
     def test_get_position(self):
         pointer_x, pointer_y = self.pointer.get_position()
diff --git a/src/mousetrap/vision.py b/src/mousetrap/vision.py
index faa61c6..74263f2 100644
--- a/src/mousetrap/vision.py
+++ b/src/mousetrap/vision.py
@@ -80,22 +80,6 @@ class HaarLoader(object):
         return haar
 
 
-class NoseLocator(object):
-    def __init__(self):
-        self._face_detector = FeatureDetector(
-                'face', scale_factor=1.5, min_neighbors=5)
-        self._nose_detector = FeatureDetector(
-                'nose', scale_factor=1.1, min_neighbors=5)
-
-    def locate(self, image):
-        face = self._face_detector.detect(image)
-        nose = self._nose_detector.detect(face['image'])
-        return {
-                'x': face['x'] + nose['center']['x'],
-                'y': face['y'] + nose['center']['y'],
-                }
-
-
 class FeatureDetector(object):
     def __init__(self, name, scale_factor=1.1, min_neighbors=3):
         '''


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