我的目的是:
提取雷达数据包:2020-08-04-16-22-48.bag中的图片数据使用python库包:rosbag、cv2、cv_bridge进行提取工作提取代码如下:
import rosbag import cv2 from cv_bridge import CvBridge bag_file = '/home/shl/2020-08-04-16-22-48.bag' bag = rosbag.Bag(bag_file, "r") bridge = CvBridge() bag_data_imgs = bag.read_messages('/ns1/usb_cam/image_raw') def extract_imgs(): i = 1 for topic, msg, t in bag_data_imgs: cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") print(type(cv_image)) # <type 'numpy.ndarray'> #cv2.imshow("Image window", cv_image) #cv2.waitKey(3) cv2.imwrite("./extract_images/%05d.png".format(i), cv_image) i+=1 if __name__ == '__main__': extract_imgs()1、程序运行后报错:ModuleNotFoundError: No module named 'Cryptodome'
解决方式:
pip install pycryptodomex
2、后面可能又报错:ModuleNotFoundError: No module named 'gnupg'
解决方式:
pip install gnupg
1、上面的两个错误刚解决,运行程序之后又报错:rosbag.bag.ROSBagException: unsupported compression type: lz4
(yolov4) shl@zhihui-mint:~/extract_rosbag_data$ python extract_bag_images.py Failed to load Python extension for LZ4 support. LZ4 compression will not be available. Traceback (most recent call last): File "extract_bag_images.py", line 24, in <module> extract_imgs() File "extract_bag_images.py", line 14, in extract_imgs for topic, msg, t in bag_data_imgs: File "/opt/ros/melodic/lib/python2.7/dist-packages/rosbag/bag.py", line 2699, in read_messages yield self.seek_and_read_message_data_record((entry.chunk_pos, entry.offset), raw, return_connection_header) File "/opt/ros/melodic/lib/python2.7/dist-packages/rosbag/bag.py", line 2833, in seek_and_read_message_data_record raise ROSBagException('unsupported compression type: %s' % chunk_header.compression) rosbag.bag.ROSBagException: unsupported compression type: lz4 (yolov4) shl@zhihui-mint:~/extract_rosbag_data$错误信息:无法加载LZ4支持的Python扩展。 LZ4压缩将不可用,这里说这是由于ROS库造成的问题,但遇到的只是警告,我这里直接是错误,用处也不是很大
尝试解决方式1(无效):
pip install lz4
1、我先使用rosbag查看一下bag文件的一些信息:
rosbag info 2020-08-04-16-22-48.bag
(base) shl@zhihui-mint:~/extract_rosbag_data$ rosbag info 2020-08-04-16-22-48.bag path: 2020-08-04-16-22-48.bag version: 2.0 duration: 5:14s (314s) start: Aug 04 2020 16:22:49.08 (1596529369.08) end: Aug 04 2020 16:28:03.36 (1596529683.36) size: 6.8 GB messages: 7439 compression: lz4 [3126/3126 chunks; 84.80%] uncompressed: 8.0 GB @ 26.2 MB/s compressed: 6.8 GB @ 22.2 MB/s (84.80%) types: sensor_msgs/Image [060021388200f6f0f447d0fcd9c64743] sensor_msgs/PointCloud2 [1158d486dd51d683ce2f1be655c3c181] topics: /ns1/usb_cam/image_raw 3125 msgs : sensor_msgs/Image /radar_projection 4314 msgs : sensor_msgs/PointCloud2 (base) shl@zhihui-mint:~/extract_rosbag_data$从上面可以看到2020-08-04-16-22-48.bag的压缩方式是:lz4
2、如果我们尝试把2020-08-04-16-22-48.bag解压后,再读取看是否有问题
rosbag decompress 2020-08-04-16-22-48.bag --output-dir ./extract_images/
3、让我们再来看一下解压后的bag文件的信息:
(base) shl@zhihui-mint:~/extract_rosbag_data/extract_images$ rosbag info 2020-08-04-16-22-48.bag path: 2020-08-04-16-22-48.bag version: 2.0 duration: 5:14s (314s) start: Aug 04 2020 16:22:49.08 (1596529369.08) end: Aug 04 2020 16:28:03.36 (1596529683.36) size: 8.0 GB messages: 7439 compression: none [3126/3126 chunks] types: sensor_msgs/Image [060021388200f6f0f447d0fcd9c64743] sensor_msgs/PointCloud2 [1158d486dd51d683ce2f1be655c3c181] topics: /ns1/usb_cam/image_raw 3125 msgs : sensor_msgs/Image /radar_projection 4314 msgs : sensor_msgs/PointCloud2 (base) shl@zhihui-mint:~/extract_rosbag_data/extract_images$4、然后再读取一次,结果又报错:ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)
(base) shl@zhihui-mint:~/extract_rosbag_data$ python extract_bag_images.py Failed to load Python extension for LZ4 support. LZ4 compression will not be available. Traceback (most recent call last): File "extract_bag_images.py", line 24, in <module> extract_imgs() File "extract_bag_images.py", line 15, in extract_imgs cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 163, in imgmsg_to_cv2 dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding) File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 99, in encoding_to_dtype_with_channels return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding)) File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2 from cv_bridge.boost.cv_bridge_boost import getCvType ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost) (base) shl@zhihui-mint:~/extract_rosbag_data$这篇博客给出了一种这种问题的解决方法
最终我解决问题的方式是:
还是用之前使用lz4压缩方式的2020-08-04-16-22-48.bag 文件但是运行的时候使用python2即可!这是因为python3不支持采用LZ4 压缩的bag包饶了一大圈,结果就是起点出了问题,FK!正确的运行命令:
python2 extract_bag_images.py
后面我尝试了没有压缩的bag包文件,依然报错:ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost) ,这个错误主要是由于cv_bridge 库包对于python3中不兼容!
解决问题参考:
stack overflowXavier AGX建立export.launch文件, 文件内容如下:
<launch> <node pkg="rosbag" type="play" name="rosbag" args="-d 2 /home/jhc/2020-08-04-16-22-48.bag"/> <node name="extract" pkg="image_view" type="extract_images" respawn="false" output="screen" cwd="ROS_HOME"> <remap from="image" to="/ns1/usb_cam/image_raw"/> </node> </launch>其中:
/home/jhc/2020-08-04-16-22-48.bag:是要解析的.bag文件的路径/ns1/usb_cam/image_raw:是.bag文件中图片所在的位置下面是提取图片的步骤 1、打开终端运行
roscore
2、再新建一个终端,使用roslaunch运行刚刚新建的export.launch文件
roslaunch export.launch
jhc@jhc-Default-string:~$ roslaunch export.launch ... logging to /home/jhc/.ros/log/7cd4fadc-0eb8-11eb-a4b8-0002a000179a/roslaunch-jhc-Default-string-8501.log Checking log directory for disk usage. This may take awhile. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB. started roslaunch server http://jhc-Default-string:40787/ SUMMARY ======== PARAMETERS * /rosdistro: kinetic * /rosversion: 1.12.16 NODES / extract (image_view/extract_images) rosbag (rosbag/play) ROS_MASTER_URI=http://localhost:11311 process[rosbag-1]: started with pid [8519] process[extract-2]: started with pid [8520] [ INFO] [1602760999.375392392]: Initialized sec per frame to 0.100000 [ INFO] [1602761001.578548030]: Saved image frame0000.jpg [ INFO] [1602761001.768274749]: Saved image frame0001.jpg [ INFO] [1602761001.975395623]: Saved image frame0002.jpg [ INFO] [1602761002.164784906]: Saved image frame0003.jpg [ INFO] [1602761002.265436061]: Saved image frame0004.jpg [ INFO] [1602761002.373386619]: Saved image frame0005.jpg [ INFO] [1602761002.565808322]: Saved image frame0006.jpg [ INFO] [1602761002.669046955]: Saved image frame0007.jpg ...... [ INFO] [1602755793.105539345]: Saved image frame2021.jpg [ INFO] [1602755793.213051297]: Saved image frame2022.jpg [ INFO] [1602755793.411856603]: Saved image frame2023.jpg [ INFO] [1602755793.616882581]: Saved image frame2024.jpg [ INFO] [1602755793.719490581]: Saved image frame2025.jpg [ INFO] [1602755793.918996288]: Saved image frame2026.jpg [rosbag-1] process has finished cleanly log file: /home/jhc/.ros/log/7cd4fadc-0eb8-11eb-a4b8-0002a000179a/rosbag-1*.log解析的图片在home下的 .ros目录里,你可以移动到你想存放的目录中!
代码如下:
import rosbag import cv2 from cv_bridge import CvBridge #bag_file = './extract_images/2020-08-04-16-22-48.bag' bag_file = './2020-08-04-16-22-48.bag' bag = rosbag.Bag(bag_file, "r") bridge = CvBridge() bag_data_imgs = bag.read_messages('/ns1/usb_cam/image_raw') def extract_imgs(): i = 1 for topic, msg, t in bag_data_imgs: cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") print(type(cv_image)) # <type 'numpy.ndarray'> #cv2.imshow("Image window", cv_image) #cv2.waitKey(3) cv2.imwrite("./extract_images/%05d.png" % i, cv_image) i+=1 if __name__ == '__main__': extract_imgs()注意:
图片序列合成视频:ffmpeg -f image2 -i %05d.png composition.mp4
♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠
