Exemple #1
0
def lan_importer(filepath, asset=None, **kwargs):
    r"""
    Importer for the LAN file format for the GOSH dataset. This is a 3D
    landmark type and so it is assumed it only applies to meshes.

    Landmark set label: LAN

    Note that the exact meaning of each landmark in this set varies,
    so all we can do is import all landmarks found under the label 'LAN'

    Parameters
    ----------
    filepath : `Path`
        Absolute filepath of the file.
    asset : `object`, optional
        An optional asset that may help with loading. This is unused for this
        implementation.
    \**kwargs : `dict`, optional
        Any other keyword arguments.

    Returns
    -------
    landmarks : :map:`LandmarkGroup`
        The landmarks including appropriate labels if available.
    """
    with open(str(filepath), 'r') as f:
        landmarks = np.fromfile(f, dtype=np.float32)[3:].reshape(
            [-1, 3]).astype(np.double)

    pointcloud = PointCloud(landmarks)
    labels_to_masks = OrderedDict([('all',
                                    np.ones(landmarks.shape[0],
                                            dtype=np.bool))])
    return LandmarkGroup(pointcloud, labels_to_masks)
def test_LandmarkGroup_in():
    points = np.ones((3, 2))
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(3, dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    assert ('all' in lgroup)
def test_LandmarkGroup_del_unlabelled():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(3, dtype=np.bool)),
                             ('lower', np.array([1, 1, 0], dtype=np.bool))])
    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    del lgroup['all']
def test_LandmarkGroup_copy_false():
    points = np.ones((10, 3))
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])
    pcloud = PointCloud(points, copy=False)
    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)
    assert (is_same_array(lgroup._pointcloud.points, points))
    assert (lgroup._labels_to_masks is mask_dict)
    assert (lgroup.lms is pcloud)
def test_LandmarkGroup_get_None():
    points = np.ones((10, 3))
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    assert lgroup[None] is not pcloud
    assert_allclose(lgroup[None].points, pcloud.points)
def test_LandmarkGroup_str():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(3, dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    out_str = lgroup.__str__()
    assert (len(out_str) > 0)
def test_LandmarkGroup_iterate():
    points = np.ones((10, 3))
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])
    pcloud = PointCloud(points, copy=False)
    target = PointCloud(points)

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    for l in lgroup:
        assert_equal(l, 'all')
Exemple #8
0
def test_LandmarkGroup_get():
    points = np.ones((3, 2))
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('lower', np.array([1, 1, 0], dtype=np.bool)),
                             ('upper', np.array([0, 0, 1], dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    assert_allclose(lgroup['lower'].n_points, 2)
    assert_allclose(lgroup['upper'].n_points, 1)
def test_LandmarkManager_get():
    points = np.ones((10, 3))
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    man = LandmarkManager()
    man._landmark_groups['test_set'] = lgroup

    assert(man['test_set'] is lgroup)
Exemple #10
0
def test_LandmarkManager_set_LandmarkGroup():
    points = np.ones((10, 3))
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])
    pcloud = PointCloud(points, copy=False)
    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    man = LandmarkManager()
    man['test_set'] = lgroup
    assert (not is_same_array(man['test_set'].lms.points, lgroup.lms.points))
    assert_allclose(man['test_set']['all'].points, np.ones([10, 3]))
    assert (man['test_set']._labels_to_masks is not lgroup._labels_to_masks)
def test_LandmarkGroup_set_ordered_labels():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(3, dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    lgroup['lower'] = [0, 1]

    assert_allclose(lgroup['lower'].n_points, 2)
    assert_allclose(lgroup['lower'].points[0, :], [0, 1])
    assert_allclose(lgroup['lower'].points[1, :], [2, 3])
def test_LandmarkManager_set():
    points = np.ones((10, 3))
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    man = LandmarkManager()
    man['test_set'] = lgroup

    assert_allclose(man._landmark_groups['test_set'].lms.points,
                    lgroup.lms.points)
    assert_equal(man._landmark_groups['test_set'].n_labels, 1)
Exemple #13
0
def test_landmarkgroup_copy_method():
    points = np.ones((10, 3))
    mask_dict = OrderedDict([('all', np.ones(10, dtype=np.bool))])
    pcloud = PointCloud(points, copy=False)

    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)
    lgroup_copy = lgroup.copy()

    assert (not is_same_array(lgroup_copy.lms.points, lgroup.lms.points))
    # Check the mask dictionary is deepcopied properly
    assert (lgroup._labels_to_masks is not lgroup_copy._labels_to_masks)
    masks = zip(lgroup_copy._labels_to_masks.values(),
                lgroup._labels_to_masks.values())
    for ms in masks:
        assert (ms[0] is not ms[1])
