Open Detection  1.0
utils.h
1 //
2 // Created by sarkar on 09.06.15.
3 //
4 
5 #ifndef OPENDETECTION_UTILS_H
6 #define OPENDETECTION_UTILS_H
7 
8 #include <sys/time.h>
9 #include <boost/preprocessor.hpp>
10 #include <boost/filesystem.hpp>
11 #include <boost/algorithm/string.hpp>
12 #include <opencv2/core/core.hpp>
13 
14 #include <fstream>
15 #include <sstream>
16 #include <iostream>
17 #include <glob.h>
18 
19 
20 
21 namespace bf = boost::filesystem;
22 
23 /** \brief misclenious helping functions; will be refactored later
24  *
25  * \author Kripasindhu Sarkar
26  *
27  */
28 
29 
30 #define X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE(r, data, elem) \
31  case elem : return BOOST_PP_STRINGIZE(elem);
32 
33 #define OD_DEFINE_ENUM_WITH_STRING_CONVERSIONS(name, enumerators) \
34  enum name { \
35  BOOST_PP_SEQ_ENUM(enumerators) \
36  }; \
37  \
38  inline const char* enumToString(name v) \
39  { \
40  switch (v) \
41  { \
42  BOOST_PP_SEQ_FOR_EACH( \
43  X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE, \
44  name, \
45  enumerators \
46  ) \
47  default: return "[Unknown " BOOST_PP_STRINGIZE(name) "]"; \
48  } \
49  }
50 
51 
52 namespace od
53 {
54 
55  template<typename T>
56  std::string toString(T Number)
57  {
58  std::ostringstream ss;
59  ss << Number;
60  return ss.str();
61  }
62 
63  std::vector<std::string> myglob(const std::string &pat);
64 
65  void normL2(cv::Mat &descriptors);
66 
67 
68  /**
69  * @brief Makes composite image from the given images. Equal number of rows and columns are preferred. Therefore, number of rows = sqrt(number of input images)
70  *
71  * @param imgs Vector of Images.
72  * @param cellSize Size of individual images to be placed inside the composite images. images from `imgs` will be resized to this size before appending.
73  * @param messages Messages to be put on the top left of each image in `imgs`. Note `message.size()` should be equal to `imgs.size()`.
74  * @return new composite image.
75  */
76  cv::Mat makeCanvasMultiImages(std::vector<cv::Mat>& imgs, cv::Size cellSize, std::vector<std::string> messages);
77 
78  cv::Scalar getHashedColor(std::string name, int constadd);
79 
80  static std::string getTexfileinObj(std::string objfilename)
81  {
82 
83  boost::filesystem::path p(objfilename);
84  std::string input_dir = boost::filesystem::path(objfilename).parent_path().c_str();
85 
86  std::ifstream input(objfilename.c_str());
87  std::string line;
88  while(getline(input, line))
89  {
90  std::istringstream iss(line);
91  std::string tok1;
92  iss >> tok1;
93  if(tok1 == "mtllib")
94  {
95  std::string tok2;
96 
97  iss >> tok2;
98  std::string linemtl;
99 
100  std::ifstream inputmtl((input_dir + "/" + tok2).c_str());
101  while(getline(inputmtl, linemtl))
102  {
103  std::istringstream issmtl(linemtl);
104  issmtl >> tok1;
105  if(tok1 == "map_Kd")
106  {
107  issmtl >> tok2;
108  return input_dir + "/" + tok2;
109  }
110  }
111  }
112  }
113  return "";
114  }
115 
116 /** \brief An utility class for Timer related operations.
117  *
118  * \author Kripasindhu Sarkar
119  *
120  */
121  class Timer
122  {
123  private:
124 
125  timeval startTime;
126 
127  public:
128 
129  double duration_;
130 
131  void start()
132  {
133  gettimeofday(&startTime, NULL);
134  }
135 
136  double stop()
137  {
138  timeval endTime;
139  long seconds, useconds;
140  double duration;
141 
142  gettimeofday(&endTime, NULL);
143 
144  seconds = endTime.tv_sec - startTime.tv_sec;
145  useconds = endTime.tv_usec - startTime.tv_usec;
146 
147  duration = seconds + useconds / 1000000.0;
148  duration_ = duration;
149  return duration;
150  }
151 
152 
153  double getDuration()
154  { return duration_; }
155 
156  static void printTime(double duration)
157  {
158  printf("%5.6f seconds\n", duration);
159  }
160  };
161 
162 
163  template<typename T, typename Ptype>
164  void printListIn(std::vector<T> list, int n = 0)
165  {
166  int num;
167  if(n == 0) num = list.size(); else num = n < list.size() ? n : list.size();
168 
169  for(int i = 0; i < num; i++)
170  std::cout << (Ptype) list[i] << " ";
171  std::cout << std::endl;
172  }
173 
174  template<typename T>
175  void printList(std::vector<T> list)
176  {
177  for(int i = 0; i < list.size(); i++)
178  std::cout << list[i] << " ";
179  std::cout << std::endl;
180  }
181 
182 
183  /** \brief Utility class for File and directory handling.
184  *
185  * \author Kripasindhu Sarkar
186  *
187  */
188  class FileUtils
189  {
190  public:
191 
192  static std::string getFirstFile(std::string base_path, std::string extension)
193  {
194  std::vector<std::string> files;
195  std::string start = "";
196  std::string ext = extension;
197  bf::path dir = base_path;
198  FileUtils::getFilesInDirectoryInternal(dir, start, files, ext);
199  if (files.size() == 0)
200  {
201  std::cout << "No file with extension " << extension << " present!\nReturning NULL";
202  return "";
203  }
204  else return files[0];
205  }
206 
207  static void getFilesInDirectoryRec(std::string base_path, std::string extension, std::vector<std::string> &files)
208  {
209  std::string start = "";
210  std::string ext = extension;
211  bf::path dir = base_path;
212  FileUtils::getFilesInDirectoryInternal(dir, start, files, ext);
213  }
214 
215  static void getFilesInDirectoryRec(std::string base_path, std::vector<std::string> extensions, std::vector<std::string> &files)
216  {
217  std::string start = "";
218  std::vector<std::string> exts = extensions;
219  bf::path dir = base_path;
220  FileUtils::getFilesInDirectoryInternal(dir, start, files, exts);
221  }
222 
223  static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector<std::string> &relative_paths, std::vector<std::string> exts)
224  {
225  bf::directory_iterator end_itr;
226  for(bf::directory_iterator itr(dir); itr != end_itr; ++itr) {
227  //check if its a directory, then get models in it
228  if(bf::is_directory(*itr)) {
229 #if BOOST_FILESYSTEM_VERSION == 3
230  std::string so_far = rel_path_so_far + (itr->path().filename()).string() + "/";
231 #else
232  std::string so_far = rel_path_so_far + (itr->path ()).filename () + "/";
233 #endif
234 
235  bf::path curr_path = itr->path();
236  getFilesInDirectoryInternal(curr_path, so_far, relative_paths, exts);
237  } else {
238  //check that it is a ply file and then add, otherwise ignore..
239  std::vector<std::string> strs;
240 #if BOOST_FILESYSTEM_VERSION == 3
241  std::string file = (itr->path().filename()).string();
242 #else
243  std::string file = (itr->path ()).filename ();
244 #endif
245 
246  boost::split(strs, file, boost::is_any_of("."));
247  std::string extension = strs[strs.size() - 1];
248 
249  bool flagfound = false;
250  for (int exti = 0; exti < exts.size(); exti ++)
251  if(file.rfind(exts[exti]) != std::string::npos)
252  { flagfound = true; break; }
253 
254  if( flagfound == true )
255  {
256 #if BOOST_FILESYSTEM_VERSION == 3
257  std::string path = rel_path_so_far + (itr->path().filename()).string();
258 #else
259  std::string path = rel_path_so_far + (itr->path ()).filename ();
260 #endif
261  std::string fullpath = rel_path_so_far + itr->path().string();
262  relative_paths.push_back(fullpath);
263  }
264  }
265  }
266  }
267 
268  static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector<std::string> &relative_paths, std::string const &ext)
269  {
270  bf::directory_iterator end_itr;
271  for(bf::directory_iterator itr(dir); itr != end_itr; ++itr) {
272  //check if its a directory, then get models in it
273  if(bf::is_directory(*itr)) {
274 #if BOOST_FILESYSTEM_VERSION == 3
275  std::string so_far = rel_path_so_far + (itr->path().filename()).string() + "/";
276 #else
277  std::string so_far = rel_path_so_far + (itr->path ()).filename () + "/";
278 #endif
279 
280  bf::path curr_path = itr->path();
281  getFilesInDirectoryInternal(curr_path, so_far, relative_paths, ext);
282  } else {
283  //check that it is a ply file and then add, otherwise ignore..
284  std::vector<std::string> strs;
285 #if BOOST_FILESYSTEM_VERSION == 3
286  std::string file = (itr->path().filename()).string();
287 #else
288  std::string file = (itr->path ()).filename ();
289 #endif
290 
291  boost::split(strs, file, boost::is_any_of("."));
292  std::string extension = strs[strs.size() - 1];
293 
294  if( file.rfind(ext) != std::string::npos )
295  {
296 #if BOOST_FILESYSTEM_VERSION == 3
297  std::string path = rel_path_so_far + (itr->path().filename()).string();
298 #else
299  std::string path = rel_path_so_far + (itr->path ()).filename ();
300 #endif
301  std::string fullpath = rel_path_so_far + itr->path().string();
302  relative_paths.push_back(fullpath);
303  }
304  }
305  }
306  }
307 
308  static void getFilesInDirectory(bf::path &dir, std::string &rel_path_so_far, std::vector<std::string> &relative_paths, std::string const &ext)
309  {
310  bf::directory_iterator end_itr;
311  for(bf::directory_iterator itr(dir); itr != end_itr; ++itr) {
312  //check if its a directory, then get models in it
313  if(bf::is_directory(*itr)) {
314 #if BOOST_FILESYSTEM_VERSION == 3
315  std::string so_far = rel_path_so_far + (itr->path().filename()).string() + "/";
316 #else
317  std::string so_far = rel_path_so_far + (itr->path ()).filename () + "/";
318 #endif
319 
320  bf::path curr_path = itr->path();
321  getFilesInDirectoryInternal(curr_path, so_far, relative_paths, ext);
322  } else {
323  //check that it is a ply file and then add, otherwise ignore..
324  std::vector<std::string> strs;
325 #if BOOST_FILESYSTEM_VERSION == 3
326  std::string file = (itr->path().filename()).string();
327 #else
328  std::string file = (itr->path ()).filename ();
329 #endif
330 
331  boost::split(strs, file, boost::is_any_of("."));
332  std::string extension = strs[strs.size() - 1];
333 
334  if( file.rfind(ext) != std::string::npos )
335  {
336 #if BOOST_FILESYSTEM_VERSION == 3
337  std::string path = rel_path_so_far + (itr->path().filename()).string();
338 #else
339  std::string path = rel_path_so_far + (itr->path ()).filename ();
340 #endif
341  std::string fullpath = rel_path_so_far + itr->path().string();
342  relative_paths.push_back(path);
343  }
344  }
345  }
346  }
347 
348  static void createTrainingDir(std::string training_dir)
349  {
350  bf::path trained_dir = training_dir;
351  if(!bf::exists(trained_dir))
352  bf::create_directory(trained_dir);
353  }
354 
355  static void getArgvArgc(std::string const &commandline, char ***argv, int &argc)
356  {
357  enum
358  {
359  kMaxArgs = 64
360  };
361 
362  argc = 0;
363  *argv = new char *[kMaxArgs];
364  (*argv)[argc++] = (char *) "program";
365 
366  char *p;
367  p = strtok((char *) commandline.c_str(), " ");
368  while(p && argc < kMaxArgs) {
369  (*argv)[argc++] = p;
370  p = strtok(0, " ");
371  }
372  }
373  };
374 }
375 #endif //OPENDETECTION_UTILS_H
std::vector< std::string > myglob(const std::string &pat)
double getDuration()
Definition: utils.h:153
An utility class for Timer related operations.
Definition: utils.h:121
std::string toString(T Number)
Definition: utils.h:56
static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector< std::string > &relative_paths, std::vector< std::string > exts)
Definition: utils.h:223
static void getFilesInDirectory(bf::path &dir, std::string &rel_path_so_far, std::vector< std::string > &relative_paths, std::string const &ext)
Definition: utils.h:308
double stop()
Definition: utils.h:136
Utility class for File and directory handling.
Definition: utils.h:188
cv::Scalar getHashedColor(std::string name, int constadd)
static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector< std::string > &relative_paths, std::string const &ext)
Definition: utils.h:268
cv::Mat makeCanvasMultiImages(std::vector< cv::Mat > &imgs, cv::Size cellSize, std::vector< std::string > messages)
Makes composite image from the given images.
static void printTime(double duration)
Definition: utils.h:156
void normL2(cv::Mat &descriptors)
static void getFilesInDirectoryRec(std::string base_path, std::vector< std::string > extensions, std::vector< std::string > &files)
Definition: utils.h:215
static void createTrainingDir(std::string training_dir)
Definition: utils.h:348
void printListIn(std::vector< T > list, int n=0)
Definition: utils.h:164
static std::string getTexfileinObj(std::string objfilename)
Definition: utils.h:80
void printList(std::vector< T > list)
Definition: utils.h:175
static void getArgvArgc(std::string const &commandline, char ***argv, int &argc)
Definition: utils.h:355
static void getFilesInDirectoryRec(std::string base_path, std::string extension, std::vector< std::string > &files)
Definition: utils.h:207
double duration_
Definition: utils.h:129
static std::string getFirstFile(std::string base_path, std::string extension)
Definition: utils.h:192
void start()
Definition: utils.h:131