Skip to content

Commit 987ae4c

Browse files
Added flags for conversion to preserve pointcloud KITTI format and to automatically de-bayer images
1 parent 0985b01 commit 987ae4c

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "vbr-devkit"
7-
version = "0.0.8"
7+
version = "0.0.9"
88
description = "Development kit for VBR SLAM dataset"
99
readme = "README.md"
1010
authors = [

python/vbr_devkit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.3"
1+
__version__ = "0.0.9"

python/vbr_devkit/datasets/kitti.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ def __init__(self, data_dir: Path, topic: str, format_fn=None):
5353
self.metadata["topic"] = topic
5454
self.metadata["num_messages"] = 0
5555

56-
def push_back(self, data: Union[PointCloudXf, Image, Imu], timestamp):
56+
def push_back(self, data: Union[PointCloudXf, Image, Imu], timestamp, *args, **kwargs):
5757
if "msg_type" not in self.metadata:
5858
self.metadata["msg_type"] = data.__class__.__name__
5959

6060
if self.metadata["msg_type"] != data.__class__.__name__:
6161
raise RuntimeError(
6262
f"TopicHandler is bound to {self.metadata['msg_type']}. Cannot handle data of type {type(data)}")
6363

64-
self.save_fn[self.metadata["msg_type"]](data, timestamp)
64+
self.save_fn[self.metadata["msg_type"]](data, timestamp, *args, **kwargs)
6565
self.timestamps.append(timestamp)
6666
self.metadata["num_messages"] += 1
6767

68-
def _save_cloud(self, data: PointCloudXf, timestamp):
68+
def _save_cloud(self, data: PointCloudXf, timestamp, *args, **kwargs):
6969
dest_path = self.data_f / Path(self.format_fn(self.metadata["num_messages"]) + ".bin")
7070
# Save fields to metadata to recover it later.
7171
# We assume fields to remain constant through data of this topic
@@ -78,16 +78,28 @@ def _save_cloud(self, data: PointCloudXf, timestamp):
7878
with open(self.data_f / ".dtype.pkl", "wb") as f:
7979
pickle.dump(data.points.dtype, f)
8080

81-
data.points.tofile(dest_path)
81+
if "pcloud_kitti_format" in kwargs:
82+
if kwargs.get("pcloud_kitti_format"):
83+
clip_points = np.stack([data.points["x"], data.points["y"], data.points["z"], data.points["intensity"]],
84+
axis=1)
85+
clip_points.tofile(dest_path)
86+
else:
87+
data.points.tofile(dest_path)
8288

83-
def _save_image(self, data: Image, timestamp: float):
89+
def _save_image(self, data: Image, timestamp: float, *args, **kwargs):
8490
dest_path = self.data_f / Path(self.format_fn(self.metadata["num_messages"]) + ".png")
91+
92+
if "rgb_convert" in kwargs:
93+
if kwargs.get("rgb_convert"):
94+
data.image = cv2.cvtColor(data.image, cv2.COLOR_BAYER_RG2RGB)
95+
data.encoding = "rgb8"
96+
8597
if not "encoding" in self.metadata.keys():
8698
self.metadata["encoding"] = data.encoding
8799

88100
cv2.imwrite(str(dest_path), data.image)
89101

90-
def _save_imu(self, data: Imu, timestamp: float):
102+
def _save_imu(self, data: Imu, timestamp: float, *args, **kwargs):
91103
if not self.imu_dest:
92104
self.imu_dest = (self.data_f / "imu.txt").open("w")
93105
self.imu_dest.write(IMU_CSV_HEADER)
@@ -109,10 +121,12 @@ def close(self):
109121

110122

111123
class KittiWriter:
112-
def __init__(self, data_dir: Path):
124+
def __init__(self, data_dir: Path, rgb_convert: bool = True, pcloud_kitti_format: bool = True, *args, **kwargs):
113125
data_dir.mkdir(parents=True, exist_ok=True)
114126
self.destination_dir = data_dir
115127
self.data_handles = {}
128+
self.rgb_convert = rgb_convert
129+
self.pcloud_kitti_format = pcloud_kitti_format
116130

117131
def __enter__(self):
118132
return self
@@ -127,8 +141,9 @@ def publish(self, timestamp, topic: str, message: Union[PointCloudXf, Image, Imu
127141
self.data_handles[topic] = KittiTopicHandler(self.destination_dir / Path(handle_dir), topic,
128142
lambda x: f"{x:010d}")
129143

130-
self.data_handles[topic].push_back(message, timestamp)
144+
self.data_handles[topic].push_back(message, timestamp, rgb_convert=self.rgb_convert,
145+
pcloud_kitti_format=self.pcloud_kitti_format)
131146

132147
def __exit__(self, exc_type, exc_val, exc_tb):
133148
for handle in self.data_handles:
134-
self.data_handles[handle].close()
149+
self.data_handles[handle].close()

python/vbr_devkit/tools/run.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ def convert(to: Annotated[OutputDataInterface, typer.Argument(help="Desired data
6363
Path, typer.Argument(help="Input bag or directory containing multiple bags", show_default=False)],
6464
output_dir: Annotated[
6565
Path, typer.Argument(help="Output directory in which the data will be stored", show_default=False)],
66-
) -> None:
66+
rgb_conversion: Annotated[
67+
bool, typer.Option(
68+
help="Enable BayerRG8->RGB conversion during conversion in KITTI format."
69+
" Disable this flag to reduce the memory footprint of the converted folder.",
70+
show_default=True)] = True,
71+
reduce_pcloud: Annotated[
72+
bool, typer.Option(
73+
help="Preserve only <x,y,z,intensity> channels during PointCloud conversion in KITTI format. "
74+
"Allows compatibility with KITTI readers but removes extra LiDAR channels",
75+
show_default=True)] = True) -> None:
6776
console.print(f"Converting {input_dir} to {to} format at {output_dir}")
6877
if to == OutputDataInterface.ros2:
6978
if not input_dir.is_dir():
@@ -75,7 +84,8 @@ def convert(to: Annotated[OutputDataInterface, typer.Argument(help="Desired data
7584
rosconvert.convert(item, output_dir / item.stem)
7685
else:
7786
with RosReader(input_dir) as reader:
78-
with OutputDataInterface_lut[to](output_dir) as writer:
87+
with OutputDataInterface_lut[to](output_dir, rgb_convert=rgb_conversion,
88+
pcloud_kitti_format=reduce_pcloud) as writer:
7989
for timestamp, topic, message in track(reader, description="Processing..."):
8090
writer.publish(timestamp, topic, message)
8191
console.print(":tada: Completed")

0 commit comments

Comments
 (0)