Beispiel #1
0
class MeshRendering:
    def __init__(self, json_file, depth_image=False):
        with open(json_file, 'r') as fin:
            json_dict = json.load(fin)
        self.camera = PinholeCamera()
        self.rotations = np.array(json_dict['rotations'])
        print('\tTotal #view={}'.format(self.rotations.shape[0]))
        self.mesh_files = json_dict['mesh_files']
        print('\tTotal #mesh={}'.format(len(self.mesh_files)))
        self.mat_file_paths = json_dict['mat_file_paths']
        self.depth_image = depth_image
        if json_dict.get('meshv_attr_dict', None) is not None:
            self.meshv_attr_dict = json_dict['meshv_attr_dict']
        if json_dict.get('meshv_list_attr_dict', None) is not None:
            self.meshv_list_attr_dict = json_dict['meshv_list_attr_dict']
        #else:
        #  if json_dict.get('correspondence_files', None) is not None:
        #    self.meshv_attr_dict = { 'correspondence': [] }

    def render(self, offset, length):
        for mesh_id in range(offset, offset + length):
            print('mesh id = {}'.format(mesh_id))
            mesh_file = self.mesh_files[mesh_id]
            mat_file_path = self.mat_file_paths[mesh_id]
            mesh = o3d.io.read_triangle_mesh(mesh_file)

            os.system('mkdir -p {}'.format(osp.dirname(mat_file_path)))
            for view_id, rotation in enumerate(self.rotations):
                transformation = np.eye(4)
                transformation[:3, :3] = rotation
                mesh.transform(transformation)
                packed = self.camera.project(mesh.vertices, mesh.triangles)
                depth_image, extrinsic, intrinsic = packed[0], packed[
                    1], packed[2]
                points3d_i, correspondence, valid_idx = packed[3], packed[
                    4], packed[5]
                mesh.transform(transformation.T)
                output_dict = {}
                if self.depth_image:
                    depth = depth_image[(valid_idx[:, 0], valid_idx[:, 1])]
                    output_dict['depth'] = depth
                    output_dict['valid_pixel_indices'] = valid_idx
                    output_dict['width'] = 320
                    output_dict['height'] = 240

                output_dict['points3d'] = points3d_i
                #output_dict['correspondence'] = gt_correspondence
                for name, attr in self.meshv_attr_dict.items():
                    output_dict[name] = np.array(attr)[correspondence]
                for name, attr in self.meshv_list_attr_dict.items():
                    output_dict[name] = np.array(attr[mesh_id])[correspondence]
                output_dict['mesh_correspondence'] = correspondence
                output_dict['rotation'] = rotation
                output_dict['intrinsic'] = intrinsic
                output_dict['extrinsic'] = extrinsic

                mat_file = mat_file_path.format(view_id)
                print('saving to %s' % mat_file)
                sio.savemat(mat_file, output_dict, do_compression=True)
        depths = []
        points3d = []
        valid_pixel_indices = []
        correspondences = []
        intrinsics = []
        extrinsics = []
        gt_feats = []
        gt_correspondences = []
        current = 0

        used_rotations = []
        for i, rotation in enumerate(rotations):
            transformation = np.eye(4)
            transformation[:3, :3] = rotation
            mesh.transform(transformation)
            depth_image, extrinsic, intrinsic, points3d_i, correspondence, valid_idx = camera.project(
                mesh.vertices, mesh.triangles)
            mesh.transform(transformation.T)
            depth = depth_image[(valid_idx[:, 0], valid_idx[:, 1])]
            helper.save_to_obj(OBJ.format(scan_id, i),
                               vis.getPointCloud(points3d_i))
            gt_correspondence = smpl_corres[correspondence]
            np.savetxt(CORRES.format(scan_id, i), gt_correspondence, '%d')
            mat_file = MAT.format(scan_id, i)
            print('saving to %s' % mat_file)
            sio.savemat(mat_file, {
                'depth': depth,
                'points3d': points3d_i,
                'valid_pixel_indices': valid_idx,
                'correspondence': gt_correspondence,
                'mesh_correspondence': correspondence,
                'rotation': rotation,