From 5a1c7fb83df36fa2e1fd5209e42090765ecf7aab Mon Sep 17 00:00:00 2001 From: Davide Sapienza Date: Thu, 26 Sep 2019 16:39:16 +0200 Subject: [PATCH] Move several cameras into a single process. This commit pairs a camera with a thread. In this way, the single process can to manage several camera. Every camera thread create one video capture thread to read its input stream. Only one camera thread can start the visualization. This commit fixes the wrong data reading from file of the read_projection_matrix function. This commit fixes the wrong orientation mask accessing of the addRoadUserfromTracker function. In the code there are two sections to test. --- demo/demo/demo.cpp | 631 +++++++++++++++++++++++++++---------------- include/classutils.h | 93 +++++-- 2 files changed, 460 insertions(+), 264 deletions(-) diff --git a/demo/demo/demo.cpp b/demo/demo/demo.cpp index 732c151..b6c5def 100644 --- a/demo/demo/demo.cpp +++ b/demo/demo/demo.cpp @@ -32,17 +32,19 @@ #include "trackutils.h" #include "plot.h" #include "tracker.h" +#include #define MAX_DETECT_SIZE 100 +std::chrono::steady_clock::time_point local_clock_start; + +std::mutex mutexgRun; + bool gRun; std::string obj_class[10]{"person", "car", "truck", "bus", "motor", "bike", "rider", "traffic light", "traffic sign", "train"}; -// sem for mainthread, detectionthread and topviewthread -// sem_vc for mainthread, videocapturethread, originalthread and disparitythread -std::mutex sem, sem_vc; -// a single mutex for each operation - the show_updates function must get all mutex -std::mutex mutex_o, mutex_de, mutex_t, mutex_di; +//mutex for some opencv operations +std::mutex mutex_cv; struct ModFrame_t{ std::vector trackers; @@ -52,91 +54,150 @@ struct ModFrame_t{ cv::Mat original_frame; tk::dnn::Yolo3Detection yolo; cv::Mat mask; + // sem for mainthread, detectionthread and topviewthread + std::mutex sem; }; struct Frame_t{ char *input; cv::Mat frame; int frame_nbr; + // sem_vc for mainthread, videocapturethread, originalthread and disparitythread + std::mutex sem_vc; +}; + +struct Camera_t{ + int CAM_IDX; + char *input; + char *pmatrix; + char *maskfile; + char *cameraCalib; + char *maskFileOrient; + bool to_show; + tk::dnn::Yolo3Detection yolo; + double adfGeoTransform[6]; }; struct Show_t{ cv::Mat original, detection, topview, disparity; bool update_o, update_de, update_t, update_di; + // a single mutex for each operation - the show_updates function must get all mutex + std::mutex mutex_o, mutex_de, mutex_t, mutex_di; }updates; void sig_handler(int signo) { std::cout << "request gateway stop\n"; + mutexgRun.lock(); gRun = false; + mutexgRun.unlock(); } void *readVideoCapture(void *x_void_ptr) { + std::cout<<"readVideoCapture start...\n"; + Frame_t *info_f = (Frame_t *) x_void_ptr; - cv::VideoCapture cap(info_f->input); - cv::Mat frame_loc; + mutex_cv.lock(); + cv::VideoCapture cap(info_f->input, cv::CAP_FFMPEG); + mutex_cv.unlock(); + cv::Mat frame_loc, frame0; int frame_nbr_loc = 0; // bool to_show = false; if (!cap.isOpened()) + { + mutexgRun.lock(); gRun = false; + mutexgRun.unlock(); + } else std::cout << "camera started\n"; - cap.set(cv::CAP_PROP_BUFFERSIZE,3); - // cap.set(CV_CAP_PROP_FPS, 10); - // // CV_CAP_PROP_BUFFERSIZE - std::cout<<"buf size: "<> frame; - // i++; - // } - - // i=0; - // start_t = std::chrono::steady_clock::now(); - // while (i> frame; - // i++; - // std::cout << " step "<(std::chrono::steady_clock::now() - step_t).count() << " ms"<(end_t - start_t).count() << " ms"<> frame_loc; + i++; + } + + i=0; + start_t = std::chrono::steady_clock::now(); + while (i> frame_loc; + mean_time = mean_time + std::chrono::duration_cast(std::chrono::steady_clock::now() - step_t).count(); + std::cout << " step "<(std::chrono::steady_clock::now() - step_t).count() << " ms"<(end_t - start_t).count() << " ms"<(local_clock_sync - local_clock_start).count()) / mean_time; + shift = (shift - (int)shift) * mean_time; + std::cout<<".-------------------------------\n"; + std::cout<<" mean time: "<(local_clock_sync - local_clock_start).count()<> frame_loc; + // mutex_cv.unlock(); + current_timestamp = std::chrono::steady_clock::now(); + shift = std::chrono::duration_cast(current_timestamp - local_clock_sync).count() ; + std::cout << " RELATIVE TIMESTAMP FRAME : "<= 0)? -(mean_time - shift) : shift; + std::cout<<"DELAY frame_"<input); printf("cap reinitialize\n"); + mutex_cv.unlock(); continue; } @@ -144,15 +205,17 @@ void *readVideoCapture(void *x_void_ptr) std::cout << " VC-TIME 1 : "<(end_t - start_t).count() << " ms"<sem_vc.lock(); info_f->frame = frame_loc.clone(); info_f->frame_nbr = frame_nbr_loc; - sem_vc.unlock(); + info_f->sem_vc.unlock(); + // usleep(50000); end_t = std::chrono::steady_clock::now(); std::cout << " VC-TIME 2 : "<(end_t - start_t).count() << " ms"<sem_vc.lock(); frame_loc = info_show_orig->frame.clone(); frame_nbr_loc = info_show_orig->frame_nbr; - sem_vc.unlock(); + info_show_orig->sem_vc.unlock(); if (frame_nbr_loc == 0) { usleep(1000000); printf("no frame received\n"); continue; } - mutex_o.lock(); + updates.mutex_o.lock(); updates.original = frame_loc.clone(); updates.update_o = true; - mutex_o.unlock(); + updates.mutex_o.unlock(); usleep(10000); //sleep 10 msec std::cout<<"originalFrame: "; TIMER_STOP } + return (void *)0; } void *detectionFrame(void *x_void_ptr) @@ -259,6 +324,7 @@ void *detectionFrame(void *x_void_ptr) int pix_x, pix_y; cv::Mat original_frame_loc; std::vector trackers; + geodetic_converter::GeodeticConverter gc; double adfGeoTransform[6]; cv::Mat H; @@ -271,15 +337,19 @@ void *detectionFrame(void *x_void_ptr) int x0, w, x1, y0, h, y1; int objClass; std::string det_class;; - float prob; + // float prob; cv::Scalar intensity; + std::vector map_p, camera_p; + int baseline = 0; + float fontScale = 0.5; + int thickness = 2; while (gRun) { TIMER_START // critical section: copy the struct in local variable // in this way we can unlock the sem for the main thread - sem.lock(); + info_show->sem.lock(); original_frame_loc = info_show->original_frame.clone(); // std::vector trackers; trackers = info_show->trackers; @@ -291,7 +361,7 @@ void *detectionFrame(void *x_void_ptr) H = info_show->H.clone(); yolo = info_show->yolo; mask = info_show->mask.clone(); - sem.unlock(); + info_show->sem.unlock(); if (trackers.empty()) { @@ -312,7 +382,7 @@ void *detectionFrame(void *x_void_ptr) y1 = b.y + h; objClass = b.cl; det_class = obj_class[b.cl]; - prob = b.prob; + // prob = b.prob; intensity = mask.at(cv::Point(int(x0 + b.w / 2), y1)); @@ -321,9 +391,6 @@ void *detectionFrame(void *x_void_ptr) //std::cout< map_p, camera_p; + map_p.clear(); + camera_p.clear(); map_p.push_back(cv::Point2f(pix_x, pix_y)); //transform camera pixel to map pixel cv::perspectiveTransform(map_p, camera_p, H.inv()); - cv::circle(original_frame_loc, cv::Point(camera_p[0].x, camera_p[0].y), 3.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0); + // std::cout<<"x,y: "<= 0 && camera_p[0].y >= 0) + cv::circle(original_frame_loc, cv::Point(camera_p[0].x, camera_p[0].y), 3.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0); } } - mutex_de.lock(); + updates.mutex_de.lock(); updates.detection = original_frame_loc.clone(); updates.update_de = true; - mutex_de.unlock(); + updates.mutex_de.unlock(); std::cout<<"detectionFrame: "; TIMER_STOP } + return (void *)0; } void *topviewFrame(void *x_void_ptr) @@ -366,8 +441,8 @@ void *topviewFrame(void *x_void_ptr) cv::Mat frame_top; cv::Mat original_frame_top; // original_frame_top = cv::imread("../demo/demo/data/map/map_geo.jpg"); - // original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670.png"); - original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670_V.png"); + original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670.png"); + // original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670_V.png"); std::vector trackers; geodetic_converter::GeodeticConverter gc; @@ -378,7 +453,7 @@ void *topviewFrame(void *x_void_ptr) TIMER_START // critical section: copy the struct in local variable // in this way we can unlock the sem for the main thread - sem.lock(); + info_show->sem.lock(); // std::vector trackers; trackers = info_show->trackers; // geodetic_converter::GeodeticConverter gc; @@ -387,7 +462,7 @@ void *topviewFrame(void *x_void_ptr) adfGeoTransform[i] = info_show->adfGeoTransform[i]; // cv::Mat H; H = info_show->H.clone(); - sem.unlock(); + info_show->sem.unlock(); if (trackers.empty()) { usleep(1000000); @@ -399,13 +474,8 @@ void *topviewFrame(void *x_void_ptr) { for (size_t p = 1; p < t.pred_list_.size(); p++) { - gc.enu2Geodetic(t.pred_list_[p].x_, t.pred_list_[p].y_, 0, &lat, &lon, &alt); - //std::cout << "lat: " << lat << " lon: " << lon << std::endl; - coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform); - - //std::cout << "pix_x: " << pix_x << " pix_y: " << pix_y << std::endl; if (pix_x < frame_top.cols && pix_y < frame_top.rows && pix_x >= 0 && pix_y >= 0) cv::circle(frame_top, cv::Point(pix_x, pix_y), 7.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0); @@ -413,15 +483,16 @@ void *topviewFrame(void *x_void_ptr) } //outputVideo<< frame_top; // ------------------------------------------------ - mutex_t.lock(); + updates.mutex_t.lock(); updates.topview = frame_top.clone(); updates.update_t = true; - mutex_t.unlock(); + updates.mutex_t.unlock(); std::cout<<"topviewFrame: "; TIMER_STOP } + return (void *)0; } void *disparityFrame(void *x_void_ptr) @@ -444,10 +515,10 @@ void *disparityFrame(void *x_void_ptr) step_t = start_t; // critical section: copy the struct in local variable // in this way we can unlock the sem for the main thread - sem_vc.lock(); + info_show_disparity->sem_vc.lock(); frame_loc = info_show_disparity->frame.clone(); frame_nbr_loc = info_show_disparity->frame_nbr; - sem_vc.unlock(); + info_show_disparity->sem_vc.unlock(); if (frame_nbr_loc == 0) { usleep(1000000); @@ -510,10 +581,10 @@ void *disparityFrame(void *x_void_ptr) // step_t_segmentation = end_t_segmentation; // //--------------------------------- - mutex_di.lock(); + updates.mutex_di.lock(); updates.disparity = disparity_frame.clone(); updates.update_di = true; - mutex_di.unlock(); + updates.mutex_di.unlock(); } pre_canny = canny.clone(); if(first_iteration) @@ -522,95 +593,100 @@ void *disparityFrame(void *x_void_ptr) std::cout<<"disparityFrame : TIME END pre canny : "<(end_t - start_t).count() << " ms"< 1) - net = argv[1]; - char *input = "../demo/demo/data/single_ped_2.mp4"; - if (argc > 2) - input = argv[2]; - char *pmatrix = "../demo/demo/data/pmundist.txt"; - if (argc > 3) - pmatrix = argv[3]; - char *tiffile = "../demo/demo/data/map_b.tif"; - if (argc > 4) - tiffile = argv[4]; - int CAM_IDX = 20936; - if (argc > 5) - CAM_IDX = atoi(argv[5]); - bool to_show = true; - if (argc > 6) - to_show = atoi(argv[6]); - char *maskfile = "../demo/demo/data/mask36.jpg"; - if (argc > 7) - maskfile = argv[7]; - char *cameraCalib = "../demo/demo/data/calib36.params"; - if (argc > 8) - cameraCalib = argv[8]; - char *maskFileOrient = "../demo/demo/data/mask_orient/6315_mask_orient.jpg"; - if (argc > 9) - maskFileOrient = argv[9]; - - tk::dnn::Yolo3Detection yolo; - yolo.init(net); - yolo.thresh = 0.25; - gRun = true; - - pthread_t visual, videocap, originalshow, detectionshow, topviewshow, disparityshow; + Camera_t *camera = (Camera_t *) x_void_ptr; + pthread_t visual, originalshow, detectionshow, topviewshow, disparityshow; + pthread_t videocap; //create video capture thread Frame_t info_f; - info_f.input = input; + info_f.input = camera->input; if (pthread_create(&videocap, NULL, readVideoCapture, (void*)&info_f)) { fprintf(stderr, "Error creating thread\n"); - return 1; + return (void *)1; }; + + bool to_show = camera->to_show; + double adfGeoTransform[6]; + for(int i=0; i<6; i++) + adfGeoTransform[i] = camera->adfGeoTransform[i]; + + ModFrame_t info_show; + if (to_show) + { + // initialize updates struct + updates.update_o = false; + updates.update_de = false; + updates.update_t = false; + updates.update_di = false; + if (pthread_create(&visual, NULL, show_updates, (void*)NULL)) + { + fprintf(stderr, "Error creating thread\n"); + return (void *)1; + }; + if (pthread_create(&originalshow, NULL, originalFrame, (void*)&info_f)) + { + fprintf(stderr, "Error creating thread\n"); + return (void *)1; + }; + if (pthread_create(&disparityshow, NULL, disparityFrame, (void*)&info_f)) + { + fprintf(stderr, "Error creating thread\n"); + return (void *)1; + }; + info_show.H = cv::Mat(cv::Size(3, 3), CV_64FC1); + if (pthread_create(&detectionshow, NULL, detectionFrame, (void*)&info_show)) + { + fprintf(stderr, "Error creating thread\n"); + return (void *)1; + }; + if (pthread_create(&topviewshow, NULL, topviewFrame, (void*)&info_show)) + { + fprintf(stderr, "Error creating thread\n"); + return (void *)1; + }; + } - - - cv::Mat frame; - cv::Mat frame_crop; - char buf_frame_crop_name [200]; - cv::Mat dnn_input; - + char* pmatrix = camera->pmatrix; /*projection matrix from camera to map*/ cv::Mat H(cv::Size(3, 3), CV_64FC1); read_projection_matrix(H, pmatrix); - + assert (cv::countNonZero(H) > 0); + // std::cout<cameraCalib, cameraMat, distCoeff); std::cout << cameraMat << std::endl; std::cout << distCoeff << std::endl; /*GPS information*/ - double *adfGeoTransform = (double *)malloc(6 * sizeof(double)); - readTiff(tiffile, adfGeoTransform); std::vector coords; - + /*socket*/ Communicator Comm(SOCK_DGRAM); - Comm.open_client_socket("127.0.0.1", 8888); + Comm.open_client_socket((char *)"127.0.0.1", 8888); + Message *m = new Message; - m->cam_idx = CAM_IDX; + m->cam_idx = camera->CAM_IDX; m->lights.clear(); - + std::cout<<"COMM\n"; /*Conversion for tracker, from gps to meters and viceversa*/ + // mutex_cv.lock(); geodetic_converter::GeodeticConverter gc; gc.initialiseReference(44.655540, 10.934315, 0); + // mutex_cv.unlock(); double east, north, up; // double lat, lon, alt; - + std::cout<<"COMMINT\n"; /*Mask info*/ - cv::Mat mask = cv::imread(maskfile, cv::IMREAD_GRAYSCALE); - cv::Mat maskOrient = cv::imread(maskFileOrient); + cv::Mat mask = cv::imread(camera->maskfile, cv::IMREAD_GRAYSCALE); + cv::Mat maskOrient = cv::imread(camera->maskFileOrient); + // cv::Mat maskOrient = cv::imread(camera->maskFileOrient, 0); /*for(int i=0; i< mask.cols; i++) { @@ -620,11 +696,10 @@ int main(int argc, char *argv[]) } } - + return 0;*/ - + std::cout<<"COMM2\n"; /*tracker infos*/ - srand(time(NULL)); std::vector trackers; std::vector cur_frame; int initial_age = -5; @@ -644,12 +719,12 @@ int main(int argc, char *argv[]) auto start_t = std::chrono::steady_clock::now(); auto step_t = std::chrono::steady_clock::now(); auto end_t = std::chrono::steady_clock::now(); - auto step_t_segmentation = std::chrono::steady_clock::now(); - auto end_t_segmentation = std::chrono::steady_clock::now(); + // auto step_t_segmentation = std::chrono::steady_clock::now(); + // auto end_t_segmentation = std::chrono::steady_clock::now(); //TODO: move in a thread // // information for the disparity map - std::vector pre_rois; + // std::vector pre_rois; // cv::Mat pre_frame; cv::Mat orig_frame; // cv::Mat canny, pre_canny, canny_RGB, pre_canny_RGB; @@ -657,56 +732,15 @@ int main(int argc, char *argv[]) // box variable tk::dnn::box b; - int x0, w, x1, y0, h, y1; + int x0, h, y1; //w, x1, y0; int objClass; std::string det_class;; - float prob; + // float prob; cv::Scalar intensity; - cv::Rect roi; - - ModFrame_t info_show; - - if (to_show) - { - // initialize updates struct - updates.update_o = false; - updates.update_de = false; - updates.update_t = false; - updates.update_di = false; - if (pthread_create(&visual, NULL, show_updates, (void*)NULL)) - { - fprintf(stderr, "Error creating thread\n"); - return 1; - }; - if (pthread_create(&originalshow, NULL, originalFrame, (void*)&info_f)) - { - fprintf(stderr, "Error creating thread\n"); - return 1; - }; - if (pthread_create(&disparityshow, NULL, disparityFrame, (void*)&info_f)) - { - fprintf(stderr, "Error creating thread\n"); - return 1; - }; - info_show.H = cv::Mat(cv::Size(3, 3), CV_64FC1); - if (pthread_create(&detectionshow, NULL, detectionFrame, (void*)&info_show)) - { - fprintf(stderr, "Error creating thread\n"); - return 1; - }; - if (pthread_create(&topviewshow, NULL, topviewFrame, (void*)&info_show)) - { - fprintf(stderr, "Error creating thread\n"); - return 1; - }; - // if (pthread_create(&detectionshow, NULL, showImages, (void *)&info_show)) - // { - // fprintf(stderr, "Error creating thread\n"); - // return 1; - // } - } - + cv::Mat frame; + cv::Mat frame_crop; + cv::Mat dnn_input; bool first_iteration = true; while (gRun) { @@ -714,12 +748,12 @@ int main(int argc, char *argv[]) start_t = std::chrono::steady_clock::now(); step_t = start_t; - sem_vc.lock(); + info_f.sem_vc.lock(); frame = info_f.frame.clone(); if(info_f.frame_nbr - frame_nbr > 1) - std::cout<<"more than one - f_n\n"; + std::cout<<"more than one - f_n (diff "<yolo.update(dnn_input); + int num_detected = camera->yolo.detected.size(); if (num_detected > MAX_DETECT_SIZE) num_detected = MAX_DETECT_SIZE; coords.clear(); - end_t = std::chrono::steady_clock::now(); std::cout << " TIME 1 : "<(end_t - step_t).count() << " ms"<CAM_IDX <<" - num detected: "<yolo.detected[i]; x0 = b.x; - w = b.w; - x1 = b.x + w; - y0 = b.y; + // w = b.w; + // x1 = b.x + w; + // y0 = b.y; h = b.h; y1 = b.y + h; objClass = b.cl; det_class = obj_class[b.cl]; - prob = b.prob; + // prob = b.prob; intensity = mask.at(cv::Point(int(x0 + b.w / 2), y1)); @@ -833,47 +865,50 @@ int main(int argc, char *argv[]) { // find the rectangular on the frame (sub-figure) - roi.x = (x0 > 0)? x0 : 0; - roi.y = (y0 > 0)? y0 : 0; - // std::cout<<"x "<= frame.cols)? frame.cols-1-roi.x : w; - roi.height = (roi.y+h >= frame.rows)? frame.rows-1-roi.y : h; + // roi.x = (x0 > 0)? x0 : 0; + // roi.y = (y0 > 0)? y0 : 0; + // // std::cout<<"x "<= frame.cols)? frame.cols-1-roi.x : w; + // roi.height = (roi.y+h >= frame.rows)? frame.rows-1-roi.y : h; // std::cout<<"w "<yolo.colors[objClass], 2); // // draw label // int baseline = 0; // float fontScale = 0.5; // int thickness = 2; // cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline); - // cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), yolo.colors[b.cl], -1); + // cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), camera->yolo.colors[b.cl], -1); // cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness); } } } - // end_t = std::chrono::steady_clock::now(); - // std::cout << " TIME 2 : "<(end_t - step_t).count() << " ms"<(end_t - step_t).count() << " ms"<objects.empty()); + if (!m->objects.empty()) + Comm.send_message(m); + // assert (trackers.size()!=0); + if (to_show) { //populate the ModFrame_t - sem.lock(); + info_show.sem.lock(); info_show.original_frame = frame.clone(); // std::vector trackers; info_show.trackers = trackers; @@ -906,9 +946,10 @@ int main(int argc, char *argv[]) info_show.adfGeoTransform[i] = adfGeoTransform[i]; // cv::Mat H; info_show.H = H.clone(); - info_show.yolo = yolo; + info_show.yolo = camera->yolo; + // std::copy(camera->yolo.begin(), camera->yolo.end(), info_show.yolo.begin()); info_show.mask = mask.clone(); - sem.unlock(); + info_show.sem.unlock(); } // update pre_frame for the disparity map @@ -918,12 +959,134 @@ int main(int argc, char *argv[]) first_iteration = false; frame_nbr++; - std::cout<<"MAIN thread: "; + std::cout<CAM_IDX<<" camera thread: "; TIMER_STOP } + return (void *)0; +} +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 1) + return -1; + + + tk::dnn::Yolo3Detection yolo[n_cameras]; + for(int i=0; i &coords, int x, int y, int detected_c double latitude, longitude; std::vector x_y, ll; x_y.push_back(cv::Point2f(x, y)); - //transform camera pixel to map pixel cv::perspectiveTransform(x_y, ll, H); //tranform to map pixel to map gps @@ -136,25 +135,24 @@ void read_projection_matrix(cv::Mat &H, char *path) size_t len = 0; ssize_t read; - float *proj_matrix = (float *)malloc(9 * sizeof(float)); - + // float *proj_matrix = (float *)malloc(9 * sizeof(float)); + double proj_matrix[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; + int i = 0; fp = fopen(path, "r"); if (fp == NULL) exit(EXIT_FAILURE); - - int i = 0; + while ((read = getline(&line, &len, fp)) != -1) { - if (3 == sscanf(line, "%f %f %f", &proj_matrix[i * 3 + 0], &proj_matrix[i * 3 + 1], &proj_matrix[i * 3 + 2])) - { + std::cout<> proj_matrix[i]) i++; - } } - free(line); fclose(fp); fillMatrix(H, proj_matrix); - - free(proj_matrix); + free(line); + // free(proj_matrix); } void draw_arrow(float angleRad, float vel, cv::Scalar color, cv::Point center, cv::Mat &frame) @@ -176,10 +174,9 @@ unsigned long long time_in_ms() return t_stamp_ms; } -void addRoadUserfromTracker(const std::vector &trackers, Message *m, geodetic_converter::GeodeticConverter &gc, const cv::Mat& maskOrient, double *adfGeoTransform) +void addRoadUserfromTracker(const std::vector &trackers, Message *m, geodetic_converter::GeodeticConverter &gc, const cv::Mat& maskOrient, double *adfGeoTransform, cv::Mat H) { m->t_stamp_ms = time_in_ms(); - m->num_objects = trackers.size(); m->objects.clear(); double lat, lon, alt; @@ -215,29 +212,65 @@ void addRoadUserfromTracker(const std::vector &trackers, Message *m, ge int pix_x, pix_y; coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform); - // std::cout<(pix_y,pix_x)< map_p, camera_p; + // std::cout<<"--- pix_x, pix_y: "<(camera_p[0].y,camera_p[0].x)[0]; + // std::cout<<"boo: "<(camera_p[0].y,camera_p[0].x)<(pix_y,pix_x)[0]; - uint8_t orientation; - if(maskOrientPixel != 0) - { - orientation = maskOrientPixel; - // std::cout<<"orientation given by the mask "<< int(orientation)< it works for grayscale image (see demo.cpp, row: "cv::Mat maskOrient = cv::imread(camera->maskFileOrient, 0);") + // TODO: include perspective transform + // std::cout<<"y, x :"<(pix_y,pix_x); + // uint8_t orientation; + // if(maskOrientPixel != 0) + // { + // orientation = maskOrientPixel; + // // std::cout<<"orientation given by the mask "<< int(orientation)<(lat), static_cast(lon), velocity, orientation, cat}; //std::cout << std::setprecision(10) << r.latitude << " , " << r.longitude << " " << int(r.speed) << " " << int(r.orientation) << " " << r.category << std::endl; m->objects.push_back(r); } } + m->num_objects = m->objects.size(); } void prepare_message(Message *m, const std::vector &coords, int idx) @@ -247,7 +280,7 @@ void prepare_message(Message *m, const std::vector &coords, int idx) m->num_objects = coords.size(); m->objects.clear(); - for (int i = 0; i < coords.size(); i++) + for (unsigned int i = 0; i < coords.size(); i++) { Categories cat;