crose72 / OperationSquirrel

Building an autonomous drone using OpenCV and CUDA and NVIDIA Jetson devices.
GNU General Public License v3.0
5 stars 2 forks source link

move parameters to json file #26

Open sbow opened 3 days ago

sbow commented 3 days ago

Why:

example, move "calibrations" to a json file like resolution in this snippet:

/********************************************************************************
 * Function: create_input_video_stream
 * Description: Create an input video stream from the attached cameras.
 ********************************************************************************/
bool create_input_video_stream(void)
{
    cap.open(0);  // Open default webcam
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 720);

    if (!cap.isOpened())
    {
        std::cout << "Error: Could not open camera" << std::endl;
        return false;
    }

    return true;
}

Use this:

https://github.com/nlohmann/json

Like this:

1st creat a "config" struct for use elsewhere in the program:

#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>

struct Config {
    int frameWidth = 1280;
    int frameHeight = 720;

    // Method to load configuration from JSON file
    void loadFromFile(const std::string& fileName) {
        std::ifstream configFile(fileName);
        if (configFile) {
            nlohmann::json configJson;
            configFile >> configJson;

            // Use structured binding to iterate and assign values dynamically
            for (auto& [key, value] : configJson.items()) {
                if (key == "frame_width") frameWidth = value.get<int>();
                else if (key == "frame_height") frameHeight = value.get<int>();
                // Add more parameters here if needed
            }
        } else {
            std::cerr << "Could not open config file: " << fileName << std::endl;
        }
    }
};

then refer to it elsewhere like:

bool create_input_video_stream(const Config& config) {
    cap.open(0);
    cap.set(cv::CAP_PROP_FRAME_WIDTH, config.frameWidth);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, config.frameHeight);

    if (!cap.isOpened()) {
        std::cout << "Error: Could not open camera" << std::endl;
        return false;
    }
    return true;
}

int main() {
    Config config;
    config.loadFromFile("config.json");

    create_input_video_stream(config);
    return 0;
}
crose72 commented 2 days ago

@sbow I think that's a great idea! I do have a json file in using to store pid controller parameters in the SquirrelDefender folder. Did you check that out? I already have a file called parameters.cpp where I have the functionality to parse that json file. But I didn't move all of the parameters to that file. We can move them all there. I'm also using a different json library. But I'm open to the one you suggested. I want it to be common for the windows and edge side if possible. What do you think?

I did fork the repo of that json library in case we want to use that in our project so we can always have a working version that we control.