Select Git revision
metadata.yaml
sandboxConfig.cpp 9.03 KiB
#include "../../inc/sandboxConfig.h"
static int saveConfigIn(char *path, YAML::Node config);
int SandboxConfig::saveAdjustingMatrixInto(char *path, cv::Mat_<float> matrix){
// convert matrix into a one layer vector
YAML::Node vec = YAML::Load("[]");
for (int y = 0; y < matrix.rows; y++){
for (int x = 0; x < matrix.cols; x++){
vec.push_back(matrix.at<float>(y, x));
}
}
YAML::Node val;
val["width"] = matrix.cols;
val["height"] = matrix.rows;
val["matrix"] = vec;
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
}
config[SCFG_MATRIX] = val;
return saveConfigIn(path, config);
}
int SandboxConfig::saveDistanceToSandboxTopInto(char *path, float distance){
YAML::Node val;
val["distance"] = distance;
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
}
config[SCFG_DISTANCE] = val;
return saveConfigIn(path, config);
}
int SandboxConfig::saveCroppingMaskInto(char *path, cv::Rect mask){
YAML::Node val;
val["x"] = mask.x;
val["y"] = mask.y;
val["width"] = mask.width;
val["height"] = mask.height;
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
}
config[SCFG_MASK] = val;
return saveConfigIn(path, config);
}
int SandboxConfig::saveBeamerPositionInto(char *path, cv::Point3f pos){
YAML::Node val;
val["x"] = pos.x;
val["y"] = pos.y;
val["z"] = pos.z;
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
}
config[SCFG_POSITION] = val;
return saveConfigIn(path, config);
}
int SandboxConfig::saveBeamerResolutionInto(char *path, cv::Size res){
YAML::Node val;
val["width"] = res.width;
val["height"] = res.height;
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
}
config[SCFG_RESOLUTION] = val;
return saveConfigIn(path, config);
}
int SandboxConfig::saveFrameProcessProfilInto(char *path, FrameProcessProfil profil){
YAML::Node val;
val["contrast"] = profil.getContrast();
val["brightness"] = profil.getBrightness();
val["minDistance"] = profil.getMinDistance();
val["cannyEdgeThreshold"] = profil.getCannyEdgeThreshold();
val["houghAccThreshold"] = profil.getHoughAccThreshold();
val["minRadius"] = profil.getMinRadius();
val["maxRadius"] = profil.getMaxRadius();
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
}
config[SCFG_PROCESS_PROFIL] = val;
return saveConfigIn(path, config);
}
int SandboxConfig::loadAdjustingMatrixFrom(char *path, Projection *projection){
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
return 1;
}
// no node for adjusting matrix
if(!config[SCFG_MATRIX]){
return 2;
};
// uncomplet data for adjusting matrix
if(!(config[SCFG_MATRIX]["width"] && config[SCFG_MATRIX]["height"] && config[SCFG_MATRIX]["matrix"])){
return 2;
}
int height = config[SCFG_MATRIX]["height"].as<int>();
int width = config[SCFG_MATRIX]["width"].as<int>();
std::vector<float> values = config[SCFG_MATRIX]["matrix"].as<std::vector<float>>();
// verify the number of values in the matrix
if(height*width != (int)values.size()){
return 2;
}
int v_i = 0;
cv::Mat matrix( height, width, CV_32F);
for(int i=0; i<height; i++){
for (int j=0; j<width; j++){
matrix.at<float>(i,j) = values.at(v_i);
v_i++;
}
}
projection->setAdjustingMatrix(matrix);
return 0;
}
int SandboxConfig::loadDistanceToSandboxTopFrom(char *path, Projection *projection){
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
return 1;
}
if(!config[SCFG_DISTANCE]){
return 2;
};
if(!(config[SCFG_DISTANCE]["distance"])){
return 2;
}
float distance = config[SCFG_DISTANCE]["distance"].as<float>();
projection->setDistanceTopSandbox(distance);
return 0;
}
int SandboxConfig::loadCroppingMaskFrom(char *path, Camera *camera){
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
return 1;
}
// no node for cropping mask
if(!config[SCFG_MASK]){
return 2;
};
// uncomplet data for cropping mask
if(!(config[SCFG_MASK]["x"] && config[SCFG_MASK]["y"] && config[SCFG_MASK]["width"] && config[SCFG_MASK]["height"])){
return 2;
}
cv::Rect mask( cv::Point( config[SCFG_MASK]["x"].as<int>(),
config[SCFG_MASK]["y"].as<int>()),
cv::Size( config[SCFG_MASK]["width"].as<int>(),
config[SCFG_MASK]["height"].as<int>()));
camera->setCroppingMask(mask);
return 0;
}
int SandboxConfig::loadBeamerPositionFrom(char *path, Beamer *beamer){
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
return 1;
}
// no node for beamer position
if(!config[SCFG_POSITION]){
return 2;
};
// uncomplet data for beamer position
if(!(config[SCFG_POSITION]["x"] && config[SCFG_POSITION]["y"] && config[SCFG_POSITION]["z"])){
return 2;
}
cv::Point3f pos( config[SCFG_POSITION]["x"].as<float>(),
config[SCFG_POSITION]["y"].as<float>(),
config[SCFG_POSITION]["z"].as<float>());
beamer->setPosition(pos);
return 0;
}
int SandboxConfig::loadBeamerResolutionFrom(char *path, Beamer *beamer){
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
return 1;
}
// no node for beamer resolution
if(!config[SCFG_RESOLUTION]){
return 2;
};
// uncomplet data for beamer resolution
if(!(config[SCFG_RESOLUTION]["height"] && config[SCFG_RESOLUTION]["width"])){
return 2;
}
cv::Size res( config[SCFG_RESOLUTION]["width"].as<int>(),
config[SCFG_RESOLUTION]["height"].as<int>());
beamer->setWidth(res.width);
beamer->setHeight(res.height);
return 0;
}
int SandboxConfig::loadFrameProcessProfilFrom(char *path, FrameProcessProfil *profil){
YAML::Node config;
try{
config = YAML::LoadFile(path);
}catch(YAML::BadFile err){
//std::cout << "[Error] No Config File found : " << err.what() << std::endl;
return 1;
}
// no node for frame process profil
if(!config[SCFG_PROCESS_PROFIL]){
return 2;
};
// uncomplet data for frame process profil
if(!( config[SCFG_PROCESS_PROFIL]["contrast"] &&
config[SCFG_PROCESS_PROFIL]["brightness"] &&
config[SCFG_PROCESS_PROFIL]["minDistance"] &&
config[SCFG_PROCESS_PROFIL]["cannyEdgeThreshold"] &&
config[SCFG_PROCESS_PROFIL]["houghAccThreshold"] &&
config[SCFG_PROCESS_PROFIL]["minRadius"] &&
config[SCFG_PROCESS_PROFIL]["maxRadius"]
)){
return 2;
}
profil->setContrast(config[SCFG_PROCESS_PROFIL]["contrast"].as<double>());
profil->setBrightness(config[SCFG_PROCESS_PROFIL]["brightness"].as<int>());
profil->setMinDistance(config[SCFG_PROCESS_PROFIL]["minDistance"].as<uint>());
profil->setCannyEdgeThreshold(config[SCFG_PROCESS_PROFIL]["cannyEdgeThreshold"].as<uint>());
profil->setHoughAccThreshold(config[SCFG_PROCESS_PROFIL]["houghAccThreshold"].as<uint>());
profil->setMinRadius(config[SCFG_PROCESS_PROFIL]["minRadius"].as<uint>());
profil->setMaxRadius(config[SCFG_PROCESS_PROFIL]["maxRadius"].as<uint>());
return 0;
}
static int saveConfigIn(char *path, YAML::Node config){
YAML::Emitter out;
out << config;
std::ofstream myfile;
try{
myfile.open(path);
}catch(std::ofstream::failure &er){
return 1;
}
myfile << out.c_str();
myfile.close();
return 0;
}