예제 #1
0
def generate_call_graph(args):
    """Performance debug function, will be (re)moved later. """
    assert args.model_shape_file, '--model_texture_file needs to be provided to save the pca model'
    assert args.model_texture_file, '--model_texture_file needs to be provided to save the pca model'
    assert args.shape_type, '--shape_type the type of dataset, see datasets module'

    dataset_module = import_dataset_module(args.shape_type)

    from pycallgraph import PyCallGraph
    from pycallgraph.output import GraphvizOutput

    graphviz = GraphvizOutput(output_file='filter_none.png')

    with PyCallGraph(output=graphviz):
        shape_model = pca.PCAModel(args.model_shape_file)
        texture_model = pca.PCAModel(args.model_texture_file)

        input_points = dataset_module.IMMPoints(filename='/data/imm_face_db/40-3m.asf')
        input_image = input_points.get_image()

        mean_points = dataset_module.IMMPoints(points_list=shape_model.mean_values)
        mean_points.get_scaled_points(input_image.shape)

        reconstruction.reconstruct_texture(
            input_image,  # src image
            input_image,  # dst image
            texture_model,
            input_points,  # shape points input
            mean_points,   # shape points mean
        )
예제 #2
0
    def __init__(self, *args, **kwargs):
        # todo get from settings
        model_texture_file = '{}/pca_{}_texture_model.npy'.format(
            FILES_DIR, DATASET)
        model_shape_file = '{}/pca_{}_shape_model.npy'.format(
            FILES_DIR, DATASET)

        self.shape_model = pca.PCAModel(model_shape_file)
        self.texture_model = pca.PCAModel(model_texture_file)

        websocket.WebSocketHandler.__init__(self, *args, **kwargs)
예제 #3
0
def test_build_texture_feature_vectors():
    shape_model = pca.PCAModel('data/test_data/pca_shape_model.npy')
    texture_model = pca.PCAModel('data/test_data/pca_texture_model.npy')

    input_points = imm.IMMPoints(filename='data/imm_face_db/40-3m.asf')
    input_image = input_points.get_image()

    mean_points = imm.IMMPoints(points_list=shape_model.mean_values)
    mean_points = mean_points.get_scaled_points(input_image.shape)
    input_points = input_points.get_scaled_points(input_image.shape)

    assert np.mean(input_points) > 1.0, 'should be greater than 1.0, because \
        it array should be scaled to the image width and height'

    assert np.mean(mean_points) > 1.0, 'should be greater than 1.0, because \
예제 #4
0
def shape():
    shape_components = 58

    shape_model = pca.PCAModel(model_shape_file)
    texture_model = pca.PCAModel(model_texture_file)

    logger.info('using %s shape_components', shape_components)
    image_filename = '/data/imm_face_db/01-1m.jpg'

    dataset_module = import_dataset_module('ibug')

    dst_image = reconstruction.reconstruct_shape_texture(
        dataset_module, shape_model, texture_model, image_filename,
        shape_components)

    cv2.imwrite('/data/reconstructed.png', dst_image)
예제 #5
0
def fit_model():
    from reconstruction import fit

    shape_components = 58
    shape_model = pca.PCAModel(model_shape_file)
    texture_model = pca.PCAModel(model_texture_file)

    logger.info('using %s shape_components', shape_components)
    image_filename = '/data/imm_face_db/01-1m.jpg'

    dataset_module = import_dataset_module('ibug')

    input_points = dataset_module.factory(filename=image_filename)
    input_image = input_points.get_image()
    input_points.get_points()

    print dir(eos)

    # scale points to output image shape. We MUST do this.
    points = input_points.get_scaled_points(input_image.shape)
    fit.fit(input_image, points)
