예제 #1
0
파일: demo.py 프로젝트: PeterZhouSZ/RingNet
def main(config, template_mesh):
    sess = tf.Session()
    model = RingNet_inference(config, sess=sess)
    input_img, proc_param, img = preprocess_image(config.img_path)
    vertices, flame_parameters = model.predict(np.expand_dims(input_img, axis=0), get_parameters=True)
    cams = flame_parameters[0][:3]
    visualize(img, proc_param, vertices[0], cams, img_name=config.out_folder + '/images/' + config.img_path.split('/')[-1][:-4])

    if config.save_obj_file:
        if not os.path.exists(config.out_folder + '/mesh'):
            os.mkdir(config.out_folder + '/mesh')
        mesh = Mesh(v=vertices[0], f=template_mesh.f)
        mesh.write_obj(config.out_folder + '/mesh/' + config.img_path.split('/')[-1][:-4] + '.obj')

    if config.save_flame_parameters:
        if not os.path.exists(config.out_folder + '/params'):
            os.mkdir(config.out_folder + '/params')
        flame_parameters_ = {'cam':  flame_parameters[0][:3], 'pose': flame_parameters[0][3:3+config.pose_params], 'shape': flame_parameters[0][3+config.pose_params:3+config.pose_params+config.shape_params],
         'expression': flame_parameters[0][3+config.pose_params+config.shape_params:]}
        np.save(config.out_folder + '/params/' + config.img_path.split('/')[-1][:-4] + '.npy', flame_parameters_)

    if config.neutralize_expression:
        from util.using_flame_parameters import make_prdicted_mesh_neutral
        if not os.path.exists(config.out_folder + '/neutral_mesh'):
            os.mkdir(config.out_folder + '/neutral_mesh')
        neutral_mesh = make_prdicted_mesh_neutral(config.out_folder + '/params/' + config.img_path.split('/')[-1][:-4] + '.npy', config.flame_model_path)
        neutral_mesh.write_obj(config.out_folder + '/neutral_mesh/' + config.img_path.split('/')[-1][:-4] + '.obj')
예제 #2
0
파일: demo.py 프로젝트: uuaix/RingNet
def main(config, template_mesh):
    sess = tf.Session()
    model = RingNet_inference(config, sess=sess)
    input_img, proc_param, img = preprocess_image(config.img_path)
    vertices, flame_parameters = model.predict(np.expand_dims(input_img, axis=0), get_parameters=True)
    cams = flame_parameters[0][:3]
    visualize(img, proc_param, vertices[0], cams, img_name=config.out_folder + '/images/' + config.img_path.split('/')[-1][:-4])

    if config.save_obj_file:
        if not os.path.exists(config.out_folder + '/mesh'):
            os.mkdir(config.out_folder + '/mesh')
        mesh = Mesh(v=vertices[0], f=template_mesh.f)
        mesh.write_obj(config.out_folder + '/mesh/' + config.img_path.split('/')[-1][:-4] + '.obj')

    if config.save_flame_parameters:
        if not os.path.exists(config.out_folder + '/params'):
            os.mkdir(config.out_folder + '/params')
        flame_parameters_ = {'cam':  flame_parameters[0][:3], 'pose': flame_parameters[0][3:3+config.pose_params], 'shape': flame_parameters[0][3+config.pose_params:3+config.pose_params+config.shape_params],
         'expression': flame_parameters[0][3+config.pose_params+config.shape_params:]}
        np.save(config.out_folder + '/params/' + config.img_path.split('/')[-1][:-4] + '.npy', flame_parameters_)

    if config.neutralize_expression:
        from util.using_flame_parameters import make_prdicted_mesh_neutral
        if not os.path.exists(config.out_folder + '/neutral_mesh'):
            os.mkdir(config.out_folder + '/neutral_mesh')
        neutral_mesh = make_prdicted_mesh_neutral(config.out_folder + '/params/' + config.img_path.split('/')[-1][:-4] + '.npy', config.flame_model_path)
        neutral_mesh.write_obj(config.out_folder + '/neutral_mesh/' + config.img_path.split('/')[-1][:-4] + '.obj')

    if config.save_texture:
        if not os.path.exists(config.flame_texture_data_path):
            print('FLAME texture data not found')
            return
        texture_data = np.load(config.flame_texture_data_path, allow_pickle=True)[()]
        texture = create_texture(img, proc_param, vertices[0], template_mesh.f, cams, texture_data)

        if not os.path.exists(config.out_folder + '/texture'):
            os.mkdir(config.out_folder + '/texture')

        cv2.imwrite(config.out_folder + '/texture/' + config.img_path.split('/')[-1][:-4] + '.png', texture[:,:,::-1])
        mesh = Mesh(v=vertices[0], f=template_mesh.f)
        mesh.vt = texture_data['vt']
        mesh.ft = texture_data['ft']
        mesh.set_texture_image(config.out_folder + '/texture/' + config.img_path.split('/')[-1][:-4] + '.png')
        mesh.write_obj(config.out_folder + '/texture/' + config.img_path.split('/')[-1][:-4] + '.obj')
