boost解析info文件

先给出info文件:

parameters
{
    MAX_STAGES 4
    MAX_DEPTH 3
    MAX_NUMTRESS 5
    MAX_NUMTHRESHS 500
    MAX_NUMFEATS 1000,1000,1000,500,500,500,400,400
    MAX_RATIO_RADIUS 0.3,0.2,0.2,0.15,0.12,0.10,0.08,0.06,0.06,0.05
    BAGGING_OVERLAP 0.4
    IS_FLIP true
}

meanface
{
    MAX_ITERS 120
    MAX_ERRORS 0.000001
    SCALE_FACTOR 1.0
    SCALE_CONST 1.0
    WIDTH_DIV 1.95
    HEIGHT_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 face
    int    __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 face
        ptree 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 train
        ptree 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值