From b06a2fa3360d027c7a24fd8c16e40eff690bbe1a Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Tue, 20 Aug 2024 10:45:45 -0600 Subject: [PATCH] Revisions for alg timing and secondary detection --- src/alg.cpp | 8 +-- src/alg_sec.cpp | 132 +++++++++++++++++++++++++++++++----------------- src/alg_sec.hpp | 16 ++++-- 3 files changed, 99 insertions(+), 57 deletions(-) diff --git a/src/alg.cpp b/src/alg.cpp index c1ae8498..187a68e3 100644 --- a/src/alg.cpp +++ b/src/alg.cpp @@ -897,7 +897,7 @@ void cls_alg::lightswitch() void cls_alg::ref_frame_update() { - int accept_timer = cam->lastrate * cam->cfg->static_object_time; + int accept_timer; int i, threshold_ref; int *ref_dyn = cam->imgs.ref_dyn; u_char *image_virgin = cam->imgs.image_vprvcy; @@ -905,11 +905,7 @@ void cls_alg::ref_frame_update() u_char *mask_final = smartmask_final; u_char *out = cam->imgs.image_motion.image_norm; - if (cam->lastrate > 5) { - /* Match rate limit */ - accept_timer /= (cam->lastrate / 3); - } - + accept_timer = cam->cfg->static_object_time * cam->cfg->framerate; threshold_ref = cam->noise * EXCLUDE_LEVEL_PERCENT / 100; for (i = cam->imgs.motionsize; i > 0; i--) { diff --git a/src/alg_sec.cpp b/src/alg_sec.cpp index 9823a24b..b7f8708c 100644 --- a/src/alg_sec.cpp +++ b/src/alg_sec.cpp @@ -45,12 +45,40 @@ static void *algsec_handler(void *arg) return nullptr; } +void cls_algsec::debug_notice(Mat &mat_dst, bool isdetect) +{ + if (handler_stop == true) { + return; + } + + if (cfg_log_level >= DBG) { + if (first_pass == true) { + MOTPLS_LOG(DBG, TYPE_ALL, NO_ERRNO + , "Secondary detect and debug enabled."); + MOTPLS_LOG(DBG, TYPE_ALL, NO_ERRNO + , "Saving source and detected images to %s" + , cfg_target_dir.c_str()); + first_pass = false; + } + if (isdetect == true) { + imwrite(cfg_target_dir + "/detect_" + method + ".jpg" + , mat_dst); + } else { + imwrite(cfg_target_dir + "/src_" + method + ".jpg" + , mat_dst); + } + } +} + void cls_algsec::image_show(Mat &mat_dst) { - //std::string testdir; - std::vector buff; //buffer for coding + std::vector buff; std::vector param(2); + if (handler_stop == true) { + return; + } + /* We check the size so that we at least fill in the first image so the * web stream will have something to start with. After feeding in at least * the first image, we rely upon the connection count to tell us whether we @@ -59,16 +87,7 @@ void cls_algsec::image_show(Mat &mat_dst) (cam->imgs.size_secondary == 0) || (cfg_log_level >= DBG)) { - if ((cfg_log_level >= DBG) && - (detected == true)) { - MOTPLS_LOG(DBG, TYPE_ALL, NO_ERRNO, "Saved detected image: %s%s%s%s" - , cfg_target_dir.c_str() - , "/detect_" - , method.c_str() - , ".jpg"); - imwrite(cfg_target_dir + "/detect_" + method + ".jpg" - , mat_dst); - } + debug_notice(mat_dst, detected); param[0] = cv::IMWRITE_JPEG_QUALITY; param[1] = 75; @@ -88,22 +107,14 @@ void cls_algsec::label_image(Mat &mat_dst std::vector fltr_weights; std::string testdir; std::size_t indx0, indx1; - std::vector buff; //buffer for coding + std::vector buff; std::vector param(2); char wstr[10]; try { detected = false; - if (cfg_log_level >= DBG) { - imwrite(cfg_target_dir + "/src_" + method + ".jpg" - , mat_dst); - MOTPLS_LOG(DBG, TYPE_ALL, NO_ERRNO, "Saved source image: %s%s%s%s" - , cfg_target_dir.c_str() - , "/src_" - , method.c_str() - , ".jpg"); - } + debug_notice(mat_dst, detected); for (indx0=0; indx0= DBG) { - imwrite(cfg_target_dir + "/src_" + method + ".jpg" - , mat_dst); - MOTPLS_LOG(DBG, TYPE_ALL, NO_ERRNO, "Saved source image: %s%s%s%s" - , cfg_target_dir.c_str() - , "/src_" - , method.c_str() - , ".jpg"); - } + debug_notice(mat_dst, detected); if (confidence < threshold) { return; @@ -196,13 +198,27 @@ void cls_algsec::get_image_roi(Mat &mat_src, Mat &mat_dst) roi.width = cam->current_image->location.width; roi.height = cam->current_image->location.height; - if ((roi.y + roi.height) > height) { + /* Images smaller than 100 cause seg faults. 112 is the nearest + multiple of 16 greater than 100*/ + if (roi.height < 112) { + roi.height = 112; + } + if ((roi.y + roi.height) > (height-112)) { + roi.y = height - roi.height; + } else if ((roi.y + roi.height) > height) { roi.height = height - roi.y; } - if ((roi.x + roi.width) > width) { + + if (roi.width < 112) { + roi.width = 112; + } + if ((roi.x + roi.width) > (width-112)) { + roi.x = width - roi.width; + } else { roi.width = width - roi.x; } + /* MOTPLS_LOG(INF, TYPE_ALL, NO_ERRNO, "Base %d %d (%dx%d) img(%dx%d)" ,cam->current_image->location.minx ,cam->current_image->location.miny @@ -211,17 +227,19 @@ void cls_algsec::get_image_roi(Mat &mat_src, Mat &mat_dst) ,width,height); MOTPLS_LOG(INF, TYPE_ALL, NO_ERRNO, "Opencv %d %d %d %d" ,roi.x,roi.y,roi.width,roi.height); - + */ mat_dst = mat_src(roi); } void cls_algsec::get_image(Mat &mat_dst) { - if ((image_type == "gray") || (image_type == "grey")) { + Mat mat_src; + + if (image_type == "grey") { mat_dst = Mat(cam->imgs.height, cam->imgs.width , CV_8UC1, (void*)image_norm); - } else if (image_type == "roi") { + } else if ((image_type == "roi") || (image_type == "greyroi")) { /*Discard really small and large images */ if ((cam->current_image->location.width < 64) || (cam->current_image->location.height < 64) || @@ -229,16 +247,20 @@ void cls_algsec::get_image(Mat &mat_dst) ((cam->current_image->location.height/cam->imgs.height) > 0.7)) { return; } - Mat mat_src = Mat(cam->imgs.height*3/2, cam->imgs.width - , CV_8UC1, (void*)image_norm); - cvtColor(mat_src, mat_src, COLOR_YUV2RGB_YV12); + if (image_type == "roi") { + mat_src = Mat(cam->imgs.height*3/2, cam->imgs.width + , CV_8UC1, (void*)image_norm); + cvtColor(mat_src, mat_src, COLOR_YUV2RGB_YV12); + } else { + mat_src = Mat(cam->imgs.height, cam->imgs.width + , CV_8UC1, (void*)image_norm); + } get_image_roi(mat_src, mat_dst); } else { - Mat mat_src = Mat(cam->imgs.height*3/2, cam->imgs.width + mat_src = Mat(cam->imgs.height*3/2, cam->imgs.width , CV_8UC1, (void*)image_norm); cvtColor(mat_src, mat_dst, COLOR_YUV2RGB_YV12); } - } void cls_algsec::detect_hog() @@ -327,7 +349,7 @@ void cls_algsec::detect_dnn() cv::exp(prob-maxProb, softmaxProb); sum = (float)cv::sum(softmaxProb)[0]; softmaxProb /= sum; - minMaxLoc(softmaxProb.reshape(1, 1), 0, &confidence, 0, &classIdPoint); + cv::minMaxLoc(softmaxProb.reshape(1, 1), 0, &confidence, 0, &classIdPoint); label_image(mat_dst, confidence, classIdPoint); @@ -362,6 +384,17 @@ void cls_algsec::load_haar() } } +void cls_algsec::load_hog() +{ + if (image_type == "roi") { + image_type = "greyroi"; + } else if ( + (image_type != "grey") && + (image_type != "greyroi")) { + image_type = "grey"; + } +} + void cls_algsec::load_dnn() { std::string line; @@ -515,9 +548,12 @@ void cls_algsec::load_params() height = cam->imgs.height; width = cam->imgs.width; frame_missed = 0; + frame_cnt = 0; too_slow = 0; in_process = false; + first_pass = true; handler_stop = false; + cfg_framerate = cam->cfg->framerate; cfg_log_level = cam->app->cfg->log_level; cfg_target_dir = cam->cfg->target_dir; @@ -544,7 +580,7 @@ void cls_algsec::load_params() if (method == "haar") { load_haar(); } else if (method == "hog") { - //load_hog(models); + load_hog(); } else if (method == "dnn") { load_dnn(); } else { @@ -568,7 +604,7 @@ void cls_algsec::handler() load_params(); interval = 1000000L / cfg_framerate; - + is_started = true; while ((handler_stop == false) && (method != "none")) { if (in_process){ if (method == "haar") { @@ -583,7 +619,7 @@ void cls_algsec::handler() SLEEP(0,interval) } } - + is_started = false; handler_stop = false; handler_finished = true; MOTPLS_LOG(INF, TYPE_ALL, NO_ERRNO,_("Secondary detection stopped.")); @@ -669,6 +705,9 @@ void cls_algsec::detect() if (method == "none") { return; } + if (is_started == false) { + return; + } if (frame_cnt > 0) { frame_cnt--; @@ -718,6 +757,7 @@ cls_algsec::cls_algsec(cls_camera *p_cam) image_norm = nullptr; params = nullptr; method = "none"; + is_started = false; pthread_mutex_init(&mutex, NULL); handler_startup(); #else diff --git a/src/alg_sec.hpp b/src/alg_sec.hpp index 4a7dce65..db9a4546 100644 --- a/src/alg_sec.hpp +++ b/src/alg_sec.hpp @@ -23,10 +23,13 @@ #ifdef HAVE_OPENCV #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wconversion" - #include "opencv2/objdetect.hpp" - #include "opencv2/dnn.hpp" - #include "opencv2/highgui.hpp" - #include "opencv2/imgproc.hpp" + #include + #include + #include + #include + #include + #include + #include #pragma GCC diagnostic pop #endif @@ -50,6 +53,8 @@ class cls_algsec { #ifdef HAVE_OPENCV cls_camera *cam; bool in_process; + bool is_started; + bool first_pass; int frame_cnt; int frame_missed; int too_slow; @@ -70,6 +75,7 @@ class cls_algsec { void load_dnn(); void load_haar(); + void load_hog(); void detect_dnn(); void detect_haar(); void detect_hog(); @@ -79,13 +85,13 @@ class cls_algsec { void label_image(cv::Mat &mat_dst, std::vector &src_pos , std::vector &src_weights); void image_show(cv::Mat &mat_dst); + void debug_notice(cv::Mat &mat_dst,bool isdetect); std::string config; ctx_params *params; std::string model_file; int frame_interval; - std::string image_type; int rotate;