免费做公众号的网站,十堰外贸网站建设,wordpress主题 幻灯片,wordpress博客名字背景#xff1a;最近在制作kimera的数据集#xff0c;尤其是运行semantic模块所需要的bag文件中有很多topic#xff0c;但是很多不知道topic中装的是什么数据#xff0c;及其格式#xff0c;所以我就想着怎么可以将bag中的topic都解析数来#xff0c;这样就能知道bag中都…背景最近在制作kimera的数据集尤其是运行semantic模块所需要的bag文件中有很多topic但是很多不知道topic中装的是什么数据及其格式所以我就想着怎么可以将bag中的topic都解析数来这样就能知道bag中都有啥了有网友提供了下面的代码我稍作了些修改可以在python3下可以顺利运行如果你的文件夹下有多个bag文件这个代码可以挨个处理这些bag文件先为每个bag文件创建一个相应的文件夹然后把bag文件拷贝进去然后将bag中的topic都解析为csv格式 #!/usr/bin/pythonThis script saves each topic in a bagfile as a csv.Accepts a filename as an optional argument. Operates on all bagfiles in current directory if no argument providedWritten by Nick Speal in May 2013 at McGill Universitys Aerospace Mechatronics Laboratory
www.speal.caSupervised by Professor Inna Sharf, Professor Meyer Nahonimport rosbag, sys, csv
import time
import string
import os #for file management make directory
import shutil #for file management, copy file#verify correct input arguments: 1 or 2
if (len(sys.argv) 2):print(invalid number of arguments: str(len(sys.argv)))print(should be 2: bag2csv.py and bagName)print(or just 1 : bag2csv.py)sys.exit(1)
elif (len(sys.argv) 2):listOfBagFiles [sys.argv[1]]numberOfFiles 1print(reading only 1 bagfile: str(listOfBagFiles[0]))
elif (len(sys.argv) 1):listOfBagFiles [f for f in os.listdir(.) if f[-4:] .bag] #get list of only bag files in current dir.numberOfFiles str(len(listOfBagFiles))print(reading all numberOfFiles bagfiles in current directory: \n)for f in listOfBagFiles:print(f)print(\n press ctrlc in the next 10 seconds to cancel \n)time.sleep(10)
else:print(bad argument(s): str(sys.argv)) #shouldnt really come upsys.exit(1)count 0
for bagFile in listOfBagFiles:count 1print(reading file str(count) of numberOfFiles : bagFile)#access bagbag rosbag.Bag(bagFile)bagContents bag.read_messages()bagName bag.filename#/home/yunlei/COOL/kalibr-cde/test/2020-06-13-11-57-29.bag#create a new directory string.rstrip(bagName, .bag)folder bagName.split(.bag)[0]try: #else already existsos.makedirs(folder)except:passshutil.copyfile(bagName, folder / bagName.split(/)[-1])#get list of topics from the baglistOfTopics []for topic, msg, t in bagContents:if topic not in listOfTopics:listOfTopics.append(topic)for topicName in listOfTopics:#Create a new CSV file for each topic folder / string.replace(topicName, /, _slash_) .csvfilename folder / topicName.split(/)[-1] .csvwith open(filename, w) as csvfile:filewriter csv.writer(csvfile, delimiter ,)firstIteration True #allows header rowfor subtopic, msg, t in bag.read_messages(topicName): # for each instant in time that has data for topicName#parse data from this instant, which is of the form of multiple lines of Name: value\n# - put it in the form of a list of 2-element listsmsgString str(msg)msgList msgString.split(\n)instantaneousListOfData []for nameValuePair in msgList:splitPair nameValuePair.split(:)for i in range(len(splitPair)): #should be 0 to 1splitPair[i] splitPair[i].strip()instantaneousListOfData.append(splitPair)#write the first row from the first element of each pairif firstIteration: # headerheaders [rosbagTimestamp] #first column headerfor pair in instantaneousListOfData:headers.append(pair[0])filewriter.writerow(headers)firstIteration False# write the value from each pair to the filevalues [str(t)] #first column will have rosbag timestampfor pair in instantaneousListOfData:if len(pair) 1:values.append(pair[1])filewriter.writerow(values)bag.close()
print(Done reading all numberOfFiles bag files.)