def _main(cat_id, example_id): from dids import Dataset import matplotlib.pyplot as plt from util3d.mayavi_vis import vis_point_cloud from mayavi import mlab colors = ( (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 1), ) image_ds = SegmentedImageDataset(cat_id) pc_ds = PointCloudDataset(cat_id) s_ds = SegmentationDataset(cat_id) ds = Dataset.zip(image_ds, pc_ds, s_ds) with ds: image, pc, s = ds[example_id] print(np.min(s)) print(np.max(s)) ns = np.max(s) + 1 plt.imshow(image) for i in range(ns - 1): cloud = pc[s == i + 1] color = colors[i % len(colors)] vis_point_cloud(cloud, color=color, scale_factor=0.02) plt.show(block=False) mlab.show()
def vis(image, cloud): import matplotlib.pyplot as plt from util3d.mayavi_vis import vis_point_cloud, mlab plt.imshow(image) plt.show(block=False) vis_point_cloud( cloud, axis_order='xzy', color=(0, 0, 1), scale_factor=0.02) mlab.show() plt.close()
def vis(image, cloud): import matplotlib.pyplot as plt from mayavi import mlab from util3d.mayavi_vis import vis_point_cloud plt.imshow(image) vis_point_cloud(cloud, color=(0, 0, 1), scale_factor=0.01) plt.show(block=False) mlab.show() plt.close()
def vis(mesh, cloud): import numpy as np from util3d.mayavi_vis import vis_mesh, vis_point_cloud from mayavi import mlab vertices, faces = (np.array(mesh[k]) for k in ('vertices', 'faces')) vis_mesh(vertices, faces, color=(0, 0, 1), axis_order='xzy') vis_point_cloud(np.array(cloud), color=(0, 1, 0), scale_factor=0.01, axis_order='xzy') mlab.show()
def vis_example_data(self, feature_data, label_data): import matplotlib.pyplot as plt from util3d.mayavi_vis import vis_point_cloud from mayavi import mlab image = feature_data['image'] point_cloud = label_data image -= np.min(image) image /= np.max(image) plt.imshow(image) plt.show(block=False) vis_point_cloud(point_cloud, color=(0, 0, 1), scale_factor=0.01) mlab.show() plt.close()
def vis(cloud): from mayavi import mlab from util3d.mayavi_vis import vis_point_cloud, vis_normals import numpy as np if hasattr(cloud, 'keys'): points = np.array(cloud['points']) normals = np.array(cloud['normals']) else: points = np.array(cloud) normals = None vis_point_cloud(points, scale_factor=0.01) if normals is not None: vis_normals(points, normals, scale_factor=0.002) mlab.show()
def vis_clouds(model_id, pre_sampled=True, n_samples=1024, edge_length_threshold=0.1, shuffle=False): import random import numpy as np from mayavi import mlab import matplotlib.pyplot as plt from dids import Dataset from shapenet.core.blender_renderings.config import RenderConfig from shapenet.core.meshes import get_mesh_dataset from util3d.mayavi_vis import vis_point_cloud from util3d.mayavi_vis import vis_mesh from template_ffd.data.ids import get_example_ids from template_ffd.inference.clouds import get_inferred_cloud_dataset from template_ffd.model import get_builder builder = get_builder(model_id) cat_id = builder.cat_id kwargs = dict(model_id=model_id, n_samples=n_samples) if not pre_sampled: kwargs['edge_length_threshold'] = edge_length_threshold cloud_dataset = get_inferred_cloud_dataset(pre_sampled=pre_sampled, **kwargs) image_dataset = RenderConfig().get_dataset(cat_id, builder.view_index) example_ids = get_example_ids(cat_id, 'eval') if shuffle: example_ids = list(example_ids) random.shuffle(example_ids) mesh_dataset = get_mesh_dataset(cat_id) zipped_dataset = Dataset.zip(image_dataset, cloud_dataset, mesh_dataset) # zipped_dataset = Dataset.zip(image_dataset, cloud_dataset) with zipped_dataset: for example_id in example_ids: image, cloud, mesh = zipped_dataset[example_id] # image, cloud = zipped_dataset[example_id] plt.imshow(image) vis_point_cloud(np.array(cloud), color=(0, 1, 0), scale_factor=0.01) v, f = (np.array(mesh[k]) for k in ('vertices', 'faces')) vis_mesh(v, f, color=(0, 0, 1), opacity=0.1, include_wireframe=False) plt.show(block=False) mlab.show() plt.close()
def vis_clouds( model_id, pre_sampled=True, n_samples=1024, edge_length_threshold=0.1, shuffle=False): import random import numpy as np from mayavi import mlab import matplotlib.pyplot as plt from dids import Dataset from shapenet.core.blender_renderings.config import RenderConfig from shapenet.core.meshes import get_mesh_dataset from util3d.mayavi_vis import vis_point_cloud from util3d.mayavi_vis import vis_mesh from template_ffd.data.ids import get_example_ids from template_ffd.inference.clouds import get_inferred_cloud_dataset from template_ffd.model import get_builder builder = get_builder(model_id) cat_id = builder.cat_id kwargs = dict(model_id=model_id, n_samples=n_samples) if not pre_sampled: kwargs['edge_length_threshold'] = edge_length_threshold cloud_dataset = get_inferred_cloud_dataset( pre_sampled=pre_sampled, **kwargs) image_dataset = RenderConfig().get_dataset(cat_id, builder.view_index) example_ids = get_example_ids(cat_id, 'eval') if shuffle: example_ids = list(example_ids) random.shuffle(example_ids) mesh_dataset = get_mesh_dataset(cat_id) zipped_dataset = Dataset.zip(image_dataset, cloud_dataset, mesh_dataset) # zipped_dataset = Dataset.zip(image_dataset, cloud_dataset) with zipped_dataset: for example_id in example_ids: image, cloud, mesh = zipped_dataset[example_id] # image, cloud = zipped_dataset[example_id] plt.imshow(image) vis_point_cloud( np.array(cloud), color=(0, 1, 0), scale_factor=0.01) v, f = (np.array(mesh[k]) for k in ('vertices', 'faces')) vis_mesh( v, f, color=(0, 0, 1), opacity=0.1, include_wireframe=False) plt.show(block=False) mlab.show() plt.close()
def vis(cat_desc, regime='e', shuffle=True): import matplotlib.pyplot as plt from mayavi import mlab from util3d.mayavi_vis import vis_point_cloud, vis_voxels all_ds = get_ds(cat_desc, regime) cat_id = cat_desc_to_id(cat_desc) example_ids = list(get_example_ids(cat_id, 'eval')) random.shuffle(example_ids) def vis_mesh(mesh, include_wireframe=False, **kwargs): from util3d.mayavi_vis import vis_mesh as vm v, f = (np.array(mesh[k]) for k in ('vertices', 'faces')) vm(v, f, include_wireframe=include_wireframe, **kwargs) with all_ds: for example_id in example_ids: print(example_id) image, gt_mesh, cloud, mesh, voxels, template_mesh = \ all_ds[example_id] plt.imshow(image) mlab.figure() vis_mesh(gt_mesh, color=(0, 0, 1)) mlab.figure() vis_mesh(mesh, color=(0, 1, 0)) mlab.figure() vis_mesh(template_mesh, color=(1, 0, 0)) mlab.figure() vis_point_cloud(np.array(cloud), scale_factor=0.01, color=(0, 1, 0)) mlab.figure() vis_voxels(voxels.data, color=(0, 1, 0)) plt.show(block=False) mlab.show() plt.close()
def vis_example_data(self, feature_data, label_data): import matplotlib.pyplot as plt from shapenet.core import cat_id_to_desc from util3d.mayavi_vis import vis_point_cloud from mayavi import mlab image = feature_data['image'] point_cloud = label_data image -= np.min(image) image /= np.max(image) plt.imshow(image) cat_ids = self.cat_id cat_index = feature_data['cat_index'] if isinstance(cat_ids, str): assert(cat_index == 0) cat_id = cat_ids else: cat_id = cat_ids[cat_index] plt.title('%s: %s' % (cat_id_to_desc(cat_id), feature_data['example_id'])) plt.show(block=False) vis_point_cloud( point_cloud, color=(0, 0, 1), scale_factor=0.01, axis_order='xzy') mlab.show() plt.close()
def vis(cat_desc, regime='e', shuffle=True): import matplotlib.pyplot as plt from mayavi import mlab from util3d.mayavi_vis import vis_point_cloud, vis_voxels all_ds = get_ds(cat_desc, regime) cat_id = cat_desc_to_id(cat_desc) example_ids = list(get_example_ids(cat_id, 'eval')) random.shuffle(example_ids) def vis_mesh(mesh, include_wireframe=False, **kwargs): from util3d.mayavi_vis import vis_mesh as vm v, f = (np.array(mesh[k]) for k in ('vertices', 'faces')) vm(v, f, include_wireframe=include_wireframe, **kwargs) with all_ds: for example_id in example_ids: print(example_id) image, gt_mesh, cloud, mesh, voxels, template_mesh = \ all_ds[example_id] plt.imshow(image) mlab.figure() vis_mesh(gt_mesh, color=(0, 0, 1)) mlab.figure() vis_mesh(mesh, color=(0, 1, 0)) mlab.figure() vis_mesh(template_mesh, color=(1, 0, 0)) mlab.figure() vis_point_cloud( np.array(cloud), scale_factor=0.01, color=(0, 1, 0)) mlab.figure() vis_voxels(voxels.data, color=(0, 1, 0)) plt.show(block=False) mlab.show() plt.close()
def map_np(self, example_id): points = np.array(self._dataset[example_id], dtype=np.float32) return sample_points(points, self._n_resamples, axis=0) def get_sampled_point_cloud_dataset( cat_id, example_ids, n_samples, n_resamples): manager = SampledPointCloudManager(cat_id, n_samples, n_resamples) base = base_dataset(example_ids) return base.map(manager.map_tf) if __name__ == '__main__': from mayavi import mlab from util3d.mayavi_vis import vis_point_cloud from shapenet.core import cat_desc_to_id, get_example_ids cat_desc = 'plane' n_samples = 16384 # n_resamples = None n_resamples = 1024 cat_id = cat_desc_to_id(cat_desc) example_ids = get_example_ids(cat_id) dataset = get_sampled_point_cloud_dataset( cat_id, example_ids, n_samples, n_resamples) pc = dataset.make_one_shot_iterator().get_next() with tf.train.MonitoredSession() as sess: while not sess.should_stop(): cloud = sess.run(pc) vis_point_cloud(cloud, color=(0, 0, 1), scale_factor=0.01) mlab.show()
cat_desc = 'plane' n_points = 16384 cat_id = cat_desc_to_id(cat_desc) show_normals = True if vis_normals: dataset = get_cloud_normal_dataset(cat_id, n_points) else: dataset = get_point_cloud_dataset(cat_id, n_points) with dataset: for example_id in dataset: data = dataset[example_id] s = random.sample(range(n_points), 1024) if show_normals: cloud = np.array(data['points']) normals = np.array(data['normals']) cloud = cloud[s] normals = normals[s] else: cloud = np.array(data) normals = None cloud = cloud[s] vis_point_cloud(cloud, axis_order='xzy', color=(0, 0, 1), scale_factor=0.002) if normals is not None: vis_normals(cloud, normals, scale_factor=0.01, axis_order='xzy') mlab.show()
def test_surface_voxels(): point_cloud = voxels_to_point_cloud(get_surface_voxels(voxels)) vis_point_cloud(point_cloud[:len(point_cloud) // 2], color=(0, 0, 1)) mlab.show()
return example_id else: return None alignments = alignments.map_keys(key_map_fn, inverse_map_fn) zipped = ZippedDataset(clouds, alignments) def transform(vals): cloud, alignment = vals R = euler_matrix(0, 0, (1 - alignment) * np.pi / 2)[:3, :3] return np.matmul(np.array(cloud), R) aligned = zipped.map(transform) with aligned: for k in aligned: cloud = aligned[k] vis_point_cloud(cloud, color=(0, 0, 1), scale_factor=0.01) mlab.show() # with zipped: # print(len(tuple(zipped))) # print(len(tuple(alignments))) # print(len(tuple(clouds))) # for k in zipped: # cloud, alignment = zipped[k]