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_str(): points = np.array([[0, 1], [2, 3], [4, 5]]) pcloud = PointCloud(points, copy=False) target = PointCloud(points) mask_dict = {'all': np.ones(3, dtype=np.bool)} lgroup = LandmarkGroup(target, 'label', pcloud, mask_dict, copy=False) out_str = lgroup.__str__() assert (len(out_str) > 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 predict_in_frame(frame_name, clip, img_type): global detector im = im_read_greyscale(frame_name, clip.path_frames, img_type, normalise=False) res_dlib = detector(im) # in the following lines a hack to figure out whether there are more than # 10 detections returned. In such a case, there should be two digits in # each group. num_res = len(res_dlib) if num_res == 0: return num1 = 1 # num1 and s1: Values if there are more than 10 detections in the image if num_res > 9: num1 = 2 s1 = '%0' + str(num1) im_pili = np.array(im.as_PILImage()) # loop over the returned detections. # By restricting the range below, we can choose the # k first (more confident) bounding boxes. # num_res to keep all, here keeping ONLY the most confident one for kk in range(0, 1): pts_end = im.path.stem + '_' + str(kk) + pts_type_out ln = im.landmarks['ffld2_' + (s1 + 'd') % kk] mio.export_landmark_file(ln, clip.path_write_ln[0] + pts_end, overwrite=True) # convert to landmarks det_frame = predictor_dlib(im_pili, pointgraph_to_rect(ln.lms)) init_pc = detection_to_pointgraph(det_frame) mio.export_landmark_file(LandmarkGroup.init_with_all_label(init_pc), clip.path_write_ln[1] + pts_end, overwrite=True)
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_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_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_create_with_all_label(): points = np.ones((10, 3)) pcloud = PointCloud(points, copy=False) lgroup = LandmarkGroup.init_with_all_label(pcloud, copy=False) assert lgroup.n_labels == 1 assert 'all' in lgroup
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_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)
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_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')
def test_LandmarkGroup_without_labels(): points = np.array([[0, 1], [2, 3], [4, 5]]) pcloud = PointCloud(points, copy=False) target = PointCloud(points) mask_dict = {'lower': np.array([1, 1, 0], dtype=np.bool), 'upper': np.array([0, 0, 1], dtype=np.bool)} lgroup = LandmarkGroup(target, 'label', 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)
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)
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)
def test_register_landmark_importer(is_file): from menpo.shape import PointCloud from menpo.landmark import LandmarkGroup lmark = LandmarkGroup.init_with_all_label(PointCloud.init_2d_grid((1, 1))) def foo_importer(filepath, **kwargs): return lmark is_file.return_value = True with patch.dict(mio.input.extensions.image_landmark_types, {}, clear=True): mio.register_landmark_importer('.foo', foo_importer) new_lmark = mio.import_landmark_file('fake.foo') assert lmark is new_lmark
def detect_in_frame(frame_name, clip, img_type): # if normalise=True in im_read_greyscale: before calling dlib detector, image should be converted to uint8 im = im_read_greyscale(frame_name, clip.path_frames, img_type, normalise=False) if not im: print(frame_name, clip.path_frames) return res_dlib = dlib_init_detector(im, group_prefix='dlib') # call dlib detector im_pili = np.array(im.as_PILImage()) for kk, g in enumerate(im.landmarks.group_labels): pts_end = im.path.stem + '_' + str(kk) + pts_type_out # define the ending of each pts that will be exported export_landmark_file(im.landmarks[g], clip.path_write_ln[0] + pts_end, overwrite=True) # from bounding box to points (dlib predictor) init_pc = detection_to_pointgraph(predictor_dlib(im_pili, pointgraph_to_rect(im.landmarks[g].lms))) export_landmark_file(LandmarkGroup.init_with_all_label(init_pc), clip.path_write_ln[1] + pts_end, overwrite=True)
def save_bounding_boxes(pattern, detector_type, group=None, sythesize_problematic=False, overwrite=False): import menpo.io as mio from menpo.landmark import LandmarkGroup from menpo.model import PCAModel try: detector = _DETECTORS[detector_type]() except KeyError: detector_list = ', '.join(list(_DETECTORS.keys())) raise ValueError('Valid detector types are: {}'.format(detector_list)) print('Running {} detector on {}'.format(detector_type, pattern)) bboxes = {img.path: detect_and_check(img, detector, group=group) for img in mio.import_images(pattern, normalise=False, verbose=True)} # find all the detections that failed problematic = filter(lambda x: x[1]['d'] is None, bboxes.items()) print('Failed to detect {} objects'.format(len(problematic))) if len(problematic) > 0 and sythesize_problematic: print('Learning detector traits and sythesizing fits for {} ' 'images'.format(len(problematic))) # get the good detections detections = filter(lambda x: x['d'] is not None, bboxes.values()) # normalize these to size [1, 1], centred on origin normed_detections = [normalize(r['gt']).apply(r['d']) for r in detections] # build a PCA model from good detections pca = PCAModel(normed_detections) for p, r in problematic: # generate a new bbox offset in the normalized space by using # our learnt PCA basis d = random_instance(pca) # apply an inverse transform to place it on the image bboxes[p]['d'] = normalize(r['gt']).pseudoinverse().apply(d) to_save = len(bboxes) if not sythesize_problematic: to_save = to_save - len(problematic) print('Saving out {} {} detections'.format(to_save, detector_type)) # All done, save out results for p, r in bboxes.items(): if r['d'] is not None: lg = LandmarkGroup.init_with_all_label(r['d']) mio.export_landmark_file(lg, p.parent / (p.stem + '_{}.ljson'.format(detector_type)), overwrite=overwrite)
def check_label_func(func, input_n_points, output_n_points): # Could be any dimensionality array = np.zeros(input_n_points) pcloud = PointCloud(array) lmark_g = LandmarkGroup.init_with_all_label(pcloud) array_result = func(array) assert isinstance(array_result, PointCloud) assert array_result.n_points == output_n_points pcloud_result = func(pcloud) assert isinstance(pcloud_result, PointCloud) assert pcloud_result.n_points == output_n_points lmark_g_result = func(lmark_g) assert isinstance(lmark_g_result, LandmarkGroup) assert lmark_g_result.lms.n_points == output_n_points
def check_label_func(func, input_n_points, output_n_points): # Could be any dimensionality array = np.zeros([input_n_points, 2]) pcloud = PointCloud(array) lmark_g = LandmarkGroup.init_with_all_label(pcloud) array_result = func(array) assert isinstance(array_result, PointCloud) assert array_result.n_points == output_n_points pcloud_result = func(pcloud) assert isinstance(pcloud_result, PointCloud) assert pcloud_result.n_points == output_n_points lmark_g_result = func(lmark_g) assert isinstance(lmark_g_result, LandmarkGroup) assert lmark_g_result.lms.n_points == output_n_points
def predict_in_frame(frame_name, clip, img_type): global detector im = im_read_greyscale(frame_name, clip.path_frames, img_type, normalise=False) res_dlib = detector(im) num_res = len(res_dlib) if num_res == 0: return num1 = 1 # num1 and s1: Values if there are more than 10 detections in the image if num_res > 9: num1 = 2 s1 = '%0' + str(num1) im_pili = np.array(im.as_PILImage()) for kk in range(0, 1): # num_res to keep all, here keeping ONLY the most confident one pts_end = im.path.stem + '_' + str(kk) + pts_type_out ln = im.landmarks['ffld2_' + (s1 + 'd') % kk] mio.export_landmark_file(ln, clip.path_write_ln[0] + pts_end, overwrite=True) # convert to landmarks det_frame = predictor_dlib(im_pili, pointgraph_to_rect(ln.lms)) init_pc = detection_to_pointgraph(det_frame) mio.export_landmark_file(LandmarkGroup.init_with_all_label(init_pc), clip.path_write_ln[1] + pts_end, overwrite=True)
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)
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 landmarkgroup_view_widget_test(): LandmarkGroup.init_with_all_label(PointCloud(pcloud2d)).view_widget()
def test_LandmarkManager_set_None_key(): pcloud = PointCloud(np.ones((10, 3)), copy=False) lgroup = LandmarkGroup.init_with_all_label(pcloud) man = LandmarkManager() man[None] = lgroup
def test_LandmarkGroup_has_nan_values(): points = np.ones((10, 3)) points[0, 0] = np.nan pcloud = PointCloud(points, copy=False) lgroup = LandmarkGroup.init_with_all_label(pcloud, copy=False) assert lgroup.has_nan_values()
def landmark3d_viewer_test(): LandmarkGroup(PointCloud(fake_triangle), OrderedDict([('all', np.ones(3, dtype=np.bool))]), copy=False).view()
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')
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)