[mousetrap/gnome3-wip: 75/240] Improve design of Camera and Image.



commit 706883db01acbe55ade0fb440db2047e6769f80a
Author: Stoney Jackson <dr stoney gmail com>
Date:   Thu Jun 12 09:57:32 2014 -0400

    Improve design of Camera and Image.
    
    * Add defaults to camera constructor.
    * Make Camera.set_dimensions public.
    * Add Image.get_width and Image.get_height.

 src/mousetrap/main.py                |   14 ++++++--------
 src/mousetrap/nose_locator_sample.py |    9 +--------
 src/mousetrap/vision.py              |   30 +++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 27 deletions(-)
---
diff --git a/src/mousetrap/main.py b/src/mousetrap/main.py
index 154a95a..e2dd9d4 100644
--- a/src/mousetrap/main.py
+++ b/src/mousetrap/main.py
@@ -6,10 +6,6 @@ import mousetrap.gui as gui
 import mousetrap.pointer as pointer
 
 
-SEARCH_FOR_CAMERA = -1
-DEVICE_INDEX = SEARCH_FOR_CAMERA
-IMAGE_MAX_WIDTH = 400
-IMAGE_MAX_HEIGHT = 300
 FPS = 5
 INTERVAL = int(round(1000.0 / FPS))
 
@@ -18,8 +14,8 @@ class Main(object):
     def __init__(self):
         self.image = None
         self.timeout_id = None
-        self.camera = vision.Camera(DEVICE_INDEX,
-                IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT)
+        self.camera = vision.Camera()
+        self.camera.set_dimensions(300, 200)
         self.locator = vision.NoseLocator()
         self.pointer = pointer.Pointer()
         self.screen = Gtk.Window().get_screen()
@@ -35,8 +31,10 @@ class Main(object):
             print 'Nose location in image: ' + str(location)
 
             # Map coordinates from image to screen.
-            x_percent = 1.0 * location['x'] / IMAGE_MAX_WIDTH
-            y_percent = 1.0 * location['y'] / IMAGE_MAX_HEIGHT
+            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
             screen = Gtk.Window().get_screen()
             x_screen = x_percent * screen.get_width()
             y_screen = y_percent * screen.get_width()
diff --git a/src/mousetrap/nose_locator_sample.py b/src/mousetrap/nose_locator_sample.py
index 7067130..807f762 100644
--- a/src/mousetrap/nose_locator_sample.py
+++ b/src/mousetrap/nose_locator_sample.py
@@ -3,15 +3,8 @@ from mousetrap.vision import Camera, NoseLocator
 
 class NoseLocatorSample(object):
     def __init__(self):
-        self._camera = None
+        self._camera = Camera()
         self._nose_locator = NoseLocator()
-        self._initialize_camera()
-
-    def _initialize_camera(self):
-        search_for_device = -1
-        self._camera = Camera(
-                device_index=search_for_device,
-                width=400, height=300)
 
     def run(self):
         image = self._camera.read_image()
diff --git a/src/mousetrap/vision.py b/src/mousetrap/vision.py
index c7bbd4a..5f3a51b 100644
--- a/src/mousetrap/vision.py
+++ b/src/mousetrap/vision.py
@@ -8,14 +8,15 @@ import cv2
 import cv
 
 
-S_CAPTURE_OPEN_ERROR = 'Device #%d does not support video capture interface'
-S_CAPTURE_READ_ERROR = 'Error while capturing. Camera disconnected?'
-
 
 class Camera(object):
-    def __init__(self, device_index, width, height):
-        self.device = self._new_capture_device(device_index)
-        self._set_dimensions(width, height)
+    S_CAPTURE_OPEN_ERROR = 'Device #%d does not support video capture interface'
+    S_CAPTURE_READ_ERROR = 'Error while capturing. Camera disconnected?'
+    SEARCH_FOR_DEVICE=-1
+
+    def __init__(self, device_index=SEARCH_FOR_DEVICE, width=400, height=300):
+        self._device = self._new_capture_device(device_index)
+        self.set_dimensions(width, height)
 
     @staticmethod
     def _new_capture_device(device_index):
@@ -28,12 +29,12 @@ class Camera(object):
 
         return capture
 
-    def _set_dimensions(self, width, height):
-        self.device.set(cv.CV_CAP_PROP_FRAME_WIDTH, width)
-        self.device.set(cv.CV_CAP_PROP_FRAME_HEIGHT, height)
+    def set_dimensions(self, width, height):
+        self._device.set(cv.CV_CAP_PROP_FRAME_WIDTH, width)
+        self._device.set(cv.CV_CAP_PROP_FRAME_HEIGHT, height)
 
     def read_image(self):
-        ret, image = self.device.read()
+        ret, image = self._device.read()
 
         if not ret:
             raise IOError(S_CAPTURE_READ_ERROR)
@@ -58,6 +59,13 @@ class Image(object):
                     self._cv_rgb_to_cv_grayscale(self._image_cv)
         return self._image_cv_grayscale
 
+    def get_width(self):
+        return self._image_cv.shape[0]
+
+    def get_height(self):
+        return self._image_cv.shape[1]
+
+
     @staticmethod
     def _cv_rgb_to_cv_grayscale(image):
         return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@@ -108,7 +116,7 @@ class NoseLocator(object):
         self._face_detector = FeatureDetector(
                 'face', scale_factor=1.5, min_neighbors=5)
         self._nose_detector = FeatureDetector(
-                'nose', scale_factor=1.3, min_neighbors=5)
+                'nose', scale_factor=1.1, min_neighbors=5)
 
     def locate(self, image):
         face = self._face_detector.detect(image)


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