[mousetrap/gnome3-wip: 141/240] Merge the two nose plugins



commit 119ed2fe032087e071f889702245a11be397a66a
Author: Kevin Brown <kbrown rediker com>
Date:   Mon Jun 23 21:16:38 2014 -0400

    Merge the two nose plugins

 src/mousetrap/config_default.yaml      |    4 +-
 src/mousetrap/plugins/nose.py          |   59 ++++++++++++++++++++++++++++++
 src/mousetrap/plugins/nose_joystick.py |   62 --------------------------------
 3 files changed, 61 insertions(+), 64 deletions(-)
---
diff --git a/src/mousetrap/config_default.yaml b/src/mousetrap/config_default.yaml
index 19dd8af..3110c4f 100644
--- a/src/mousetrap/config_default.yaml
+++ b/src/mousetrap/config_default.yaml
@@ -1,7 +1,7 @@
 assembly:
 - mousetrap.plugins.camera.CameraPlugin
 - mousetrap.plugins.display.DisplayPlugin
-- mousetrap.plugins.nose_joystick.NoseJoystickPlugin
+- mousetrap.plugins.nose.NoseJoystickPlugin
 - mousetrap.plugins.eyes.EyesPlugin
 camera:
   device_index: -1
@@ -32,7 +32,7 @@ classes:
     nose_detector:
       min_neighbors: 5
       scale_factor: 1.1
-  mousetrap.plugins.nose_joystick.NoseJoystickPlugin:
+  mousetrap.plugins.nose.NoseJoystickPlugin:
     threshold: 5
 haar_files:
   face: haars/haarcascade_frontalface_default.xml
diff --git a/src/mousetrap/plugins/nose.py b/src/mousetrap/plugins/nose.py
index d087bf8..c4a37ba 100644
--- a/src/mousetrap/plugins/nose.py
+++ b/src/mousetrap/plugins/nose.py
@@ -35,6 +35,65 @@ class NosePlugin(interface.Plugin):
         return self._location
 
 
+class NoseJoystickPlugin(interface.Plugin):
+
+    def __init__(self, config):
+        self._config = config
+        self._threshold = config[self]['threshold']
+        self._nose_locator = NoseLocator(config)
+        self._initial_image_location = (0, 0)
+        self._last_delta = (0, 0)
+
+    def run(self, app):
+        self._app = app
+        location = None
+        try:
+            point_image = self._nose_locator.locate(app.image)
+            point_screen = self._convert_image_to_screen_point(*point_image)
+            location = point_screen
+        except FeatureNotFoundException:
+            location = app.pointer.get_position()
+            location = self._apply_delta_to_point(location, self._last_delta)
+        app.pointer.set_position(location)
+
+    def _apply_delta_to_point(self, point, delta):
+        delta_x, delta_y = delta
+        point_x, point_y = point
+
+        if delta_x == 0 and delta_y == 0:
+            return None
+
+        point_x += delta_x
+        point_y += delta_y
+
+        return (point_x, point_y)
+
+    def _convert_image_to_screen_point(self, image_x, image_y):
+        initial_x, initial_y = self._initial_image_location
+
+        if initial_x == 0 and initial_y == 0:
+            self._initial_image_location = (image_x, image_y)
+
+            return self._initial_image_location
+
+        delta_x = initial_x - image_x
+        delta_y = image_y - initial_y
+
+        if abs(delta_x) < self._threshold:
+            delta_x = 0
+
+        if abs(delta_y) < self._threshold:
+            delta_y = 0
+
+        delta = (delta_x, delta_y)
+
+        self._last_delta = delta
+
+        location = self._app.pointer.get_position()
+
+        return self._apply_delta_to_point(location, delta)
+
+
 class NoseLocator(object):
     def __init__(self, config):
         self._config = config


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