コード例 #1
0
    def test_identical(self):
        lf = LandmarkFace(np.random.random((68, 2)), np.zeros((12, 13)))

        for i in range(68):
            for j in range(68):
                ref_vector = lf.points[j] - lf.points[i]
                assert lf.angle(i, j, reference_vector=ref_vector) == 0
コード例 #2
0
    def test_no_faces(self, monkeypatch):
        monkeypatch.setattr('pychubby.detect.face_rectangle',
                            lambda *args, **kwargs: ([], []))
        img = np.random.random((10, 11))

        with pytest.raises(ValueError):
            LandmarkFace.estimate(img)
コード例 #3
0
    def test_rad2deg(self):
        lf = LandmarkFace(np.random.random((68, 2)), np.zeros((12, 13)))

        for i in range(68):
            for j in range(68):
                res_rad = lf.angle(i, j, use_radians=True)
                res_deg = lf.angle(i, j)
                assert math.degrees(res_rad) == res_deg
コード例 #4
0
    def test_precise_value(self):
        points = np.random.random((68, 2))
        points[0] = [2, 3]
        points[1] = [5, 7]

        lf = LandmarkFace(points, np.zeros((12, 13)))

        assert lf.euclidean_distance(0, 1) == 5
        assert lf.euclidean_distance(1, 0) == 5
コード例 #5
0
    def test_constructor(self):
        with pytest.raises(ValueError):
            LandmarkFaces()

        with pytest.raises(TypeError):
            LandmarkFaces('a')

        with pytest.raises(ValueError):
            points = np.random.random((68, 2))
            lf_1 = LandmarkFace(points, np.zeros((12, 13)))
            lf_2 = LandmarkFace(points, np.ones((12, 13)))
            LandmarkFaces(lf_1, lf_2)
コード例 #6
0
    def test_plot(self, monkeypatch):
        mock = Mock()

        monkeypatch.setattr('pychubby.detect.plt', mock)

        lf = LandmarkFace(np.random.random((68, 2)), np.random.random(
            (12, 13)))

        lf.plot()

        if PYTHON_VERSION > 3.5:
            mock.figure.assert_called()
            mock.scatter.assert_called()
            mock.imshow.assert_called()
コード例 #7
0
    def test_duplicate_landmarks(self):
        img = np.zeros((10, 11))
        points = np.random.random((67, 2))
        points = np.vstack([points, np.array([points[-1]])])

        with pytest.raises(ValueError):
            LandmarkFace(points, img)
コード例 #8
0
    def test_constructor_wrong_input(self):

        img = np.zeros((10, 11))
        points = np.random.random((12, 2))

        with pytest.raises(ValueError):
            LandmarkFace(points, img)
コード例 #9
0
ファイル: actions.py プロジェクト: vkovalchuk/pychubby
    def perform(self, lfs):
        """Perform actions on multiple faces.

        Parameters
        ----------
        lfs : LandmarkFaces
            Instance of ``LandmarkFaces``.

        Returns
        -------
        new_lfs : LandmarkFaces
            Instance of a ``LandmarkFaces`` after taking the action on each face.

        df : DisplacementField
            Displacement field representing the transformation between the old and new image.

        """
        if isinstance(lfs, LandmarkFace):
            lfs = LandmarkFaces(lfs)

        n_actions = len(self.per_face_action)
        n_faces = len(lfs)

        if n_actions not in {1, n_faces}:
            raise ValueError(
                "Number of actions ({}) is different from number of faces({})".
                format(n_actions, n_faces))

        lf_list_new = []
        for lf, a in zip(
                lfs,
                self.per_face_action if n_actions != 1 else n_faces *
                self.per_face_action,
        ):
            lf_new, _ = a.perform(lf) if a is not None else (lf, None)
            lf_list_new.append(lf_new)

        # Overall displacement
        img = lfs[0].img
        shape = img.shape[:2]
        old_points = np.vstack([lf.points for lf in lfs])
        new_points = np.vstack([lf.points for lf in lf_list_new])

        df = DisplacementField.generate(shape,
                                        old_points,
                                        new_points,
                                        anchor_corners=True,
                                        function="linear")

        # Make sure same images
        img_final = df.warp(img)
        lfs_new = LandmarkFaces(
            *[LandmarkFace(lf.points, img_final) for lf in lf_list_new])

        return lfs_new, df
コード例 #10
0
ファイル: test_reference.py プロジェクト: vkovalchuk/pychubby
    def test_all(self):
        lf = LandmarkFace(np.random.random((68, 2)), np.zeros((12, 13)))

        rs = DefaultRS()

        rs.estimate(lf)

        random_ref_points = np.random.random((10, 2))

        assert np.allclose(rs.inp2ref(rs.ref2inp(random_ref_points)),
                           random_ref_points)
