Exemple #1
0
def meshlab_poisson(pointcloud):
    r''' Runs the meshlab ball pivoting algorithm.

    Args:
        pointcloud (numpy tensor): input point cloud
    '''
    with tempfile.TemporaryDirectory() as tmpdir:
        script_path = os.path.join(tmpdir, 'script.mlx')
        input_path = os.path.join(tmpdir, 'input.ply')
        output_path = os.path.join(tmpdir, 'out.off')

        # Write script
        with open(script_path, 'w') as f:
            f.write(FILTER_SCRIPT_RECONSTRUCTION)

        # Write pointcloud
        export_pointcloud(pointcloud, input_path, as_text=False)

        # Export
        env = os.environ
        subprocess.Popen(
            'meshlabserver -i ' + input_path + ' -o ' + output_path + ' -s ' +
            script_path,
            env=env,
            cwd=os.getcwd(),
            shell=True,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        ).wait()
        mesh = trimesh.load(output_path, process=False)

    return mesh
Exemple #2
0
    def __call__(self, inputs):
        self.model.eval()
        with torch.no_grad():
            points = self.model(inputs)

        export_pointcloud(
            points.squeeze(0).cpu().numpy(),
            '/is/sg/lmescheder/Desktop/points.ply')

        batch_size = points.size(0)
        T = points.size(1)

        # Subsample points if necessary
        if T != self.pointcloud_n:
            idx = torch.randint(low=0,
                                high=T,
                                size=(batch_size, self.pointcloud_n),
                                device=self.device)
            idx = idx[:, :, None].expand(batch_size, self.pointcloud_n, 3)

            points = torch.gather(points, dim=1, index=idx)

        return points
Exemple #3
0
    def export(self, points, pointcloud_dir, modelname, start_idx=0):
        ''' Saves a sequence of point clouds.

        Args:
            points (numpy array): point cloud points
            pointcloud_dir (str): pointcloud directory
            modelname (str): model name
        '''
        model_folder = os.path.join(pointcloud_dir, modelname,
                                    '%05d' % start_idx)

        if not os.path.isdir(model_folder):
            os.makedirs(model_folder)

        out_files = []
        for i, pc in enumerate(points):

            pc_out_file = os.path.join(
                model_folder,
                '%s_%04d_%04d.ply' % (modelname, start_idx, i + 1))
            export_pointcloud(pc, pc_out_file)
            out_files.append(pc_out_file)

        return out_files
Exemple #4
0
        except TypeError:
            mesh, stats_dict = out, {}
        time_dict.update(stats_dict)

        # Write output
        mesh_out_file = os.path.join(mesh_dir, "%s.off" % modelname)
        mesh.export(mesh_out_file)
        out_file_dict["mesh"] = mesh_out_file

    if generate_pointcloud:
        t0 = time.time()
        pointcloud = generator.generate_pointcloud(data)
        time_dict["pcl"] = time.time() - t0
        pointcloud_out_file = os.path.join(pointcloud_dir,
                                           "%s.ply" % modelname)
        export_pointcloud(pointcloud, pointcloud_out_file)
        out_file_dict["pointcloud"] = pointcloud_out_file

    if cfg["generation"]["copy_input"]:
        # Save inputs
        if input_type == "img":
            inputs_path = os.path.join(in_dir, "%s.jpg" % modelname)
            inputs = tf.squeeze(data["inputs"], axis=0)
            visualize_data(inputs, "img", inputs_path)
            out_file_dict["in"] = inputs_path
        elif input_type == "voxels":
            inputs_path = os.path.join(in_dir, "%s.off" % modelname)
            inputs = tf.squeeze(data["inputs"], axis=0)
            voxel_mesh = VoxelGrid(inputs).to_mesh()
            voxel_mesh.export(inputs_path)
            out_file_dict["in"] = inputs_path
Exemple #5
0
    if c_it < vis_n_outputs:
        # Save inputs
        if cfg['generation']['copy_input']:
            if input_type == 'pcl_seq':
                inputs = data['inputs'][0]
                L = inputs.shape[0]
                inputs_base_path = os.path.join(in_dir, modelname,
                                                '%04d' % start_idx)
                if not os.path.exists(inputs_base_path):
                    os.makedirs(inputs_base_path)
                inputs_path = [
                    os.path.join(inputs_base_path, '%04d.ply' % i)
                    for i in range(L)
                ]
                for i in range(L):
                    export_pointcloud(inputs[i].cpu().numpy(), inputs_path[i],
                                      False)
                    out_file_dict['in_%d' % i] = inputs_path[i]

        # Save output files
        for k, filepath in out_file_dict.items():
            ext = os.path.splitext(filepath)[1]
            out_file = os.path.join(generation_vis_dir,
                                    '%02d_%s%s' % (c_it, k, ext))
            shutil.copyfile(filepath, out_file)

    model_counter[category_id] += 1

# Create pandas dataframe and save
time_df = pd.DataFrame(time_dicts)
time_df.set_index(['idx'], inplace=True)
time_df.to_pickle(out_time_file)