提取rosbag数据

提取bag中的lidar数据

  • 打开ros
roscore
  • 另起窗口,输入提取lidar命令
rosrun pcl_ros pointcloud_to_pcd 2022-09-26-15-37-33.bag input:=/rslidar_points /home/robo/20220926/2022-09-26-15-37-33.bag/lidar
  • 另起窗口play
rosbag paly 2022-09-26-15-37-33.bag

提取bag中的图像数据

#!/usr/bin/env python
import roslib
# roslib.load_manifest('learning_tf')
import rospy
import math
import tf
from sensor_msgs.msg import Image, CompressedImage
import geometry_msgs.msg
from cv_bridge import CvBridge, CvBridgeError
import cv2
import pickle
import os
bridge = CvBridge()  # Instantiate CvBridge
image_downsample_rate = 1.0
i = 0
image_time = rospy.Time()
bag_path = '/home/robo/Desktop/f50023616/AIbag/2022-09-26-15-48-14.bag'

def image_callback(msg):
    global image_time, i
    image_time = msg.header.stamp
    #rospy.loginfo(image_time.to_sec())
    # if i % image_downsample_rate == 0:
    image_dir = "/home/robo/20220926/154814/img/"
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    try:
        # Convert ROS CompressedImage message to OpenCV2
        cv2_img = bridge.compressed_imgmsg_to_cv2(msg, "bgr8")
        cv2.imwrite('{}/{}.jpeg'.format(image_dir, image_time.to_sec()), cv2_img)
        rospy.loginfo("save image:{:.2f}".format(image_time.to_sec())) 
    except CvBridgeError, e:
        rospy.loginfo(e)
    # i += 1 


        
if __name__ == '__main__':
    rospy.init_node('tf_listener')
    listener = tf.TransformListener()
    pose_pool = {}
    rate = rospy.Rate(32.0)  # 32 hz, query pose for each stamp
    rospy.loginfo("start listening")
    
    
    ## get image data
    image_topic = "/flir_adk/image_raw/compressed"   
    rospy.Subscriber(image_topic, CompressedImage, image_callback, queue_size=200)
    rospy.spin()
    
    # ## get time-correlated pose data including [x, y, z, Quaternions]
    # listener.waitForTransform("map", "base_link", image_time, rospy.Duration(8.0))

    # while not rospy.is_shutdown():
#         try:
#             #now = rospy.Time.now()
#             # rospy.loginfo("listener time:{}".format(image_time.to_sec()))
#             listener.waitForTransform("map", "base_link", image_time, rospy.Duration(8.0))
#             (trans, rot) = listener.lookupTransform("map", "base_link", image_time)  # rospy.Time(0)
            
#             pose_dict = {round(image_time.to_sec(), 2): trans[:2] + rot}  #x, y, [x,y,z,w]
#             pose_pool.update(pose_dict)
#             rospy.loginfo(pose_dict)
#         except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
#             rospy.loginfo("exception")
#             continue
            
#         rate.sleep()


#     ## save pose pool and image pool
#     now = rospy.get_time()  
#     with open('{}/pose_data_{}.pkl'.format(bag_path, now), 'wb') as f:
#         pickle.dump(pose_pool, f, protocol=pickle.HIGHEST_PROTOCOL)
#         rospy.loginfo("finsih pickling pose data:{}, length:{}".format(now, len(pose_pool)))

[git token]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

你可能感兴趣的:(备忘录模式,github)