Skip to content

Instantly share code, notes, and snippets.

@prerakmody
Last active June 14, 2023 10:07
Show Gist options
  • Save prerakmody/0520abde6ab4506e05252ccf8cda32dd to your computer and use it in GitHub Desktop.
Save prerakmody/0520abde6ab4506e05252ccf8cda32dd to your computer and use it in GitHub Desktop.
Functions to read rosbag files

Installation

  1. Windows (ros-noetic with pytorch)
    conda create -y -n XOSight-Pattern2 "python=3.8" "mamba>=0.22.1" -c conda-forge
    conda activate XOSight-Pattern2
    mamba install ros-noetic-desktop -c robostack -c robostack-experimental -c conda-forge --override-channels
    mamba install vs2019_win-64
    conda deactivate
    conda activate XOSight-Pattern2
    mamba install rosdep
    rosdep init
    rosdep update
    
    pip install --extra-index-url https://rospypi.github.io/simple/ rosbag rospy cv_bridge sensor_msgs geometry_msgs roslz4 
    pip install opencv-contrib-python
    python -c "import rosbag, rospy, sensor_msgs, cv_bridge"
    
    conda install pytorch torchvision cudatoolkit=11.6 -c pytorch -c conda-forge
    
    • Ref: To install ros-noetic (Link), To install python mappings i.e. rospypi (Link)
import tqdm
import rosbag # only python2.7 (http://wiki.ros.org/kinetic/Installation/Ubuntu)
import numpy as np
from sensor_msgs import point_cloud2
## Step1
rosbag_filename = 'myfile.bag'
bag = rosbag.Bag(rosbag_filename)
print (' - filename : ', bag.filename)
print (' - get_message_count : ', bag.get_message_count())
print (' - start_time : ', bag.get_start_time())
print (' - end_time : ', bag.get_end_time())
print (' - size : ', bag.size)
print (' - mode : ', bag.mode)
print ('\n\n')
## Step2 - Method1 - Get all topics
topics = []
print (' - get_message_count : ', bag.get_message_count())
with tqdm.tqdm_notebook(total = bag.get_message_count()) as pbar:
for idx, (topic, msg, t) in enumerate(bag.read_messages()):
pbar.update(1)
topics.append(topic)
topics = np.array(topics)
print (' - topics : ', np.unique(topics))
## Step2 - Method2 - Get all topics
topics = [topic for topic in bag.get_type_and_topic_info().topics]
for topic in sorted(topics):
print (topic)
## Step3 - Print messages of a topic
# for idx, (topic, msg, t) in enumerate(bag.read_messages()):
for idx, (topic, msg, t) in enumerate(bag.read_messages(topics=['/velodyne_packets'])):
print (topic, type(msg), t.to_time())
import tqdm
import rosbag # only python2.7 (http://wiki.ros.org/kinetic/Installation/Ubuntu)
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
## Step1 - Read .rosbag
rosbag_filename = 'myfile.bag'
bag = rosbag.Bag(rosbag_filename)
## Step2 - Read Compressed Images Packets
print (' - start_time : ', bag.get_start_time())
print (' - end_time : ', bag.get_end_time())
topics = ['image_raw_left/compressed', 'image_raw_right/compressed']
with tqdm.tqdm_notebook(total = bag.get_message_count(topics)) as pbar:
for idx, (topic, msg, t) in enumerate(bag.read_messages(topics=topics)):
pbar.update(1)
print (topic, type(msg), t.to_time())
img = cv2.imdecode(np.fromstring(msg.data, np.uint8), cv2.IMREAD_COLOR)
plt.imshow(img)
filename = os.path.join('/home/ubuntu/data/image_raw_left', str(t.to_time()) + ".jpg")
cv2.imwrite(filename, img)
if idx > 150:
break
import tqdm
import rosbag # only python2.7 (http://wiki.ros.org/kinetic/Installation/Ubuntu)
import numpy as np
from sensor_msgs import point_cloud2
from sensor_msgs.msg import CompressedImage
## Step1 - Read .rosbag
rosbag_filename = 'myfile.bag'
bag = rosbag.Bag(rosbag_filename)
## Step2 - Read Velodyne Packets
for idx, (topic, msg, t) in enumerate(bag.read_messages(topics=['/velodyne_packets'])):
print (topic, type(msg), t.to_time())
packet = msg.serialize
print (' - header : ', packet.im_self.header)
data = packet.im_self.packets
print (' - Total Packets : ', len(data))
print ('data[0] : ', type(data[0]))
print ('data[0].stamp : ', type(data[0].stamp))
print ('data[0].data : ', type(data[0].data))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment