Point Cloud Library (PCL)  1.14.1-dev
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 #pragma once
9 
10 #include <pcl/common/pcl_filesystem.h>
11 #include <pcl/memory.h>
12 #include <pcl/ml/dt/decision_tree_data_provider.h>
13 #include <pcl/recognition/face_detection/face_common.h>
14 
15 #include <boost/algorithm/string.hpp>
16 
17 #include <fstream>
18 #include <string>
19 
20 
21 namespace pcl
22 {
23  namespace face_detection
24  {
25  template<class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
26  class FaceDetectorDataProvider: public pcl::DecisionTreeTrainerDataProvider<FeatureType, DataSet, LabelType, ExampleIndex, NodeType>
27  {
28  private:
29  int num_images_;
30  std::vector<std::string> image_files_;
31  bool USE_NORMALS_;
32  int w_size_;
33  int patches_per_image_;
34  int min_images_per_bin_;
35 
36  void getFilesInDirectory(pcl_fs::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths, std::string & ext)
37  {
38  for (const auto& dir_entry : pcl_fs::directory_iterator(dir))
39  {
40  //check if its a directory, then get models in it
41  if (pcl_fs::is_directory (dir_entry))
42  {
43  std::string so_far = rel_path_so_far + (dir_entry.path ().filename ()).string () + "/";
44  pcl_fs::path curr_path = dir_entry.path ();
45  getFilesInDirectory (curr_path, so_far, relative_paths, ext);
46  } else
47  {
48  //check that it is a ply file and then add, otherwise ignore..
49  std::vector < std::string > strs;
50  std::string file = (dir_entry.path ().filename ()).string ();
51  boost::split (strs, file, boost::is_any_of ("."));
52  std::string extension = strs[strs.size () - 1];
53 
54  if (extension == ext)
55  {
56  std::string path = rel_path_so_far + (dir_entry.path ().filename ()).string ();
57  relative_paths.push_back (path);
58  }
59  }
60  }
61  }
62 
63  inline bool readMatrixFromFile(const std::string& file, Eigen::Matrix4f & matrix)
64  {
65 
66  std::ifstream in;
67  in.open (file.c_str (), std::ifstream::in);
68  if (!in.is_open ())
69  {
70  return false;
71  }
72 
73  char linebuf[1024];
74  in.getline (linebuf, 1024);
75  std::string line (linebuf);
76  std::vector < std::string > strs_2;
77  boost::split (strs_2, line, boost::is_any_of (" "));
78 
79  for (int i = 0; i < 16; i++)
80  {
81  matrix (i / 4, i % 4) = static_cast<float> (atof (strs_2[i].c_str ()));
82  }
83 
84  return true;
85  }
86 
87  bool check_inside(int col, int row, int min_col, int max_col, int min_row, int max_row)
88  {
89  return col >= min_col && col <= max_col && row >= min_row && row <= max_row;
90  }
91 
92  template<class PointInT>
93  void cropCloud(int min_col, int max_col, int min_row, int max_row, pcl::PointCloud<PointInT> & cloud_in, pcl::PointCloud<PointInT> & cloud_out)
94  {
95  cloud_out.width = max_col - min_col + 1;
96  cloud_out.height = max_row - min_row + 1;
97  cloud_out.resize (cloud_out.width * cloud_out.height);
98  for (unsigned int u = 0; u < cloud_out.width; u++)
99  {
100  for (unsigned int v = 0; v < cloud_out.height; v++)
101  {
102  cloud_out.at (u, v) = cloud_in.at (min_col + u, min_row + v);
103  }
104  }
105 
106  cloud_out.is_dense = cloud_in.is_dense;
107  }
108 
109  public:
110 
111  using Ptr = shared_ptr<FaceDetectorDataProvider<FeatureType, DataSet, LabelType, ExampleIndex, NodeType>>;
112  using ConstPtr = shared_ptr<const FaceDetectorDataProvider<FeatureType, DataSet, LabelType, ExampleIndex, NodeType>>;
113 
115  {
116  w_size_ = 80;
117  USE_NORMALS_ = false;
118  num_images_ = 10;
119  patches_per_image_ = 20;
120  min_images_per_bin_ = -1;
121  }
122 
123  void setPatchesPerImage(int n)
124  {
125  patches_per_image_ = n;
126  }
127 
128  void setMinImagesPerBin(int n)
129  {
130  min_images_per_bin_ = n;
131  }
132 
133  void setUseNormals(bool use)
134  {
135  USE_NORMALS_ = use;
136  }
137 
138  void setWSize(int size)
139  {
140  w_size_ = size;
141  }
142 
143  void setNumImages(int n)
144  {
145  num_images_ = n;
146  }
147 
148  void initialize(std::string & data_dir);
149 
150  //shuffle file and get the first num_images_ as requested by a tree
151  //extract positive and negative samples
152  //create training examples and labels
153  void getDatasetAndLabels(DataSet & data_set, std::vector<LabelType> & label_data, std::vector<ExampleIndex> & examples) override;
154  };
155  }
156 }
shared_ptr< const DecisionTreeTrainerDataProvider< FeatureType, DataSet, LabelType, ExampleIndex, NodeType > > ConstPtr
shared_ptr< DecisionTreeTrainerDataProvider< FeatureType, DataSet, LabelType, ExampleIndex, NodeType > > Ptr
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:262
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
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
void initialize(std::string &data_dir)
void getDatasetAndLabels(DataSet &data_set, std::vector< LabelType > &label_data, std::vector< ExampleIndex > &examples) override
Virtual function called to obtain training examples and labels before training a specific tree.
Defines functions, macros and traits for allocating and using memory.