5 #ifndef OPENDETECTION_UTILS_H
6 #define OPENDETECTION_UTILS_H
9 #include <boost/preprocessor.hpp>
10 #include <boost/filesystem.hpp>
11 #include <boost/algorithm/string.hpp>
12 #include <opencv2/core/core.hpp>
21 namespace bf = boost::filesystem;
30 #define X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE(r, data, elem) \
31 case elem : return BOOST_PP_STRINGIZE(elem);
33 #define OD_DEFINE_ENUM_WITH_STRING_CONVERSIONS(name, enumerators) \
35 BOOST_PP_SEQ_ENUM(enumerators) \
38 inline const char* enumToString(name v) \
42 BOOST_PP_SEQ_FOR_EACH( \
43 X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE, \
47 default: return "[Unknown " BOOST_PP_STRINGIZE(name) "]"; \
58 std::ostringstream ss;
63 std::vector<std::string>
myglob(
const std::string &pat);
65 void normL2(cv::Mat &descriptors);
76 cv::Mat
makeCanvasMultiImages(std::vector<cv::Mat>& imgs, cv::Size cellSize, std::vector<std::string> messages);
83 boost::filesystem::path p(objfilename);
84 std::string input_dir = boost::filesystem::path(objfilename).parent_path().c_str();
86 std::ifstream input(objfilename.c_str());
88 while(getline(input, line))
90 std::istringstream iss(line);
100 std::ifstream inputmtl((input_dir +
"/" + tok2).c_str());
101 while(getline(inputmtl, linemtl))
103 std::istringstream issmtl(linemtl);
108 return input_dir +
"/" + tok2;
133 gettimeofday(&startTime, NULL);
139 long seconds, useconds;
142 gettimeofday(&endTime, NULL);
144 seconds = endTime.tv_sec - startTime.tv_sec;
145 useconds = endTime.tv_usec - startTime.tv_usec;
147 duration = seconds + useconds / 1000000.0;
158 printf(
"%5.6f seconds\n", duration);
163 template<
typename T,
typename Ptype>
167 if(n == 0) num = list.size();
else num = n < list.size() ? n : list.size();
169 for(
int i = 0; i < num; i++)
170 std::cout << (Ptype) list[i] <<
" ";
171 std::cout << std::endl;
177 for(
int i = 0; i < list.size(); i++)
178 std::cout << list[i] <<
" ";
179 std::cout << std::endl;
192 static std::string
getFirstFile(std::string base_path, std::string extension)
194 std::vector<std::string> files;
195 std::string start =
"";
196 std::string ext = extension;
197 bf::path dir = base_path;
199 if (files.size() == 0)
201 std::cout <<
"No file with extension " << extension <<
" present!\nReturning NULL";
204 else return files[0];
209 std::string start =
"";
210 std::string ext = extension;
211 bf::path dir = base_path;
215 static void getFilesInDirectoryRec(std::string base_path, std::vector<std::string> extensions, std::vector<std::string> &files)
217 std::string start =
"";
218 std::vector<std::string> exts = extensions;
219 bf::path dir = base_path;
223 static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector<std::string> &relative_paths, std::vector<std::string> exts)
225 bf::directory_iterator end_itr;
226 for(bf::directory_iterator itr(dir); itr != end_itr; ++itr) {
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() +
"/";
232 std::string so_far = rel_path_so_far + (itr->path ()).filename () +
"/";
235 bf::path curr_path = itr->path();
239 std::vector<std::string> strs;
240 #if BOOST_FILESYSTEM_VERSION == 3
241 std::string file = (itr->path().filename()).
string();
243 std::string file = (itr->path ()).filename ();
246 boost::split(strs, file, boost::is_any_of(
"."));
247 std::string extension = strs[strs.size() - 1];
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; }
254 if( flagfound ==
true )
256 #if BOOST_FILESYSTEM_VERSION == 3
257 std::string path = rel_path_so_far + (itr->path().filename()).
string();
259 std::string path = rel_path_so_far + (itr->path ()).filename ();
261 std::string fullpath = rel_path_so_far + itr->path().string();
262 relative_paths.push_back(fullpath);
268 static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector<std::string> &relative_paths, std::string
const &ext)
270 bf::directory_iterator end_itr;
271 for(bf::directory_iterator itr(dir); itr != end_itr; ++itr) {
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() +
"/";
277 std::string so_far = rel_path_so_far + (itr->path ()).filename () +
"/";
280 bf::path curr_path = itr->path();
284 std::vector<std::string> strs;
285 #if BOOST_FILESYSTEM_VERSION == 3
286 std::string file = (itr->path().filename()).
string();
288 std::string file = (itr->path ()).filename ();
291 boost::split(strs, file, boost::is_any_of(
"."));
292 std::string extension = strs[strs.size() - 1];
294 if( file.rfind(ext) != std::string::npos )
296 #if BOOST_FILESYSTEM_VERSION == 3
297 std::string path = rel_path_so_far + (itr->path().filename()).
string();
299 std::string path = rel_path_so_far + (itr->path ()).filename ();
301 std::string fullpath = rel_path_so_far + itr->path().string();
302 relative_paths.push_back(fullpath);
308 static void getFilesInDirectory(bf::path &dir, std::string &rel_path_so_far, std::vector<std::string> &relative_paths, std::string
const &ext)
310 bf::directory_iterator end_itr;
311 for(bf::directory_iterator itr(dir); itr != end_itr; ++itr) {
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() +
"/";
317 std::string so_far = rel_path_so_far + (itr->path ()).filename () +
"/";
320 bf::path curr_path = itr->path();
324 std::vector<std::string> strs;
325 #if BOOST_FILESYSTEM_VERSION == 3
326 std::string file = (itr->path().filename()).
string();
328 std::string file = (itr->path ()).filename ();
331 boost::split(strs, file, boost::is_any_of(
"."));
332 std::string extension = strs[strs.size() - 1];
334 if( file.rfind(ext) != std::string::npos )
336 #if BOOST_FILESYSTEM_VERSION == 3
337 std::string path = rel_path_so_far + (itr->path().filename()).
string();
339 std::string path = rel_path_so_far + (itr->path ()).filename ();
341 std::string fullpath = rel_path_so_far + itr->path().string();
342 relative_paths.push_back(path);
350 bf::path trained_dir = training_dir;
351 if(!bf::exists(trained_dir))
352 bf::create_directory(trained_dir);
355 static void getArgvArgc(std::string
const &commandline,
char ***argv,
int &argc)
363 *argv =
new char *[kMaxArgs];
364 (*argv)[argc++] = (
char *)
"program";
367 p = strtok((
char *) commandline.c_str(),
" ");
368 while(p && argc < kMaxArgs) {
375 #endif //OPENDETECTION_UTILS_H
std::vector< std::string > myglob(const std::string &pat)
An utility class for Timer related operations.
std::string toString(T Number)
static void getFilesInDirectoryInternal(bf::path &dir, std::string &rel_path_so_far, std::vector< std::string > &relative_paths, std::vector< std::string > exts)
static void getFilesInDirectory(bf::path &dir, std::string &rel_path_so_far, std::vector< std::string > &relative_paths, std::string const &ext)
Utility class for File and directory handling.
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)
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)
void normL2(cv::Mat &descriptors)
static void getFilesInDirectoryRec(std::string base_path, std::vector< std::string > extensions, std::vector< std::string > &files)
static void createTrainingDir(std::string training_dir)
void printListIn(std::vector< T > list, int n=0)
static std::string getTexfileinObj(std::string objfilename)
void printList(std::vector< T > list)
static void getArgvArgc(std::string const &commandline, char ***argv, int &argc)
static void getFilesInDirectoryRec(std::string base_path, std::string extension, std::vector< std::string > &files)
static std::string getFirstFile(std::string base_path, std::string extension)