Ejemplo n.º 1
0
def vis():
    from util3d.mesh.obj_io import parse_obj
    from util3d.mayavi_vis import vis_mesh
    from mayavi import mlab
    import matplotlib.pyplot as plt

    for fn in fns:
        path = os.path.join(folder, fn)
        image = imread(path)
        p0 = '%s.obj' % path[:-4]
        vertices, faces = parse_obj(p0)[:2]
        p1 = '%s_template.obj' % path[:-4]
        tv, tf = parse_obj(p1)[:2]
        # cloud = np.load('%s_cloud.npy' % path[:-4])
        assert(np.all(tf == faces))
        print(np.max(np.abs(vertices - tv)))
        # mlab.figure()
        # vis_point_cloud(cloud, color=(0, 1, 0), scale_factor=0.02)
        mlab.figure()
        vis_mesh(vertices, faces, include_wireframe=False, color=(0, 1, 0))
        mlab.figure()
        vis_mesh(tv, tf, include_wireframe=False)
        plt.figure()
        plt.imshow(image)
        # plt.show(block=False)
        # mlab.show()
        # plt.close()
    plt.show(block=False)
    mlab.show()
Ejemplo n.º 2
0
    def vis_prediction_data(
            self, prediction_data, feature_data, label_data=None):
        import matplotlib.pyplot as plt
        from util3d.mayavi_vis import vis_mesh
        from mayavi import mlab
        image = feature_data['image']
        dp = prediction_data['dp']
        probs = prediction_data['probs']

        if not hasattr(self, '_mesh_fn') or self._mesh_fn is None:
            self._mesh_fn = self.get_prediction_to_mesh_fn()
        image -= np.min(image)
        image /= np.max(image)
        plt.imshow(image)

        mesh = self._mesh_fn(probs, dp)
        vertices, faces, original_vertices = (
            mesh[k] for k in('vertices', 'faces', 'original_vertices'))
        mlab.figure()
        vis_mesh(vertices, faces, color=(0, 1, 0), include_wireframe=False)
        mlab.figure()
        vis_mesh(
            original_vertices, faces, color=(1, 0, 0), include_wireframe=False)

        plt.show(block=False)
        mlab.show()
        plt.close()
Ejemplo n.º 3
0
 def vis(vertices, faces, split_faces, centroids, normals,
         original_normals):
     from mayavi import mlab
     from util3d.mayavi_vis import vis_mesh, vis_normals
     for n in (normals, normals_original):
         mlab.figure()
         for i, f in enumerate(split_faces):
             vis_mesh(vertices, f, color=colors[i % len(colors)])
         vis_normals(centroids, n, scale_factor=10)
     mlab.show()
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
def vis(mesh):
    from mayavi import mlab
    from util3d.mayavi_vis import vis_mesh, vis_normals
    from util3d.mesh.sample import sample_faces_with_normals
    v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
    n_points = 1024
    p, n = sample_faces_with_normals(v, f, n_points)
    print(np.min(v, axis=0))
    print(np.max(v, axis=0))
    vis_mesh(v, f)
    vis_normals(p, n)
    mlab.show()
Ejemplo n.º 6
0
def vis_mesh(vertices, faces, original_vertices, **kwargs):
    from util3d.mayavi_vis import vis_mesh
    from mayavi import mlab
    mlab.figure()
    vis_mesh(
        vertices=vertices, faces=faces, color=(0, 1, 0),
        include_wireframe=False)
    mlab.figure()
    vis_mesh(
        vertices=original_vertices, faces=faces, color=(1, 0, 0),
        include_wireframe=False)
    mlab.show()
Ejemplo n.º 7
0
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()
Ejemplo n.º 8
0
def get_inference(model_id, example_id, ext='png', edge_length_threshold=0.02):
    import tensorflow as tf
    from template_ffd.model import get_builder
    import PIL
    import numpy as np
    from shapenet.image import with_background
    builder = get_builder(model_id)
    cat_id = builder.cat_id

    example_ids = [example_id]
    paths = [get_path(cat_id, e, ext) for e in example_ids]
    for path in paths:
        if not os.path.isfile(path):
            raise Exception('No file at path %s' % path)

    def gen():
        for example_id, path in zip(example_ids, paths):
            image = np.array(PIL.Image.open(path))
            image = with_background(image, 255)
            yield example_id, image

    render_params = builder.params.get('render_params', {})
    shape = tuple(render_params.get('shape', (192, 256)))
    shape = shape + (3,)

    def input_fn():
        ds = tf.data.Dataset.from_generator(
            gen, (tf.string, tf.uint8), ((), shape))
        example_id, image = ds.make_one_shot_iterator().get_next()
        # image_content = tf.read_file(path)
        # if ext == 'png':
        #     image = tf.image.decode_png(image_content)
        # elif ext == 'jpg':
        #     image = tf.image.decode_jpg(image_content)
        # else:
        #     raise ValueError('ext must be in ("png", "jpg")')
        image.set_shape((192, 256, 3))
        image = tf.image.per_image_standardization(image)
        example_id = tf.expand_dims(example_id, axis=0)
        image = tf.expand_dims(image, axis=0)
        return dict(example_id=example_id, image=image)

    estimator = builder.get_estimator()
    mesh_fn = builder.get_prediction_to_mesh_fn(edge_length_threshold)
    for pred in estimator.predict(input_fn):
        example_id = pred.pop('example_id')
        mesh = mesh_fn(**pred)
        vis_mesh(**mesh)
