[mousetrap/gnome3-wip: 71/240] Add Image to ecapsulate conversions.
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 71/240] Add Image to ecapsulate conversions.
- Date: Mon, 8 Sep 2014 15:18:31 +0000 (UTC)
commit 528883abf2b3c16bbbc8aaefa73dc6b940c71913
Author: Stoney Jackson <dr stoney gmail com>
Date: Wed Jun 11 21:07:06 2014 -0400
Add Image to ecapsulate conversions.
src/mousetrap/gui.py | 2 +-
src/mousetrap/nose_locator_sample.py | 8 +-----
src/mousetrap/vision.py | 41 +++++++++++++++++++++++++--------
3 files changed, 34 insertions(+), 17 deletions(-)
---
diff --git a/src/mousetrap/gui.py b/src/mousetrap/gui.py
index b59fab6..20472a4 100644
--- a/src/mousetrap/gui.py
+++ b/src/mousetrap/gui.py
@@ -29,7 +29,7 @@ class ImageWindow(object):
def _get_pixbuf_from_image(image):
if isinstance(image, GdkPixbuf.Pixbuf):
return image
- return _cvimage_to_pixbuf(image)
+ return _cvimage_to_pixbuf(image.to_cv())
def _cvimage_to_pixbuf(cvimage):
diff --git a/src/mousetrap/nose_locator_sample.py b/src/mousetrap/nose_locator_sample.py
index cbd60bd..7067130 100644
--- a/src/mousetrap/nose_locator_sample.py
+++ b/src/mousetrap/nose_locator_sample.py
@@ -1,4 +1,4 @@
-from mousetrap.vision import Camera, ImageConverter, NoseLocator
+from mousetrap.vision import Camera, NoseLocator
class NoseLocatorSample(object):
@@ -14,14 +14,10 @@ class NoseLocatorSample(object):
width=400, height=300)
def run(self):
- image = self._read_grayscale_image()
+ image = self._camera.read_image()
nose = self._nose_locator.locate(image)
print nose
- def _read_grayscale_image(self):
- image = self._camera.read_image()
- return ImageConverter.rgb_to_grayscale(image)
-
if __name__ == '__main__':
NoseLocatorSample().run()
diff --git a/src/mousetrap/vision.py b/src/mousetrap/vision.py
index 161e8bf..7af3273 100644
--- a/src/mousetrap/vision.py
+++ b/src/mousetrap/vision.py
@@ -38,12 +38,28 @@ class Camera(object):
if not ret:
raise IOError(S_CAPTURE_READ_ERROR)
- return image
+ return Image(image)
-class ImageConverter(object):
+
+class Image(object):
+ def __init__(self, image_cv, is_grayscale=False):
+ self._image_cv = image_cv
+ self._is_grayscale = is_grayscale
+ self._image_cv_grayscale = None
+ if self._is_grayscale:
+ self._image_cv_grayscale = self._image_cv
+
+ def to_cv(self):
+ return self._image_cv
+
+ def to_cv_grayscale(self):
+ if self._image_cv_grayscale is None:
+ self._image_cv_grayscale = \
+ self._cv_rgb_to_cv_grayscale(self._image_cv)
+ return self._image_cv_grayscale
@staticmethod
- def rgb_to_grayscale(image):
+ def _cv_rgb_to_cv_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@@ -94,8 +110,8 @@ class NoseLocator(object):
self._nose_detector = FeatureDetector(
'nose', scale_factor=1.3, min_neighbors=5)
- def locate(self, image_grayscale):
- face = self._face_detector.detect(image_grayscale)
+ def locate(self, image):
+ face = self._face_detector.detect(image)
nose = self._nose_detector.detect(face['image'])
return {
'x': face['x'] + nose['center']['x'],
@@ -117,13 +133,13 @@ class FeatureDetector(object):
self._name = name
self._single = None
self._plural = None
- self._image_grayscale = None
+ self._image = None
self._cascade = HaarLoader.from_name(name)
self._scale_factor = scale_factor
self._min_neighbors = min_neighbors
- def detect(self, image_grayscale):
- self._image_grayscale = image_grayscale
+ def detect(self, image):
+ self._image = image
self._detect_plural()
self._exit_if_none_detected()
self._unpack_first()
@@ -133,7 +149,9 @@ class FeatureDetector(object):
def _detect_plural(self):
self._plural = self._cascade.detectMultiScale(
- self._image_grayscale, self._scale_factor, self._min_neighbors)
+ self._image.to_cv_grayscale(),
+ self._scale_factor,
+ self._min_neighbors)
def _exit_if_none_detected(self):
if len(self._plural) == 0:
@@ -154,4 +172,7 @@ class FeatureDetector(object):
to_y = single['y'] + single['height']
from_x = single['x']
to_x = single['x'] + single['width']
- single["image"] = self._image_grayscale[from_y:to_y, from_x:to_x]
+ image_cv_grayscale = self._image.to_cv_grayscale()
+ single["image"] = Image(
+ image_cv_grayscale[from_y:to_y, from_x:to_x],
+ is_grayscale=True)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]