-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathload_data.py
43 lines (41 loc) · 1.64 KB
/
load_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import os.path as osp
import rosbag
import numpy as np
import argparse
from cv_bridge import CvBridge
import cv2
from sensor_msgs import point_cloud2
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--root')
parser.add_argument('--bagid')
args = parser.parse_args()
root = args.root
radar_root = osp.join(root, 'radar')
lidar_root = osp.join(root, 'lidar')
if not osp.isdir(radar_root):
os.mkdir(radar_root)
if not osp.isdir(lidar_root):
os.mkdir(lidar_root)
topics = ['/velodyne_points', '/talker1/Navtech/Polar', '/navsat/odom']
bagfiles = sorted([f for f in os.listdir(root) if '.bag' in f and args.bagid in f])
v = 1
for bag in bagfiles:
bag = rosbag.Bag(osp.join(root, bag))
for topic, msg, t in bag.read_messages(topics):
timestamp = int(msg.header.stamp.to_nsec() * 1e-3)
if topic == '/navsat/odom':
vel = msg.twist.twist.linear
v = np.sqrt(vel.x**2 + vel.y**2)
if topic == '/velodyne_points':
if v > 0.1:
continue
cloud_points = list(point_cloud2.read_points(msg, skip_nans=True))
points = np.array(cloud_points, dtype=np.float32)
points.astype(np.float32).tofile(osp.join(lidar_root, '{}.bin'.format(timestamp)))
if topic == '/talker1/Navtech/Polar':
if v > 0.1:
continue
img = CvBridge().imgmsg_to_cv2(msg, desired_encoding="mono8")
cv2.imwrite(osp.join(radar_root, '{}.png'.format(timestamp)), img)