[mousetrap/gnome3-wip: 63/240] Refactor into a class.



commit 903afae6fc5ab940ba4e1fea81d17bc466e1831d
Author: Stoney Jackson <dr stoney gmail com>
Date:   Wed Jun 11 09:26:24 2014 -0400

    Refactor into a class.

 run.py |   84 ++++++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 45 insertions(+), 39 deletions(-)
---
diff --git a/run.py b/run.py
index f20534b..7a421cd 100644
--- a/run.py
+++ b/run.py
@@ -4,65 +4,71 @@ import sys
 
 from mousetrap.vision import Camera, HaarLoader, ImageConverter
 
-# Initialize the camera and get the frame
 
-camera = Camera(-1, 400, 300)
+class TrackerSample(object):
 
-image = camera.read_image()
+    def run(self):
+        # Initialize the camera and get the frame
 
-# Convert the image to grayscale
+        camera = Camera(-1, 400, 300)
 
-gray = ImageConverter.rgb_to_grayscale(image)
+        image = camera.read_image()
 
-# Import the haar cascades
+        # Convert the image to grayscale
 
-cascades = {
-    "face": HaarLoader.from_name("face"),
-    "nose": HaarLoader.from_name("nose")
-}
+        gray = ImageConverter.rgb_to_grayscale(image)
 
-# Detect faces using the cascade
-# Use a 1.5 scale to ensure the head is always found
-# Requiring 5 neighbors helps discard invalid faces
+        # Import the haar cascades
 
-faces = cascades["face"].detectMultiScale(gray, 1.5, 5)
+        cascades = {
+                "face": HaarLoader.from_name("face"),
+                "nose": HaarLoader.from_name("nose")
+                }
 
-# Fail early if there were no faces
+        # Detect faces using the cascade
+        # Use a 1.5 scale to ensure the head is always found
+        # Requiring 5 neighbors helps discard invalid faces
 
-if not len(faces):
-    sys.exit(1)
+        faces = cascades["face"].detectMultiScale(gray, 1.5, 5)
 
-# Unpack the detected face into a more readable dictionary
+        # Fail early if there were no faces
 
-face = {}
-face["x"], face["y"], face["width"], face["height"] = faces[0]
+        if not len(faces):
+            sys.exit(1)
 
-# Get the smaller image for the detected face
+        # Unpack the detected face into a more readable dictionary
 
-face["image"] = gray[face["y"]:face["y"] + face["height"],
-                     face["x"]:face["x"] + face["width"]]
+        face = {}
+        face["x"], face["y"], face["width"], face["height"] = faces[0]
 
-# Detect noses using the cascade
+        # Get the smaller image for the detected face
 
-noses = cascades["nose"].detectMultiScale(face["image"], 1.3, 5)
+        face["image"] = gray[face["y"]:face["y"] + face["height"],
+                face["x"]:face["x"] + face["width"]]
 
-# Fail early is there are no noses
+        # Detect noses using the cascade
 
-if not len(noses):
-    sys.exit(1)
+        noses = cascades["nose"].detectMultiScale(face["image"], 1.3, 5)
 
-# Unpack the nose
+        # Fail early is there are no noses
 
-nose = {}
-nose["x"], nose["y"], nose["width"], nose["height"] = noses[0]
+        if not len(noses):
+            sys.exit(1)
 
-# Determine the central point of the nose
-# This is done relative to the face
-# It should probably be done relative to the entire picture
+        # Unpack the nose
 
-nose["center"] = {
-    "x": (nose["x"] + nose["width"]) / 2,
-    "y": (nose["y"] + nose["height"]) / 2,
-}
+        nose = {}
+        nose["x"], nose["y"], nose["width"], nose["height"] = noses[0]
 
-print nose
+        # Determine the central point of the nose
+        # This is done relative to the face
+        # It should probably be done relative to the entire picture
+
+        nose["center"] = {
+                "x": (nose["x"] + nose["width"]) / 2,
+                "y": (nose["y"] + nose["height"]) / 2,
+                }
+
+        print nose
+
+TrackerSample().run()


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