コード例 #11
0
    def test_incorrect_input(self):
        points = np.random.random((68, 2))
        lf = LandmarkFace(points, np.zeros((12, 13)))

        with pytest.raises(TypeError):
            lf[(1, 2)]

        with pytest.raises(TypeError):
            lf[[32.1]]

        with pytest.raises(ValueError):
            lf[np.zeros((2, 2))]
コード例 #12
0
def smile(img):
    img_path = img
    img = cv2.imread(img_path)
    img8 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    lf = LandmarkFace.estimate(img8)
    from pychubby.actions import Smile

    a = Smile(scale=0.2)
    new_lf, df = a.perform(lf)
    ani = create_animation(df, img)
    plt.imsave('output_image.gif', ani)

    plt.show()
コード例 #13
0
    def test_multiple_faces(self, monkeypatch):
        img = np.random.random((10, 11))

        monkeypatch.setattr('pychubby.detect.face_rectangle',
                            lambda *args, **kwargs: (2 * [None], 2 * [None]))

        monkeypatch.setattr('pychubby.detect.landmarks_68', lambda *args:
                            (np.random.random((68, 2)), None))

        with pytest.raises(ValueError):
            LandmarkFace.estimate(img, allow_multiple=False)

        lfs = LandmarkFace.estimate(img, allow_multiple=True)

        assert isinstance(lfs, LandmarkFaces)
        assert len(lfs) == 2

        # only feed invalid, empty entry for LandmarkFaces constructor
        monkeypatch.setattr('pychubby.detect.landmarks_68', lambda *args:
                            (np.zeros((68, 2)), None))

        with pytest.raises(ValueError):
            LandmarkFace.estimate(img, allow_multiple=True)
コード例 #14
0
    def test_overall(self, monkeypatch):
        img = np.random.random((10, 11))

        monkeypatch.setattr('pychubby.detect.face_rectangle',
                            lambda *args, **kwargs: ([None], [None]))

        monkeypatch.setattr('pychubby.detect.landmarks_68', lambda *args:
                            (np.random.random((68, 2)), None))

        lf = LandmarkFace.estimate(img)

        assert isinstance(lf, LandmarkFace)
        assert lf.points.shape == (68, 2)
        assert lf.img.shape == (10, 11)
コード例 #15
0
ファイル: test_actions.py プロジェクト: vkovalchuk/pychubby
    def test_overall(self, random_lf, per_face_action):
        lf_1 = random_lf
        lf_2 = LandmarkFace(random_lf.points + np.random.random((68, 2)),
                            random_lf.img)

        lfs = LandmarkFaces(lf_1, lf_2)

        a = Multiple(per_face_action)

        new_lfs, df = a.perform(lfs)

        assert isinstance(new_lfs, LandmarkFaces)
        assert isinstance(df, DisplacementField)
        assert len(lfs) == len(new_lfs)
コード例 #16
0
    def test_with_int(self):
        random_state = 2
        np.random.seed(random_state)

        points = np.random.random((68, 2))
        lf = LandmarkFace(points, np.zeros((12, 13)))

        # one by one
        for i in range(68):
            assert np.allclose(lf[i], points[i])
        # random list of indices
        ixs = np.random.randint(0, 68, size=10)

        assert np.allclose(lf[ixs], points[ixs])
        assert np.allclose(lf[[x.item() for x in ixs]], points[ixs])
コード例 #17
0
        def f(*args, **kwargs):
            """Perform an action."""
            inp_img = kwargs.pop("inp_img")
            out_img = kwargs.pop("out_img")

            img = plt.imread(str(inp_img))
            lf = LandmarkFace.estimate(img)
            cls = getattr(pychubby.actions, self.name)
            a = pychubby.actions.Multiple(cls(**kwargs))

            new_lf, df = a.perform(lf)

            if out_img is not None:
                plt.imsave(str(out_img), new_lf[0].img)
            else:
                new_lf.plot(show_landmarks=False, show_numbers=False)
コード例 #18
0
    def test_with_str(self):
        random_state = 2
        np.random.seed(random_state)

        ix2name = {v: k for k, v in LANDMARK_NAMES.items()}

        points = np.random.random((68, 2))
        lf = LandmarkFace(points, np.zeros((12, 13)))

        # one by one
        for i in range(68):
            assert np.allclose(lf[ix2name[i]], points[i])
        # random list of indices
        ixs = np.random.randint(0, 68, size=10)
        strings = [ix2name[x] for x in ixs]

        assert np.allclose(lf[strings], points[ixs])
