2 #include <boost/foreach.hpp> 5 #include <opencv-3.3.1-dev/opencv2/opencv.hpp> 6 #include <opencv-3.3.1-dev/opencv2/videoio.hpp> 10 #include <rqt_bag_exporter/CloseBag.h> 11 #include <rqt_bag_exporter/EstimateVideoFps.h> 12 #include <rqt_bag_exporter/ExportToCSVAction.h> 13 #include <rqt_bag_exporter/ExportToVideoAction.h> 14 #include <rqt_bag_exporter/GetDuration.h> 15 #include <rqt_bag_exporter/ListTopics.h> 16 #include <rqt_bag_exporter/OpenBag.h> 17 #include <sensor_msgs/CompressedImage.h> 18 #include <std_msgs/Bool.h> 19 #include <std_msgs/Duration.h> 20 #include <std_msgs/Float32.h> 21 #include <std_msgs/Float64.h> 22 #include <std_msgs/Int16.h> 23 #include <std_msgs/Int32.h> 24 #include <std_msgs/Int64.h> 25 #include <std_msgs/Int8.h> 26 #include <std_msgs/String.h> 27 #include <std_msgs/Time.h> 28 #include <std_msgs/UInt16.h> 29 #include <std_msgs/UInt32.h> 30 #include <std_msgs/UInt64.h> 31 #include <std_msgs/UInt8.h> 34 std::shared_ptr<rosbag::Bag>
bag;
40 rqt_bag_exporter::ExportToVideoGoal goal(*gh.
getGoal());
41 rqt_bag_exporter::ExportToVideoResult result;
42 rqt_bag_exporter::ExportToVideoFeedback feedback;
44 if (!
bag ||
bag->getSize() <= 0)
46 result.error =
"No valid bag file is opened, call open_bag service to open one.";
47 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
53 int pos(file_name.find_last_of(
"."));
54 file_name.erase(pos, std::string::npos);
55 pos = file_name.find_last_of(
"/");
56 file_name.erase(0, pos + 1);
57 std::string topic_name(goal.topic_name);
58 pos = topic_name.find_first_of(
"/");
59 topic_name.erase(0, pos + 1);
60 std::replace(topic_name.begin(), topic_name.end(),
'/',
'_');
62 std::vector<std::string> topics;
63 topics.push_back(goal.topic_name);
67 result.error =
"Topic '" + goal.topic_name +
"' doesn't exist in this bag file";
68 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
73 feedback.progress_msg =
"Starting...";
74 feedback.progress_value = 0;
77 cv::VideoWriter video;
81 const std::string video_name(goal.directory +
"/" + file_name +
"_" + topic_name +
".mkv");
84 sensor_msgs::CompressedImage::ConstPtr
s = first.
instantiate<sensor_msgs::CompressedImage>();
89 cv::Mat image = cv::imdecode(s->data, 1);
90 if (!video.open(video_name, cv::VideoWriter::fourcc(
'X',
'2',
'6',
'4'), goal.fps, image.size()))
92 result.error =
"Cannot open video file for writing";
93 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
100 result.error =
"Could not convert image";
101 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
111 if (gh.
getGoalStatus().status != actionlib_msgs::GoalStatus::ACTIVE)
114 if ((rosbag_begin.toSec() + goal.start_time) > m.
getTime().
toSec())
117 if ((rosbag_begin.toSec() + goal.end_time) < m.
getTime().
toSec())
120 long unsigned real_time(m.
getTime().
sec - (rosbag_begin.sec + goal.start_time));
121 long unsigned coeficient(100 / (goal.end_time - goal.start_time));
122 long unsigned transmit_value(real_time * coeficient);
123 feedback.progress_msg =
"Exporting video topic " + goal.topic_name;
124 feedback.progress_value = transmit_value;
126 sensor_msgs::CompressedImage::ConstPtr
s = m.
instantiate<sensor_msgs::CompressedImage>();
131 cv::Mat image = cv::imdecode(s->data, 1);
136 result.error =
"Could not convert image";
142 feedback.progress_msg =
"Writing video file";
143 feedback.progress_value = 100;
147 feedback.progress_msg =
"Success";
148 feedback.progress_value = 100;
156 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE
157 || gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::PREEMPTING)
164 rqt_bag_exporter::ExportToCSVGoal goal;
165 rqt_bag_exporter::ExportToCSVResult result;
166 rqt_bag_exporter::ExportToCSVFeedback feedback;
169 if (!
bag ||
bag->getSize() <= 0)
171 result.error =
"No valid bag file is opened, call open_bag service to open one.";
172 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
177 std::vector<std::string> topics;
178 topics.push_back(goal.topic_name);
180 if (view.
size() == 0)
182 result.error =
"Topic '" + goal.topic_name +
"' doesn't exist in this bag file";
183 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
188 feedback.progress_msg =
"Starting CSV export";
189 feedback.progress_value = 0;
192 std::ofstream csv_file;
194 int pos = file_name.find_last_of(
".");
195 file_name.erase(pos, std::string::npos);
196 pos = file_name.find_last_of(
"/");
197 file_name.erase(0, pos + 1);
198 std::string topic_name(goal.topic_name);
199 pos = topic_name.find_first_of(
"/");
200 topic_name.erase(0, pos + 1);
201 std::replace(topic_name.begin(), topic_name.end(),
'/',
'_');
202 std::string path_file(goal.directory +
"/" + file_name +
"_" + topic_name +
".csv");
203 csv_file.open(path_file);
204 if (!csv_file.is_open())
206 result.error =
"Failed to open CSV file " + path_file;
207 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE)
211 csv_file <<
"Time, " << goal.topic_name << std::endl;
215 if (gh.
getGoalStatus().status != actionlib_msgs::GoalStatus::ACTIVE)
217 if ((rosbag_begin.toSec() + goal.start_time) > m.
getTime().
toSec())
221 if ((rosbag_begin.toSec() + goal.end_time) < m.
getTime().
toSec())
225 std::uint64_t real_time(m.
getTime().
sec - (rosbag_begin.sec + goal.start_time));
226 std::uint64_t coeficient(100 / (goal.end_time - goal.start_time));
227 std::uint64_t transmit_value(real_time * coeficient);
228 std::string transmit_msg(
"Exporting topic " + goal.topic_name);
229 feedback.progress_msg = transmit_msg;
230 feedback.progress_value = transmit_value;
232 double time(m.
getTime().
toSec() - (rosbag_begin.toSec() + goal.start_time));
234 std_msgs::Float32::ConstPtr ft_32 = m.
instantiate<std_msgs::Float32>();
236 csv_file << time <<
", " << ft_32->data << std::endl;
238 std_msgs::Float64::ConstPtr ft_64 = m.
instantiate<std_msgs::Float64>();
240 csv_file << time <<
", " << ft_64->data << std::endl;
242 std_msgs::Int8::ConstPtr int_8 = m.
instantiate<std_msgs::Int8>();
244 csv_file << time <<
", " << int_8->data << std::endl;
246 std_msgs::Int16::ConstPtr int_16 = m.
instantiate<std_msgs::Int16>();
248 csv_file << time <<
", " << int_16->data << std::endl;
250 std_msgs::Int32::ConstPtr int_32 = m.
instantiate<std_msgs::Int32>();
252 csv_file << time <<
", " << int_32->data << std::endl;
254 std_msgs::Int64::ConstPtr int_64 = m.
instantiate<std_msgs::Int64>();
256 csv_file << time <<
", " << int_64->data << std::endl;
258 std_msgs::UInt8::ConstPtr uint_8 = m.
instantiate<std_msgs::UInt8>();
260 csv_file << time <<
", " << uint_8->data << std::endl;
262 std_msgs::UInt16::ConstPtr uint_16 = m.
instantiate<std_msgs::UInt16>();
264 csv_file << time <<
", " << uint_16->data << std::endl;
266 std_msgs::UInt32::ConstPtr uint_32 = m.
instantiate<std_msgs::UInt32>();
268 csv_file << time <<
", " << uint_32->data << std::endl;
270 std_msgs::UInt64::ConstPtr uint_64 = m.
instantiate<std_msgs::UInt64>();
272 csv_file << time <<
", " << uint_64->data << std::endl;
274 std_msgs::Bool::ConstPtr Bool_ = m.
instantiate<std_msgs::Bool>();
276 csv_file << time <<
", " << (Bool_->data ?
"True" :
"False") << std::endl;
278 std_msgs::Duration::ConstPtr dur = m.
instantiate<std_msgs::Duration>();
280 csv_file << time <<
", " << dur->data << std::endl;
282 std_msgs::String::ConstPtr str = m.
instantiate<std_msgs::String>();
284 csv_file << time <<
", " << str->data << std::endl;
286 std_msgs::Time::ConstPtr time_ = m.
instantiate<std_msgs::Time>();
288 csv_file << time <<
", " << time_->data.toSec() << std::endl;
291 feedback.progress_msg =
"Writing CSV file";
292 feedback.progress_value = 100;
296 feedback.progress_msg =
"Sucess";
297 feedback.progress_value = 100;
307 if (gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::ACTIVE
308 || gh.
getGoalStatus().status == actionlib_msgs::GoalStatus::PREEMPTING)
312 bool openBag(rqt_bag_exporter::OpenBag::Request &req,
313 rqt_bag_exporter::OpenBag::Response &res)
316 if (req.bag_file.find(
".bag") == std::string::npos)
318 res.error =
"Not a bag file";
332 res.error = e.what();
340 bool closeBag(rqt_bag_exporter::CloseBag::Request &,
341 rqt_bag_exporter::CloseBag::Response &)
343 if (!
bag ||
bag->getSize() != 0)
351 rqt_bag_exporter::ListTopics::Response &res)
353 if (!
bag ||
bag->getSize() <= 0)
355 res.error =
"No valid bag file is opened, call open_bag service to open one.";
360 std::vector<const rosbag::ConnectionInfo *> connection_infos = view.
getConnections();
363 res.topics_name.push_back(info->
topic);
364 res.topics_type.push_back(info->
datatype);
370 rqt_bag_exporter::EstimateVideoFps::Response &res)
372 if (!
bag ||
bag->getSize() <= 0)
374 res.error =
"No valid bag file is opened, call open_bag service to open one.";
384 rqt_bag_exporter::GetDuration::Response &res)
386 if (!
bag ||
bag->getSize() <= 0)
388 res.error =
"No valid bag file is opened, call open_bag service to open one.";
400 ros::init(argc, argv,
"rqt_bag_exporter_servers");
410 action_server_1.
start();
417 action_server_2.
start();
void exportVideoCancelCb(actionlib::ServerGoalHandle< rqt_bag_exporter::ExportToVideoAction > gh)
void exportVideo(actionlib::ServerGoalHandle< rqt_bag_exporter::ExportToVideoAction > gh)
void publishFeedback(const Feedback &feedback)
std::vector< const ConnectionInfo * > getConnections()
std::string bag_file_name("")
std::shared_ptr< rosbag::Bag > bag
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::shared_ptr< const Goal > getGoal() const
void exportCsvCancelCb(actionlib::ServerGoalHandle< rqt_bag_exporter::ExportToCSVAction > gh)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void setCanceled(const Result &result=Result(), const std::string &text=std::string(""))
ros::Time const & getTime() const
void exportCsv(actionlib::ServerGoalHandle< rqt_bag_exporter::ExportToCSVAction > gh)
void setAccepted(const std::string &text=std::string(""))
bool closeBag(rqt_bag_exporter::CloseBag::Request &, rqt_bag_exporter::CloseBag::Response &)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
int main(int argc, char **argv)
boost::shared_ptr< T > instantiate() const
bool getDuration(rqt_bag_exporter::GetDuration::Request &, rqt_bag_exporter::GetDuration::Response &res)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
bool estimateVideoFps(rqt_bag_exporter::EstimateVideoFps::Request &req, rqt_bag_exporter::EstimateVideoFps::Response &res)
bool openBag(rqt_bag_exporter::OpenBag::Request &req, rqt_bag_exporter::OpenBag::Response &res)
bool listTopics(rqt_bag_exporter::ListTopics::Request &, rqt_bag_exporter::ListTopics::Response &res)
actionlib_msgs::GoalStatus getGoalStatus() const
ROSCPP_DECL void waitForShutdown()