detector_node.cpp
Go to the documentation of this file.
00001 #include <blort_ros/detector_node.h>
00002 
00003 DetectorNode::DetectorNode(std::string root)
00004   : nh_("blort_detector"), it_(nh_), root_(root), detector(0)
00005 {
00006   cam_info_sub = nh_.subscribe("/blort_camera_info", 1, &DetectorNode::cam_info_callback, this);
00007   image_pub = it_.advertise("image_result", 1);
00008   //debug_pub = it_.advertise("image_debug", 1);
00009   cam_info_service = nh_.advertiseService("set_camera_info", &DetectorNode::setCameraInfoCb, this);
00010   counter = 0;
00011 
00012   nh_.param<double>("nn_match_threshold", nn_match_threshold, 0.65); //0.55
00013   nh_.param<int>("ransac_n_points_to_match", ransac_n_points_to_match, 4); //4
00014 
00015 }
00016 
00017 DetectorNode::~DetectorNode()
00018 {
00019   if(detector != 0)
00020     delete(detector);
00021 }
00022 
00023 bool DetectorNode::recovery(blort_msgs::RecoveryCall::Request &req,
00024                             blort_msgs::RecoveryCall::Response &resp)
00025 {
00026   if(detector != 0)
00027   {
00028     ROS_INFO("Detector called the %u-th time.", ++counter);
00029     bool result;
00030     resp.object_ids = req.object_ids;
00031     std::vector<std::string> object_ids;
00032     for(size_t i = 0; i < req.object_ids.size(); ++i)
00033     {
00034       object_ids.push_back(req.object_ids[i]);
00035     }
00036     if(!req.Image.data.empty())
00037     {
00038       cv_bridge::CvImagePtr cv_ptr;
00039       try
00040       {
00041         cv_ptr = cv_bridge::toCvCopy(req.Image, sensor_msgs::image_encodings::BGR8);
00042         ROS_INFO_STREAM("\n\nENCODING = " << req.Image.encoding << "\n\n\n");
00043       }
00044       catch (cv_bridge::Exception& e)
00045       {
00046         ROS_ERROR("cv_bridge exception: %s", e.what());
00047         return false;
00048       }
00049       result = detector->recovery(object_ids, cv_ptr->image, resp);
00050     } else {
00051       ROS_INFO("Running detector on latest image.");
00052       result = detector->recoveryWithLast(object_ids, resp);
00053     }
00054     cv_bridge::CvImage out_msg;
00055     out_msg.header = req.Image.header;
00056     out_msg.header.stamp = ros::Time::now();
00057     out_msg.encoding = sensor_msgs::image_encodings::BGR8;
00058     out_msg.image = detector->getImage();
00059     image_pub.publish(out_msg.toImageMsg());
00060     ROS_INFO_STREAM("= > detector_node returned " << result);
00061 
00062     return result;
00063   } else {
00064     ROS_ERROR("blort_detector service called while the core is uninitialized.");
00065     return false;
00066   }
00067 }
00068 
00069 bool DetectorNode::setCameraInfoCb(blort_msgs::SetCameraInfo::Request &req,
00070                                    blort_msgs::SetCameraInfo::Response &resp)
00071 {
00072   if(detector == 0)
00073     cam_info_callback(req.CameraInfo);
00074   return true;
00075 }
00076 
00077 void DetectorNode::reconf_callback(blort_ros::DetectorConfig &config, uint32_t level)
00078 {
00079   if(detector != 0)
00080   {
00081     detector->reconfigure(config);
00082     ROS_INFO("Detector confidence threshold set to %f", config.recovery_conf_threshold);
00083   } else {
00084     ROS_WARN("Please publish camera_info for the tracker initialization.");
00085   }
00086 }
00087 
00088 void DetectorNode::cam_info_callback(const sensor_msgs::CameraInfo &msg)
00089 {
00090   if(detector == 0)
00091   {
00092     ROS_INFO("Camera parameters received, ready to run.");
00093     cam_info_sub.shutdown();
00094     detector = new blort_ros::GLDetector(msg, root_);
00095     if(nn_match_threshold != 0.0)
00096       detector->setNNThreshold(nn_match_threshold);
00097     if(ransac_n_points_to_match != 0)
00098       detector->setRansacNPointsToMatch(ransac_n_points_to_match);
00099     pose_service = nh_.advertiseService("pose_service", &DetectorNode::recovery, this);
00100     // lines for dynamic_reconfigure
00101     server_ = std::auto_ptr<dynamic_reconfigure::Server<blort_ros::DetectorConfig> >
00102         (new dynamic_reconfigure::Server<blort_ros::DetectorConfig>());
00103     f_ = boost::bind(&DetectorNode::reconf_callback, this, _1, _2);
00104     server_->setCallback(f_);
00105   }
00106 }
00107 
00108 int main(int argc, char *argv[] )
00109 {
00110   if(argc < 2)
00111   {
00112     ROS_ERROR("The first command line argument should be the package root!");
00113     return -1;
00114   }
00115   ros::init(argc, argv, "blort_detector");
00116   DetectorNode node(argv[1]);
00117   ros::spin();
00118   return 0;
00119 }
00120 


blort_ros
Author(s): Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:39