예제 #6
0
def save_pca_model_texture(args):
    """
    Save the U, s, Vt and mean of all the asf datafiles given by the asf
    files.

    It is saved in the following way:

        np.load(filename, np.assary([Vt, mean_values])

        And accessed by:

        Vtm = np.load(args.model_file_texture)

        Vt = Vtm[0]
        mean_values = Vtm[1][0]

    """
    assert args.files, '--files should be given'
    assert args.model_shape_file, '--model_texture_file needs to be provided to save the pca model'
    assert args.shape_type, '--shape_type the type of dataset, see datasets module'

    shape_model = pca.PCAModel(args.model_shape_file)

    dataset_module = import_dataset_module(args.shape_type)
    mean_points = dataset_module.factory(points_list=shape_model.mean_values)

    textures = aam.build_texture_feature_vectors(
        args.files,
        dataset_module.get_image_with_landmarks,  # function
        mean_points,
        shape_model.triangles
    )

    mean_texture = aam.get_mean(textures)
    _, s, Vt, n_components = pca.pca(textures, mean_texture)

    pca.save(Vt, s, n_components, mean_texture, shape_model.triangles, args.model_texture_file)

    logger.info('texture pca model saved in %s', args.model_texture_file)
예제 #7
0
                col.imshow(np.reshape(data[(r * 4) + c, :, :], (8, 8)),
                           cmap="gray")
                c += 1
            r += 1
        plt.show()

    # vectorize the images
    return np.reshape(data, (num_samples, size * size))


if __name__ == "__main__":
    np.random.seed(0)
    p = 0.08
    params = {
        "num_neurons": 16,
        "input_dims": 64,
        "alpha": 0.1,
        "beta": 0.05,
        "gamma": 0.1,
        "lamb": 10,
        "epsilon": 0.01,
        "p": p,
        "num_updates": 1000,
        "num_transient": 100
    }
    model = m.Model(params)
    model.train(generate_bars)

    pca_model = pca.PCAModel(np.random.randn(64, 16), 0.0001, 1000, p)
    pca_model.train(generate_bars)
예제 #8
0
def show_reconstruction(args):
    assert args.model_shape_file, '--model_texture_file needs to be provided to save the pca model'
    assert args.model_texture_file, '--model_texture_file needs to be provided to save the pca model'
    assert args.shape_type, '--shape_type the type of dataset, see datasets module'
    assert args.files, '--files should be given'


    shape_model = pca.PCAModel(args.model_shape_file)
    texture_model = pca.PCAModel(args.model_texture_file)

    dataset_module = import_dataset_module(args.shape_type)
    mean_points = dataset_module.factory(points_list=shape_model.mean_values)

    shape_eigenvalues_multiplier = np.ones(5, dtype=np.float32)

    for face in args.files:
        input_points = dataset_module.factory(filename=face)
        input_image = input_points.get_image()

        mean_points.get_scaled_points(input_image.shape)

        input_image_copy = input_image.copy()
        input_points_copy = copy.deepcopy(input_points)

        output_points = dataset_module.factory(
            points_list=input_points.get_points()
        )

        # scale by scaling the Vt matrix
        shape_Vt = shape_model.Vt

        shape_Vt = reconstruction.scale_eigenvalues(
            shape_Vt, shape_eigenvalues_multiplier
        )

        # recontruct the shape
        reconstruction.reconstruct_shape(
            output_points,
            shape_model,
            shape_Vt=shape_Vt  # overwrite by scaled Vt
        )

        # use the new shape ane mean points to reconstruct
        reconstruction.reconstruct_texture(
            input_image_copy,  # src image
            input_image_copy,  # dst image
            texture_model,
            input_points_copy,  # shape points input
            mean_points,    # shape points mean
            output_points
        )

        output_points.get_scaled_points(input_image.shape)
        output_points.draw_triangles(image=input_image_copy, show_points=False)

        dst = reconstruction.get_texture(
            mean_points, texture_model.mean_values
        )

        cv2.imshow('dst', input_image_copy)
        k = cv2.waitKey(0) & 0xff

        if k == 27:
            break

    cv2.destroyAllWindows()