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