c++的json读取操作

2023-05-16

        使用的开源库是nlohmann / json。后续操作也都是基于该开源库操作。

本地json文件如下:

{"model_config":{
	"model_type":"paddlex",
	"model_cfg_file":"./models/yolov3/model.yml", 
	"model_key":"",
	"model_filename":"./models/yolov3/model.pdmodel",
	"model_params_filename":"./models/yolov3/model.pdiparams",
	"model_use_gpu":true,
	"model_gpu_id":0,
	"model_use_trt":false,
	"is_input_shape":true,
	"model_input_shape_max":{"image":[1,3,608,608]},
	"model_input_shape_min":{"image":[1,3,608,608]},
	"model_input_shape_optim":{"image":[1,3,608,608]},
    "input_shape_path":"config.pdmodl"},
"task_config":{
	"filter_ROI":[123,123,234,234,345,345],
	"filter_ai_type":["clas", "seg"],
	"filter_model_id":[25,36],
	"object_select_type":[["person","hat","red"],["dog","eye"]],
	"conf_thr":0.5}
}

上述json文件读取操作如下:

#include <iostream>
#include <fstream>
#include <numeric>
#include <vector>
#include <string>

#include "json.hpp"
using Json = nlohmann::json;

typedef struct ModelConfig_T
{
	std::string                             model_type;
	std::string                             model_cfg_file;
	// 解密模型。默认为空,表示加载普通模型;如果非空则用该key解密模型并加载部署
	std::string                             model_key;
	std::string                             model_filename;
	std::string                             model_params_filename;
	bool                                    model_use_gpu;
	int                                     model_gpu_id;
	bool                                    model_use_trt;
	bool                                    model_is_input_shape;

	std::map<std::string, std::vector<int>> model_input_shape_max;
	std::map<std::string, std::vector<int>> model_input_shape_min;
	std::map<std::string, std::vector<int>> model_input_shape_optim;
	std::string model_input_shape_path;
}ModelConfig;

typedef struct TaskConfig_T
{
	std::vector<int>                        filter_ROI;
	std::vector<std::string>                filter_ai_type;
	std::vector<int>                        filter_model_id;
	std::vector<std::vector<std::string>>   object_select_type;
	double                                  conf_thr;            // 本次操作全局有效
}TaskConfig;

    ModelConfig m_model_config;
	TaskConfig  m_task_config;

    Json config_json;
    std::ifstream(m_config_path) >> config_json;

    m_model_config.model_type = config_json.at("model_config").at("model_type").get<std::string>();
    m_model_config.model_cfg_file = config_json.at("model_config").at("model_cfg_file").get<std::string>();
	m_model_config.model_key = config_json.at("model_config").at("model_key").get<std::string>();
	m_model_config.model_filename = config_json.at("model_config").at("model_filename").get<std::string>();
	m_model_config.model_params_filename = config_json.at("model_config").at("model_params_filename").get<std::string>();
	m_model_config.model_use_gpu = config_json.at("model_config").at("model_use_gpu").get<bool>();
	m_model_config.model_gpu_id = config_json.at("model_config").at("model_gpu_id").get<int>();
	m_model_config.model_use_trt = config_json.at("model_config").at("model_use_trt").get<bool>();
	m_model_config.model_is_input_shape = config_json.at("model_config").at("is_input_shape").get<bool>();
	
	//std::cout << config_json.at("model_config").at("model_input_shape_max") << std::endl;

	for (auto item : config_json.at("model_config").at("model_input_shape_max").items())
	{
		std::vector<int> tmp;
		for (int i = 0; i < item.value().size(); i++)
		{
			tmp.push_back((int)item.value()[i]);
		}
		m_model_config.model_input_shape_max.insert(std::pair<std::string, std::vector<int>>(item.key(), item.value()));
		//m_model_input_shape_max[item.key()] = { item.value()[0],item.value()[0],item.value()[0],item.value()[0]};
	}

	for (auto item : config_json.at("model_config").at("model_input_shape_min").items())
	{
		std::vector<int> tmp;
		for (int i = 0; i < item.value().size(); i++)
		{
			tmp.push_back((int)item.value()[i]);
		}
		m_model_config.model_input_shape_min.insert(std::pair<std::string, std::vector<int>>(item.key(), item.value()));
		//m_model_input_shape_max[item.key()] = { item.value()[0],item.value()[0],item.value()[0],item.value()[0]};
	}

	for (auto item : config_json.at("model_config").at("model_input_shape_optim").items())
	{
		std::vector<int> tmp;
		for (int i = 0; i < item.value().size(); i++)
		{
			tmp.push_back((int)item.value()[i]);
		}
		m_model_config.model_input_shape_optim.insert(std::pair<std::string, std::vector<int>>(item.key(), item.value()));
		//m_model_input_shape_max[item.key()] = { item.value()[0],item.value()[0],item.value()[0],item.value()[0]};
	}

	m_model_config.model_input_shape_path = config_json.at("model_config").at("input_shape_path").get<std::string>();

    m_task_config.filter_ROI = config_json.at("task_config").at("filter_ROI").get<std::vector<int>>();
	m_task_config.filter_ai_type = config_json.at("task_config").at("filter_ai_type").get<std::vector<std::string>>();
	m_task_config.filter_model_id = config_json.at("task_config").at("filter_model_id").get<std::vector<int>>();
	m_task_config.object_select_type = config_json.at("task_config").at("object_select_type").get<std::vector<std::vector<std::string>>>();
	m_task_config.conf_thr = config_json.at("task_config").at("conf_thr").get<double>();

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c++的json读取操作 的相关文章

