Point Cloud Library (PCL)  1.14.1-dev
rf_face_detector_trainer.h
1 /*
2  * rf_face_detector_trainer.h
3  *
4  * Created on: 22 Sep 2012
5  * Author: Aitor Aldoma
6  */
7 
8 #pragma once
9 
10 #include "pcl/recognition/face_detection/face_detector_data_provider.h"
11 #include "pcl/recognition/face_detection/rf_face_utils.h"
12 #include "pcl/ml/dt/decision_forest.h"
13 
14 namespace pcl
15 {
17  {
18  private:
19  int w_size_ {80};
20  int max_patch_size_ {40};
21  int stride_sw_ {4};
22  int ntrees_ {10};
23  std::string forest_filename_ {"forest.txt"};
24  int nfeatures_ {10000};
25  float thres_face_ {1.f};
26  int num_images_ {1000};
27  float trans_max_variance_ {1600.f};
28  std::size_t min_votes_size_;
29  int used_for_pose_ {std::numeric_limits<int>::max ()};
30  bool use_normals_ {false};
31  std::string directory_;
32  float HEAD_ST_DIAMETER_ {0.2364f};
33  float larger_radius_ratio_ {1.5f};
34  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > head_center_votes_{};
35  std::vector<std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > > head_center_votes_clustered_{};
36  std::vector<std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > > head_center_original_votes_clustered_{};
37  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > angle_votes_{};
38  std::vector<float> uncertainties_{};
39  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > head_clusters_centers_{};
40  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > head_clusters_rotation_{};
41 
43  pcl::PointCloud<pcl::PointXYZI>::Ptr face_heat_map_{};
44 
47 
48  std::string model_path_ {"face_mesh.ply"};
49  bool pose_refinement_ {false};
50  int icp_iterations_{};
51 
52  pcl::PointCloud<pcl::PointXYZ>::Ptr model_original_{};
53  float res_ {0.005f};
54 
55  public:
56 
57  RFFaceDetectorTrainer() = default;
58 
59  virtual ~RFFaceDetectorTrainer() = default;
60 
61  /*
62  * Set name of the file in which RFFaceDetectorTrainer will store the forest.
63  */
64  void setForestFilename(std::string & ff)
65  {
66  forest_filename_ = ff;
67  }
68 
69  void setUseNormals(bool use)
70  {
71  use_normals_ = use;
72  }
73 
74  void setWSize(int s)
75  {
76  w_size_ = s;
77  }
78 
79  /*
80  * Training parameters
81  */
82 
83  void setDirectory(std::string & dir)
84  {
85  directory_ = dir;
86  }
87  void setNumTrainingImages(int num)
88  {
89  num_images_ = num;
90  }
91 
92  void setNumTrees(int num)
93  {
94  ntrees_ = num;
95  }
96 
97  void setNumFeatures(int num)
98  {
99  nfeatures_ = num;
100  }
101 
102  /*
103  * Detection parameters
104  */
105 
106  void setModelPath(std::string & model);
107 
108  void setPoseRefinement(bool do_it, int iters = 5)
109  {
110  pose_refinement_ = do_it;
111  icp_iterations_ = iters;
112  }
113 
115  {
116  thres_face_ = p;
117  }
118 
119  void setLeavesFaceMaxVariance(float max)
120  {
121  trans_max_variance_ = max;
122  }
123 
124  void setWStride(int s)
125  {
126  stride_sw_ = s;
127  }
128 
129  void setFaceMinVotes(int mv)
130  {
131  min_votes_size_ = mv;
132  }
133 
135  {
136  used_for_pose_ = n;
137  }
138 
139  /*
140  * This forest is used to detect faces.
141  */
143  {
144  forest_ = forest;
145  }
146 
147  /*
148  * Get functions
149  */
150 
152  {
153  heat_map = face_heat_map_;
154  }
155 
156  //get votes
158  {
159  votes_cloud->points.resize (head_center_votes_.size ());
160  votes_cloud->width = head_center_votes_.size ();
161  votes_cloud->height = 1;
162 
163  for (std::size_t i = 0; i < head_center_votes_.size (); i++)
164  {
165  (*votes_cloud)[i].getVector3fMap () = head_center_votes_[i];
166  }
167  }
168 
170  {
171  votes_cloud->points.resize (head_center_votes_.size ());
172  votes_cloud->width = head_center_votes_.size ();
173  votes_cloud->height = 1;
174 
175  int p = 0;
176  for (std::size_t i = 0; i < head_center_votes_clustered_.size (); i++)
177  {
178  for (std::size_t j = 0; j < head_center_votes_clustered_[i].size (); j++, p++)
179  {
180  (*votes_cloud)[p].getVector3fMap () = head_center_votes_clustered_[i][j];
181  (*votes_cloud)[p].intensity = 0.1f * static_cast<float> (i);
182  }
183  }
184 
185  votes_cloud->points.resize (p);
186  }
187 
189  {
190  votes_cloud->points.resize (head_center_votes_.size ());
191  votes_cloud->width = head_center_votes_.size ();
192  votes_cloud->height = 1;
193 
194  int p = 0;
195  for (std::size_t i = 0; i < head_center_original_votes_clustered_.size (); i++)
196  {
197  for (std::size_t j = 0; j < head_center_original_votes_clustered_[i].size (); j++, p++)
198  {
199  (*votes_cloud)[p].getVector3fMap () = head_center_original_votes_clustered_[i][j];
200  (*votes_cloud)[p].intensity = 0.1f * static_cast<float> (i);
201  }
202  }
203 
204  votes_cloud->points.resize (p);
205  }
206 
207  //get heads
208  void getDetectedFaces(std::vector<Eigen::VectorXf> & faces)
209  {
210  for (std::size_t i = 0; i < head_clusters_centers_.size (); i++)
211  {
212  Eigen::VectorXf head (6);
213  head[0] = head_clusters_centers_[i][0];
214  head[1] = head_clusters_centers_[i][1];
215  head[2] = head_clusters_centers_[i][2];
216  head[3] = head_clusters_rotation_[i][0];
217  head[4] = head_clusters_rotation_[i][1];
218  head[5] = head_clusters_rotation_[i][2];
219  faces.push_back (head);
220  }
221  }
222  /*
223  * Other functions
224  */
226  {
227  input_ = cloud;
228  }
229 
231  {
232  face_heat_map_ = heat_map;
233  }
234 
237  void detectFaces();
238  };
239 }
Class representing a decision forest.
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:395
void getVotes(pcl::PointCloud< pcl::PointXYZI >::Ptr &votes_cloud)
void getDetectedFaces(std::vector< Eigen::VectorXf > &faces)
void getFaceHeatMap(pcl::PointCloud< pcl::PointXYZI >::Ptr &heat_map)
void setDirectory(std::string &dir)
void setInputCloud(pcl::PointCloud< pcl::PointXYZ >::Ptr &cloud)
void setForestFilename(std::string &ff)
void setForest(pcl::DecisionForest< NodeType > &forest)
void setPoseRefinement(bool do_it, int iters=5)
void getVotes(pcl::PointCloud< pcl::PointXYZ >::Ptr &votes_cloud)
virtual ~RFFaceDetectorTrainer()=default
void setFaceHeatMapCloud(pcl::PointCloud< pcl::PointXYZI >::Ptr &heat_map)
void setModelPath(std::string &model)
void getVotes2(pcl::PointCloud< pcl::PointXYZI >::Ptr &votes_cloud)
#define PCL_EXPORTS
Definition: pcl_macros.h:325