video_capture_display.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
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     //updateStartCapture();
00072     start_capture_property_->setBool(false); // always false when starting up
00073     context_->queueRender();
00074   }
00075 
00076   void VideoCaptureDisplay::onEnable()
00077   {
00078     start_capture_property_->setBool(false); // always false when starting up
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       // start capture!
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); // always false when starting up
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     // convert QPixmap into cv::Mat
00164   }
00165 }
00166 
00167 
00168 #include <pluginlib/class_list_macros.h>
00169 PLUGINLIB_EXPORT_CLASS(jsk_rviz_plugins::VideoCaptureDisplay, rviz::Display)
00170 


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Sun Sep 13 2015 22:29:03