def test_LandmarkGroup_without_labels():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('lower', np.array([1, 1, 0], dtype=np.bool)),
                             ('upper', np.array([0, 0, 1], dtype=np.bool))])
    lgroup = LandmarkGroup(pcloud, mask_dict, copy=False)

    new_lgroup = lgroup.without_labels('upper')

    assert_equal(new_lgroup.n_labels, 1)
    assert_equal(new_lgroup.n_landmarks, 2)
    assert ('lower' in new_lgroup)

    new_lgroup = lgroup.without_labels(['upper'])

    assert_equal(new_lgroup.n_labels, 1)
    assert_equal(new_lgroup.n_landmarks, 2)
    assert ('lower' in new_lgroup)
Exemple #15
0
def bnd_importer(filepath, asset=None, **kwargs):
    r"""
    Importer for the BND file format for the BU-3DFE dataset. This is a 3D
    landmark type and so it is assumed it only applies to meshes.

    Landmark set label: BND

    Landmark labels:

    +---------------+
    | label         |
    +===============+
    | left_eye      |
    | right_eye     |
    | left_eyebrow  |
    | right_eyebrow |
    | nose          |
    | mouth         |
    | chin          |
    +---------------+

    Parameters
    ----------
    filepath : `Path`
        Absolute filepath of the file.
    asset : `object`, optional
        An optional asset that may help with loading. This is unused for this
        implementation.
    \**kwargs : `dict`, optional
        Any other keyword arguments.

    Returns
    -------
    landmarks : :map:`LandmarkGroup`
        The landmarks including appropriate labels if available.
    """
    with open(str(filepath), 'r') as f:
        landmarks = f.read()

    # Remove blank lines
    landmark_text = [l for l in landmarks.splitlines() if l.rstrip()]
    landmark_text = [l.split() for l in landmark_text]

    n_points = len(landmark_text)
    landmarks = np.zeros([n_points, 3])
    for i, l in enumerate(landmark_text):
        # Skip the first number as it's an index into the mesh
        landmarks[i, :] = np.array(
            [float(l[1]), float(l[2]), float(l[3])], dtype=np.float)

    pointcloud = PointCloud(landmarks)
    labels_to_masks = OrderedDict([
        ('left_eye', _indices_to_mask(n_points, np.arange(8))),
        ('right_eye', _indices_to_mask(n_points, np.arange(8, 16))),
        ('left_eyebrow', _indices_to_mask(n_points, np.arange(16, 26))),
        ('right_eyebrow', _indices_to_mask(n_points, np.arange(26, 36))),
        ('nose', _indices_to_mask(n_points, np.arange(36, 48))),
        ('mouth', _indices_to_mask(n_points, np.arange(48, 68))),
        ('chin', _indices_to_mask(n_points, np.arange(68, 83)))
    ])
    return LandmarkGroup(pointcloud, labels_to_masks)
def test_LandmarkGroup_create_incorrect_shape():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict(['all', np.ones(5, dtype=np.bool)])

    LandmarkGroup(pcloud, mask_dict, copy=False)
def test_LandmarkGroup_create_no_mask():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)

    LandmarkGroup(pcloud, None, copy=False)
def test_LandmarkGroup_pass_non_ordered_dict():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = {'all': np.ones(3, dtype=np.bool)}

    LandmarkGroup(pcloud, mask_dict, copy=False)
def test_LandmarkGroup_create_unlabelled():
    points = np.array([[0, 1], [2, 3], [4, 5]])
    pcloud = PointCloud(points, copy=False)
    mask_dict = OrderedDict([('all', np.zeros(3, dtype=np.bool))])

    LandmarkGroup(pcloud, mask_dict, copy=False)
Exemple #20
0
from nose.tools import raises
import numpy as np
import menpo.io as mio
from mock import patch, PropertyMock
from collections import OrderedDict
from menpo.landmark import LandmarkGroup
from menpo.shape import PointCloud
from menpo.image import Image

