@@ -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
111123class 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 ()
0 commit comments