Skip to content
Snippets Groups Projects
Commit ac736fa0 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Refactor configuration to common code

parent 0e5bf6d1
Branches
Tags
No related merge requests found
...@@ -114,6 +114,7 @@ endif() ...@@ -114,6 +114,7 @@ endif()
SET(CMAKE_USE_RELATIVE_PATHS ON) SET(CMAKE_USE_RELATIVE_PATHS ON)
add_subdirectory(common/cpp)
add_subdirectory(renderer) add_subdirectory(renderer)
add_subdirectory(net) add_subdirectory(net)
add_subdirectory(vision) add_subdirectory(vision)
... ...
......
{ {
"vision": {
"middlebury": { "middlebury": {
"dataset": "", "dataset": "",
"threshold": 10.0, "threshold": 10.0,
...@@ -61,6 +62,7 @@ ...@@ -61,6 +62,7 @@
}, },
"stream": { "stream": {
"name": "dummy" "name": "dummy"
}
}, },
"representation": { "representation": {
"net": { "net": {
... ...
......
set(COMMONSRC
src/config.cpp
src/configuration.cpp
)
add_library(ftlcommon ${COMMONSRC})
target_include_directories(ftlcommon PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE src)
target_link_libraries(ftlcommon glog::glog)
#ifndef _FTL_COMMON_CONFIGURATION_HPP_
#define _FTL_COMMON_CONFIGURATION_HPP_
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace ftl {
extern nlohmann::json config;
std::vector<std::string> configure(int argc, char **argv, const std::string &app);
};
#endif // _FTL_COMMON_CONFIGURATION_HPP_
#include <glog/logging.h>
#include <ftl/config.h>
#include <nlohmann/json.hpp>
#include <ftl/configuration.hpp>
#include <fstream>
#include <string>
#include <map>
#include <iostream>
using nlohmann::json;
using std::ifstream;
using std::string;
using std::map;
using std::vector;
// Store loaded configuration
namespace ftl {
json config;
};
using ftl::config;
/**
* Find and load a JSON configuration file
*/
static bool findConfiguration(const string &file, const vector<string> &paths,
const std::string &app) {
ifstream i;
if (file != "") i.open(file);
if (!i.is_open()) i.open("./config.json");
if (!i.is_open()) i.open(FTL_LOCAL_CONFIG_ROOT "/config.json");
if (!i.is_open()) i.open(FTL_GLOBAL_CONFIG_ROOT "/config.json");
if (!i.is_open()) return false;
i >> config;
config = config[app];
return true;
}
/**
* Generate a map from command line option to value
*/
static map<string, string> read_options(char ***argv, int *argc) {
map<string, string> opts;
while (*argc > 0) {
string cmd((*argv)[0]);
if (cmd[0] != '-') break;
size_t p;
if ((p = cmd.find("=")) == string::npos) {
opts[cmd.substr(2)] = "true";
} else {
opts[cmd.substr(2, p-2)] = cmd.substr(p+1);
}
(*argc)--;
(*argv)++;
}
return opts;
}
/**
* Put command line options into json config. If config element does not exist
* or is of a different type then report an error.
*/
static void process_options(const map<string, string> &opts) {
for (auto opt : opts) {
if (opt.first == "config") continue;
if (opt.first == "version") {
std::cout << "FTL Vision Node - v" << FTL_VERSION << std::endl;
std::cout << FTL_VERSION_LONG << std::endl;
exit(0);
}
try {
auto ptr = json::json_pointer("/"+opt.first);
// TODO(nick) Allow strings without quotes
auto v = json::parse(opt.second);
if (v.type() != config.at(ptr).type()) {
LOG(ERROR) << "Incorrect type for argument " << opt.first;
continue;
}
config.at(ptr) = v;
} catch(...) {
LOG(ERROR) << "Unrecognised option: " << opt.first;
}
}
}
vector<string> ftl::configure(int argc, char **argv, const std::string &app) {
argc--;
argv++;
// Process Arguments
auto options = read_options(&argv, &argc);
vector<string> paths;
while (argc-- > 0) {
paths.push_back(argv[0]);
}
if (!findConfiguration(options["config"], paths, app)) {
LOG(FATAL) << "Could not find any configuration!";
}
process_options(options);
return paths;
}
...@@ -5,7 +5,6 @@ include_directories(${PROJECT_SOURCE_DIR}/vision/include) ...@@ -5,7 +5,6 @@ include_directories(${PROJECT_SOURCE_DIR}/vision/include)
add_subdirectory(lib) add_subdirectory(lib)
set(CVNODESRC set(CVNODESRC
../common/cpp/src/config.cpp
src/main.cpp src/main.cpp
src/calibrate.cpp src/calibrate.cpp
src/local.cpp src/local.cpp
...@@ -38,6 +37,7 @@ if (CUDA_FOUND) ...@@ -38,6 +37,7 @@ if (CUDA_FOUND)
endif (CUDA_FOUND) endif (CUDA_FOUND)
add_executable(ftl-vision ${CVNODESRC}) add_executable(ftl-vision ${CVNODESRC})
add_dependencies(ftl-vision ftlcommon)
add_dependencies(ftl-vision ftlnet) add_dependencies(ftl-vision ftlnet)
add_dependencies(ftl-vision ftlrender) add_dependencies(ftl-vision ftlrender)
add_dependencies(ftl-vision libelas) add_dependencies(ftl-vision libelas)
...@@ -47,6 +47,6 @@ set_property(TARGET ftl-vision PROPERTY CUDA_SEPARABLE_COMPILATION ON) ...@@ -47,6 +47,6 @@ set_property(TARGET ftl-vision PROPERTY CUDA_SEPARABLE_COMPILATION ON)
endif() endif()
#target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include) #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(ftl-vision ftlrender Threads::Threads ZLIB::ZLIB libelas ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ftlnet) target_link_libraries(ftl-vision ftlcommon ftlrender Threads::Threads ZLIB::ZLIB libelas ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ftlnet)
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
#include <glog/logging.h> #include <glog/logging.h>
#include <ftl/config.h> #include <ftl/configuration.hpp>
#include <ctpl_stl.h> #include <ctpl_stl.h>
#include <string> #include <string>
...@@ -53,78 +53,8 @@ using std::mutex; ...@@ -53,78 +53,8 @@ using std::mutex;
using std::unique_lock; using std::unique_lock;
using cv::Mat; using cv::Mat;
using json = nlohmann::json; using json = nlohmann::json;
using std::ifstream; using ftl::config;
// Store loaded configuration
static json config;
/**
* Find and load a JSON configuration file
*/
static bool findConfiguration(const string &file) {
ifstream i;
if (file != "") i.open(file);
if (!i.is_open()) i.open("./config.json");
if (!i.is_open()) i.open(FTL_LOCAL_CONFIG_ROOT "/config.json");
if (!i.is_open()) i.open(FTL_GLOBAL_CONFIG_ROOT "/config.json");
if (!i.is_open()) return false;
i >> config;
return true;
}
/**
* Generate a map from command line option to value
*/
map<string, string> read_options(char ***argv, int *argc) {
map<string, string> opts;
while (*argc > 0) {
string cmd((*argv)[0]);
if (cmd[0] != '-') break;
size_t p;
if ((p = cmd.find("=")) == string::npos) {
opts[cmd.substr(2)] = "true";
} else {
opts[cmd.substr(2, p-2)] = cmd.substr(p+1);
}
(*argc)--;
(*argv)++;
}
return opts;
}
/**
* Put command line options into json config. If config element does not exist
* or is of a different type then report an error.
*/
static void process_options(const map<string, string> &opts) {
for (auto opt : opts) {
if (opt.first == "config") continue;
if (opt.first == "version") {
std::cout << "FTL Vision Node - v" << FTL_VERSION << std::endl;
std::cout << FTL_VERSION_LONG << std::endl;
exit(0);
}
try {
auto ptr = json::json_pointer("/"+opt.first);
// TODO(nick) Allow strings without quotes
auto v = json::parse(opt.second);
if (v.type() != config.at(ptr).type()) {
LOG(ERROR) << "Incorrect type for argument " << opt.first;
continue;
}
config.at(ptr) = v;
} catch(...) {
LOG(ERROR) << "Unrecognised option: " << opt.first;
}
}
}
static void run(const string &file) { static void run(const string &file) {
ctpl::thread_pool pool(2); ctpl::thread_pool pool(2);
...@@ -230,19 +160,11 @@ static void run(const string &file) { ...@@ -230,19 +160,11 @@ static void run(const string &file) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
argc--; auto paths = ftl::configure(argc, argv, "vision");
argv++;
// Process Arguments
auto options = read_options(&argv, &argc);
if (!findConfiguration(options["config"])) {
LOG(FATAL) << "Could not find any configuration!";
}
process_options(options);
// Choose normal or middlebury modes // Choose normal or middlebury modes
if (config["middlebury"]["dataset"] == "") { if (config["middlebury"]["dataset"] == "") {
run((argc > 0) ? argv[0] : ""); run((paths.size() > 0) ? paths[0] : "");
} else { } else {
ftl::middlebury::test(config); ftl::middlebury::test(config);
} }
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment