[mousetrap/gnome3-wip: 61/240] Add HaarLoader and ImageConverter
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 61/240] Add HaarLoader and ImageConverter
- Date: Mon, 8 Sep 2014 15:17:40 +0000 (UTC)
commit ca44b2b207c9613ad2e5b0022e7e2f96f36e0b9b
Author: Kevin Brown <kbrown rediker com>
Date: Tue Jun 10 21:08:06 2014 -0400
Add HaarLoader and ImageConverter
This adds a HaarLoader class which will load a haar file based on
a predefined name. This will cache the loaded haars within the
thread, so they will not be constantly loaded. Haar cascades can
be loaded from files and cached to the thread using an optional
cache name.
This also adds basic ImageConverter class which will convert images
from RGB to grayscale. Later this will be used for other imaging
related tasks.
These, like many things which will be committed tonight, need some
tests to back them up.
run.py | 16 +++++++------
src/mousetrap/vision.py | 54 +++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 61 insertions(+), 9 deletions(-)
---
diff --git a/run.py b/run.py
index 0b3eb90..51a27fc 100644
--- a/run.py
+++ b/run.py
@@ -8,23 +8,23 @@ PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__f
sys.path.append(PROJECT_PATH)
-from mousetrap.camera import Camera
+from mousetrap.vision import Camera, HaarLoader, ImageConverter
# Initialize the camera and get the frame
-camera = Camera()
-camera.start_camera()
-image = camera.get_image()
+camera = Camera(-1, 400, 300)
+
+image = camera.read_image()
# Convert the image to grayscale
-gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+gray = ImageConverter.rgb_to_grayscale(image)
# Import the haar cascades
cascades = {
- "face": cv2.CascadeClassifier("src/mousetrap/haars/haarcascade_frontalface_default.xml"),
- "nose": cv2.CascadeClassifier("src/mousetrap/haars/haarcascade_mcs_nose.xml"),
+ "face": HaarLoader.from_name("face"),
+ "nose": HaarLoader.from_name("nose")
}
# Detect faces using the cascade
@@ -70,3 +70,5 @@ nose["center"] = {
"x": (nose["x"] + nose["width"]) / 2,
"y": (nose["y"] + nose["height"]) / 2,
}
+
+print nose
diff --git a/src/mousetrap/vision.py b/src/mousetrap/vision.py
index 23d9cfb..c2f7fdd 100644
--- a/src/mousetrap/vision.py
+++ b/src/mousetrap/vision.py
@@ -5,10 +5,10 @@ this module instead.
'''
import cv2
-from cv2 import cv
+import cv
-S_CAPTURE_OPEN_ERROR = 'Device #%s does not support video capture interface'
+S_CAPTURE_OPEN_ERROR = 'Device #%d does not support video capture interface'
S_CAPTURE_READ_ERROR = 'Error while capturing. Camera disconnected?'
@@ -20,9 +20,12 @@ class Camera(object):
@staticmethod
def _new_capture_device(device_index):
capture = cv2.VideoCapture(device_index)
+
if not capture.isOpened():
capture.release()
+
raise IOError(S_CAPTURE_OPEN_ERROR % device_index)
+
return capture
def _set_dimensions(self, width, height):
@@ -31,7 +34,54 @@ class Camera(object):
def read_image(self):
ret, image = self.device.read()
+
if not ret:
raise IOError(S_CAPTURE_READ_ERROR)
+
return image
+class ImageConverter(object):
+
+ @staticmethod
+ def rgb_to_grayscale(image):
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+
+
+class HaarLoader(object):
+ _haar_files = {
+ "face": "haars/haarcascade_frontalface_default.xml",
+ "nose": "haars/haarcascade_mcs_nose.xml",
+ }
+
+ _haar_cache = {}
+
+ @staticmethod
+ def from_name(name):
+ if not name in HaarLoader._haar_files:
+ # TODO: Throw an exception
+ pass
+
+ haar_file = HaarLoader._haar_files[name]
+
+ haar = HaarLoader.from_file(haar_file, name)
+
+ return haar
+
+ @staticmethod
+ def from_file(file, cache_name=None):
+ import os
+
+ if cache_name in HaarLoader._haar_cache:
+ return HaarLoader._haar_cache[name]
+
+ current_dir = os.path.dirname(os.path.realpath(__file__))
+
+ haar_file = os.path.join(current_dir, file)
+
+ haar = cv2.CascadeClassifier(haar_file)
+
+ if not cache_name is None:
+ if not cache_name in HaarLoader._haar_cache:
+ HaarLoader._haar_cache[cache_name] = haar
+
+ return haar
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]