human_assisted_object_detection_alg_node.cpp
Go to the documentation of this file.
00001 #include "human_assisted_object_detection_alg_node.h"
00002 
00003 HumanAssistedObjectDetectionAlgNode::HumanAssistedObjectDetectionAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<HumanAssistedObjectDetectionAlgorithm>(), 
00005   it_(this->public_node_handle_)
00006 {
00007   //init class attributes if necessary
00008   //this->loop_rate_ = 2;//in [Hz]
00009 
00010   // [init publishers]
00011   this->output_image_publisher_ = it_.advertise("output_image", 1);
00012   
00013   // [init subscribers]
00014   this->face_detect_subscriber_ = this->public_node_handle_.subscribe("face_detect", 100, &HumanAssistedObjectDetectionAlgNode::face_detect_callback, this);
00015   this->input_image_subscriber_ = it_.subscribe("input_image", 1, &HumanAssistedObjectDetectionAlgNode::input_image_callback, this);
00016   
00017   // [init services]
00018   this->learn_server_ = this->public_node_handle_.advertiseService("learn", &HumanAssistedObjectDetectionAlgNode::learnCallback, this);
00019   this->detect_server_ = this->public_node_handle_.advertiseService("detect", &HumanAssistedObjectDetectionAlgNode::detectCallback, this);
00020   this->init_server_ = this->public_node_handle_.advertiseService("init", &HumanAssistedObjectDetectionAlgNode::initCallback, this);
00021   
00022   // [init clients]
00023   
00024   // [init action servers]
00025   
00026   // [init action clients]
00027   this->receiving_detections=false;
00028   this->receiving_images=false;
00029 }
00030 
00031 HumanAssistedObjectDetectionAlgNode::~HumanAssistedObjectDetectionAlgNode(void)
00032 {
00033   // [free dynamic memory]
00034 }
00035 
00036 void HumanAssistedObjectDetectionAlgNode::mainNodeThread(void)
00037 {
00038   // [fill msg structures]
00039   //this->Image_msg_.data = my_var;
00040 
00041   // [fill srv structure and make request to the server]
00042   
00043   // [fill action structure and make request to the action server]
00044 
00045   // [publish messages]
00046 }
00047 
00048 /*  [subscriber callbacks] */
00049 void HumanAssistedObjectDetectionAlgNode::face_detect_callback(const face_detector_mono::RectArray::ConstPtr& msg) 
00050 { 
00051   //ROS_INFO("HumanAssistedObjectDetectionAlgNode::face_detect_callback: New Message Received");
00052   //use appropiate mutex to shared variables if necessary 
00053   //this->alg_.lock(); 
00054   this->face_detect_mutex_.enter(); 
00055 
00056   if(msg->rects.size()!=0)// there is at least one face detected
00057   {
00058     this->face_rects.rects.resize(msg->rects.size());
00059     for(unsigned int i=0; i<msg->rects.size(); i++)
00060     {
00061       this->face_rects.rects[i].x=msg->rects[i].x;
00062       this->face_rects.rects[i].width=msg->rects[i].width;
00063       this->face_rects.rects[i].y=msg->rects[i].y;
00064       this->face_rects.rects[i].height=msg->rects[i].height;
00065     }
00066     this->receiving_detections=true;
00067   } 
00068   else
00069   {
00070     this->receiving_detections=false;
00071   }
00072   //std::cout << msg->data << std::endl; 
00073 
00074   //unlock previously blocked shared variables 
00075   //this->alg_.unlock(); 
00076   this->face_detect_mutex_.exit(); 
00077 }
00078 void HumanAssistedObjectDetectionAlgNode::input_image_callback(const sensor_msgs::Image::ConstPtr& msg) 
00079 { 
00080   static bool first=true;
00081 
00082   //ROS_INFO("HumanAssistedObjectDetectionAlgNode::input_image_callback: New Message Received"); 
00083 
00084   //use appropiate mutex to shared variables if necessary 
00085   this->alg_.lock(); 
00086   this->input_image_mutex_.enter(); 
00087 
00088   //std::cout << msg->data << std::endl; 
00089   this->input_image=msg;
00090   if(first)
00091   {
00092     this->receiving_images=true;
00093     first=false;
00094   }
00095 
00096   //unlock previously blocked shared variables 
00097   this->input_image_mutex_.exit(); 
00098   this->alg_.unlock(); 
00099 }
00100 
00101 /*  [service callbacks] */
00102 bool HumanAssistedObjectDetectionAlgNode::learnCallback(iri_human_assisted_object_detection::haod_learn::Request &req, iri_human_assisted_object_detection::haod_learn::Response &res) 
00103 { 
00104   ROS_INFO("HumanAssistedObjectDetectionAlgNode::learnCallback: New Request Received!"); 
00105 
00106   //use appropiate mutex to shared variables if necessary 
00107   //this->alg_.lock(); 
00108   //this->learn_mutex_.enter(); 
00109 
00110   this->alg_.learn_detector(req.valid);
00111   this->alg_.get_output_image(this->Image_msg_);
00112   this->output_image_publisher_.publish(this->Image_msg_);
00113 
00114   //unlock previously blocked shared variables 
00115   //this->alg_.unlock(); 
00116   //this->learn_mutex_.exit(); 
00117 
00118   return true; 
00119 }
00120 bool HumanAssistedObjectDetectionAlgNode::detectCallback(iri_human_assisted_object_detection::haod_detect::Request &req, iri_human_assisted_object_detection::haod_detect::Response &res) 
00121 { 
00122   ROS_INFO("HumanAssistedObjectDetectionAlgNode::detectCallback: New Request Received!"); 
00123 
00124   //use appropiate mutex to shared variables if necessary 
00125   //this->alg_.lock(); 
00126   //this->detect_mutex_.enter(); 
00127 
00128   if(this->alg_.detect(this->input_image))// need help
00129   {
00130     res.success=true;
00131     ROS_INFO("HumanAssistedObjectDetectionAlgNode::detectCallback: assistance needed!");
00132   }
00133   else
00134   {
00135     res.success=false;
00136   }
00137   this->alg_.get_output_image(this->Image_msg_);
00138   this->alg_.get_output_rect(this->rect);
00139   res.x=rect.x;
00140   res.y=rect.y;
00141   this->output_image_publisher_.publish(this->Image_msg_);
00142 
00143   //unlock previously blocked shared variables 
00144   //this->alg_.unlock(); 
00145   //this->detect_mutex_.exit(); 
00146 
00147   return true; 
00148 }
00149 bool HumanAssistedObjectDetectionAlgNode::initCallback(iri_human_assisted_object_detection::haod_init::Request &req, iri_human_assisted_object_detection::haod_init::Response &res) 
00150 { 
00151 
00152   ROS_INFO("HumanAssistedObjectDetectionAlgNode::initCallback: New Request Received!"); 
00153   //use appropiate mutex to shared variables if necessary 
00154   //this->alg_.lock(); 
00155   //this->init_mutex_.enter(); 
00156 
00157   this->face_detect_mutex_.enter();
00158   if(this->receiving_detections && this->receiving_images)
00159   {
00160     for(unsigned int i=0;i<this->face_rects.rects.size();i++)
00161     {
00162       if(req.x > (this->face_rects.rects[i].x-this->face_rects.rects[i].width/2.0) && 
00163          req.x < (this->face_rects.rects[i].x+this->face_rects.rects[i].width/2.0) &&
00164          req.y > (this->face_rects.rects[i].y-this->face_rects.rects[i].height/2.0) && 
00165          req.y < (this->face_rects.rects[i].y+this->face_rects.rects[i].height/2.0))
00166       {
00167         if(this->alg_.init_detector(this->input_image,this->face_rects.rects[i]))// success
00168           res.success=true;
00169         else
00170          res.success=false; 
00171       }
00172       this->face_detect_mutex_.exit();
00173       return true;
00174     }
00175     res.success=false;
00176   }
00177   else
00178     res.success=false;
00179 
00180   //unlock previously blocked shared variables 
00181   //this->alg_.unlock(); 
00182   //this->init_mutex_.exit(); 
00183   this->face_detect_mutex_.exit();
00184   return true; 
00185 }
00186 
00187 /*  [action callbacks] */
00188 
00189 /*  [action requests] */
00190 
00191 void HumanAssistedObjectDetectionAlgNode::node_config_update(Config &config, uint32_t level)
00192 {
00193   this->alg_.lock();
00194 
00195   this->alg_.unlock();
00196 }
00197 
00198 void HumanAssistedObjectDetectionAlgNode::addNodeDiagnostics(void)
00199 {
00200 }
00201 
00202 /* main function */
00203 int main(int argc,char *argv[])
00204 {
00205   return algorithm_base::main<HumanAssistedObjectDetectionAlgNode>(argc, argv, "human_assisted_object_detection_alg_node");
00206 }


iri_human_assisted_object_detection
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 20:01:33