Skip to content
Snippets Groups Projects
Commit 99f1b35c authored by Houssaini Ziyad's avatar Houssaini Ziyad
Browse files

Add new directory containing python parser ros

parent fa7392cf
No related branches found
No related tags found
No related merge requests found
import os
import argparse
#La bibliothèque permettant de lire les fichiers rosbag (ROS1 Noetic)
from bagpy import bagreader
import pandas as pd
import shutil
def save_rosbags_topics(file_name : str, directory : str):
"""
This function export all the informations from the ROSBAG into a csv file
which contains the topics, the variable types, the number of samples and the frequency
samples. The most interesting part it is the topic name inorder to extract all the data
saved in this topic
"""
if(directory not in os.listdir()):
os.mkdir(directory)
else:
shutil.rmtree(directory)
os.mkdir(directory)
for content in file_name:
content_split = content.split("/")[-1]
#Case when you specify the directory containing the rosbags files or read directory the file
b = bagreader(content)
df_topics = b.topic_table
df_topics.to_csv(os.path.join(directory,content_split[:-5]+'.csv'))
print("Save the topics in csv format : {} and the name is {}".format(directory,content_split[:-5]+'.csv'))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Export all topics into a csv format")
parser.add_argument('filenames' , type=str , nargs='+' , help='Can support read one or multplies bagfile')
parser.add_argument('-d' , '--output_directory' , help='Folder Name to export all csv topics files')
args = parser.parse_args()
save_rosbags_topics(args.filenames , args.output_directory)
from scipy.io import savemat
from bagpy import bagreader
import bagpy
import pandas as pd
import os
import argparse
import shutil
def convert_topics_to_file(files , topics , directory):
if(directory not in os.listdir()):
os.mkdir(directory)
else:
shutil.rmtree(directory)
os.mkdir(directory)
for file in files:
b = bagreader(file)
for topic in topics:
try:
data_frame_string = b.message_by_topic(topic)
data_frame= pd.read_csv(data_frame_string)
file_split = file.split('/')[-1][:-4]
if(file_split not in os.listdir(directory)):
os.mkdir(os.path.join(directory , file_split))
else:
shutil.rmtree(os.path.join(directory , file_split))
os.mkdir(os.path.join(directory , file_split))
data_frame.to_csv(os.path.join(directory , file_split , file_split+'.csv'))
print("Convert the topic {} to a csv format in the directory {}".format(topic , os.path.join(directory , file_split)))
data_mat = data_frame.to_dict('list')
topic_name = topic.replace('/' , '_')
savemat(os.path.join(directory , file_split , file_split+'.mat') , data_mat)
print("Convert the topic {} to a matlab format in the directory {}".format(topic , os.path.join(directory , file_split+'.mat')))
except Exception as e:
print('Unable to read the topic or other probles ... {}'.format(e))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Export all topics into a csv format or matlab format")
parser.add_argument('files' , nargs='+' , type=str , help="Read the rosbag files")
parser.add_argument('-t', '--topics' , nargs='+' , help="Read different topics save to the rosbags")
parser.add_argument('-d' , '--directory_output' , help="Save the output data" )
args = parser.parse_args()
print(args.files , args.topics , args.directory_output)
convert_topics_to_file(args.files , args.topics , args.directory_output)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment