<jupyter_start><jupyter_text>load models<jupyter_code># shape model
__model_path = Path('/vol/atlas/homes/aroussos/results/fit3Dto2D/model/ver2016-12-12_LSFMfrmt_maxNpcInf/all_all_all.mat')

shape_model_dict = load_tassos_lsfm_combined_model(__model_path)
shape_model = shape_model_dict['shape_model']

# template landmarks
landmarks = m3io.import_landmark_file('/homes/yz4009/wd/gitdev/face2d3d/ibug68.ljson').lms.from_vector(shape_model.mean().lms.points[mappings.fw_index_for_lms()])

# texture_model
texture_model = mappings.load_itwmm_texture_fast_dsift_fw()

# morphable model
diagonal = 185
mm = ColouredMorphableModel(shape_model, texture_model, (landmarks), 
                            holistic_features=fast_dsift, diagonal=diagonal)
# fitter
fitter = LucasKanadeMMFitter(mm, n_shape=200, n_texture=200,
                             n_samples=8000, n_scales=1, 
                             camera_cls=PerspectiveCamera)<jupyter_output>/vol/phoebe/yz4009/src/miniconda3/envs/gitdev/lib/python3.5/site-packages/menpo/shape/pointcloud.py:261: MenpoDeprecationWarning: The .lms property is deprecated. LandmarkGroups are now shapes themselves - so you can use them directly anywhere you previously used .lms.Simply remove ".lms" from your code and things will work as expected (and this warning will go away)
  MenpoDeprecationWarning)<jupyter_text>tensorflow init<jupyter_code>sys.path.append('/homes/yz4009/wd/gitdev/DeepLearning/Applications/FaceHGNet/')

import networks
slim = tf.contrib.slim

with tf.Graph().as_default() as lms_graph:
    images_input = tf.placeholder(tf.float32, shape=(None, None, None, 3), name='input_images')
    net_model = networks.DNFaceMultiView('')
    with tf.variable_scope('net'):
        with slim.arg_scope([slim.batch_norm, slim.layers.dropout], is_training=False):
            lms_heatmap_prediction,states = net_model._build_network(images_input)

def get_landmarks(pimg, bb=None):
#     
    if bb is None:
        bboxes = detect(pimg.pixels_with_channels_at_back()*255)
        if len(bboxes) > 0:
            index = np.argmin([np.linalg.norm(b.centre_of_bounds() - pimg.centre()) for b in bboxes])
            bb = bboxes[index]
        else:
            bb = PointCloud(image.bounds()).bounding_box()

    centre = bb.centre_of_bounds()
    
    bmin,bmax = bb.bounds()
    diagnal = np.linalg.norm(bmax - bmin)
    scale = diagnal/250
    # crop 384
    cimg, trans, c_scale = utils.crop_image(pimg, centre, scale, [384,384])

    rimg = cimg
    # crop to 256 * 256
    offset = (rimg.shape[0] - 256) // 2

    image = rimg.crop((offset,offset),(offset+256,offset+256))

    input_pixels =image.pixels_with_channels_at_back()

    model_path = '/vol/atlas/homes/jiankang_share/Grigorios/MultiviewHG/ckpt/menpo_3D_68/model.ckpt-223664'
    with tf.Session(graph=lms_graph) as sess:
        variables_to_restore = slim.get_variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)
        saver.restore(sess, model_path)
        lms_hm_prediction = sess.run(lms_heatmap_prediction, feed_dict={images_input:input_pixels[None,...]})

    lms_hm_prediction_filter = np.stack(list(map(
        lambda x: scipy.ndimage.filters.gaussian_filter(*x),
        zip(lms_hm_prediction[0].transpose(2,0,1), [3] * 68))))

    hs = np.argmax(np.max(lms_hm_prediction_filter, 2), 1)
    ws = np.argmax(np.max(lms_hm_prediction_filter, 1), 1)
    pts_predictions = np.stack([hs,ws]).T

    orig_pts = trans.apply(PointCloud((pts_predictions + np.array([offset,offset]))))
    
    return orig_pts<jupyter_output><empty_output><jupyter_text>Fit<jupyter_code>def fit(image, export=False):

    if len(image.pixels.shape) == 2:
        image.pixels = np.stack([image.pixels,image.pixels,image.pixels])

    if image.pixels.shape[0] == 1:
        image.pixels = np.concatenate([image.pixels,image.pixels,image.pixels], axis=0)

    
    initial_shape = get_landmarks(image)
    result = fitter.fit_from_shape(image, initial_shape, max_iters=20,
                                   camera_update=True,
                                   focal_length_update=False,
                                   reconstruction_weight=1,
                                   shape_prior_weight=.4e8,
                                   texture_prior_weight=1.,
                                   landmarks_prior_weight=1e5,
                                   return_costs=True, init_shape_params_from_lms=False)




    mesh = ColouredTriMesh(result.final_mesh.points, result.final_mesh.trilist)


    def transform(mesh):
        return result._affine_transforms[-1].apply(result.camera_transforms[-1].apply(mesh))


    mesh_in_img = transform(lambertian_shading(mesh))
    expr_dir = image.path.parent
    p = image.path.stem
    raster = rasterize_mesh(mesh_in_img, image.shape)

    uv_shape = (600, 1000)
    template = shape_model.mean()
    unwrapped_template = optimal_cylindrical_unwrap(template).apply(template)

    minimum = unwrapped_template.bounds(boundary=0)[0]
    unwrapped_template = Translation(-minimum).apply(unwrapped_template)
    unwrapped_template.points = unwrapped_template.points[:, [1, 0]]
    unwrapped_template.points[:, 0] = unwrapped_template.points[:, 0].max() - unwrapped_template.points[:, 0]
    unwrapped_template.points *= np.array([.40, .31])
    unwrapped_template.points *= np.array([uv_shape])

    bcoords_img, tri_index_img = rasterize_barycentric_coordinate_images(unwrapped_template, uv_shape)
    TI = tri_index_img.as_vector()
    BC = bcoords_img.as_vector(keep_channels=True).T

    def masked_texture(mesh_in_image, background):

        sample_points_3d = mesh_in_image.project_barycentric_coordinates(BC, TI)

        texture = bcoords_img.from_vector(background.sample(sample_points_3d.points[:, :2]))

        return texture


    uv = masked_texture(mesh_in_img, image)

    t = TexturedTriMesh(result.final_mesh.points, image_coords_to_tcoords(uv.shape).apply(unwrapped_template).points , uv, mesh_in_img.trilist)

    if export:
        m3io.export_textured_mesh(t, str(expr_dir / Path(p).with_suffix('.mesh.obj')), overwrite=True)
        mio.export_image(raster, str(expr_dir / Path(p).with_suffix('.render.jpg')), overwrite=True)
    
    return t<jupyter_output><empty_output><jupyter_text>Run<jupyter_code>image = mio.import_image('/homes/yz4009/Downloads/2793330.jpg')

image.view()
fitted_image = fit(image)
fitted_image.view()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11549775507044434, 'fraction_numerical': 0.023223409196470042, 'mean_word_length': 3.654178674351585, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '539330', 'license': 'MIT', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2222, 'n_words': 308, 'path': '3DMM Face.ipynb', 'repo_name': 'yuxiang-zhou/MLProjects', 'size': '6459'}