Ejemplo n.º 1
0
def affine():
    import menpo.io as mio
    takeo = mio.import_builtin_asset.takeo_ppm()
    # Use a bounding box of ground truth landmarks to create template
    takeo.landmarks['bounding_box'] = takeo.landmarks['PTS'].lms.bounding_box()
    template = takeo.crop_to_landmarks(group='bounding_box', boundary=10)
    template.view()
    from menpofit.lk import LucasKanadeFitter, InverseCompositional, SSD
    from menpo.feature import no_op

    fitter = LucasKanadeFitter(template,
                               group='bounding_box',
                               algorithm_cls=InverseCompositional,
                               residual_cls=SSD,
                               scales=(0.5, 1.0),
                               holistic_features=no_op)
    print fitter

    from menpofit.fitter import noisy_shape_from_bounding_box

    gt_bb = takeo.landmarks['bounding_box'].lms
    # generate perturbed bounding box
    init_bb = noisy_shape_from_bounding_box(fitter.reference_shape,
                                            gt_bb,
                                            noise_percentage=0.1,
                                            allow_alignment_rotation=True)
    # fit image
    result = fitter.fit_from_bb(takeo, init_bb, gt_shape=gt_bb, max_iters=80)

    # result.view()

    from menpowidgets import visualize_images
    visualize_images(fitter.warped_images(result.image, result.shapes))
Ejemplo n.º 2
0
def test_AAM(fitter, images):
    for image in images:
        image = mio.import_image(image)
        image = image.as_greyscale()
        initial_bbox = image.landmarks['PTS'].bounding_box()
        gt_shape = image.landmarks['PTS'].lms
        initial_shape = noisy_shape_from_bounding_box(gt_shape,
                                                      gt_shape.bounding_box())
        image.landmarks['boundingbox'] = initial_bbox
        image.landmarks['init_shape'] = initial_shape
        image.view_landmarks(group='boundingbox',
                             line_colour='red',
                             render_markers=False,
                             line_width=4)
        image.view_landmarks(group='init_shape')
        # fit image
        result = fitter.fit_from_bb(image,
                                    initial_bbox,
                                    max_iters=[15, 5],
                                    gt_shape=image.landmarks['PTS'].lms)
        # print result
        print(result)

        # fit image
        result1 = fitter.fit_from_shape(image,
                                        initial_shape,
                                        max_iters=[15, 5],
                                        gt_shape=image.landmarks['PTS'].lms)
        # print result
        print(result1)

        result.view(render_initial_shape=True)
Ejemplo n.º 3
0
def fitting_img(image, fitter):
    # detect = load_dlib_frontal_face_detector()
    # bboxes = detect(image)
    # initial_bbox = bboxes[0]
    # fitting_result = fitter.fit_from_bb(image, initial_bbox,
    #                                     #max_iters=[15, 5]
    #                                     )
    # obtain groubnd truth (original) landmarks
    gt_s = image.landmarks['PTS']
    # generate initialization landmarks
    initial_s = noisy_shape_from_bounding_box(gt_s, gt_s.bounding_box())
    
    fitting_result = fitter.fit_from_shape(image, initial_s,max_iters=20, gt_shape=gt_s, return_costs=True)

    return fitting_result
Ejemplo n.º 4
0
def get_noisy_init_from_bb(reference_shape, bb, noise_percentage=.02):
    """Roughly aligns a reference shape to a bounding box.
    This adds some uniform noise for translation and scale to the
    aligned shape.
    Args:
      reference_shape: a numpy array [num_landmarks, 2]
      bb: bounding box, a numpy array [4, ]
      noise_percentage: noise presentation to add.
    Returns:
      The aligned shape, as a numpy array [num_landmarks, 2]
    """
    bb = PointCloud(bb)
    reference_shape = PointCloud(reference_shape)

    bb = noisy_shape_from_bounding_box(
        reference_shape,
        bb,
        noise_percentage=[noise_percentage, 0,
                          noise_percentage]).bounding_box()

    return align_shape_with_bounding_box(reference_shape, bb).points
Ejemplo n.º 5
0
def get_noisy_init_from_bb(reference_shape, bb, noise_percentage=.02):
    """Roughly aligns a reference shape to a bounding box.

    This adds some uniform noise for translation and scale to the
    aligned shape.

    Args:
      reference_shape: a numpy array [num_landmarks, 2]
      bb: bounding box, a numpy array [4, ]
      noise_percentage: noise presentation to add.
    Returns:
      The aligned shape, as a numpy array [num_landmarks, 2]
    """
    bb = PointCloud(bb)
    reference_shape = PointCloud(reference_shape)

    bb = noisy_shape_from_bounding_box(
        reference_shape, bb, 
        noise_percentage=[noise_percentage, 0, noise_percentage]
    ).bounding_box()

    return align_shape_with_bounding_box(reference_shape, bb).points
Ejemplo n.º 6
0
 def noisy_shape_from_bounding_box(self, bounding_box, noise_type='uniform',
                                   noise_percentage=0.1, rotation=False):
     return noisy_shape_from_bounding_box(
         self.reference_shape, bounding_box, noise_type=noise_type,
         noise_percentage=noise_percentage, rotation=rotation)
Ejemplo n.º 7
0
            i = i.as_greyscale(mode='luminosity')

        # append it to the list
        images.append(i)
    return images


# 测试集
image_path_test = "D:/电信研究院/人脸矫正/labelme-master/examples/transfer/test"
test_images = load_database(image_path_test, 0.5, max_images=5)
fitting_results = []
for i in test_images:
    # obtain original landmarks
    gt_s = i.landmarks['PTS'].lms
    # generate perturbed landmarks
    s = noisy_shape_from_bounding_box(gt_s, gt_s.bounding_box())
    # fit image
    fr = fitter.fit_from_shape(i, s, gt_shape=gt_s)
    fitting_results.append(fr)
    print('fr',type(fr),fr.final_shape.points,fr)
# from menpowidgets import visualize_fitting_result
# #预测
# image_path_pred = "D:/电信研究院/人脸矫正/labelme-master/examples/transfer/pred"
#
# pred_images = []
# # load landmarked images
# for i in mio.import_images(image_path_pred, max_images=None, verbose=True):
#     # convert it to grayscale if needed
#     if i.n_channels == 3:
#         i = i.as_greyscale(mode='luminosity')
#