Пример #1
0
 def setUp(self):
     self.backdrop = cv2.imread('../tests/test_backdrop.jpg')
     self.hat = AttachedOverlayDecorator.construct_from_graphic_path(
         '../sprites/Party_Hat.png')
     self.base_component = CachingLandmarkComponent(
         TestDecorator.landmark_detector)
     self.base_component.set_image(self.backdrop)
Пример #2
0
def overlay_example():
    detector_data_path = '../../data/shape_predictor_68_face_landmarks.dat'
    landmark_detector = LandmarkDetector(detector_data_path)
    base_component = LandmarkComponent(landmark_detector)
    hat = AllFaces.wrap(SpriteDecorator(base_component,
                                        '../sprites/Party_Hat'))
    glasses = AllFaces.wrap(SpriteDecorator(hat, '../sprites/sunglasses'))

    use_webcam = False
    video_stream = cv2.VideoCapture("../../videos/trump.mp4")
    if use_webcam:
        video_stream = cv2.VideoCapture(0)
    while True:
        ret, frame = video_stream.read()
        frame = cv2.resize(frame, (0, 0), fx=0.6, fy=0.6)
        if use_webcam:
            frame = cv2.flip(frame, 1)
        base_component.set_image(frame)
        output = glasses.get_image()
        output = cv2.resize(output, (0, 0), fx=2.0, fy=2.0)
        cv2.imshow("Frame", output)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
    video_stream.release()
Пример #3
0
def predicting_example():
    pygame.init()
    # Instanciate a new landmark detector
    detector_data_path = '../../data/shape_predictor_68_face_landmarks.dat'
    landmark_detector = LandmarkDetector(predictor_file=detector_data_path)

    # Create a frame component with landmarks
    base_component = LandmarkComponent(landmark_detector)

    # Instantiate and initialize the trained predictor
    predictor = PredictorInterface('../examples/mouth.pkl')
    predictor.initialize()

    landmarks = LandmarkOverlayDecorator(base_component)

    # Add decorator for the predictor
    predictor_decorator = ClassDecorator(parent_component=landmarks,
                                         classifier=predictor)
    sound = SoundDecorator(landmarks, "../sounds/boring.wav")
    predictor_decorator.set_decorator_for_class(sound, 0)
    multifaces = AllFaces.wrap(predictor_decorator)

    hat = AllFaces.wrap(SpriteDecorator(multifaces, '../sprites/Party_Hat'))

    use_webcam = True
    video_stream = cv2.VideoCapture("../../videos/trump.mp4")
    if use_webcam:
        video_stream = cv2.VideoCapture(0)
    while True:
        ret, frame = video_stream.read()
        frame = cv2.resize(frame, (0, 0), fx=1.0, fy=1.0)
        if use_webcam:
            frame = cv2.flip(frame, 1)
        base_component.set_image(frame)
        output = hat.get_image()
        output = cv2.resize(output, (0, 0), fx=1.0, fy=1.0)
        cv2.imshow("Frame", output)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
    video_stream.release()
def predicting_example():
    pygame.init()
    # Instanciate a new landmark detector
    detector_data_path = '../../data/shape_predictor_68_face_landmarks.dat'
    landmark_detector = LandmarkDetector(predictor_file=detector_data_path)

    # Create a frame component with landmarks
    base_component = LandmarkComponent(landmark_detector)

    # Instantiate and initialize the trained predictor
    predictor = PredictorInterface('../examples/mouth.pkl')
    predictor.initialize()

    landmarks = LandmarkOverlayDecorator(base_component)

    # Add decorator for the predictor
    predictor_decorator = ClassDecorator(parent_component=landmarks, classifier=predictor)
    sound = SoundDecorator(landmarks, "../sounds/boring.wav")
    predictor_decorator.set_decorator_for_class(sound, 0)
    multifaces = AllFaces.wrap(predictor_decorator)

    hat = AllFaces.wrap(SpriteDecorator(multifaces, '../sprites/Party_Hat'))

    use_webcam = True
    video_stream = cv2.VideoCapture("../../videos/trump.mp4")
    if use_webcam:
        video_stream = cv2.VideoCapture(0)
    while True:
        ret, frame = video_stream.read()
        frame = cv2.resize(frame, (0, 0), fx=1.0, fy=1.0)
        if use_webcam:
            frame = cv2.flip(frame, 1)
        base_component.set_image(frame)
        output = hat.get_image()
        output = cv2.resize(output, (0, 0), fx=1.0, fy=1.0)
        cv2.imshow("Frame", output)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
    video_stream.release()
