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 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
예제 #3
0
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)
예제 #5
0
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
예제 #6
0
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
예제 #7
0
파일: bbox.py 프로젝트: nontas/menpobench
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)
예제 #8
0
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 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 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()
예제 #12
0
def landmarkgroup_view_widget_test():
    LandmarkGroup.init_with_all_label(PointCloud(pcloud2d)).view_widget()