In [ ]:
%matplotlib inline
import menpo.io as mio
from menpo.feature import no_op, igo, hog, sparse_hog, double_igo
import numpy as np
from menpo.landmark import labeller, ibug_face_68

# method to load a database
def load_database(path_to_images, crop_percentage, max_images=None):
    images = []
    # load landmarked images
    for i in mio.import_images(path_to_images, max_images=max_images, verbose=True):
        # crop image
        i.crop_to_landmarks_proportion_inplace(crop_percentage)
        
        labeller(i, 'PTS', ibug_face_68)
        
        # convert it to grayscale if needed
        if i.n_channels == 3:
            i = i.as_greyscale(mode='luminosity')
            
        # append it to the list
        images.append(i)
    return images

In [ ]:
#training_images = load_database('/mnt/data/nontas/trainset/', 0.5)
#training_images = load_database('/mnt/data/nontas/train200/', 0.5)
training_images = load_database('/home/ea1812/Desktop/data/nontas/Documents/Research/Databases/LFPWtrain/trainset/', 0.5, max_images=5)
#fitting_images = load_database('/home/ea1812/Desktop/data/nontas/Documents/Research/Databases/LFPWtest/testset/', 0.5)

In [ ]:
i = training_images[0]
print i.landmarks.
print i

In [ ]:
# Star tree
adjacency_array = np.empty((67, 2), dtype=np.int32)
for i in range(68):
    if i < 34:
        adjacency_array[i, 0] = 34
        adjacency_array[i, 1] = i
    elif i > 34:
        adjacency_array[i-1, 0] = 34
        adjacency_array[i-1, 1] = i

root_vertex = 34

In [ ]:
# MST tree
adjacency_array = np.array([[ 0,  1], [ 1,  2], [ 2,  3], [ 3,  4], [ 4,  5], [ 5,  6], [ 6,  7], [ 7,  8], [ 8,  9], 
                            [ 8, 57], [ 9, 10], [57, 58], [57, 56], [57, 66], [10, 11], [58, 59], [56, 55], [66, 67], 
                            [66, 65], [11, 12], [65, 63], [12, 13], [63, 62], [63, 53], [13, 14], [62, 61], [62, 51],
                            [53, 64], [14, 15], [61, 49], [51, 50], [51, 52], [51, 33], [64, 54], [15, 16], [49, 60],
                            [33, 32], [33, 34], [33, 29], [60, 48], [32, 31], [34, 35], [29, 30], [29, 28], [28, 27],
                            [27, 22], [27, 21], [22, 23], [21, 20], [23, 24], [20, 19], [24, 25], [19, 18], [25, 26],
                            [25, 44], [18, 17], [18, 37], [44, 43], [44, 45], [37, 38], [45, 46], [38, 39], [46, 47],
                            [39, 40], [47, 42], [40, 41], [41, 36], [0, 17], [16, 26]])
root_vertex = None

In [ ]:
from antonakoscvpr2015.menpo.builder import APSBuilder

In [ ]:
aps = APSBuilder(adjacency_array=adjacency_array, 
                 root_vertex=root_vertex, 
                 features=no_op, 
                 patch_shape=(17, 17),
                 normalization_diagonal=100,
                 n_levels=2, 
                 downscale=2, 
                 scaled_shape_models=True,
                 max_shape_components=None,
                 n_appearance_parameters=100,
                 use_procrustes=True).build(training_images, group='ibug_face_68', verbose=True)

In [ ]:
print aps.shape_models[0].mean().points

In [ ]:
# save model
from cvpr15.utils import pickle_dump, pickle_load

#pickle_dump(aps, '/home/ea1812/Desktop/model_doubleIgo_withoutProcrustes.pickle')

aps = pickle_load('/home/ea1812/Desktop/model_doubleIgo_withProcrustes.pickle')

In [ ]:
from menpofit.visualize import visualize_shape_model
%matplotlib inline
visualize_shape_model(aps.shape_models)

In [ ]:
aps.random_instance(as_tree=True).view()

In [ ]:
# PLOT GAUSSIANS
from cvpr15.utils import plot_gaussian_ellipse
import matplotlib.pyplot as plt
from menpo.shape import PointTree
import numpy as np

m = 0

cov = aps.appearance_models[m][1]
mean_shape = aps.shape_models[m].mean().points
for e in range(aps.tree.n_edges):
    # find vertices
    parent = aps.tree.adjacency_array[e, 0]
    child = aps.tree.adjacency_array[e, 1]
    
    # relative location mean
    rel_loc_mean = mean_shape[child, :] - mean_shape[parent, :]
    
    # relative location cov
    n_points = aps.deformation_models[0].shape[0] / 2
    s1 = -aps.deformation_models[m][2*child, 2*parent]
    s2 = -aps.deformation_models[m][2*child+1, 2*parent+1]
    s3 = -aps.deformation_models[m][2*child, 2*parent+1]
    cov_mat = np.linalg.inv(np.array([[s1, s3], [s3, s2]]))
    
    # plot ellipse
    plot_gaussian_ellipse(cov_mat, mean_shape[parent, :] + rel_loc_mean, n_std=2, facecolor='none', edgecolor='r')

# plot mean shape points
aps.shape_models[m].mean().view_on(plt.gcf().number)

# create and plto edge connections
PointTree(mean_shape, aps.tree.adjacency_array, aps.tree.root_vertex).view_on(plt.gcf().number)

In [ ]:
from antonakoscvpr2015.menpo.fitter import LucasKanadeAPSFitter
from antonakoscvpr2015.menpo.algorithm import Forward, Inverse

fitter = LucasKanadeAPSFitter(aps, algorithm=Inverse, n_shape=[3, 6])

In [ ]:
from menpo.visualize import progress_bar_str, print_dynamic

fitting_results = []
perc1 = 0.
perc2 = 0.
n_images = len(fitting_images)
for j, i in enumerate(fitting_images):
    gt_s = i.landmarks['PTS'].lms
    initial_s = fitter.perturb_shape(gt_s, noise_std=0.04)
    fitting_result = fitter.fit(i, initial_s, gt_shape=gt_s)
    fitting_results.append(fitting_result)

    # print
    final_error = fitting_result.final_error(error_type='me_norm')
    initial_error = fitting_result.initial_error(error_type='me_norm')
    if final_error <= 0.03:
        perc1 += 1.
    if final_error <= 0.04:
        perc2 += 1.
    print_dynamic('- {0} - [<=0.03: {1:.1f}%, <=0.04: {2:.1f}%] - '
                  'Image {3}/{4} (error: {5:.3f} --> {6:.3f})'.format(
        progress_bar_str(float(j + 1.) / n_images, show_bar=False),
        perc1 * 100. / n_images, perc2 * 100. / n_images, j + 1,
        n_images, initial_error, final_error))
    
print_dynamic('- Fitting completed: [<=0.03: {0:.1f}%, <=0.04: '
              '{1:.1f}%]\n'.format(perc1 * 100. / n_images,
                                   perc2 * 100. / n_images))

In [ ]:
%matplotlib inline
from menpofit.visualize import visualize_fitting_results
visualize_fitting_results(fitting_results)

In [ ]: