예제 #1
0
 def __init__(self, cascade_filename):
     super(MrPeanut, self).__init__(window_name="Senor Cacahuete")
     self.detector = FeatureDetector(cascade_filename)
     self.eye_ratio = 0.15
     self.pupil_ratio = 0.05
     self.fill_color = CV_RGB(255, 255, 255)
     self.color = CV_RGB(0, 0, 0)
     self.thickness = 4
예제 #2
0
 def __init__(self, cascade_filename):
     super(GooglyEyes, self).__init__(window_name="Googly Eyes")
     self.detector = FeatureDetector(cascade_filename)
     self.eye_ratio = 0.15
     self.pupil_ratio = 0.05
     self.fill_color = CV_RGB(255, 255, 255)
     self.color = CV_RGB(0, 0, 0)
     self.thickness = 4
     self.pupils = {"left": 0.0, "right": 0.0}
     self.prng = Random()
예제 #3
0
class MrPeanut(Webcam):
    def __init__(self, cascade_filename):
        super(MrPeanut, self).__init__(window_name="Senor Cacahuete")
        self.detector = FeatureDetector(cascade_filename)
        self.eye_ratio = 0.15
        self.pupil_ratio = 0.05
        self.fill_color = CV_RGB(255, 255, 255)
        self.color = CV_RGB(0, 0, 0)
        self.thickness = 4

    def process_frame(self, frame):
        faces = self.detector.detect(frame)
        for f in faces:
            eyeline = int(f.upper_left.y + (0.4 * f.height))
            left_eye = cvPoint(int(f.upper_left.x + (0.33 * f.width)), eyeline)
            self.draw_monocle(frame, left_eye, f.height)
            self.draw_hat(frame, f)
        cvShowImage(self.window_name, frame)

    def draw_monocle(self, frame, center, height):
        eye_size = int(self.eye_ratio * height)
        cvCircle(frame, center, eye_size, self.fill_color, CV_FILLED, CV_AA)
        cvCircle(frame, center, eye_size, self.color, self.thickness, CV_AA)

    def draw_hat(self, frame, rect):
        face_midline = rect.upper_left.x + (rect.width / 2.0)
        brim_line = rect.upper_left.y - (rect.height * 0.1)
        brim_width = rect.width * 1.4
        brim_height = 40
        hat_width = rect.width * 0.8
        hat_height = 100
        cvRectangle(
            frame,
            cvPoint(int(face_midline) - int(brim_width / 2.0), int(brim_line)),
            cvPoint(int(face_midline) + int(brim_width / 2.0), int(brim_line) + brim_height),
            self.color,
            CV_FILLED,
            CV_AA,
        )
        cvRectangle(
            frame,
            cvPoint(int(face_midline) - int(hat_width / 2.0), int(brim_line) - hat_height),
            cvPoint(int(face_midline) + int(hat_width / 2.0), int(brim_line)),
            self.color,
            CV_FILLED,
            CV_AA,
        )
예제 #4
0
class GooglyEyes(Webcam):    
    def __init__(self, cascade_filename):
        super(GooglyEyes, self).__init__(window_name="Googly Eyes")
        self.detector = FeatureDetector(cascade_filename)
        self.eye_ratio = 0.15
        self.pupil_ratio = 0.05
        self.fill_color = CV_RGB(255, 255, 255)
        self.color = CV_RGB(0, 0, 0)
        self.thickness = 4
        self.pupils = {"left": 0.0, "right": 0.0}
        self.prng = Random()
    
    def process_frame(self, frame):
        faces = self.detector.detect(frame)
        for f in faces:
            eyeline = int(f.upper_left.y + (0.4 * f.height)) 
            left_eye = int(f.upper_left.x + (0.33 * f.width))
            right_eye = int(f.lower_right.x - (0.33 * f.width))
            self.draw_eyes(frame, eyeline, left_eye, right_eye, f.height)
        cvShowImage(self.window_name, frame)
    
    def draw_eyes(self, frame, eyeline, left_eye_x, right_eye_x, height):
        left_eye = Point(left_eye_x, eyeline)
        right_eye = Point(right_eye_x, eyeline)
        self.draw_eye(frame, left_eye, "left", height)
        self.draw_eye(frame, right_eye, "right", height)
    
    def draw_eye(self, frame, center, tag, height):
        eye_size = int(self.eye_ratio * height)
        cvCircle(frame, center.to_cvPoint(), eye_size, self.fill_color, CV_FILLED, CV_AA)
        cvCircle(frame, center.to_cvPoint(), eye_size, self.color, self.thickness, CV_AA)
        self.draw_pupil(frame, center, tag, height, eye_size)
    
    def draw_pupil(self, frame, center, tag, height, eye_size):
        self.pupils[tag] = (self.pupils[tag] + (self.prng.choice((-2, 1)) * (pi / 10.0))) % (2 * pi)
        inner_eye_size = eye_size * 0.8
        googly_x = center.x + int(inner_eye_size * cos(self.pupils[tag]))
        googly_y = center.y + int(inner_eye_size * sin(self.pupils[tag]))
        pupil_size = int(self.pupil_ratio * height)
        cvCircle(frame, cvPoint(googly_x, googly_y), pupil_size, self.color, CV_FILLED, CV_AA)