Point Cloud Library (PCL)  1.8.0
face_detector_data_provider.h
1 /*
2  * face_detector_data_provider.h
3  *
4  * Created on: Sep 2, 2012
5  * Author: aitor
6  */
7 
8 #ifndef FACE_DETECTOR_DATA_PROVIDER_H_
9 #define FACE_DETECTOR_DATA_PROVIDER_H_
10 
11 #include "pcl/common/common.h"
12 #include "pcl/recognition/face_detection/face_common.h"
13 #include <pcl/ml/dt/decision_tree_data_provider.h>
14 #include <boost/filesystem/operations.hpp>
15 #include <iostream>
16 #include <fstream>
17 #include <string>
18 #include <boost/algorithm/string.hpp>
19 
20 namespace bf = boost::filesystem;
21 
22 namespace pcl
23 {
24  namespace face_detection
25  {
26  template<class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
27  class FaceDetectorDataProvider: public pcl::DecisionTreeTrainerDataProvider<FeatureType, DataSet, LabelType, ExampleIndex, NodeType>
28  {
29  private:
30  int num_images_;
31  std::vector<std::string> image_files_;
32  bool USE_NORMALS_;
33  int w_size_;
34  int patches_per_image_;
35  int min_images_per_bin_;
36 
37  void getFilesInDirectory(bf::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths, std::string & ext)
38  {
39  bf::directory_iterator end_itr;
40  for (bf::directory_iterator itr (dir); itr != end_itr; ++itr)
41  {
42  //check if its a directory, then get models in it
43  if (bf::is_directory (*itr))
44  {
45 #if BOOST_FILESYSTEM_VERSION == 3
46  std::string so_far = rel_path_so_far + (itr->path ().filename ()).string () + "/";
47 #else
48  std::string so_far = rel_path_so_far + (itr->path ()).filename () + "/";
49 #endif
50 
51  bf::path curr_path = itr->path ();
52  getFilesInDirectory (curr_path, so_far, relative_paths, ext);
53  } else
54  {
55  //check that it is a ply file and then add, otherwise ignore..
56  std::vector < std::string > strs;
57 #if BOOST_FILESYSTEM_VERSION == 3
58  std::string file = (itr->path ().filename ()).string ();
59 #else
60  std::string file = (itr->path ()).filename ();
61 #endif
62 
63  boost::split (strs, file, boost::is_any_of ("."));
64  std::string extension = strs[strs.size () - 1];
65 
66  if (extension.compare (ext) == 0)
67  {
68 #if BOOST_FILESYSTEM_VERSION == 3
69  std::string path = rel_path_so_far + (itr->path ().filename ()).string ();
70 #else
71  std::string path = rel_path_so_far + (itr->path ()).filename ();
72 #endif
73 
74  relative_paths.push_back (path);
75  }
76  }
77  }
78  }
79 
80  inline bool readMatrixFromFile(std::string file, Eigen::Matrix4f & matrix)
81  {
82 
83  std::ifstream in;
84  in.open (file.c_str (), std::ifstream::in);
85  if (!in.is_open ())
86  {
87  return false;
88  }
89 
90  char linebuf[1024];
91  in.getline (linebuf, 1024);
92  std::string line (linebuf);
93  std::vector < std::string > strs_2;
94  boost::split (strs_2, line, boost::is_any_of (" "));
95 
96  for (int i = 0; i < 16; i++)
97  {
98  matrix (i / 4, i % 4) = static_cast<float> (atof (strs_2[i].c_str ()));
99  }
100 
101  return true;
102  }
103 
104  bool check_inside(int col, int row, int min_col, int max_col, int min_row, int max_row)
105  {
106  if (col >= min_col && col <= max_col && row >= min_row && row <= max_row)
107  return true;
108 
109  return false;
110  }
111 
112  template<class PointInT>
113  void cropCloud(int min_col, int max_col, int min_row, int max_row, pcl::PointCloud<PointInT> & cloud_in, pcl::PointCloud<PointInT> & cloud_out)
114  {
115  cloud_out.width = max_col - min_col + 1;
116  cloud_out.height = max_row - min_row + 1;
117  cloud_out.points.resize (cloud_out.width * cloud_out.height);
118  for (unsigned int u = 0; u < cloud_out.width; u++)
119  {
120  for (unsigned int v = 0; v < cloud_out.height; v++)
121  {
122  cloud_out.at (u, v) = cloud_in.at (min_col + u, min_row + v);
123  }
124  }
125 
126  cloud_out.is_dense = cloud_in.is_dense;
127  }
128 
129  public:
130 
132  {
133  w_size_ = 80;
134  USE_NORMALS_ = false;
135  num_images_ = 10;
136  patches_per_image_ = 20;
137  min_images_per_bin_ = -1;
138  }
139 
141  {
142 
143  }
144 
145  void setPatchesPerImage(int n)
146  {
147  patches_per_image_ = n;
148  }
149 
150  void setMinImagesPerBin(int n)
151  {
152  min_images_per_bin_ = n;
153  }
154 
155  void setUseNormals(bool use)
156  {
157  USE_NORMALS_ = use;
158  }
159 
160  void setWSize(int size)
161  {
162  w_size_ = size;
163  }
164 
165  void setNumImages(int n)
166  {
167  num_images_ = n;
168  }
169 
170  void initialize(std::string & data_dir);
171 
172  //shuffle file and get the first num_images_ as requested by a tree
173  //extract positive and negative samples
174  //create training examples and labels
175  void getDatasetAndLabels(DataSet & data_set, std::vector<LabelType> & label_data, std::vector<ExampleIndex> & examples);
176  };
177  }
178 }
179 
180 #endif /* FACE_DETECTOR_DATA_PROVIDER_H_ */
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:283
void getDatasetAndLabels(DataSet &data_set, std::vector< LabelType > &label_data, std::vector< ExampleIndex > &examples)
Virtual function called to obtain training examples and labels before training a specific tree...
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
Define standard C methods and C++ classes that are common to all methods.
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
void initialize(std::string &data_dir)
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values).
Definition: point_cloud.h:418