Skip to content

Commit a3f9059

Browse files
committed
Add KittiVelodyneData()
1 parent 35d3640 commit a3f9059

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

src/xyz.py

+59-15
Original file line numberDiff line numberDiff line change
@@ -5594,36 +5594,80 @@ def test_load(self):
55945594

55955595

55965596
class KittiCameraData:
5597-
""" KittiCameraDataset """
5598-
def __init__(self, cam_idx, seq_path):
5597+
""" Kitti Camera Data"""
5598+
def __init__(self, cam_idx, seq_dir: Path):
55995599
self.cam_idx = cam_idx
5600-
self.seq_path = seq_path
5601-
self.cam_path = Path(self.seq_path, "image_" + str(self.cam_idx).zfill(2))
5602-
self.img_dir = Path(self.cam_path, "data")
5600+
self.seq_dir = seq_dir
5601+
self.cam_path = self.seq_dir / ("image_" + str(self.cam_idx).zfill(2))
5602+
self.img_dir = self.cam_path / "data"
56035603
self.img_paths = sorted(glob.glob(str(Path(self.img_dir, "*.png"))))
56045604

56055605

5606+
class KittiVelodyneData:
5607+
""" Kitti Velodyne Data"""
5608+
def __init__(self, seq_dir: Path):
5609+
self.seq_dir = seq_dir
5610+
self.velodyne_path = Path(self.seq_dir, "velodyne_points")
5611+
self.bins_dir = self.velodyne_path / "data"
5612+
5613+
ts_file = self.velodyne_path / "timestamps.txt"
5614+
ts_start_file = self.velodyne_path / "timestamps_start.txt"
5615+
ts_end_file = self.velodyne_path / "timestamps_end.txt"
5616+
5617+
self.timestamps = self._load_timestamps(ts_file)
5618+
self.timestamps_start = self._load_timestamps(ts_start_file)
5619+
self.timestamps_end = self._load_timestamps(ts_end_file)
5620+
self.bin_paths = sorted(self.bins_dir.glob("*.bin"))
5621+
5622+
def _load_timestamps(self, data_file: Path) -> list[np.int64]:
5623+
""" Load timestamps from file """
5624+
f = open(data_file, "r")
5625+
5626+
timestamps = []
5627+
for line in f:
5628+
line = line.strip()
5629+
dt = line.split('.')[0]
5630+
dt_obj = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
5631+
seconds = dt_obj.timestamp()
5632+
nanoseconds = int(line.split('.')[1])
5633+
timestamps.append(int(seconds * 1e9) + nanoseconds)
5634+
5635+
f.close()
5636+
return timestamps
5637+
5638+
def load_scan(self, ts: np.int64) -> MatNx4:
5639+
""" Load scan based on timestamp """
5640+
index = self.timestamps.index(ts)
5641+
bin_path = self.bin_paths[index]
5642+
pcd = np.fromfile(bin_path, dtype=np.float32).reshape(-1, 4)
5643+
return pcd
5644+
5645+
56065646
class KittiRawDataset:
56075647
""" KittiRawDataset """
5608-
def __init__(self, data_dir, date, seq, is_sync):
5648+
def __init__(self, data_dir: Path, date: str, seq: str, is_sync: bool):
56095649
# Paths
56105650
self.data_dir = data_dir
56115651
self.date = date
5652+
self.date_dir = data_dir / self.date
56125653
self.seq = seq.zfill(4)
56135654
self.sync = "sync" if is_sync else "extract"
56145655
self.seq_name = "_".join([self.date, "drive", self.seq, self.sync])
5615-
self.seq_path = Path(self.data_dir, self.date, self.seq_name)
5656+
self.seq_dir = Path(self.data_dir, self.date, self.seq_name)
56165657

56175658
# Camera data
5618-
self.cam0_data = KittiCameraData(0, self.seq_path)
5619-
self.cam1_data = KittiCameraData(1, self.seq_path)
5620-
self.cam2_data = KittiCameraData(2, self.seq_path)
5621-
self.cam3_data = KittiCameraData(3, self.seq_path)
5659+
self.cam0_data = KittiCameraData(0, self.seq_dir)
5660+
self.cam1_data = KittiCameraData(1, self.seq_dir)
5661+
self.cam2_data = KittiCameraData(2, self.seq_dir)
5662+
self.cam3_data = KittiCameraData(3, self.seq_dir)
5663+
5664+
# Velodyne data
5665+
self.velodyne_data = KittiVelodyneData(self.seq_dir)
56225666

56235667
# Calibration
5624-
calib_cam_to_cam_filepath = Path(self.data_dir, "calib_cam_to_cam.txt")
5625-
calib_imu_to_velo_filepath = Path(self.data_dir, "calib_imu_to_velo.txt")
5626-
calib_velo_to_cam_filepath = Path(self.data_dir, "calib_velo_to_cam.txt")
5668+
calib_cam_to_cam_filepath = Path(self.date_dir, "calib_cam_to_cam.txt")
5669+
calib_imu_to_velo_filepath = Path(self.date_dir, "calib_imu_to_velo.txt")
5670+
calib_velo_to_cam_filepath = Path(self.date_dir, "calib_velo_to_cam.txt")
56275671
self.calib_cam_to_cam = self._read_calib_file(calib_cam_to_cam_filepath)
56285672
self.calib_imu_to_velo = self._read_calib_file(calib_imu_to_velo_filepath)
56295673
self.calib_velo_to_cam = self._read_calib_file(calib_velo_to_cam_filepath)
@@ -5716,7 +5760,7 @@ def plot_frames(self):
57165760
T_BC3 = self.get_camera_extrinsics(3)
57175761

57185762
plt.figure()
5719-
ax = plt.axes(projection='3d')
5763+
ax: Axes3D = plt.axes(projection='3d')
57205764
plot_tf(ax, eye(4), size=0.1, name="imu")
57215765
plot_tf(ax, T_BV, size=0.1, name="velo")
57225766
plot_tf(ax, T_BC0, size=0.1, name="cam0")

0 commit comments

Comments
 (0)