INVR parser.
| 39 | |
| 40 | # TODO parser读取点云数据,需要parser.points & parser.points_rgb |
| 41 | class Parser: |
| 42 | """INVR parser.""" |
| 43 | |
| 44 | def __init__( |
| 45 | self, |
| 46 | data_dir: str, |
| 47 | extension: str = ".png", |
| 48 | set: str = "train", |
| 49 | normalize: bool = False, |
| 50 | # Anymore parameters? |
| 51 | ): |
| 52 | self.data_dir = data_dir |
| 53 | self.extension = extension |
| 54 | self.set = set |
| 55 | |
| 56 | # Read json file according to data_dir |
| 57 | if os.path.exists(os.path.join(self.data_dir, "transforms_train.json")): |
| 58 | print("Found transforms_train.json file, assuming Blender data set!") |
| 59 | else: |
| 60 | assert False, "Could not recognize scene type!" |
| 61 | |
| 62 | if set == "train": |
| 63 | # Read Training Dataset |
| 64 | with open(os.path.join(self.data_dir, "transforms_train.json")) as json_file: |
| 65 | contents = json.load(json_file) |
| 66 | elif set == "test": |
| 67 | with open(os.path.join(self.data_dir, "transforms_test.json")) as json_file: |
| 68 | contents = json.load(json_file) |
| 69 | else: |
| 70 | assert False, "Could not recognize set type!" |
| 71 | |
| 72 | # Assuiming Intrinsics of all cameras are the same, which may not always be true |
| 73 | self.width = contents["w"] |
| 74 | self.height = contents["h"] |
| 75 | self.fl_x = contents["fl_x"] |
| 76 | self.fl_y = contents["fl_y"] |
| 77 | self.cx = contents["cx"] |
| 78 | self.cy = contents["cy"] |
| 79 | self.K = torch.zeros(3,3) |
| 80 | self.K[0, 0] = self.fl_x; self.K[1, 1] = self.fl_y; self.K[0, 2] = self.cx; self.K[1, 2] = self.cy; self.K[2, 2] = 1 |
| 81 | |
| 82 | self.image_id = []; self.timestamp = []; self.c2w=[]; self.R = []; self.T = []; self.image = [] |
| 83 | |
| 84 | # Read frames |
| 85 | frames = contents["frames"] |
| 86 | tbar = tqdm(range(len(frames))) |
| 87 | def frame_read_fn(idx_frame): |
| 88 | idx = idx_frame[0] # equals to image id? |
| 89 | frame = idx_frame[1] |
| 90 | timestamp = frame.get('time', 0.0) |
| 91 | cam_name = os.path.join(self.data_dir, frame.get('file_path', f'{idx:04d}') + extension) |
| 92 | |
| 93 | # NeRF 'transform_matrix' is a camera-to-world transform |
| 94 | c2w = np.array(frame["transform_matrix"]) |
| 95 | # change from OpenGL/Blender camera axes (Y up, Z back) to COLMAP (Y down, Z forward) |
| 96 | c2w[:3, 1:3] *= -1 |
| 97 | # get the world-to-camera transform and set R, T |
| 98 | w2c = np.linalg.inv(c2w) |