def predicting_example():
    # Instanciate a new landmark detector
    detector_data_path = '../../data/shape_predictor_68_face_landmarks.dat'
    landmark_detector = LandmarkDetector(predictor_file=detector_data_path)

    # Create a frame component with landmarks
    base_component = LandmarkComponent(landmark_detector)

    # Instantiate and initialize the trained predictor
    predictor = PredictorInterface('../examples/blink.pkl')
    predictor.initialize()

    # Add decorator for the predictor
    predictor_decorator = ClassDecorator(parent_component=base_component,
                                         classifier=predictor)
    sunglasses = SpriteDecorator(base_file_name='../sprites/sunglasses')
    eyes = SpriteDecorator(base_file_name='../sprites/eyes')
    predictor_decorator.set_decorator_for_class(sunglasses, 0)
    predictor_decorator.set_decorator_for_class(eyes, 1)

    multifaces = AllFaces.wrap(predictor_decorator)

    use_webcam = False
    video_stream = cv2.VideoCapture("../../videos/trump.mp4")
    if use_webcam:
        video_stream = cv2.VideoCapture(0)
    while True:
        ret, frame = video_stream.read()
        frame = cv2.resize(frame, (0, 0), fx=0.6, fy=0.6)
        if use_webcam:
            frame = cv2.flip(frame, 1)
        base_component.set_image(frame)
        output = multifaces.get_image()
        output = cv2.resize(output, (0, 0), fx=2.0, fy=2.0)
        cv2.imshow("Frame", output)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
    video_stream.release()
def predicting_example():
    # Instanciate a new landmark detector
    detector_data_path = '../../data/shape_predictor_68_face_landmarks.dat'
    landmark_detector = LandmarkDetector(predictor_file=detector_data_path)

    # Create a frame component with landmarks
    base_component = LandmarkComponent(landmark_detector)

    # Instantiate and initialize the trained predictor
    predictor = PredictorInterface('../examples/blink.pkl')
    predictor.initialize()

    # Add decorator for the predictor
    predictor_decorator = ClassDecorator(parent_component=base_component, classifier=predictor)
    sunglasses = SpriteDecorator(base_file_name='../sprites/sunglasses')
    eyes = SpriteDecorator(base_file_name='../sprites/eyes')
    predictor_decorator.set_decorator_for_class(sunglasses, 0)
    predictor_decorator.set_decorator_for_class(eyes, 1)

    multifaces = AllFaces.wrap(predictor_decorator)

    use_webcam = False
    video_stream = cv2.VideoCapture("../../videos/trump.mp4")
    if use_webcam:
        video_stream = cv2.VideoCapture(0)
    while True:
        ret, frame = video_stream.read()
        frame = cv2.resize(frame, (0, 0), fx=0.6, fy=0.6)
        if use_webcam:
            frame = cv2.flip(frame, 1)
        base_component.set_image(frame)
        output = multifaces.get_image()
        output = cv2.resize(output, (0, 0), fx=2.0, fy=2.0)
        cv2.imshow("Frame", output)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
    video_stream.release()
Пример #7
0
 def test_forgot_input(self):
     base_component = CachingLandmarkComponent(
         TestDecorator.landmark_detector)
     base_component.get_landmarks()