コード例 #19
0
def perform(*args, **kwargs):
    """Perform an action."""

    action = kwargs.get("action", None)
    logger.info("action: {} START".format(action))
    # Get uploaded file information:
    file = connexion.request.files["inp_img"]

    if action not in cli.ALL_ACTIONS:
        return problem(
            title="Wrong Action",
            detail="The action: {} does not exist.".format(action),
            status=400,
        )

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join("/tmp/", filename))

        logger.info("inp_file: {}".format(file.filename))
        img = plt.imread("/tmp/{}".format(filename))

        logger.info("LandmarkFace.estimate")
        lf = LandmarkFace.estimate(img)
        logger.info("pychubby action: {}".format(action))
        cls = getattr(pychubby.actions, action)
        a = pychubby.actions.Multiple(cls())

        logger .info("new_lf: a.perform")
        new_lf, df = a.perform(lf)
        out_img = "/tmp/chubbyfied_{}".format(file.filename)
        logger.info("Saving output image: {}".format(out_img))
        plt.imsave(str(out_img), new_lf[0].img)

        logger.info("action: {} END".format(action))
        return send_file(out_img), 200
    else:
        return problem(
            title="File not allowed",
            detail="The Input File: '{}' is not allowed.".format(file.filename),
            status=400,
        )
コード例 #20
0
def photo():
    img = request.args.get('photob62')
    imgdata = base64.b64decode(img)
    img = cv2.imread(io.BytesIO(base64.b64decode(imgdata)))
    img = cv2.imread(img)
    img8 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    lf = LandmarkFace.estimate(img8)

    from pychubby.actions import Smile, OpenEyes, Multiple, RaiseEyebrow, StretchNostrils, AbsoluteMove

    smile = Smile(scale=0.2)

    new_lf, df = smile.perform(lf)  # lf defined above

    # new_lf.plot(show_landmarks=False)
    plt.imsave('output_image.png', new_lf.img)
    import base64
    encoded = base64.b64encode(open("output_image.png", "rb").read())
    return encoded
コード例 #21
0
ファイル: actions.py プロジェクト: vkovalchuk/pychubby
    def pts2inst(new_points, lf, **interpolation_kwargs):
        """Generate instance of LandmarkFace via interpolation.

        Parameters
        ----------
        new_points : np.ndarray
            Array of shape `(N, 2)` representing the x and y coordinates of the
            new landmark points.

        lf : LandmarkFace
            Instance of a ``LandmarkFace`` before taking any actions.

        interpolation_kwargs : dict
            Interpolation parameters passed onto scipy.

        Returns
        -------
        new_lf : LandmarkFace
            Instance of a ``LandmarkFace`` after taking an action.

        df : DisplacementField
            Displacement field representing per pixel displacements between the
            old and new image.

        """
        if not interpolation_kwargs:
            interpolation_kwargs = {"function": "linear"}

        df = DisplacementField.generate(lf.img.shape[:2],
                                        lf.points,
                                        new_points,
                                        anchor_edges=True,
                                        **interpolation_kwargs)

        new_img = df.warp(lf.img)

        return LandmarkFace(new_points, new_img), df
コード例 #22
0
    def test_identical(self):
        lf = LandmarkFace(np.random.random((68, 2)), np.zeros((12, 13)))

        for lix in range(68):
            assert lf.euclidean_distance(lix, lix) == 0
コード例 #23
0
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            # show the face number
            cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            # loop over the (x, y)-coordinates for the facial landmarks
            # and draw them on the image
            for (x, y) in shape:
                cv2.circle(image, (x, y), 2, (0, 0, 255, 255), -1)
                cv2.circle(blank_image, (x, y), 2, (0, 0, 255, 0), -1)
        cv2.imshow('Video', blank_image)
        cv2.imshow('video1', image)

        # img = frame
        print(frame.shape)
        try:
            lf = LandmarkFace.estimate(img)

            a_per_face = Pipeline([Smile()])
            a_all = Multiple(a_per_face)

            new_lf, _ = a_all.perform(lf)
            new_img = new_lf
            #new_lf.plot(figsize=(5, 5), show_numbers=False)
        except:
            pass
        # data = np.fromstring(new_lf, dtype=np.uint8, sep='')
        # cv2.imshow('video', data)
        cam.send(blank_image)
        cam.sleep_until_next_frame()

        if cv2.waitKey(1) & 0xFF == ord('q'):
コード例 #24
0
ファイル: test_actions.py プロジェクト: vkovalchuk/pychubby
def random_lf():
    return LandmarkFace(np.random.random((68, 2)), np.zeros((10, 12)))
コード例 #25
0
def lf():
    points = np.random.random((68, 2))
    return LandmarkFace(points, np.zeros((12, 13)))