先给出info文件:
parameters
{MAX_STAGES 4MAX_DEPTH 3MAX_NUMTRESS 5MAX_NUMTHRESHS 500MAX_NUMFEATS 1000,1000,1000,500,500,500,400,400MAX_RATIO_RADIUS 0.3,0.2,0.2,0.15,0.12,0.10,0.08,0.06,0.06,0.05BAGGING_OVERLAP 0.4IS_FLIP true
}meanface
{MAX_ITERS 120MAX_ERRORS 0.000001SCALE_FACTOR 1.0SCALE_CONST 1.0WIDTH_DIV 1.95HEIGHT_DIV 1.60
}train
{FACE_DETECT_PATH haarcascade_frontalface_alt2.xml LANDMARK_TYPE LANDMARK_TYPE_68 dataset{0{DATA_PATH E:\\dataset\\lfpw\\trainset\\Path_Images.txt}1{DATA_PATH E:\\datasets\\afw\\Path_Images.txt}}
}
接下来我们给出使用boost如何解析上述文件:
params.h内容:
#ifndef PARAMS_H
#define PARAMS_H#include <string>
#include <vector>using sParams = struct sParams{int __max_numstage;int __max_depth;int* __max_numfeats = nullptr;int __max_numtrees;int __max_numthreshs;double __bagging_overlap;double* __max_raio_radius = nullptr;bool __isflip;//mean faceint __procrustes_iters;double __procrustes_errors;double __facebox_scale_factor;double __facebox_scale_const;double __facebox_width_div;double __facebox_height_div;std::string __landmark_type;std::vector<std::string> __images_path;
};#endif
具体的读取方法:
#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include "params.h"
#include <iostream>namespace po = boost::program_options;
using boost::filesystem::path;
using boost::property_tree::ptree;void Init_Params(sParams *params)
{params->__max_numfeats = NULL;params->__max_raio_radius = NULL;
}int main(int argc, char *argv[])
{path configfile = "./train.info";ptree pt;sParams params;try {read_info(configfile.string(), pt);}catch (const boost::property_tree::ptree_error& e) {std::cout << std::string("Error reading the config file: ") + e.what() << std::endl;return -EXIT_FAILURE;}try{ptree parameters = pt.get_child("parameters");params.__max_numstage = parameters.get<int>("MAX_STAGES");params.__max_depth = parameters.get<int>("MAX_DEPTH");params.__max_numtrees = parameters.get<int>("MAX_NUMTRESS");params.__max_numthreshs = parameters.get<int>("MAX_NUMTHRESHS");params.__bagging_overlap = parameters.get<double>("BAGGING_OVERLAP");params.__isflip = parameters.get<bool>("IS_FLIP");std::string str_numfeats = parameters.get<std::string>("MAX_NUMFEATS");std::string str_ration = parameters.get<std::string>("MAX_RATIO_RADIUS");std::vector<std::string> split_numfeats;std::vector<std::string> split_ration;boost::split(split_numfeats, str_numfeats, boost::is_any_of(","));int num_numfeats = 0;for (int i = 0; i < split_numfeats.size(); i++){if (split_numfeats[i] != std::string(",")){num_numfeats++;}}params.__max_numfeats = new int[num_numfeats];int index = 0;for (int i = 0; i < split_numfeats.size(); i++){if (split_numfeats[i] != std::string(",")){params.__max_numfeats[index] = boost::lexical_cast<int>(split_numfeats[i]);index++;}}boost::split(split_ration, str_ration, boost::is_any_of(","));int num_ration = 0;for (int i = 0; i < split_ration.size(); i++){if (split_ration[i] != std::string(",")){num_ration++;}}params.__max_raio_radius = new double[num_ration];for (int i = 0, index = 0; i < split_ration.size(); i++){if (split_ration[i] != std::string(",")){params.__max_raio_radius[index] = boost::lexical_cast<double>(split_ration[i]);index++;}}//init mean faceptree meanface = pt.get_child("meanface");params.__procrustes_iters = meanface.get<int>("MAX_ITERS");params.__procrustes_errors = meanface.get<double>("MAX_ERRORS");params.__facebox_scale_factor = meanface.get<double>("SCALE_FACTOR");params.__facebox_scale_const = meanface.get<double>("SCALE_CONST");params.__facebox_width_div = meanface.get<double>("WIDTH_DIV");params.__facebox_height_div = meanface.get<double>("HEIGHT_DIV");//init trainptree train = pt.get_child("train");std::string facedetect_file = train.get<std::string>("FACE_DETECT_PATH");params.__landmark_type = train.get<std::string>("LANDMARK_TYPE");ptree dataset = train.get_child("dataset");for (auto it = dataset.begin(); it != dataset.end(); it++){std::string dataset_str = it->second.get<std::string>("DATA_PATH");params.__images_path.push_back(dataset_str);}}catch (const boost::property_tree::ptree_error& error){std::cout << std::string("Parsing config: ") + error.what() << std::endl;return -EXIT_FAILURE;}delete params.__max_numfeats;delete params.__max_raio_radius;return EXIT_SUCCESS;
}