def save(subdir, b, p, dp, faces, gt_mesh, image, duration=5, fps=50): from scipy.misc import imsave from util3d.mesh.obj_io import write_obj imsave(os.path.join(subdir, 'image.png'), image) v, f = (np.array(gt_mesh[k]) for k in ('vertices', 'faces')) write_obj(os.path.join(subdir, 'gt_mesh.obj'), v, f) save_anim(subdir, b, p, dp, faces, duration, fps)
def save(): import tensorflow as tf from util3d.mesh.obj_io import write_obj from shapenet.image import with_background from template_ffd.model import get_builder builder = get_builder(model_id) mesh_fn = builder.get_prediction_to_mesh_fn(0.02) cloud_fn = builder.get_prediction_to_cloud_fn() graph = tf.Graph() with graph.as_default(): image = tf.placeholder(shape=(192, 256, 3), dtype=tf.uint8) std_image = tf.image.per_image_standardization(image) std_image = tf.expand_dims(std_image, axis=0) example_id = tf.constant(['blah'], dtype=tf.string) spec = builder.get_estimator_spec( dict(example_id=example_id, image=std_image), None, tf.estimator.ModeKeys.PREDICT) predictions = spec.predictions probs_tf = predictions['probs'] dp_tf = predictions['dp'] saver = tf.train.Saver() with tf.Session(graph=graph) as sess: saver.restore(sess, tf.train.latest_checkpoint(builder.model_dir)) for fn in fns: path = os.path.join(folder, fn) image_data = np.array(imread(path)) if image_data.shape[-1] == 4: image_data = with_background(image_data, (255, 255, 255)) probs, dp = sess.run( [probs_tf, dp_tf], feed_dict={image: image_data}) probs = probs[0] dp = dp[0] mesh = mesh_fn(probs, dp) cloud = cloud_fn(probs, dp)['cloud'] v, ov, f = ( mesh[k] for k in('vertices', 'original_vertices', 'faces')) path = '%s.obj' % path[:-4] write_obj(path, v, f) p2 = '%s_template.obj' % path[:-4] np.save('%s_cloud.npy' % path[:-4], cloud) write_obj(p2, ov, f)
def export(cat_desc, example_ids, regime='e'): from scipy.misc import imsave from util3d.mesh.obj_io import write_obj import os all_ds = get_ds(cat_desc, regime) base = os.path.realpath(os.path.dirname(__file__)) with all_ds: for example_id in example_ids: folder = os.path.join( base, 'big_table_results', cat_desc, example_id) if not os.path.isdir(folder): os.makedirs(folder) image, gt_mesh, cloud, mesh, voxels, template_mesh = \ all_ds[example_id] imsave(os.path.join(folder, 'image.png'), image) v, f = (np.array(mesh[k]) for k in ('vertices', 'faces')) write_obj(os.path.join(folder, 'deformed.obj'), v, f) v, f = (np.array(template_mesh[k]) for k in ('vertices', 'faces')) write_obj(os.path.join(folder, 'template.obj'), v, f) v, f = (np.array(gt_mesh[k]) for k in ('vertices', 'faces')) write_obj(os.path.join(folder, 'model.obj'), v, f) np.save(os.path.join( folder, 'inferred_cloud.npy'), np.array(cloud)) path = os.path.join(folder, 'deformed.binvox') voxels.save(path)
def export(example_id): import os from util3d.mesh.obj_io import write_obj from scipy.misc import imsave save_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'top_k_results', example_id) if not os.path.isdir(save_dir): os.makedirs(save_dir) with all_ds: print(example_id) image, gt_mesh, predictions = all_ds[example_id] meshes = top_k_mesh_fn( *(np.array(predictions[k]) for k in ('probs', 'dp'))) for i, mesh in enumerate(meshes): ov, v, f = ( mesh[k] for k in ('original_vertices', 'vertices', 'faces')) write_obj(os.path.join(save_dir, 'template%d.obj' % i), ov, f) write_obj(os.path.join(save_dir, 'deformed%d.obj' % i), v, f) v, f = (np.array(gt_mesh[k]) for k in ('vertices', 'faces')) write_obj(os.path.join(save_dir, 'ground_truth.obj'), v, f) imsave(os.path.join(save_dir, 'image.png'), image)
def export(example_id): import os from util3d.mesh.obj_io import write_obj from scipy.misc import imsave save_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'top_k_results', example_id) if not os.path.isdir(save_dir): os.makedirs(save_dir) with all_ds: print(example_id) image, gt_mesh, predictions = all_ds[example_id] meshes = top_k_mesh_fn(*(np.array(predictions[k]) for k in ('probs', 'dp'))) for i, mesh in enumerate(meshes): ov, v, f = (mesh[k] for k in ('original_vertices', 'vertices', 'faces')) write_obj(os.path.join(save_dir, 'template%d.obj' % i), ov, f) write_obj(os.path.join(save_dir, 'deformed%d.obj' % i), v, f) v, f = (np.array(gt_mesh[k]) for k in ('vertices', 'faces')) write_obj(os.path.join(save_dir, 'ground_truth.obj'), v, f) imsave(os.path.join(save_dir, 'image.png'), image)
def get_mesh(): import numpy as np from modelnet.parsed import get_saved_dataset dataset = get_saved_dataset('ModelNet40', 'train', 'toilet') with dataset as ds: key = tuple(ds.keys())[2] mesh = ds[key] vertices, faces = (np.array(mesh[k]) for k in ('vertices', 'faces')) return vertices, faces vertices, faces = get_mesh() v2, f2 = clean(vertices, faces) write_obj('/tmp/original.obj', vertices, faces) write_obj('/tmp/cleaned.obj', v2, f2) print(vertices.shape) print(faces.shape) print(v2.shape) print(f2.shape) # def vis(v0, f0, v1, f1): # from mayavi import mlab # from util3d.mayavi_vis import vis_mesh # mlab.figure() # vis_mesh(v0, f0, color=(0, 0, 1)) # mlab.figure() # vis_mesh(v1, f1, color=(0, 1, 0)) # mlab.show() #