pc = PointCloud(np.random.random([100, 3]))
test_lg = LandmarkGroup(
    pc, OrderedDict([('all', np.ones(pc.n_points, dtype=np.bool))]))
test_img = Image(np.random.random([100, 100]))
fake_path = '/tmp/test.fake'


@patch('menpo.io.output.base.landmark_types')
@patch('menpo.io.output.base.Path.exists')
@patch('menpo.io.output.base.Path.open')
def test_export_filepath_overwrite_exists(mock_open, exists, landmark_types):
    exists.return_value = True
    mio.export_landmark_file(fake_path, test_lg, overwrite=True)
    mock_open.assert_called_once_with('wb')
    landmark_types.__getitem__.assert_called_once_with('.fake')
    export_function = landmark_types.__getitem__.return_value
    export_function.assert_called_once()


@patch('menpo.io.output.base.landmark_types')
@patch('menpo.io.output.base.Path.exists')
@patch('menpo.io.output.base.Path.open')
Exemple #21
0
def landmark3d_viewer_test():
    LandmarkGroup(PointCloud(fake_triangle),
                  OrderedDict([('all', np.ones(3, dtype=np.bool))]),
                  copy=False).view()
Exemple #22
0
def lm3_importer(filepath, asset=None, **kwargs):
    r"""
    Importer for the LM3 file format from the bosphorus dataset. This is a 3D
    landmark type and so it is assumed it only applies to meshes.

    Landmark set label: LM3

    Landmark labels:

    +------------------------+
    | label                  |
    +========================+
    | outer_left_eyebrow     |
    | middle_left_eyebrow    |
    | inner_left_eyebrow     |
    | inner_right_eyebrow    |
    | middle_right_eyebrow   |
    | outer_right_eyebrow    |
    | outer_left_eye_corner  |
    | inner_left_eye_corner  |
    | inner_right_eye_corner |
    | outer_right_eye_corner |
    | nose_saddle_left       |
    | nose_saddle_right      |
    | left_nose_peak         |
    | nose_tip               |
    | right_nose_peak        |
    | left_mouth_corner      |
    | upper_lip_outer_middle |
    | right_mouth_corner     |
    | upper_lip_inner_middle |
    | lower_lip_inner_middle |
    | lower_lip_outer_middle |
    | chin_middle            |
    +------------------------+

    Parameters
    ----------
    filepath : `Path`
        Absolute filepath of the file.
    asset : `object`, optional
        An optional asset that may help with loading. This is unused for this
        implementation.
    \**kwargs : `dict`, optional
        Any other keyword arguments.

    Returns
    -------
    landmarks : :map:`LandmarkGroup`
        The landmarks including appropriate labels if available.
    """
    with open(str(filepath), 'r') as f:
        landmarks = f.read()

    # Remove comments and blank lines
    landmark_text = [
        l for l in landmarks.splitlines() if (l.rstrip() and not '#' in l)
    ]

    # First line says how many landmarks there are: 22 Landmarks
    # So pop it off the front
    num_points = int(landmark_text.pop(0).split()[0])
    xs = []
    ys = []
    zs = []
    labels = []

    # The lines then alternate between the labels and the coordinates
    for i in xrange(num_points * 2):
        if i % 2 == 0:  # label
            # Lowercase, remove spaces and replace with underscores
            l = landmark_text[i]
            l = '_'.join(l.lower().split())
            labels.append(l)
        else:  # coordinate
            p = landmark_text[i].split()
            xs.append(float(p[0]))
            ys.append(float(p[1]))
            zs.append(float(p[2]))

    xs = np.array(xs, dtype=np.float).reshape((-1, 1))
    ys = np.array(ys, dtype=np.float).reshape((-1, 1))
    zs = np.array(zs, dtype=np.float).reshape((-1, 1))

    pointcloud = PointCloud(np.hstack([xs, ys, zs]))
    # Create the mask whereby there is one landmark per label
    # (identity matrix)
    masks = np.eye(num_points).astype(np.bool)
    masks = np.vsplit(masks, num_points)
    masks = [np.squeeze(m) for m in masks]
    labels_to_masks = OrderedDict(zip(labels, masks))
    return LandmarkGroup(pointcloud, labels_to_masks)