ObjectDetectorClass.cpp
Go to the documentation of this file.
00001 
00033 #include "ros/ros.h"
00034 #include <string>
00035 #include <vector>
00036 
00037 #include "ObjectDetectorClass.h"
00038 #include "ObjectDetectorProvider.h"
00039 #include "CameraBridge.h"
00040 #include "CameraBridgeFactory.h"
00041 
00042 #include "DUtils.h"
00043 
00044 using namespace std;
00045 
00046 // ---------------------------------------------------------------------------
00047 
00048 ObjectDetectorClass::ObjectDetectorClass(): 
00049   m_node_handle(NULL), m_provider(NULL)
00050 {
00051 }
00052 
00053 // ---------------------------------------------------------------------------
00054 
00055 ObjectDetectorClass::~ObjectDetectorClass()
00056 { 
00057   delete m_provider;
00058   delete m_node_handle;
00059 }
00060 
00061 // ---------------------------------------------------------------------------
00062 
00063 void ObjectDetectorClass::runObjectDetectorNode(int argc, char *argv[])
00064 {
00065   m_node_name = "ObjectDetector";
00066   ros::init(argc, argv, m_node_name.c_str());
00067 
00068         parseArgs(argc, argv);
00069         initiateNode();
00070         
00071         spinNode();
00072 }
00073 
00074 // ---------------------------------------------------------------------------  
00075 
00076 void ObjectDetectorClass::runObjectDetectorDummyNode(int argc, char *argv[],
00077   const std::vector<std::string> &local_paths)
00078 {
00079   m_node_name = "ObjectDetectorDummy";
00080   ros::init(argc, argv, m_node_name.c_str());
00081   
00082   parseArgs(argc, argv);
00083         initiateNode();
00084 
00085         vector<string>::const_iterator sit;
00086         for(sit = local_paths.begin(); sit != local_paths.end(); ++sit)
00087         {
00088           // load local model
00089           std_msgs::StringPtr uri(new std_msgs::String);
00090     uri->data = *sit;
00091     m_provider->TopicNewModel(uri);
00092         }
00093         
00094         spinNode();
00095 }
00096 
00097 // ---------------------------------------------------------------------------  
00098 
00099 void ObjectDetectorClass::parseArgs(int argc, char *argv[])
00100 {
00101   m_arguments.camera_model = "";
00102         m_arguments.debug_mode = false;
00103         //m_arguments.topic_name = "/camera_info";
00104         //m_arguments.color_model = "bw";
00105         
00106         // parse arguments
00107         int c;
00108         while ((c = getopt (argc, argv, "vdc:")) != -1)
00109         {
00110                 switch (c){
00111                         case 'd': 
00112                                 m_arguments.debug_mode = true;
00113                                 break;
00114                         
00115                         case 'c':
00116                                 m_arguments.camera_model = optarg;
00117                                 break;
00118                   
00119                   case 'v':
00120                     m_arguments.visualization_mode = true;
00121                     break;
00122                         
00123                         // Not used
00124                         //case 't':
00125                         //  m_arguments.topic_name = optarg;
00126                         //  break;
00127                         
00128                         // Not used
00129                         //case 'm': 
00130                         //  m_arguments.color_model = optarg;
00131                         //  break;
00132                 }
00133         }
00134         
00135         /*
00136         const string &s = m_arguments.color_model;
00137         if(s != "bw" && s != "rgb" && s != "bgr" && s != "bayer_bg" 
00138           && s != "bayer_gb" && s != "bayer_rg" && s != "bayer_gr")
00139         {
00140           ROS_WARN("Color model %s is not valid. Assuming %s", s.c_str(), "bayer_bg");
00141           m_arguments.color_model = "bayer_bg";
00142         }
00143         */
00144         
00145         if(!m_arguments.camera_model.empty())
00146   {
00147     if(!CameraBridgeFactory::IsValid(m_arguments.camera_model))
00148     {
00149       ROS_WARN("Camera %s is not known. Camera parameters will be retrieved "
00150         "from the camera info topic", m_arguments.camera_model.c_str() );
00151       m_arguments.camera_model = "";
00152     }
00153   }
00154         
00155 }
00156 
00157 // ---------------------------------------------------------------------------  
00158 
00159 void ObjectDetectorClass::initiateNode()
00160 {
00161         m_node_handle = new ros::NodeHandle;
00162 
00163         if(m_arguments.camera_model.empty())
00164         {
00165           m_provider = new ObjectDetectorProvider(*m_node_handle);
00166         }
00167         else
00168         {       
00169     CameraBridge camera = CameraBridgeFactory::Create(m_arguments.camera_model);
00170     m_provider = new ObjectDetectorProvider(*m_node_handle, camera);
00171   }
00172   m_provider->init();
00173 
00174   std::string debug_dir = "";
00175   if(m_arguments.debug_mode) createDebugDir(debug_dir);
00176 
00177   m_provider->SetDebugMode(m_arguments.debug_mode, debug_dir);
00178   m_provider->SetVisualizationMode(m_arguments.visualization_mode);
00179   
00180 
00181         if(m_arguments.camera_model.empty())
00182                 ROS_INFO("%s initiated, listening to topic for camera parameters", 
00183                   m_node_name.c_str());
00184         else
00185                 ROS_INFO("%s initiated with predefined parameters of camera %s", 
00186                   m_node_name.c_str(), m_arguments.camera_model.c_str());
00187 
00188   if(m_arguments.debug_mode)
00189           ROS_DEBUG("Debug mode is enabled under directory %s", debug_dir.c_str());
00190         else
00191     ROS_DEBUG("Debug mode is disabled");
00192 }
00193 
00194 // ---------------------------------------------------------------------------  
00195 
00196 void ObjectDetectorClass::spinNode()
00197 {
00198   ROS_INFO("%s is listening...", m_node_name.c_str());
00199   ros::spin();
00200 }
00201 
00202 // ---------------------------------------------------------------------------  
00203 
00204 void ObjectDetectorClass::createDebugDir(std::string &debug_dir) const
00205 {
00206   if(!DUtils::FileFunctions::DirExists("debug"))
00207     DUtils::FileFunctions::MkDir("debug");
00208   
00209   DUtils::Timestamp t(DUtils::Timestamp::CURRENT_TIME);
00210   debug_dir = "debug/debug_" + t.Format(true);
00211   
00212   DUtils::FileFunctions::MkDir(debug_dir.c_str());
00213 }
00214 
00215 // ---------------------------------------------------------------------------  
00216 
00217 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:01