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