背景:项目开发过程,模型文件路径较多,初始化中需要读取多个路径。方便管理统一写在一个json文件中。

config.json
{
  "yolov5_mnn":{
    "model_path": "../model/mmrotate/barcode_sim.mnn"
  }
}
demo.cpp
#include <iostream>
#include <fstream>

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"   


    
std::ifstream configFile("../config.json");
// 将文件内容读入字符串
std::string content((std::istreambuf_iterator<char>(configFile)),
                        std::istreambuf_iterator<char>());

// 关闭文件
configFile.close();

// 解析 JSON
rapidjson::Document jsonConfig;
if (!jsonConfig.Parse(content.c_str()).HasParseError()) {
    if (jsonConfig.HasMember("yolov5_mnn")) {
        const rapidjson::Value& yolov5_mnn = jsonConfig["yolov5_mnn"];
        const rapidjson::Value &model_path = yolov5_mnn["model_path"];
        if (model_path.IsString()) {
            yolov5_mnn_path = model_path.GetString();
            std::cout << yolov5_mnn_path << std::endl;
        }
    }
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