在Ubuntu和Windows中使用相同的代码读取ros包文件会产生不同的格式

2024-06-25 06:51:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试使用python bagpy库从ros包中读取数据。我已经在Windows中编写了这方面的代码,但最近已切换到Ubuntu。如果我现在尝试在Ubuntu中运行相同的代码,则输出的格式不同。这是我用来读取数据的代码:

import pandas as pd 
import bagpy 
from bagpy import bagreader

class import_DVS:

    def __init__(self, dir): 
        #Directory of event data 
        self.dir = dir
        
        
    def read_data(self):
        
        #Reading data
        b = bagreader(self.dir)
        
        #Selecting relevant entries
        newmsg = b.message_by_topic(topic = 'raw_data')
        
            
        return newmsg


dir_event = 'path'
DVS_data = import_DVS(dir_event)
newmsg = DVS_data.read_data()

当我在Windows中运行此代码时,varibale'newmsg'是一个包含所有行李数据的列表。这是该列表的第一个条目:

newmsg[0]


header:
  seq:10
  stamp:
     secs: 1455208733
     nscecs: 468284623
  frame:_id: ´´
height: 180 
width: 240 
events: 
 -
     x: 90 
     y: 47 
     ts: 
       secs: 1455208733
       nsecs: 468284623
     polarity: False 
-
     x: 82
     y: 135
     ts: 
       secs: 1455208733
       nsecs: 468330623
     polarity: False 
-
     x: 73
     y: 150
     ts: 
       secs: 1455208733
       nsecs: 468333623
     polarity: False 
-
....


该列表包含另一个列表(事件),其中每个条目都有一组数据,我可以分别访问这些数据。使用newmsg[0].events[0].x e、 g.允许我访问事件列表第一个元素中的x

但是,当我在Ubuntu中运行相同的代码时,会返回一个csv文件:

newmsg 


'//home/yvonne/Documents/uni_stuff/thesis/code/DVS_data/08_17/DVS-IMU-RADAR/trial1/DVS_BAG/aw_data.csv'

我尝试通过读取带有熊猫的csv文件来访问事件列表中的元素,如下所示:

newmsg = pd.read_csv(newmsg)
newmsg.events[0]

'[x: 167\ny: 86\nts: \n  secs: 0\n  nsecs:  27729000\npolarity: False, x: 127\ny: 112\nts: \n  secs: 0\n  nsecs:  27729000\npolarity: False, x: 128\ny: 5\nts: \n  secs: 0\n  nsecs:  27730000\npolarity: False, x: 205\ny: 39\nts: \n  secs: 0\n  nsecs:  27730000\npolarity: False, x: 233\ny: 179\nts: \n  secs: 0\n  nsecs:  27731000\npolarity: False, x: 89\ny: 91\nts: \n  secs: 0\n  nsecs:  27731000\npolarity: False, x: 213\ny: 28\nts: \n  secs: 0\n  nsecs:  27733000\npolarity: False, x: 83\ny: 8\nts: \n  secs: 0\n  nsecs:  27734000\npolarity: False, x: 226\ny: 9\nts: \n  secs: 0\n  nsecs:  27734000\npolarity: False, x: 143\ny: 121\nts: \n  secs: 0\n  nsecs:  27735000\npolarity: False, x: 183\ny: 176\nts: \n  secs: 0\n  nsecs:  27735000\npolarity: False, x: 207\ny: 132\nts: \n  secs: 0\n  nsecs:  27736000\npolarity: False, x: 48\ny: 32\nts: \n  secs: 0\n  nsecs:  27736000\npolarity: False, x: 193\ny: 69\nts: \n  secs: 0\n  nsecs:  27739000\npolarity: False, x: 229\ny: 60\nts: \n......


这将返回一个包含所有事件数据的字符串,这样我就无法再访问单个条目。我完全不知道是什么导致了这种差异,因此,如果有人知道为什么会发生这种情况,或者我如何在不必遍历大量字符串的情况下仍然可以访问事件列表的各个元素,我将非常感激:)


Tags: 代码importselffalse列表datadir事件
1条回答
网友
1楼 · 发布于 2024-06-25 06:51:54

你有没有在Windows和Ubuntu中检查bagpy的版本

您可以通过键入pip list来检查版本,并检查的版本是什么 bagpy安装在您的Windows和Ubuntu中

根据目前的文件: https://jmscslgroup.github.io/bagpy/bagpy_example.html

调用message_by_topic返回CSV格式的解码消息的文件名。 因此,请在代码段后面添加以下两行代码:

import pandas as pd
df = pd.DataFrame(newmsg)

相关问题 更多 >