Beispiel #1
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)
Beispiel #2
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
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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
Beispiel #6
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
Beispiel #7
0
    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
Beispiel #8
0
    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)
Beispiel #9
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))]
Beispiel #10
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()
Beispiel #11
0
    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)
Beispiel #12
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])
Beispiel #13
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])
Beispiel #14
0
    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
Beispiel #15
0
def random_lf():
    return LandmarkFace(np.random.random((68, 2)), np.zeros((10, 12)))
Beispiel #16
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
Beispiel #17
0
def lf():
    points = np.random.random((68, 2))
    return LandmarkFace(points, np.zeros((12, 13)))