Skip to content

Commit

Permalink
Revisions for alg timing and secondary detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Dave committed Aug 20, 2024
1 parent 7c8ba90 commit a3c04ef
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 57 deletions.
8 changes: 2 additions & 6 deletions src/alg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,19 +897,15 @@ 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;
u_char *ref = cam->imgs.ref;
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--) {
Expand Down
132 changes: 86 additions & 46 deletions src/alg_sec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uchar> buff; //buffer for coding
std::vector<uchar> buff;
std::vector<int> 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
Expand All @@ -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;
Expand All @@ -88,22 +107,14 @@ void cls_algsec::label_image(Mat &mat_dst
std::vector<double> fltr_weights;
std::string testdir;
std::size_t indx0, indx1;
std::vector<uchar> buff; //buffer for coding
std::vector<uchar> buff;
std::vector<int> 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<src_pos.size(); indx0++) {
Rect r = src_pos[indx0];
Expand Down Expand Up @@ -151,16 +162,7 @@ void cls_algsec::label_image(Mat &mat_dst, double confidence, Point classIdPoint

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);

if (confidence < threshold) {
return;
Expand Down Expand Up @@ -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
Expand All @@ -211,34 +227,40 @@ 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) ||
((cam->current_image->location.width/cam->imgs.width) > 0.7) ||
((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()
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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") {
Expand All @@ -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."));
Expand Down Expand Up @@ -669,6 +705,9 @@ void cls_algsec::detect()
if (method == "none") {
return;
}
if (is_started == false) {
return;
}

if (frame_cnt > 0) {
frame_cnt--;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/alg_sec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <opencv2/objdetect.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#pragma GCC diagnostic pop
#endif

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -79,13 +85,13 @@ class cls_algsec {
void label_image(cv::Mat &mat_dst, std::vector<cv::Rect> &src_pos
, std::vector<double> &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;

Expand Down

0 comments on commit a3c04ef

Please sign in to comment.