Ejemplo n.º 1
0
 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']
Ejemplo n.º 2
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)
                        help='starting index [default: 0]')
    parser.add_argument('--length',
                        type=int,
                        default=0,
                        help='number of meshes to render [default: 44]')
    args = parser.parse_args()
    n_views = args.num_views
    SCAN = '%s/SHREC19/mesh/{}.ply' % (PATH_TO_DATA)
    GTCORRES = '%s/SHREC19/mesh/{}.corres' % (PATH_TO_DATA)
    rotation_path = '%s/SHREC19/render_rotations' % (PATH_TO_DATA)
    render_path = '%s/SHREC19/scans' % (PATH_TO_DATA)
    OBJ = '%s/{0:03d}_{1:03d}.obj' % (render_path)
    CORRES = '%s/{0:03d}_{1:03d}.corres' % (render_path)
    MAT = '%s/{0:03d}_{1:03d}.mat' % (render_path)

    camera = PinholeCamera()
    os.system('mkdir -p %s' % rotation_path)
    if not os.path.exists('%s/%d.txt' % (rotation_path, n_views - 1)):
        thetas = np.linspace(0, np.pi * 2, n_views)
        rotations = [
            linalg.rodriguez(
                np.array([0., 1., 0.]) * thetas[i] + np.random.randn(3) * 0.2)
            for i in range(n_views)
        ]
        for i, rotation in enumerate(rotations):
            np.savetxt('%s/%d.txt' % (rotation_path, i), rotation)
    else:
        rotations = [
            np.loadtxt('%s/%d.txt' % (rotation_path, i)).reshape((3, 3))
            for i in range(n_views)
        ]