Skip to content

Commit ebc9133

Browse files
committed
clean up setup_jrdb_dataset.py
1 parent 4c89a1c commit ebc9133

File tree

1 file changed

+2
-224
lines changed

1 file changed

+2
-224
lines changed

dr_spaam/bin/setup_jrdb_dataset.py

Lines changed: 2 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -65,226 +65,6 @@ def extract_laser_from_rosbag(split):
6565
bag.close()
6666

6767

68-
def _get_frame_image_dict(cameras_dict, sequence_name):
69-
"""Get a dictionary containing images of the frame, for looking up image files.
70-
71-
Args:
72-
cameras_dict (list[dict]): frames_img.json["data"][frame]["cameras"]
73-
sequence_name (str)
74-
75-
Return:
76-
im_dict (dict): Keys "image_stitched" or "image_X", where X is camera id.
77-
"""
78-
im_dict = {}
79-
for cam_dict in cameras_dict:
80-
url = cam_dict["url"]
81-
assert sequence_name in url
82-
fname = os.path.split(url)[1]
83-
cam_name = cam_dict["name"]
84-
if "stitched" in cam_name:
85-
im_dict["image_stitched"] = {
86-
"url": os.path.join("./images/image_stitched", sequence_name, fname),
87-
"timestamp": cam_dict["timestamp"],
88-
"name": "image_stitched",
89-
}
90-
else:
91-
cam_name = "image_" + cam_name[-1]
92-
im_dict[cam_name] = {
93-
"url": os.path.join("images", cam_name, sequence_name, fname),
94-
"timestamp": cam_dict["timestamp"],
95-
"name": cam_name,
96-
}
97-
98-
return im_dict
99-
100-
101-
def _get_frame_pointcloud_dict(pointclouds_dict, sequence_name):
102-
"""Get a dictionary containing pointclouds of the frame, for looking up
103-
corresponding files.
104-
105-
Args:
106-
pointclouds_dict (list[dict]): frames_pc.json["data"][frame]["pointclouds"]
107-
sequence_name (str)
108-
109-
Return:
110-
pc_dict (dict): Keys "lower_velodyne" or "upper_velodyne
111-
"""
112-
pc_dict = {}
113-
for pc in pointclouds_dict:
114-
url = pc["url"]
115-
assert sequence_name in url
116-
fname = os.path.split(url)[1]
117-
pc_name = pc["name"]
118-
pc_dict[pc_name] = {
119-
"url": os.path.join("pointclouds", pc_name, sequence_name, fname),
120-
"timestamp": pc["timestamp"],
121-
"name": pc_name,
122-
}
123-
124-
return pc_dict
125-
126-
127-
def _add_laser_to_frames_dict(frames_dict, sequence_name, laser_dir):
128-
"""Add laser information to each frame in frames_dict
129-
130-
Args:
131-
frames_dict (list[dict]): frames_img.json["data"] (or similar)
132-
sequence_name (str)
133-
laser_dir (str): Directory to the laser data of the same sequence
134-
135-
Returns:
136-
frames_dict (list[dict]): Same frames dict with an additional field for laser
137-
"""
138-
# timestamps of all frame
139-
frames_timestamp = [fd["timestamp"] for fd in frames_dict]
140-
frames_timestamp = np.array(frames_timestamp, dtype=np.float64)
141-
142-
# timestamps of laser
143-
laser_timestamp = np.loadtxt(
144-
os.path.join(laser_dir, _output_laser_timestamp_fname), dtype=np.float64
145-
)
146-
147-
# match timestamp
148-
del_t = np.abs(frames_timestamp.reshape(-1, 1) - laser_timestamp.reshape(1, -1))
149-
argmin_del_t = np.argmin(del_t, axis=1)
150-
# min_del_t = del_t[np.arange(del_t.shape[0]), argmin_del_t]
151-
# argmin_del_t[min_del_t > max_del_t] = -1 # no matching data found
152-
153-
# add matching laser
154-
for f_idx, (f_dict, closest_laser_idx) in enumerate(zip(frames_dict, argmin_del_t)):
155-
min_del_t = del_t[f_idx, closest_laser_idx]
156-
if min_del_t > 0.1 or min_del_t < 0:
157-
print(
158-
"Bad matching between frame and laser timestamp, "
159-
"sequence {}, frame index {}, frame timestamp {}, laser timestamp {}, "
160-
"timestamp difference {}".format(
161-
sequence_name,
162-
f_idx,
163-
frames_timestamp[f_idx],
164-
laser_timestamp[closest_laser_idx],
165-
min_del_t,
166-
)
167-
)
168-
169-
f_dict["laser"] = {
170-
"url": os.path.join(
171-
_output_laser_dir_name,
172-
sequence_name,
173-
_laser_idx_to_fname(closest_laser_idx),
174-
),
175-
"name": "laser_combined",
176-
"timestamp": laser_timestamp[closest_laser_idx],
177-
}
178-
179-
return frames_dict
180-
181-
182-
def _match_laser_with_image_one_sequence(split, sequence_name):
183-
"""Write in timestamp dir a json file that contains url to matching laser and
184-
stitched image file. Existing files will be overwritten.
185-
186-
Args:
187-
split (str): "train" or "test"
188-
sequence_name (str):
189-
"""
190-
data_dir = os.path.join(_jrdb_dir, split + "_dataset")
191-
192-
timestamp_dir = os.path.join(data_dir, "timestamps", sequence_name)
193-
laser_dir = os.path.join(data_dir, _output_laser_dir_name, sequence_name)
194-
195-
im_frames_file = os.path.join(timestamp_dir, "frames_img.json")
196-
with open(im_frames_file, "r") as f:
197-
im_frames = json.load(f)
198-
199-
im_laser_frames = {"data": []}
200-
for im_frame in im_frames["data"]:
201-
im_laser_frame = {
202-
"images": _get_frame_image_dict(im_frame["cameras"], sequence_name),
203-
"frame_id": im_frame["frame_id"],
204-
"timestamp": im_frame["timestamp"],
205-
}
206-
im_laser_frames["data"].append(im_laser_frame)
207-
208-
# add matching laser scan for each frame
209-
im_laser_frames["data"] = _add_laser_to_frames_dict(
210-
im_laser_frames["data"], sequence_name, laser_dir
211-
)
212-
213-
# check url is correct
214-
for frame_dict in im_laser_frames["data"]:
215-
for _, v in frame_dict["images"].items():
216-
url = v["url"]
217-
assert os.path.isfile(os.path.join(data_dir, url))
218-
laser_url = frame_dict["laser"]["url"]
219-
assert os.path.isfile(os.path.join(data_dir, laser_url))
220-
221-
# write to file
222-
frame_fname = os.path.join(timestamp_dir, _output_frames_laser_im_fname)
223-
with open(frame_fname, "w") as fp:
224-
json.dump(im_laser_frames, fp)
225-
226-
227-
def _match_laser_with_pointcloud_one_sequence(split, sequence_name):
228-
"""Write in timestamp dir a json file that contains url to matching laser and
229-
pointcloud file. Existing files will be overwritten.
230-
231-
Args:
232-
split (str): "train" or "test"
233-
sequence_name (str):
234-
"""
235-
data_dir = os.path.join(_jrdb_dir, split + "_dataset")
236-
237-
timestamp_dir = os.path.join(data_dir, "timestamps", sequence_name)
238-
laser_dir = os.path.join(data_dir, "lasers", sequence_name)
239-
240-
pc_frames_file = os.path.join(timestamp_dir, "frames_pc.json")
241-
with open(pc_frames_file, "r") as f:
242-
pc_frames = json.load(f)
243-
244-
pc_laser_frames = {"data": []}
245-
for pc_frame in pc_frames["data"]:
246-
pc_laser_frame = {
247-
"pointclouds": _get_frame_pointcloud_dict(
248-
pc_frame["pointclouds"], sequence_name
249-
),
250-
"frame_id": pc_frame["frame_id"],
251-
"timestamp": pc_frame["timestamp"],
252-
}
253-
pc_laser_frames["data"].append(pc_laser_frame)
254-
255-
# add matching laser scan for each frame
256-
pc_laser_frames["data"] = _add_laser_to_frames_dict(
257-
pc_laser_frames["data"], sequence_name, laser_dir
258-
)
259-
260-
# check url is correct
261-
for frame_dict in pc_laser_frames["data"]:
262-
for _, v in frame_dict["pointclouds"].items():
263-
url = v["url"]
264-
assert os.path.isfile(os.path.join(data_dir, url))
265-
laser_url = frame_dict["laser"]["url"]
266-
assert os.path.isfile(os.path.join(data_dir, laser_url))
267-
268-
# write to file
269-
frame_fname = os.path.join(timestamp_dir, _output_frames_laser_pc_fname)
270-
with open(frame_fname, "w") as fp:
271-
json.dump(pc_laser_frames, fp)
272-
273-
274-
def match_laser_with_image_and_pointcloud(split):
275-
sequence_names = os.listdir(
276-
os.path.join(_jrdb_dir, split + "_dataset", "timestamps")
277-
)
278-
for idx, seq_name in enumerate(sequence_names):
279-
print(
280-
"({}/{}) Match laser data for sequence {}".format(
281-
idx + 1, len(sequence_names), seq_name
282-
)
283-
)
284-
_match_laser_with_image_one_sequence(split, seq_name)
285-
_match_laser_with_pointcloud_one_sequence(split, seq_name)
286-
287-
28868
def _match_pc_im_laser_one_sequence(split, sequence_name):
28969
"""Write in timestamp dir a json file that contains url to matching pointcloud,
29070
laser, and image. Existing files will be overwritten. Pointcloud is used as
@@ -383,9 +163,7 @@ def match_pc_im_laser(split):
383163

384164

385165
if __name__ == "__main__":
386-
# extract_laser_from_rosbag("train")
387-
# match_laser_with_image_and_pointcloud("train")
166+
extract_laser_from_rosbag("train")
388167
match_pc_im_laser("train")
389-
# extract_laser_from_rosbag("test")
390-
# match_laser_with_image_and_pointcloud("test")
168+
extract_laser_from_rosbag("test")
391169
match_pc_im_laser("test")

0 commit comments

Comments
 (0)