c++_读取yaml的配置文件

参考博客:

c++读取文件之---yaml-cpp使用_AI小花猫的博客-CSDN博客

【极简】c++ 使用 opencv FileStorage、FileNode 读取 YAML 文件中参数设置_我才是一卓的博客-CSDN博客

使用yaml的文件配置参数,

布尔值用true和false表示

使用缩进表示层级关系,缩进时不允许使用Tab键,只允许使用空格

缩进两个空格或者三个空格都可以,但是相同层级的元素必须左对齐,

冒号后面需要带一个空格

对大小写敏感

%YAML:1.0

isGPU: true
region: 
   model_path: models/0702_xmaoi_region.onnx
   is_rgb: true
   input_w: 640
   input_h: 640
   conf_threshold: 0.3
   nms_threshold: 0.3

用c++的语言,有两种方法读取yaml的配置文件。

方式一:使用yaml-cpp的第三方库

在github上找到yaml-cpp的工程链接:GitHub - jbeder/yaml-cpp: A YAML parser and emitter in C++g

git clone下载该代码,在widnows上用cmake-gui打开该工程,

 在Configure之后,需要勾选YAML_BUILD_SHARED_LIBS,然后Generate生成vs2019的解决方案,用vs2019打开该工程,可编译出Debug,Release的dll文件。

然后新建一个项目,包含进来yaml-cpp生成的库文件:

C/C++->常规->附加包含目录:

D:\github_code\code3_cpp\yaml-cpp\include

链接器->常规->附加库目录:

D:\github_code\code3_cpp\yaml-cpp\build\Debug

链接器->输入->附加依赖项:

yaml-cppd.lib

#include<iostream>
#include"yaml-cpp/yaml.h"

struct ConfigModel {
    std::string model_path;
    bool is_rgb;
    int input_w;
    int input_h;
    float conf_threshold;
    float nms_threshold;
    int crop_size;
    int stride;
};

struct Config {
    bool isGPU;
    struct ConfigModel region;
    struct ConfigModel crack;
    struct ConfigModel edge_break;
};


int LoadConfigFile1(const std::string file_name, Config& config) {
    YAML::Node cfg_yml = YAML::LoadFile(file_name);
    if (!cfg_yml) {
        std::cout << "Open config File:" << file_name << " failed.";
        return -1;
    }

    std::cout << "parse config file..." << std::endl;

    if (cfg_yml["isGPU"])
        config.isGPU = cfg_yml["isGPU"].as<bool>();
    else
        return -1;
    //region
    if (cfg_yml["region"]["model_path"])
        config.region.model_path = cfg_yml["region"]["model_path"].as<std::string>();
    else
        return -1;
    if (cfg_yml["region"]["is_rgb"])
        config.region.is_rgb = cfg_yml["region"]["is_rgb"].as<bool>();
    else
        return -1;
    if (cfg_yml["region"]["input_w"])
        config.region.input_w = cfg_yml["region"]["input_w"].as<int>();
    else
        return -1;
    if (cfg_yml["region"]["input_h"])
        config.region.input_h = cfg_yml["region"]["input_h"].as<int>();
    else
        return -1;
    if (cfg_yml["region"]["conf_threshold"])
        config.region.conf_threshold = cfg_yml["region"]["conf_threshold"].as<float>();
    else
        return -1;
    if (cfg_yml["region"]["nms_threshold"])
        config.region.nms_threshold = cfg_yml["region"]["nms_threshold"].as<float>();
    else
        return -1;
    
    return 0;
}

方式二:使用opencv中的FileStorage,FileNode读取yaml中的参数

cv::FileStorage 支持 YAML 1.0 和 YAML 1.1 语法规范。
因此 YAML 文件中需要在首行必须加上 %YAML:1.0 或 %YAML:1.1,否则将会报错。

#include<iostream>
#include <opencv2/opencv.hpp>


int LoadConfigFile(const std::string file_name, Config& config) {
    cv::FileStorage fs(file_name, cv::FileStorage::READ);

    // 检查文件是否打开成功
    if (!fs.isOpened()) {
        std::cout << "Open config File:" << file_name << " failed.";
        return -1;
    }

    std::cout << "parse config file..." << std::endl;

    cv::FileNode node;
    node = fs["isGPU"];
    if (!node.isNone() && !node.empty()) {
        node >> config.isGPU;
    }
    else
        return -1;

    //region
    node = fs["region"]["model_path"];
    if (!node.isNone() && !node.empty()) {
        node >> config.region.model_path;
    }
    else
        return -1;
    node = fs["region"]["is_rgb"];
    if (!node.isNone() && !node.empty()) {
        node >> config.region.is_rgb;
    }
    else
        return -1;
    node = fs["region"]["input_w"];
    if (!node.isNone() && !node.empty()) {
        node >> config.region.input_w;
    }
    else
        return -1;
    node = fs["region"]["input_h"];
    if (!node.isNone() && !node.empty()) {
        node >> config.region.input_h;
    }
    else
        return -1;
    node = fs["region"]["conf_threshold"];
    if (!node.isNone() && !node.empty()) {
        node >> config.region.conf_threshold;
    }
    else
        return -1;
    node = fs["region"]["nms_threshold"];
    if (!node.isNone() && !node.empty()) {
        node >> config.region.nms_threshold;
    }
    else
        return -1;

    std::cout << "isGPU:" << config.isGPU << std::endl;
    std::cout << "region:" << std::endl;
    std::cout << "\tmodels_path:" << config.region.model_path << std::endl;
    std::cout << "\tis_rgb:" << config.region.is_rgb << std::endl;
    std::cout << "\tinput_w:" << config.region.input_w << std::endl;
    std::cout << "\tinput_h:" << config.region.input_h << std::endl;
    std::cout << "\tconf_threshold:" << config.region.conf_threshold << std::endl;
    std::cout << "\tnms_threshold:" << config.region.nms_threshold << std::endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值