예제 #3
0
def main(config):
    print('Tensorflow version {}'.format(tf.__version__))

    print("Input Dir: <{}>".format(config.in_folder))
    print("Output Dir: <{}>".format(config.out_folder))

    img_paths = glob.glob(os.path.join(config.in_folder, '*'))

    if (config.save_viz or config.save_obj_file):
        template_mesh = Mesh(filename='./flame_model/FLAME_sample.ply')

    sess = tf.Session()
    model = RingNet_inference(config, sess=sess)

    pre_process_times = []
    inference_times = []
    start = time.time()
    for img_path in img_paths:
        pre_start = time.time()
        input_img, proc_param, img = preprocess_image(img_path,
                                                      config.img_size)
        pre_end = time.time()
        duration = pre_end - pre_start
        pre_process_times.append(duration)

        inf_start = time.time()
        vertices, flame_parameters = model.predict(np.expand_dims(input_img,
                                                                  axis=0),
                                                   get_parameters=True)
        inf_end = time.time()
        duration = inf_end - inf_start
        inference_times.append(duration)

        if config.save_viz:
            if not os.path.exists(config.out_folder + '/images'):
                os.mkdir(config.out_folder + '/images')

            cams = flame_parameters[0][:3]
            renderer = vis_util.SMPLRenderer(faces=template_mesh.f)
            # visualize(img, proc_param, vertices[0], cams, renderer, img_name=config.out_folder + '/images/' + img_path.split('/')[-1][:-4])
            visualize_single_row(img,
                                 proc_param,
                                 vertices[0],
                                 cams,
                                 renderer,
                                 img_name=config.out_folder + '/images/' +
                                 img_path.split('/')[-1][:-4])

        if config.save_obj_file:
            if not os.path.exists(config.out_folder + '/mesh'):
                os.mkdir(config.out_folder + '/mesh')
            mesh = Mesh(v=vertices[0], f=template_mesh.f)
            mesh.write_obj(config.out_folder + '/mesh/' +
                           img_path.split('/')[-1][:-4] + '.obj')

        if config.save_flame_parameters:
            if not os.path.exists(config.out_folder + '/params'):
                os.mkdir(config.out_folder + '/params')
            flame_parameters_ = {
                'cam':
                flame_parameters[0][:3],
                'pose':
                flame_parameters[0][3:3 + config.pose_params],
                'shape':
                flame_parameters[0][3 + config.pose_params:3 +
                                    config.pose_params + config.shape_params],
                'expression':
                flame_parameters[0][3 + config.pose_params +
                                    config.shape_params:]
            }
            np.save(
                config.out_folder + '/params/' + img_path.split('/')[-1][:-4] +
                '.npy', flame_parameters_)

        if config.neutralize_expression:
            from util.using_flame_parameters import make_prdicted_mesh_neutral
            if not os.path.exists(config.out_folder + '/neutral_mesh'):
                os.mkdir(config.out_folder + '/neutral_mesh')
            neutral_mesh = make_prdicted_mesh_neutral(
                config.out_folder + '/params/' + img_path.split('/')[-1][:-4] +
                '.npy', config.flame_model_path)
            neutral_mesh.write_obj(config.out_folder + '/neutral_mesh/' +
                                   img_path.split('/')[-1][:-4] + '.obj')
    end = time.time()
    overall_duration = end - start

    mean_pre_process_time = np.mean(pre_process_times)
    mean_inference_time = np.mean(inference_times)

    print('mean_pre_process_time = {}'.format(mean_pre_process_time))
    print('mean_inference_time = {}'.format(mean_inference_time))

    n_images = len(img_paths)
    throughput = n_images / (np.sum(pre_process_times) +
                             np.sum(inference_times))
    print('total images = {} throughput = {}/s'.format(n_images, throughput))

    throughput = n_images / overall_duration
    print('total images = {} duration {} throughput = {}/s'.format(
        n_images, overall_duration, throughput))