/*! * Initialize the parameters using a configuration file * @param[in] yamlConfigFile the full path to the configuration file to parse * @param[in] classFolder the full path to the classifiers folder if not defined at runtime * @exception std::runtime_error error accessing or parsing the configuration file */ void DetectionParams::loadFromFile(const std::string& yamlConfigFile, const std::string& classFolder) throw(std::runtime_error){ try{ cv::FileStorage fs(yamlConfigFile, cv::FileStorage::READ); configFileName = yamlConfigFile; if (!fs.isOpened()) { throw std::runtime_error("CONFIG PARSER ERROR :: Couldn't load configuration file: " + yamlConfigFile + "\n"); } //if the folder containing the classifiers is not specified read it from the config file if (classFolder.empty()){ classifiersFolder = (std::string)fs["ClassifiersFolder"]; if (classifiersFolder.empty()) throw (std::runtime_error("CONFIG PARSER ERROR :: Classifiers Folder not specified. \n")); } else classifiersFolder = classFolder; //replace separators with the right ones fixPathString(classifiersFolder); cascadeFile = (std::string)fs["CascadeFile"]; if (cascadeFile.empty()) throw (std::runtime_error("CONFIG PARSER ERROR :: Cascade Classifier not specified. \n")); cascadeFile = classifiersFolder + cascadeFile; //full filename svmModelFile = (std::string)fs["SVMFile"]; if (svmModelFile.empty()) throw (std::runtime_error("CONFIG PARSER ERROR :: SVM Classifier not specified. \n")); svmModelFile = classifiersFolder + svmModelFile; //full filename svmModelFile2 = (std::string)fs["SVMFile2"]; if (svmModelFile2.empty()){ //throw (std::runtime_error("CONFIG PARSER ERROR :: SVM Classifier not specified. \n")); use3Stages_ = false; } else{ svmModelFile2 = classifiersFolder + svmModelFile2; //full filename std::string tmpNeg = (std::string)fs["negLabel"]; std::string tmpPos = (std::string)fs["posLabel"]; if (tmpNeg.empty() || tmpPos.empty()) throw (std::runtime_error("CONFIG PARSER ERROR :: Labels for 3rd stage are not specified. \n")); else{ labels.push_back(tmpNeg); labels.push_back(tmpPos); } use3Stages_ = true; } cv::FileNode n, n2; n = fs["minWinSize"]; if (n.empty()) { throw std::runtime_error("Parser Error :: Cascade Minimum Window Size not specified.\n"); } n2 = n["width"]; if (n2.empty()) { throw std::runtime_error("Parser Error :: Cascade Minimum Window Size width not specified.\n"); } cascadeMinWin.width = (int)n2; n2 = n["height"]; if (n2.empty()) { throw std::runtime_error("Parser Error :: Cascade Minimum Window Size height not specified.\n"); } cascadeMinWin.height = (int)n2; n = fs["HOG_winSize"]; if (n.empty()) { throw(std::runtime_error("Parser Error :: HOG Window Size not specified.\n")); } n2 = n["width"]; if (n2.empty()) { throw std::runtime_error("Parser Error :: HOG Window Size width not specified.\n"); } hogWinSize.width = (int)n2; n2 = n["height"]; if (n2.empty()) { throw std::runtime_error("Parser Error :: HOG Window Size height not specified.\n"); } hogWinSize.height = (int)n2; n = fs["maxWinSizeFactor"]; const float cascadeMaxWinFactor = (n.empty() ? 8.f : (float)n[0]); cascadeMaxWin.width = cascadeMinWin.width * cascadeMaxWinFactor; cascadeMaxWin.height = cascadeMinWin.height * cascadeMaxWinFactor; n = fs["CroppingFactors"]; if (n.empty()) { croppingFactors[0] = 1.f; croppingFactors[1] = 1.f; } else { n2 = n["width"]; croppingFactors[0] = (n2.empty() ? 1.f : (float)n2); n2 = n["height"]; croppingFactors[1] = (n2.empty() ? 1.f : (float)n2); } n = fs["ScaleFactor"]; scalingFactor = (n.empty() ? 1. : (float)n); n = fs["CascadeScaleFactor"]; cascadeScaleFactor = (n.empty() ? 1.1 : (float)n); n = fs["SVMThreshold"]; SVMThreshold = (n.empty() ? .5 : (float)n); n = fs["maxAgePreConfirmation"]; maxAgePreConfirmation = (n.empty() ? 5 : (int)n); n = fs["maxAgePostConfirmation"]; maxAgePostConfirmation = (n.empty() ? 15 : (int)n); n = fs["nHangOverFrames"]; nHangOverFrames = (n.empty() ? 3 : (int)n); init_ = true; } catch (std::exception& e) { throw(e); } }
static bool tmpSearchPath(std::string path, std::vector<SearchFileInfo> & files, bool recursion) { if (path.length() == 0) { return false; } path = fixPathString(path); auto wildcard = subString(path, "/", true, true); if (!wildcard.second.empty()) { path = wildcard.first + "/"; } #ifdef WIN32 WIN32_FIND_DATAA fd; std::string findpath = path; findpath.append("*"); HANDLE hFile = FindFirstFileA(findpath.c_str(), &fd); if (hFile == INVALID_HANDLE_VALUE) { return false; } SearchFileInfo file; do { if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(fd.cFileName, ".") != 0 && strcmp(fd.cFileName, "..") != 0) { memset(&file, 0, sizeof(file)); file.bDir = true; strcpy_s(file.filename, sizeof(file.filename), fd.cFileName); sprintf(file.fullpath, "%s%s", path.c_str(), fd.cFileName); if (wildcard.second.empty()) { files.push_back(file); } if (recursion) { tmpSearchPath(fixPathString(file.fullpath) + wildcard.second, files, recursion); } } } else { memset(&file, 0, sizeof(file)); file.bDir = false; file.filesize = fd.nFileSizeHigh; file.filesize = file.filesize << 32; file.filesize += fd.nFileSizeLow; strcpy_s(file.filename, sizeof(file.filename), fd.cFileName); sprintf(file.fullpath, "%s%s", path.c_str(), fd.cFileName); if (wildcard.second.empty() || compareStringWildcard(file.filename, wildcard.second)) { files.push_back(file); } } } while (FindNextFileA(hFile, &fd)); FindClose(hFile); #else DIR *dp; struct dirent *entry; struct stat statbuf; if ((dp = opendir(path.c_str())) == NULL) { return false; } SearchFileInfo file; while ((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if (S_ISDIR(statbuf.st_mode)) { if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) { continue; } memset(&file, 0, sizeof(file)); file.bDir = true; file.filesize = statbuf.st_size; strcpy(file.filename, entry->d_name); sprintf(file.fullpath, "%s%s", path.c_str(), entry->d_name); if (wildcard.second.empty()) { files.push_back(file); } if (recursion) { tmpSearchPath(fixPathString(file.fullpath)+wildcard.second, files, recursion); } } else { memset(&file, 0, sizeof(file)); file.bDir = false; file.filesize = statbuf.st_size; strcpy(file.filename, entry->d_name); file.fullpath[0] = '\0'; if (wildcard.second.empty() || compareStringWildcard(file.filename, wildcard.second)) { files.push_back(file); } } } closedir(dp); #endif return true; }