Ejemplo n.º 9
0
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()
Ejemplo n.º 10
0
    with inf_mesh_dataset:
        with get_mesh_dataset(cat_id) as gt_mesh_dataset:
            example_ids = list(inf_mesh_dataset.keys())
            if shuffle:
                random.shuffle(example_ids)

            for example_id in example_ids:
                inf = inf_mesh_dataset[example_id]
                gt = gt_mesh_dataset[example_id]
                mlab.figure()
                vis(inf, color=(0, 1, 0), opacity=0.2)
                mlab.figure()
                vis(gt, opacity=0.2)
                mlab.show()


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('model_id', help='id of model defined in params')
    parser.add_argument('-t',
                        '--edge_length_threshold',
                        default=None,
                        type=float)
    parser.add_argument('-w', '--wireframe', action='store_true')
    parser.add_argument('-s', '--shuffle', action='store_true')
    args = parser.parse_args()
    vis_mesh(args.model_id, args.edge_length_threshold, args.wireframe,
             args.shuffle)
Ejemplo n.º 11
0
def vis_segmented_mesh(vertices, faces, **kwargs):
    for i, f in enumerate(faces):
        vis_mesh(vertices, f, color=colors[i % nc], **kwargs)
Ejemplo n.º 12
0
from shapenet.core import cat_desc_to_id
from shapenet.core.objs import get_obj_file_dataset
from mayavi import mlab
from util3d.mayavi_vis import vis_mesh
from util3d.mesh.obj_io import parse_obj_file

cat_desc = 'plane'
cat_id = cat_desc_to_id(cat_desc)


def map_fn(f):
    return parse_obj_file(f)[:2]


dataset = get_obj_file_dataset(cat_id).map(map_fn)

with dataset:
    for k, (vertices, faces) in dataset.items():
        print(k)
        vis_mesh(vertices, faces, axis_order='xzy')
        mlab.show()
Ejemplo n.º 13
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from mayavi import mlab
from util3d.mayavi_vis import vis_mesh
from util3d.mesh.bounding_mesh import BoundingMeshConfig
from modelnet.parsed import get_saved_dataset

print('Warning: extremely experimental')

dataset_id = 'ModelNet40'
mode = 'train'
category = 'toilet'

config = BoundingMeshConfig('bounding-convex-decomposition')

with get_saved_dataset(dataset_id, mode, category) as ds:
    for example_id in ds:
        mesh = ds[example_id]
        v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
        v2, f2 = config.convert_mesh(v, f)
        mlab.figure()
        vis_mesh(v, f)
        mlab.figure()
        vis_mesh(v2, f2)
        print(v.shape, f.shape)
        print(v2.shape, f2.shape)
        mlab.show()
Ejemplo n.º 14
0
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from shapenet.core.meshes import get_mesh_dataset
from shapenet.core import cat_desc_to_id
from util3d.mayavi_vis import vis_mesh
from mayavi import mlab

desc = 'plane'

cat_id = cat_desc_to_id(desc)
with get_mesh_dataset(cat_id) as mesh_dataset:
    for example_id in mesh_dataset:
        example_group = mesh_dataset[example_id]
        vertices, faces = (np.array(example_group[k])
                           for k in ('vertices', 'faces'))
        vis_mesh(vertices, faces, color=(0, 0, 1), axis_order='xzy')
        mlab.show()
Ejemplo n.º 15
0
 def vis(mesh, **kwargs):
     v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
     vis_mesh(v, f, include_wireframe=wireframe, **kwargs)
Ejemplo n.º 16
0
    with inf_mesh_dataset:
        with get_mesh_dataset(cat_id) as gt_mesh_dataset:
            example_ids = list(inf_mesh_dataset.keys())
            if shuffle:
                random.shuffle(example_ids)

            for example_id in example_ids:
                inf = inf_mesh_dataset[example_id]
                gt = gt_mesh_dataset[example_id]
                mlab.figure()
                vis(inf, color=(0, 1, 0), opacity=0.2)
                mlab.figure()
                vis(gt, opacity=0.2)
                mlab.show()


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        'model_id', help='id of model defined in params')
    parser.add_argument('-t', '--edge_length_threshold', default=None,
                        type=float)
    parser.add_argument('-w', '--wireframe', action='store_true')
    parser.add_argument('-s', '--shuffle', action='store_true')
    args = parser.parse_args()
    vis_mesh(
        args.model_id, args.edge_length_threshold, args.wireframe,
        args.shuffle)
Ejemplo n.º 17
0
def vis_segmented_mesh(vertices, faces, **kwargs):
    for i, f in enumerate(faces):
        vis_mesh(vertices, f, color=colors[i % nc], **kwargs)
Ejemplo n.º 18
0
 def vis(mesh, **kwargs):
     v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
     vis_mesh(v, f, include_wireframe=wireframe, **kwargs)
Ejemplo n.º 19
0
import os
from mayavi import mlab
from util3d.mayavi_vis import vis_mesh
from util3d.mesh.off import OffObject
from util3d.mesh.geom import triangulated_faces
from util3d.mesh.sch import get_convex_hull

folder = os.path.realpath(os.path.dirname(__file__))
bunny_path = os.path.join(folder, 'data', 'bunny.off')
bunny = OffObject.from_path(bunny_path)

vertices = bunny.vertices
faces = tuple(triangulated_faces(bunny.faces))

mlab.figure()
vis_mesh(vertices, faces)

cf = get_convex_hull(vertices)
# mlab.figure()
vis_mesh(vertices, cf, color=(1, 0, 0), opacity=0.1)
mlab.show()