gpt4 book ai didi

python - 在 Python 3 中导入 Rosbag

转载 作者:行者123 更新时间:2023-12-04 12:40:36 31 4
gpt4 key购买 nike

我正在尝试从 Python 3 读取 rosbag 文件。
我安装了 ROS2 (Eloquent Elusor),它应该支持 Python 3。

当我跑

import rosbag
bag = rosbag.Bag('test.bag')

从 Python 2.7 开始,它可以工作。
当我在 Python 3 中尝试相同时,我得到:
ModuleNotFoundError: No module named 'rosbag'

我也试过这样的事情: sudo apt install python-rosbag , sudo apt install python3-rospkgpip3 install rospkg ,但他们没有帮助。

我应该怎么做才能从 Python 3 打开 rosbag 文件?

[编辑]
这是调用 pip3 install rospkg后的输出:
Requirement already satisfied: rospkg in ./rosbag-env/lib/python3.6/site-packages
Requirement already satisfied: catkin-pkg in ./rosbag-env/lib/python3.6/site-packages (from rospkg)
Requirement already satisfied: distro in ./rosbag-env/lib/python3.6/site-packages (from rospkg)
Requirement already satisfied: PyYAML in ./rosbag-env/lib/python3.6/site-packages (from rospkg)
Requirement already satisfied: pyparsing in ./rosbag-env/lib/python3.6/site-packages (from catkin-pkg->rospkg)
Requirement already satisfied: python-dateutil in ./rosbag-env/lib/python3.6/site-packages (from catkin-pkg->rospkg)
Requirement already satisfied: docutils in ./rosbag-env/lib/python3.6/site-packages (from catkin-pkg->rospkg)
Requirement already satisfied: six>=1.5 in ./rosbag-env/lib/python3.6/site-packages (from python-dateutil->catkin-pkg->rospkg)

最佳答案

您可以使用 bagpy包以在 Python 中读取 .bag 文件。可以使用pip安装

pip install bagpy
简要文档位于 https://jmscslgroup.github.io/bagpy/
以下是示例代码片段:
import bagpy
from bagpy import bagreader

b = bagreader('09-23-59.bag')

# get the list of topics
print(b.topic_table)

# get all the messages of type velocity
velmsgs = b.vel_data()
veldf = pd.read_csv(velmsgs[0])
plt.plot(veldf['Time'], veldf['linear.x'])

# quickly plot velocities
b.plot_vel(save_fig=True)

# you can animate a timeseries data
bagpy.animate_timeseries(veldf['Time'], veldf['linear.x'], title='Velocity Timeseries Plot')

关于python - 在 Python 3 中导入 Rosbag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794328/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com