2.23 使用python解析.bag数据集(无需虚拟机和ROS)

文章介绍了一种使用Python解析rosbag格式数据的方法,避免了通过安装rosbag软件或Docker在Linux环境下进行数据提取的复杂过程。提供了代码示例,该代码从rosbag文件中生成图像和steering_angles,适用于自动驾驶相关的数据处理。通过安装bagpy等相关库,可以在Python环境中直接处理rosbag数据,并将结果保存为图片和CSV文件。
摘要由CSDN通过智能技术生成

方法描述

rosbag格式的数据不能直接使用,需要读取出jpg图像和csv文件,网上的大多教程都是安装rosbag软件或者docker软件,然后在linux终端下执行一系列的命令进行提取,这太复杂!

找到一篇使用python解析rosbag数据的代码放在这里供大家参考:

Udacity CH2 数据集下载及python 解析
 

'''
Generate images and steering angles from hmb3.bag
Modified from
https://github.com/udacity/self-driving-car/blob/master/steering-models/evaluation/generator.py
'''
import argparse
import rosbag
from io import StringIO, BytesIO
 
from keras_preprocessing import image
from rosbag import bag
from scipy import misc
import numpy as np
import csv
import os
KEY_NAME = {
    '/vehicle/steering_report': 'steering',
    '/center_camera/image_color/c': 'image',
}
 
def update(msg, d):
    key = KEY_NAME.get(msg[0])
    if key is None: return
    d[key] = msg
 
def gen(bagpath):
 
    print("Getting bag")
 
    bag = rosbag.Bag(bagpath)
    print('Got bag')
 
    image = {}
    total = bag.get_message_count()
    count = 0
    for e in bag.read_messages():
 
        count += 1
        if count % 10000 == 0:
            print ( count / total)
        if e[0] in ['/center_camera/image_color/compressed']:
            #print(e)
            if len({'steering'} - set(image.keys())):
                continue
            if image['steering'][1].speed < 5.: continue
            s = BytesIO(e[1].data)
            img = misc.imread(s)
            yield img, np.copy(img), image['steering'][1].speed,\
                  image['steering'][1].steering_wheel_angle, e[2].to_nsec()
            last_ts = e[2].to_nsec()
        else:
            update(e, image)
 
 
if __name__ == '__main__':
    img_paths = image.list_pictures('hmb/testing/', ext='jpg') #解析出来的图片的保存路径
    data_iter = gen('./testing/HMB_3.bag')  # rosbag文件所在路径
    next(data_iter)
    with open('hmb/testing/hmb3_steering.csv', 'w') as hmb1csv: #保存解析出来的csv文件
        writer = csv.writer(hmb1csv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        writer.writerow([str.encode('timestamp'),str.encode('steering angle')])
 
        for image_pred, image_disp, speed, steering, ts in data_iter:
            misc.imsave('hmb/testing/'+ str(ts) + '.jpg', image_disp) #保存解析的图片
            #print(ts)
            csvcontent = []
            csvcontent.append(ts)
            csvcontent.append(steering)
            writer.writerow(csvcontent)

遇到问题

会遇到的问题就是如何安装rosbag?

import rosbag

经过试验好几个解决方法,最好就是结合以下步骤,按顺序安装:

pip install bagpy
pip install roslib -i https://rospypi.github.io/simple/
pip install genpy -i https://rospypi.github.io/simple/
pip install genmsg -i https://rospypi.github.io/simple/
pip install roslz4 -i https://rospypi.github.io/simple/
pip install pycrypto

如果遇到报错:VC14 则更改为

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pycryptodome
pip install rosbag -i https://rospypi.github.io/simple/

最后再安装rosbag

这里给出几个安装源:

清华大学 :https://pypi.tuna.tsinghua.edu.cn/simple/

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/

华中科技大学:http://pypi.hustunique.com/

豆瓣源:http://pypi.douban.com/simple/

腾讯源:http://mirrors.cloud.tencent.com/pypi/simple

华为镜像源:https://repo.huaweicloud.com/repository/pypi/simple/

格式:

pip install [包名] -i [pip源URL]

参考的解决方案:

conda虚拟环境使用rosbag等包

rosbag库的安装

.python 报错汇总-- pip install pycrypto2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值