随机推荐

  • 原生OKHttp以及OKHttpUtil的使用

    Android系统提供了两种HTTP通信类 xff0c HttpURLConnection和HttpClient 尽管Google在大部分安卓版本中推荐使用HttpURLConnection xff0c 但是这个类相比HttpClient实
  • c++编译器配置错误问题clang: error: linker command failed with exit code 1 (use -v to see invocation)

    背景 xff1a ubuntu18 04系统 之前装其他程序的时候安装了一些软件 xff0c 不知道什么时候g 43 43 编译器从 usr bin c 43 43 组里面给删掉了 xff0c 默认的编译器成了clang 43 43 xff
  • apollo7.0------浅谈激光雷达运动补偿(二)--计算解析

    背景介绍 运动补偿相关介绍参考第一篇博客 xff1a apollo7 0 浅谈激光雷达运动补偿 龙性的腾飞的博客 CSDN博客 lidar运动补偿 本篇博客主要解释一下上篇博客中运动补偿的计算部分 xff0c 简单来说就是一个利用四元数球面
  • Matlab激光雷达相机联合标定经验分享

    一 背景介绍 联合标定是做多传感器融合的基础工作 xff0c 也是一个没有最好只有更好的研究方向 xff0c 相关论文也是层出不穷 xff0c 网上也有许多开源的工作 xff0c 包括Autoware的工具箱我也试过 xff0c 感觉标定效
  • 如何更改Ubuntu系统的输出为HDMI(耳机,扬声器)?

    由于需要用HDMI外接音频设备 xff0c 故想要改变电脑输出 xff0c 本人用的为Ubuntu14 04 开始在网上搜寻怎么更改 xff0c 查到需在声音设置里面更改音频输出为HDMI xff0c 可当我打开声音设置 xff0c 嗯 x
  • PPT中插入图片背景透明化小技巧

    新版的编辑器真不适应 xff0c 费劲 xff01 xff01 xff01 最近两天做开题答辩ppt xff0c 发现了ppt中处理图片背景的一个小技巧 xff0c 在此分享给大家 PPT一般会带有背景图片 xff0c 那种带浅色调logo
  • ROS发布Float32MultiArray消息C++/Python

    在ros下发布一个字符串消息或整数消息 xff0c 网上例程不少 xff0c ROSwiki上也有教程 xff0c 有时就需要一次发送不止一个数据 xff0c 这时候就得用到数组了 xff0c C 43 43 的也好找 xff0c 不过py
  • c++中string、char *、char[]相互转换

    一 string转char 主要有三种方法可以将str转换为char 类型 xff0c 分别是 xff1a data c str copy 其中 xff0c copy 可能会报安全性错误 xff0c 自行解决即可 3 1 data 方法 s
  • char数组与char指针

    转载来源 xff1a https www cnblogs com nzbbody p 3553222 html https blog csdn net jack 20 article details 78913202 一 0 的添加 存在的
  • linux下tcpdump的使用

    简介 用简单的话来定义tcpdump xff0c 就是 xff1a dump the traffic on a network xff0c 根据使用者的定义对网络上的数据包进行截获的包分析工具 tcpdump可以将网络中传送的数据包的 头
  • khadas vim3安装ros1

    khadas vim3 按照网上的方法可以正常安装ros2 xff0c 但是按照ros1则可能会有一些奇奇怪怪的问题导致按照失败 xff0c 不过在一位群友的帮助下 xff0c 找到了解决的方法 khadas vim3 将源换为下面 xff
  • 【Android】CMake添加多个c文件

    1 准备工作 先下相关的插件 xff0c 进入setting xff0c 勾选这LLDB NDK CMake三个 xff0c 点击OK后即可下载 2 Native C 43 43 工程 简单总结一下CMake使用的操作步骤 1 新建Nati
  • 什么是字节序(端序、低端字节序、高端字节序、网络字节序)

    前言 一个内容为12 xff08 字符串 xff09 的文本文件 xff0c 它的第一个字节是什么 xff08 小端序 xff09 xff1f 如果你的回答是0x32 xff0c 那你真的应该好好理解下字节序了 如下图所示 xff0c 我这
  • APM中电机输出分析

    一 APM类分析 老规矩 xff0c 先上类图 xff08 1 xff09 如图 xff08 1 xff09 所示 xff0c AP Motors是大部分电机类的父类 xff0c 是AC AttitudeControl姿态控制类的保护型成员
  • 解决安装ROS时出现的sudo rosdep init错误问题

    解决安装ROS时出现的sudo rosdep init错误问题 目前安装ROS时输入sudo rosdep init的命令时 xff0c 可能会出现以下的错误 xff1a ERROR cannot download default sour
  • JS实现HTTP请求头-Basic Authorization

    HTTP协议中的 Authorization 请求消息头含有服务器用于验证用户代理身份的凭证 xff0c 通常会在服务器返回401 Unauthorized 状态码以及WWW Authenticate 消息头之后在后续请求中发送此消息头 A
  • C语言Post和Get方法 ,拿过去直接用

    C语言post 和get 方法的实现 我自己实现的post 和get 请求方法 xff0c 可以直接使用在单片机上 xff0c 比如ESP32 上 xff0c OPl1000 上面 xff0c 下面直接上代码 span class toke
  • QT中图表类QChart系列之(1)-基本用法,画折线图、各个类之间的关系

    参考 xff1a https www cnblogs com yunhaisoft p 5180127 html 首先要注意3点 xff1a xff08 1 xff09 在 pro文件中添加 xff1a QT 43 61 charts xf
  • STM32使用FIFO实现USART串口发送中断

    fifo就不要造轮子了 xff0c 用现成的就行了 linux内核中有目前人类写出的基于c语言的最强FIFO xff0c 请自行搜索学习 巧夺天工的kfifo xff0c kfifo精妙无比 xff0c 实在是高 xff0c 其中用到的环回
  • c++的json读取操作

    使用的开源库是nlohmann json 后续操作也都是基于该开源库操作 本地json文件如下 xff1a 34 model config 34 34 model type 34 34 paddlex 34 34 model cfg fil