From d5ae26dfef539af2e4141527e2b68a2fd43d6d7f Mon Sep 17 00:00:00 2001 From: Davide Sapienza Date: Tue, 15 Oct 2019 09:59:39 +0200 Subject: [PATCH] Add configuration file to read CLASS parameters This commit changes the parameters reading. It introduces getopt to read input parameters from command line and it uses a configuration yaml file to read the input parameters for the network, the map and the cameras. This commit fixes a bug in message sending to the aggregator. Signed-off-by: Davide Sapienza --- demo/demo/demo.cpp | 127 +++++++------------------- include/configuration.h | 21 +++++ src/class_src/configuration.cpp | 154 ++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+), 94 deletions(-) create mode 100644 include/configuration.h create mode 100644 src/class_src/configuration.cpp diff --git a/demo/demo/demo.cpp b/demo/demo/demo.cpp index 3d311ca..0786483 100644 --- a/demo/demo/demo.cpp +++ b/demo/demo/demo.cpp @@ -4,6 +4,7 @@ #include "Yolo3Detection.h" #include "message.h" #include "visualization.h" +#include "configuration.h" #include "tracker.h" #include "../masa_protocol/include/send.hpp" @@ -483,7 +484,7 @@ void *computationTask(void *x_void_ptr) } std::cout << "There are " << trackers.size() << " trackers" << std::endl; //prepare message with tracker info - if (trackers.size() == 0) + if (trackers.size() != 0) { // mutex_cv.lock(); addRoadUserfromTracker(trackers, m, gc, maskOrient, adfGeoTransform, H); @@ -533,120 +534,58 @@ int main(int argc, char *argv[]) std::cout << "detection\n"; signal(SIGINT, sig_handler); srand(time(NULL)); - bool no_params = false; //flag to indicate if there are camera parameters or we use mp4 test video - char *net = (char *)"yolo3_coco4.rt"; - if (argc > 1) - net = argv[1]; - char *tiffile = (char *)"../demo/demo/data/map_b.tif"; - if (argc > 2) - tiffile = argv[2]; - char *n; - if (argc > 3) - { - n = argv[3]; - if (strcmp(n, "-n")) - return -1; - }; - int n_cameras = 0; - if (argc > 4) - { - n_cameras = atoi(argv[4]); - if (argc < 5 + 7 * n_cameras) - { - std::cout << "too few parameters\n"; - return -1; - } - if (!n_cameras) - { - n_cameras = 1; - no_params = true; - } - } - Camera_t cameras[n_cameras]; - bool *to_show = (bool *)malloc(n_cameras * sizeof(bool)); - if (no_params) - { - cameras[0].CAM_IDX = 20936; - cameras[0].input = (char *)"../demo/demo/data/single_ped_2.mp4"; - cameras[0].pmatrix = (char *)"../demo/demo/data/pmundist.txt"; - cameras[0].maskfile = (char *)"../demo/demo/data/mask36.jpg"; - cameras[0].cameraCalib = (char *)"../demo/demo/data/calib36.params"; - cameras[0].maskFileOrient = (char *)"../demo/demo/data/mask_orient/6315_mask_orient.jpg"; - cameras[0].to_show = true; - to_show[0] = true; - } - else - { - for (int i = 0; i < n_cameras; i++) - cameras[i].CAM_IDX = atoi(argv[5 + i]); - for (int i = 0; i < n_cameras; i++) - cameras[i].input = argv[5 + n_cameras + i]; - for (int i = 0; i < n_cameras; i++) - cameras[i].pmatrix = argv[5 + 2 * n_cameras + i]; - for (int i = 0; i < n_cameras; i++) - cameras[i].maskfile = argv[5 + 3 * n_cameras + i]; - for (int i = 0; i < n_cameras; i++) - cameras[i].cameraCalib = argv[5 + 4 * n_cameras + i]; - for (int i = 0; i < n_cameras; i++) - cameras[i].maskFileOrient = argv[5 + 5 * n_cameras + i]; - for (int i = 0; i < n_cameras; i++) - { - cameras[i].to_show = atoi(argv[5 + 6 * n_cameras + i]); //only one camera can be shown - to_show[i] = atoi(argv[5 + 6 * n_cameras + i]); - } - } - //TODO now only one camera can be visualized - int check_visualization = 0; - for (int i = 0; i < n_cameras; i++) - check_visualization += to_show[i]; - if (check_visualization > 1) + Parameters_t par; + + if(!read_parameters(argc, argv, &par)) return -1; - - tk::dnn::Yolo3Detection yolo[n_cameras]; - for (int i = 0; i < n_cameras; i++) - { - yolo[i].init(net); - yolo[i].thresh = 0.25; - } + + // tk::dnn::Yolo3Detection yolo[par.n_cameras]; + // for(int i=0; i +#include + +#include + +struct Parameters_t +{ + char *net; + char *tiffile; + int n_cameras; + Camera_t *cameras; +}; + +void readCamerasParametersYaml(const std::string &camerasParams, Parameters_t *par); +bool read_parameters(int argc, char *argv[], Parameters_t *par); + +#endif /*CONFIGURATION_H*/ \ No newline at end of file diff --git a/src/class_src/configuration.cpp b/src/class_src/configuration.cpp new file mode 100644 index 0000000..bda2024 --- /dev/null +++ b/src/class_src/configuration.cpp @@ -0,0 +1,154 @@ +#include "configuration.h" + +/* Read the configuration file. The yaml file contains the information about the network, + * the map and the cameras. +**/ +void readCamerasParametersYaml(const std::string &camerasParams, Parameters_t *par) +{ + + std::string cam, tmp; + YAML::Node config = YAML::LoadFile(camerasParams); + tmp = config["weights"].as();//config["lastLogin"].as() + par->net = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->net, tmp.c_str()); + par->net[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<net<(); + par->tiffile = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->tiffile, tmp.c_str()); + par->tiffile[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<tiffile<n_cameras; i++) + { + cam = std::to_string(par->cameras[i].CAM_IDX); + // std::cout<<"read "<(); + par->cameras[i].input = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->cameras[i].input, tmp.c_str()); + par->cameras[i].input[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<cameras[i].input<(); + par->cameras[i].pmatrix = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->cameras[i].pmatrix, tmp.c_str()); + par->cameras[i].pmatrix[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<cameras[i].pmatrix<(); + par->cameras[i].maskfile = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->cameras[i].maskfile, tmp.c_str()); + par->cameras[i].maskfile[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<cameras[i].maskfile<(); + par->cameras[i].cameraCalib = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->cameras[i].cameraCalib, tmp.c_str()); + par->cameras[i].cameraCalib[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<cameras[i].cameraCalib<(); + par->cameras[i].maskFileOrient = (char *) malloc((strlen(tmp.c_str())+1) * sizeof(char)); + strcpy(par->cameras[i].maskFileOrient, tmp.c_str()); + par->cameras[i].maskFileOrient[strlen(tmp.c_str())] = '\0'; + // std::cout<<"- "<cameras[i].maskFileOrient<n_cameras = 0; + int opt; + char *help = "Yolo3 demo\nCommand:\n -i\tencrypted parameters file\n -n\tnumer of cameras\n \tlist of camera numbers (see -n)\n \tlist of flags for the visualization (see -n)\n\n"; + // first read n_cameras parameter and/or help option + while((opt = getopt(argc, argv, ":n:i:h")) != -1) + { + switch(opt) + { + case 'n': //number of cameras + std::cout<<"number of cameras"<n_cameras = atoi(optarg); + break; + case 'h': + std::cout<<"help"<n_cameras = 1; + break; + case '?': + printf("unknown option: %c\n", optopt); + break; + } + } + par->cameras = (Camera_t *) malloc(par->n_cameras * sizeof(Camera_t)); + bool *to_show = (bool *) malloc(par->n_cameras * sizeof(bool)); + if(no_params) + { + par->net = "yolo3_coco4.rt"; + par->tiffile = "../demo/demo/data/map_b.tif"; + par->cameras[0].CAM_IDX = 20936; + par->cameras[0].input = (char *)"../demo/demo/data/single_ped_2.mp4"; + par->cameras[0].pmatrix = (char *)"../demo/demo/data/pmundist.txt"; + par->cameras[0].maskfile = (char *)"../demo/demo/data/mask36.jpg"; + par->cameras[0].cameraCalib = (char *)"../demo/demo/data/calib36.params"; + par->cameras[0].maskFileOrient = (char *)"../demo/demo/data/mask_orient/6315_mask_orient.jpg"; + par->cameras[0].to_show = true; + to_show[0] = true; + } + else + { + int i = 0; + for(; optind < argc; optind++) + { + if(i==par->n_cameras) + break; + printf("extra arguments: %s\n", argv[optind]); + par->cameras[i].CAM_IDX = atoi(argv[optind]); + i++; + } + i=0; + for(; optind < argc; optind++) + { + if(i==par->n_cameras) + break; + printf("extra arguments: %s\n", argv[optind]); + par->cameras[i].to_show = atoi(argv[optind]); //only one camera can be shown + to_show[i] = par->cameras[i].to_show; + i++; + } + // TODO: now only one camera can be visualized + int check_visualization=0; + for(int i = 0; in_cameras; i++) + check_visualization += to_show[i]; + if(check_visualization > 1) + return false; + //decrypt the input file, save it in tmp directory + char s[200] = ""; + strcat(s, "openssl enc -aes-256-cbc -d -in "); + strcat(s, params.c_str()); + strcat(s, " -base64 -md sha1 -out /tmp/decrypt.yaml"); + if(system(s)) + { + fprintf(stderr, "Error system\n"); + return false; + } + // read the file yaml to get the camera parameters + readCamerasParametersYaml("/tmp/decrypt.yaml", par); + // delete the file decrypted + if (system("rm /tmp/decrypt.yaml")) + { + fprintf(stderr, "Error system\n"); + return false; + } + } + return true; +} \ No newline at end of file