gldetector.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Modified BSD License)
00003  *
00004  *  Copyright (c) 2012, PAL Robotics, S.L.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of PAL Robotics, S.L. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * @file gldetector.cpp
00035  * @author Bence Magyar
00036  * @date June 2012
00037  * @version 0.1
00038  * @brief Class of GLDetector which wraps the detector core of BLORT.
00039 */
00040 
00041 #include <blort_ros/gldetector.h>
00042 #include <blort/Tracker/utilities.hpp>
00043 #include <blort/TomGine/tgModelLoader.h>
00044 #include <ros/console.h>
00045 #include <sstream>
00046 #include <iostream>
00047 #include <blort/blort/pal_util.h>
00048 #include <boost/foreach.hpp>
00049 
00050 using namespace blort_ros;
00051 
00052 GLDetector::GLDetector(const sensor_msgs::CameraInfo& camera_info, const std::string& config_root)
00053 {
00054   //this line should force opengl to run software rendering == no GPU
00055   //putenv("LIBGL_ALWAYS_INDIRECT=1");
00056 
00057   rec3dcounter = 0;
00058   recovery_conf_threshold = 0.05;
00059 
00060   //FIXME: make these ROS parameters or eliminate them and use the content as parameters
00061   std::string tracking_ini(blort_ros::addRoot("config/tracking.ini", config_root));
00062   std::vector<std::string> ply_models(0), sift_files(0), model_names(0);
00063   GetPlySiftFilenames(tracking_ini.c_str(), ply_models, sift_files, model_names);
00064   blort::buildFromFiles(ply_models, sift_files, model_names, objects, sift_index);
00065 
00066   recognizer = boost::shared_ptr<blortRecognizer::Recognizer3D>(
00067         new blortRecognizer::Recognizer3D(blortRecognizer::CameraParameter(camera_info), config_root, true));
00068   sift_files_count = 0;
00069   for(size_t i = 0; i < objects.size(); ++i)
00070   {
00071     for(size_t j = 0; j < objects[i].recog_data.size(); ++j)
00072     {
00073       recognizer->loadModelFromFile("Resources/sift/" + objects[i].recog_data[j].sift_file);
00074       sift_files_count++;
00075     }
00076   }
00077   image_ = cvCreateImage( cvSize(camera_info.width, camera_info.height), 8, 3 );
00078 }
00079 
00080 bool GLDetector::recovery(std::vector<std::string> & obj_ids, const cv::Mat& image,
00081                           blort_msgs::RecoveryCall::Response &resp)
00082 {
00083   last_image = image;
00084   *image_ = last_image;
00085 
00086   return recoveryWithLast(obj_ids, resp);
00087 }
00088 
00089 bool GLDetector::recoveryWithLast(std::vector<std::string> & obj_ids,
00090                                   blort_msgs::RecoveryCall::Response &resp)
00091 {
00092   double ticksBefore = cv::getTickCount();
00093 
00094   std::map< std::string, boost::shared_ptr<TomGine::tgPose> > recPoses;
00095   std::map<std::string, double> confs;
00096   std::map<std::string, bool> select;
00097   BOOST_FOREACH(const blort::ObjectEntry& obj, objects)
00098   {
00099     // initialize a pose and set the select flag for each sift file
00100     for(size_t j = 0; j < obj.recog_data.size(); ++j)
00101     {
00102       recPoses[obj.recog_data[j].sift_file] =
00103           boost::shared_ptr<TomGine::tgPose>(new TomGine::tgPose());
00104       // if the object is in the query, select flag will be true, false otherwise
00105       if(std::find(obj_ids.begin(), obj_ids.end(), obj.name) != obj_ids.end())
00106       {
00107         select[obj.recog_data[j].sift_file] = true;
00108         ROS_ERROR_STREAM("Recoverycall for sift file: " << obj.recog_data[j].sift_file);
00109       }
00110       else
00111       {
00112         select[obj.recog_data[j].sift_file] = false;
00113       }
00114     }
00115   }
00116   recognizer->recognize(image_, recPoses, confs, select);
00117 
00118   bool found_one = false;
00119   resp.object_founds.resize(obj_ids.size());
00120   resp.Poses.resize(obj_ids.size());
00121   for(size_t i = 0; i < objects.size(); ++i)
00122   {
00123     blort::RecogData recog = blort::getBest(objects[i]);
00124     ROS_INFO_STREAM("object (" << objects[i].name
00125                     << ") conf: " << confs[recog.sift_file]);
00126     // if the recovery's confidence is high enough then propose this new pose
00127     const bool found = ( confs[recog.sift_file] > recovery_conf_threshold );
00128     if(found)
00129     {
00130       found_one = true;
00131       resp.object_ids.push_back(objects[i].name);
00132       resp.object_founds.push_back(true);
00133       resp.Poses.push_back(blort_ros::tgPose2RosPose(*(recPoses[recog.sift_file])));
00134     }
00135   }
00136   ROS_WARN_STREAM("Tried to recover for the " << rec3dcounter++ << ". time.");
00137   ROS_INFO_STREAM("Recovery execution time: "
00138                   << 1000*(cv::getTickCount() - ticksBefore)/cv::getTickFrequency()
00139                   << " ms");
00140   return found_one;
00141 }
00142 
00143 void GLDetector::reconfigure(blort_ros::DetectorConfig config)
00144 {
00145   recovery_conf_threshold = config.recovery_conf_threshold;
00146 }
00147 
00148 cv::Mat GLDetector::getImage()
00149 {
00150   cv::Mat tmp;
00151   tmp = recognizer->getImage();
00152   return tmp.empty()?last_image:tmp; // do we need a copy?
00153 }
00154 
00155 cv::Mat GLDetector::getDebugImage()
00156 {
00157   cv::Mat tmp;
00158   tmp = recognizer->getDebugImage();
00159   return tmp;
00160 }
00161 
00162 GLDetector::~GLDetector()
00163 {
00164   cvReleaseImage(&image_);
00165 }


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