$search
00001 #include "vfh_recognizer_fs/vfh_recognition.h" 00002 #include <pcl/filters/statistical_outlier_removal.h> 00003 #include <pcl/filters/radius_outlier_removal.h> 00004 #include <fstream> 00005 #include <iostream> 00006 00007 namespace vfh_recognizer_fs 00008 { 00009 template<template<class > class Distance> 00010 bool 00011 VFHRecognizerFS<Distance>::getPointCloudFromId (pcl::PointCloud<pcl::PointNormal> & cloud, std::string id) 00012 { 00013 /*std::stringstream view_file; 00014 view_file << root_dir << "views/view_" << id << ".pcd"; 00015 pcl::io::loadPCDFile (view_file.str (), cloud);*/ 00016 00017 //get the vfh - view map 00018 size_t last_found; 00019 last_found=id.find_last_of("_"); 00020 00021 std::string model_id = id.substr(0,last_found); 00022 00023 std::stringstream vfh_view_map; 00024 vfh_view_map << root_dir << "vfh_views_map/vfh_view_" << id << ".txt"; 00025 00026 std::ifstream in; 00027 in.open (vfh_view_map.str ().c_str (), std::ifstream::in); 00028 00029 char linebuf[256]; 00030 in.getline (linebuf, 256); 00031 std::string line (linebuf); 00032 00033 std::vector < std::string > strs; 00034 boost::split (strs, line, boost::is_any_of (" ")); 00035 00036 std::stringstream view_file; 00037 view_file << root_dir << "views/view_" << model_id << "_" << strs[0] << ".pcd"; 00038 std::cout << "Going to get point cloud" << view_file.str() << std::endl; 00039 pcl::io::loadPCDFile(view_file.str(), cloud); 00040 00041 return (true); 00042 } 00043 00044 template<template<class > class Distance> 00045 bool 00046 VFHRecognizerFS<Distance>::getVFHHistogramFromVFHId (pcl::PointCloud<pcl::VFHSignature308> & vfh_descriptor, 00047 std::string vfh_id) 00048 { 00049 00050 std::stringstream vfh_file; 00051 vfh_file << root_dir << "vfhs/vfh_" << vfh_id << ".pcd"; 00052 pcl::io::loadPCDFile (vfh_file.str (), vfh_descriptor); 00053 return (true); 00054 } 00055 00056 template<template<class > class Distance> 00057 bool 00058 VFHRecognizerFS<Distance>::getVFHRollOrientationFromId ( 00059 pcl::PointCloud<pcl::VFHSignature308> & vfh_orientation_signature, 00060 std::string id) 00061 { 00062 std::stringstream vfh_roll_file; 00063 vfh_roll_file << root_dir << "vfhs_roll_orientation/vfh_orientation_" << id << ".pcd"; 00064 pcl::io::loadPCDFile (vfh_roll_file.str (), vfh_orientation_signature); 00065 return (true); 00066 } 00067 00068 template<template<class > class Distance> 00069 bool 00070 VFHRecognizerFS<Distance>::getVFHRollOrientationFromIdThroughView ( 00071 pcl::PointCloud<pcl::VFHSignature308> & vfh_orientation_signature, 00072 std::string vfh_id) 00073 { 00074 return false; 00075 } 00076 00077 template<template<class > class Distance> 00078 bool 00079 VFHRecognizerFS<Distance>::getVFHCentroidFromVFHid (std::vector<float> & centroid, std::string id) 00080 { 00081 std::stringstream centroid_file; 00082 centroid_file << root_dir << "centroids/centroid_" << id << ".txt"; 00083 std::ifstream in; 00084 in.open (centroid_file.str ().c_str (), std::ifstream::in); 00085 00086 char linebuf[256]; 00087 in.getline (linebuf, 256); 00088 std::string line (linebuf); 00089 00090 std::cout << line << std::endl; 00091 00092 std::vector < std::string > strs; 00093 boost::split (strs, line, boost::is_any_of (" ")); 00094 00095 centroid[0] = atof (strs[0].c_str ()); 00096 centroid[1] = atof (strs[1].c_str ()); 00097 centroid[2] = atof (strs[2].c_str ()); 00098 return false; 00099 } 00100 00101 template<template<class > class Distance> 00102 bool 00103 VFHRecognizerFS<Distance>::getViewCentroidFromVFHid (std::vector<float> & centroid, std::string id) 00104 { 00105 return false; 00106 } 00107 00108 template<template<class > class Distance> 00109 bool 00110 VFHRecognizerFS<Distance>::getPoseFromId (std::string id, geometry_msgs::Pose & pose) 00111 { 00112 00113 //read from file 00114 std::stringstream transform_file; 00115 transform_file << root_dir << "transforms/transform_" << id << ".txt"; 00116 std::ifstream in; 00117 in.open (transform_file.str ().c_str (), std::ifstream::in); 00118 00119 char linebuf[256]; 00120 in.getline (linebuf, 256); 00121 std::string line (linebuf); 00122 00123 std::cout << line << std::endl; 00124 00125 std::vector < std::string > strs; 00126 boost::split (strs, line, boost::is_any_of (" ")); 00127 00128 pose.position.x = atof (strs[0].c_str ()); 00129 pose.position.y = atof (strs[1].c_str ()); 00130 pose.position.z = atof (strs[2].c_str ()); 00131 00132 pose.orientation.x = atof (strs[3].c_str ()); 00133 pose.orientation.y = atof (strs[4].c_str ()); 00134 pose.orientation.z = atof (strs[5].c_str ()); 00135 pose.orientation.w = atof (strs[6].c_str ()); 00136 00137 in.close (); 00138 return true; 00139 } 00140 00141 template<template<class > class Distance> 00142 std::string 00143 VFHRecognizerFS<Distance>::getModelId (std::string id) 00144 { 00145 //TODO: should split id and get the model_id 00146 return id; 00147 } 00148 00149 template<template<class > class Distance> 00150 int 00151 VFHRecognizerFS<Distance>::getVFHId (std::string id) 00152 { 00153 return atoi (id.c_str ()); 00154 } 00155 ; 00156 00157 /*template <template<class> class Distance> 00158 int VFHRecognizerFS< Distance >::initializeFromViews(std::vector<boost::shared_ptr<household_objects_database::DatabaseView> > & views) { 00159 int minid = vfh_recognizer_db::buildTreeFromViews (models_, data_, views); 00160 index_ = new flann::Index<DistT> (data_, flann::LinearIndexParams ()); 00161 index_->buildIndex (); 00162 return minid; 00163 }*/ 00164 00165 template<template<class > class Distance> 00166 bool 00167 VFHRecognizerFS<Distance>::initialize (bool reloadFiles, bf::path dpath, std::string root_dir, int linear) 00168 { 00169 bf::path training_data_h5_file_name = dpath / "training_data.h5"; 00170 bf::path training_data_list_file_name = dpath / "training_data.list"; 00171 bf::path index_filename = dpath / "kdtree.idx"; 00172 00173 this->root_dir = root_dir; 00174 00175 if (reloadFiles || !boost::filesystem::exists (training_data_h5_file_name) 00176 || !boost::filesystem::exists (training_data_list_file_name) || !boost::filesystem::exists (index_filename)) 00177 { 00178 //read training data from DB and build kdtree 00179 std::cout << "Calling tree build from files" << std::endl; 00180 std::string vfhs_dir = root_dir + "vfhs"; 00181 vfh_recognizer_fs::buildTreeFromFiles (models_, data_, vfhs_dir); 00182 std::cout << "Tree build from DB:" << models_.size () << " " << data_.rows << std::endl; 00183 if (linear == 0) 00184 { 00185 index_ = new flann::Index<DistT> (data_, flann::LinearIndexParams ()); 00186 } 00187 else 00188 { 00189 index_ = new flann::Index<DistT> (data_, flann::KDTreeIndexParams (4)); 00190 } 00191 index_->buildIndex (); 00192 00193 if (reloadFiles) 00194 { 00195 //files could exists, try to delete them 00196 if (boost::filesystem::exists (training_data_h5_file_name)) 00197 boost::filesystem::remove (training_data_h5_file_name); 00198 00199 if (boost::filesystem::exists (training_data_list_file_name)) 00200 boost::filesystem::remove (training_data_list_file_name); 00201 00202 if (boost::filesystem::exists (index_filename)) 00203 boost::filesystem::remove (index_filename); 00204 } 00205 00206 if (!boost::filesystem::exists (dpath)) 00207 { 00208 ROS_WARN ("%s does not exist. Not able to save training_data!", dpath.string ().c_str ()); 00209 } 00210 else 00211 { 00212 //SAVE the kdtree + training_list 00213 flann::save_to_file (data_, training_data_h5_file_name.string ().c_str (), "training_data"); 00214 saveFileList (models_, training_data_list_file_name.string ().c_str ()); 00215 index_->save (index_filename.string ().c_str ()); 00216 } 00217 } 00218 else 00219 { 00220 loadFileList (models_, training_data_list_file_name.string ()); 00221 flann::load_from_file (data_, training_data_h5_file_name.string (), "training_data"); 00222 std::cout << "Training data found. Loaded" << (int)data_.rows << "VFH models from:" << 00223 training_data_h5_file_name.string () << training_data_list_file_name.string () << std::endl; 00224 00225 index_ = new flann::Index<DistT> (data_, flann::SavedIndexParams (index_filename.string ().c_str ())); 00226 index_->buildIndex (); 00227 } 00228 00229 return true; 00230 } 00231 00232 template<template<class > class Distance> 00233 bool 00234 VFHRecognizerFS<Distance>::detect (const sensor_msgs::PointCloud2ConstPtr& msg, int nModels, 00235 std::vector<std::string>& model_ids, std::vector<geometry_msgs::Pose> & poses, 00236 std::vector<float>& confidences, bool use_fitness_score) 00237 { 00238 00239 std::vector < std::string > vfh_ids; 00240 this->detect_ (msg, nModels, model_ids, poses, confidences, vfh_ids, use_fitness_score); 00241 00242 return true; 00243 } 00244 00245 template class VFHRecognizerFS<flann::ChiSquareDistance> ; 00246 //template class VFHRecognizerFS<flann::L1> ; 00247 //template class VFHRecognizerFS<flann::L2> ; 00248 //template class VFHRecognizerFS<flann::L2_Simple> ; 00249 template class VFHRecognizerFS<flann::HistIntersectionDistance> ; 00250 template class VFHRecognizerFS<flann::HistIntersectionUnionDistance> ; 00251 //template class VFHRecognizerFS<flann::KL_Divergence> ; 00252 }