Пример #8
0
class TestDecorator(ImageTestCase):
    @classmethod
    def setUpClass(cls):
        detector_data_path = '../data/shape_predictor_68_face_landmarks.dat'
        cls.landmark_detector = LandmarkDetector(detector_data_path)

    def setUp(self):
        self.backdrop = cv2.imread('../tests/test_backdrop.jpg')
        self.hat = AttachedOverlayDecorator.construct_from_graphic_path(
            '../sprites/Party_Hat.png')
        self.base_component = CachingLandmarkComponent(
            TestDecorator.landmark_detector)
        self.base_component.set_image(self.backdrop)

    def test_overlay_landmarks(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        result = landmark_overlay.get_image()
        self.assertUnalteredImage(result)

    def test_move_sprite(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_angle_offset(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.angle_offset = 90
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_perpendicular_offset(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.perpendicular_offset = -50
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_parallel_offset(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.parallel_offset = 30.5
        self.expected_landmark_distance = 300.0
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_scale(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.expected_landmark_distance = 600.0
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_multifaces(self):
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.perpendicular_offset = -20
        self.hat.expected_landmark_distance = 300.0
        multifaces = MultiFacesLoopDecorator(self.base_component)
        self.hat.parent = multifaces.get_start_of_loop_component()
        multifaces.set_end_of_loop_component(self.hat)
        result = multifaces.get_image()
        self.assertUnalteredImage(result)

    def test_multifaces_wrap(self):
        self.hat.parent = self.base_component
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.perpendicular_offset = -20
        self.hat.expected_landmark_distance = 300.0
        multifaces = MultiFacesLoopDecorator.wrap(self.hat)
        result = multifaces.get_image()
        self.assertUnalteredImage(result)

    def test_forgot_input(self):
        base_component = CachingLandmarkComponent(
            TestDecorator.landmark_detector)
        base_component.get_landmarks()

    def set_up_classification_decorator(self):
        self.classification_decorator = ClassificationDecorator(
            self.base_component)
        self.classifier = ConstantClassifier()
        first_class_decorator = self.hat
        second_class_decorator = LandmarkOverlayDecorator()
        self.classification_decorator.classifier = self.classifier
        self.classification_decorator.set_decorator_for_class(
            first_class_decorator, 0)
        self.classification_decorator.set_decorator_for_class(
            second_class_decorator, 1)

    def test_classification_first_class(self):
        self.set_up_classification_decorator()
        self.classifier.prediction = 0
        result = self.classification_decorator.get_image()
        self.assertUnalteredImage(result)

    def test_classification_second_class(self):
        self.set_up_classification_decorator()
        self.classifier.prediction = 1
        result = self.classification_decorator.get_image()
        self.assertUnalteredImage(result)

    def test_classification_nonsense_class(self):
        self.set_up_classification_decorator()
        self.classifier.prediction = "nonsense"
        result = self.classification_decorator.get_image()
        self.assertUnalteredImage(result)
Пример #9
0
 def test_forgot_input(self):
     base_component = CachingLandmarkComponent(TestDecorator.landmark_detector)
     base_component.get_landmarks()
Пример #10
0
 def setUp(self):
     self.backdrop = cv2.imread('../tests/test_backdrop.jpg')
     self.hat = AttachedOverlayDecorator.construct_from_graphic_path('../sprites/Party_Hat.png')
     self.base_component = CachingLandmarkComponent(TestDecorator.landmark_detector)
     self.base_component.set_image(self.backdrop)
Пример #11
0
class TestDecorator(ImageTestCase):
    @classmethod
    def setUpClass(cls):
        detector_data_path = '../data/shape_predictor_68_face_landmarks.dat'
        cls.landmark_detector = LandmarkDetector(detector_data_path)

    def setUp(self):
        self.backdrop = cv2.imread('../tests/test_backdrop.jpg')
        self.hat = AttachedOverlayDecorator.construct_from_graphic_path('../sprites/Party_Hat.png')
        self.base_component = CachingLandmarkComponent(TestDecorator.landmark_detector)
        self.base_component.set_image(self.backdrop)

    def test_overlay_landmarks(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        result = landmark_overlay.get_image()
        self.assertUnalteredImage(result)

    def test_move_sprite(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_angle_offset(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.angle_offset = 90
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_perpendicular_offset(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.perpendicular_offset = -50
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_parallel_offset(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.parallel_offset = 30.5
        self.expected_landmark_distance = 300.0
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_scale(self):
        landmark_overlay = LandmarkOverlayDecorator(self.base_component)
        self.hat.parent = landmark_overlay
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.expected_landmark_distance = 600.0
        result = self.hat.get_image()
        self.assertUnalteredImage(result)

    def test_multifaces(self):
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.perpendicular_offset = -20
        self.hat.expected_landmark_distance = 300.0
        multifaces = MultiFacesLoopDecorator(self.base_component)
        self.hat.parent = multifaces.get_start_of_loop_component()
        multifaces.set_end_of_loop_component(self.hat)
        result = multifaces.get_image()
        self.assertUnalteredImage(result)

    def test_multifaces_wrap(self):
        self.hat.parent = self.base_component
        self.hat.sprite_anchors = ((25, 105), (60, 110))
        self.hat.anchored_landmark_indexes = (21, 22)
        self.hat.perpendicular_offset = -20
        self.hat.expected_landmark_distance = 300.0
        multifaces = MultiFacesLoopDecorator.wrap(self.hat)
        result = multifaces.get_image()
        self.assertUnalteredImage(result)

    def test_forgot_input(self):
        base_component = CachingLandmarkComponent(TestDecorator.landmark_detector)
        base_component.get_landmarks()

    def set_up_classification_decorator(self):
        self.classification_decorator = ClassificationDecorator(self.base_component)
        self.classifier = ConstantClassifier()
        first_class_decorator = self.hat
        second_class_decorator = LandmarkOverlayDecorator()
        self.classification_decorator.classifier = self.classifier
        self.classification_decorator.set_decorator_for_class(first_class_decorator, 0)
        self.classification_decorator.set_decorator_for_class(second_class_decorator, 1)

    def test_classification_first_class(self):
        self.set_up_classification_decorator()
        self.classifier.prediction = 0
        result = self.classification_decorator.get_image()
        self.assertUnalteredImage(result)

    def test_classification_second_class(self):
        self.set_up_classification_decorator()
        self.classifier.prediction = 1
        result = self.classification_decorator.get_image()
        self.assertUnalteredImage(result)

    def test_classification_nonsense_class(self):
        self.set_up_classification_decorator()
        self.classifier.prediction = "nonsense"
        result = self.classification_decorator.get_image()
        self.assertUnalteredImage(result)