Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "video_capture_display.h"
00037 #include <rviz/display_context.h>
00038 #include <rviz/view_manager.h>
00039 #include <rviz/display_group.h>
00040 #include <rviz/display.h>
00041 #include <rviz/render_panel.h>
00042 #include <QImage>
00043
00044 namespace jsk_rviz_plugins
00045 {
00046 VideoCaptureDisplay::VideoCaptureDisplay():
00047 Display(), capturing_(false), first_time_(true)
00048 {
00049 start_capture_property_ = new rviz::BoolProperty(
00050 "start capture", false, "start capture",
00051 this, SLOT(updateStartCapture()));
00052 file_name_property_ = new rviz::StringProperty(
00053 "filename", "output.avi",
00054 "filename", this, SLOT(updateFileName()));
00055 fps_property_ = new rviz::FloatProperty(
00056 "fps", 30.0,
00057 "fps", this, SLOT(updateFps()));
00058 fps_property_->setMin(0.1);
00059 }
00060
00061 VideoCaptureDisplay::~VideoCaptureDisplay()
00062 {
00063 delete start_capture_property_;
00064 delete file_name_property_;
00065 }
00066
00067 void VideoCaptureDisplay::onInitialize()
00068 {
00069 updateFileName();
00070 updateFps();
00071
00072 start_capture_property_->setBool(false);
00073 context_->queueRender();
00074 }
00075
00076 void VideoCaptureDisplay::onEnable()
00077 {
00078 start_capture_property_->setBool(false);
00079 context_->queueRender();
00080 }
00081
00082 void VideoCaptureDisplay::updateFileName()
00083 {
00084 if (capturing_) {
00085 ROS_WARN("cannot change name wile recording");
00086 file_name_property_->setStdString(file_name_);
00087 }
00088 else {
00089 file_name_ = file_name_property_->getStdString();
00090 int access_result = access(file_name_.c_str(), W_OK);
00091 ROS_INFO("access_result to %s: %d", file_name_.c_str(), access_result);
00092 if (access_result) {
00093 setStatus(rviz::StatusProperty::Error, "File", "NOT Writable");
00094 }
00095 else {
00096 setStatus(rviz::StatusProperty::Ok, "File", "Writable");
00097 }
00098 }
00099 }
00100
00101 void VideoCaptureDisplay::updateStartCapture()
00102 {
00103 ROS_INFO("updateStartCapture");
00104 if (first_time_) {
00105 ROS_WARN("ignore first time capture enabling");
00106 }
00107 else {
00108
00109 if (start_capture_property_->getBool()) {
00110 capturing_ = true;
00111 startCapture();
00112 }
00113 else {
00114 capturing_ = false;
00115 stopCapture();
00116 }
00117 }
00118 }
00119
00120 void VideoCaptureDisplay::updateFps()
00121 {
00122 fps_ = fps_property_->getFloat();
00123 }
00124
00125 void VideoCaptureDisplay::startCapture()
00126 {
00127 ROS_INFO("start capturing");
00128 frame_counter_ = 0;
00129 rviz::RenderPanel* panel = context_->getViewManager()->getRenderPanel();
00130 int width = panel->width();
00131 int height = panel->height();
00132 writer_.open(file_name_, CV_FOURCC_DEFAULT, fps_, cv::Size(width, height));
00133 }
00134
00135 void VideoCaptureDisplay::stopCapture()
00136 {
00137 ROS_INFO("stop capturing");
00138 writer_.release();
00139 frame_counter_ = 0;
00140 }
00141
00142 void VideoCaptureDisplay::update(float wall_dt, float ros_dt)
00143 {
00144 if (first_time_) {
00145 ROS_WARN("force to disable capturing");
00146 start_capture_property_->setBool(false);
00147 first_time_ = false;
00148 return;
00149 }
00150 if (capturing_) {
00151 rviz::RenderPanel* panel = context_->getViewManager()->getRenderPanel();
00152 QPixmap screenshot
00153 = QPixmap::grabWindow(context_->getViewManager()->getRenderPanel()->winId());
00154 QImage src = screenshot.toImage().convertToFormat(QImage::Format_RGB888);
00155 cv::Mat image(src.height(), src.width(), CV_8UC3,
00156 (uchar*)src.bits(), src.bytesPerLine());
00157 writer_ << image;
00158 ++frame_counter_;
00159 if (frame_counter_ % 100 == 0) {
00160 ROS_INFO("taking %d frames as video", frame_counter_);
00161 }
00162 }
00163
00164 }
00165 }
00166
00167
00168 #include <pluginlib/class_list_macros.h>
00169 PLUGINLIB_EXPORT_CLASS(jsk_rviz_plugins::VideoCaptureDisplay, rviz::Display)
00170