From 7ab51226b2f8e37155392ed7f6f4caf2e38b9cdc Mon Sep 17 00:00:00 2001 From: Maurice ONeal Date: Thu, 29 Sep 2022 11:37:10 -0400 Subject: [PATCH] v1.4.t2 re-formed the stats output and moved it out of the motion detect function. block pixel diff counts will now no longer stop at the threshold at each block. it will now count the entire block and output the results in the stats. the code now also pick the block with the highest pixDiff instead of stopping at the first block with a high pixDiff. --- src/common.cpp | 2 ++ src/common.h | 16 +++------ src/main.cpp | 6 ++++ src/mo_detect.cpp | 87 +++++++++++++++++++++++++++------------------- src/mo_detect.h | 11 +++++- src/obj_detect.cpp | 11 ++++-- 6 files changed, 83 insertions(+), 50 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index bbf00d1..36b358d 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -224,6 +224,8 @@ bool rdConf(shared_t *share) share->vidExt = "mp4"; share->recLoopWait = false; share->skipCmd = false; + share->statData = string(); + share->stat = stringstream(share->statData); share->network = dnn::readNet("/etc/mow/yolov5s.onnx"); share->classNames = loadClassList(); diff --git a/src/common.h b/src/common.h index 48ceb78..9c3fe5e 100644 --- a/src/common.h +++ b/src/common.h @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -34,22 +36,14 @@ using namespace std; using namespace std::filesystem; #define BUF_SZ 10 -#define APP_VER "1.4.t1" - -#define ST_LEN 7 -#define ST_PIXDIFF 0 -#define ST_BLOCK_POS_X 1 -#define ST_BLOCK_POS_Y 2 -#define ST_BLOCK_LEN_X 3 -#define ST_BLOCK_LEN_Y 4 -#define ST_CONFIDENCE 5 -#define ST_CLASS_SCORE 6 +#define APP_VER "1.4.t2" struct shared_t { - vector stat; + stringstream stat; vector classNames; dnn::Net network; + string statData; string recordUrl; string outDir; string postCmd; diff --git a/src/main.cpp b/src/main.cpp index 5fe41e4..ca1ad6e 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -72,6 +72,12 @@ void recLoop(shared_t *share) th2.join(); + ofstream file(string(cleanDir(share->buffDir) + "/stat").c_str()); + + file << share->statData; + + file.close(); + if (!share->skipCmd) { system(share->postCmd.c_str()); diff --git a/src/mo_detect.cpp b/src/mo_detect.cpp index 293eee6..adfb8c3 100644 --- a/src/mo_detect.cpp +++ b/src/mo_detect.cpp @@ -27,7 +27,7 @@ bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share) return diff != 0; } -void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, Rect *block, bool *mod, shared_t *share) +void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, vector *results, mutex *secMutex, shared_t *share) { auto pnts = 0; @@ -41,26 +41,21 @@ void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, R if (pixDiff(pixA, pixB, share)) { pnts += 1; - - if (pnts >= share->blockThresh) - { - share->stat[ST_PIXDIFF] = to_string(pnts); - share->stat[ST_BLOCK_POS_X] = to_string(colOffs); - share->stat[ST_BLOCK_POS_Y] = to_string(rowOffs); - share->stat[ST_BLOCK_LEN_X] = to_string(cols); - share->stat[ST_BLOCK_LEN_Y] = to_string(rows); - - block->x = colOffs; - block->y = rowOffs; - - block->height = rows; - block->width = cols; - - *mod = true; return; - } } } } + + struct sec_t res; + + res.x = colOffs; + res.y = rowOffs; + res.xSize = cols; + res.ySize = rows; + res.pixDiff = pnts; + + lock_guard guard(*secMutex); + + results->push_back(res); } bool imgDiff(Mat prev, Mat next, Rect *block, shared_t *share) @@ -70,20 +65,55 @@ bool imgDiff(Mat prev, Mat next, Rect *block, shared_t *share) auto moInBlock = false; vector threads; + vector results; + mutex secMutex; - for (auto x = 0; (x < numOfXBlocks) && !moInBlock; x += share->blockX) + for (auto x = 0; x < numOfXBlocks; x += share->blockX) { - for (auto y = 0; (y < numOfYBlocks) && !moInBlock; y += share->blockY) + // spawn all of the block motion detection threads. + for (auto y = 0; y < numOfYBlocks; y += share->blockY) { - threads.push_back(thread(secDiff, prev, next, share->blockY, share->blockX, y, x, block, &moInBlock, share)); + threads.push_back(thread(secDiff, prev, next, share->blockY, share->blockX, y, x, &results, &secMutex, share)); } } for (auto &&thr : threads) { + // wait for all of the threads to finish. thr.join(); } + auto maxPixDiff = 0; + auto blockPick = 0; + + for (auto i = 0; i < results.size(); ++i) + { + // out of all of the results returned form the threads, pick + // the block with the highest amount of pixDiff. + share->stat << "block_thread:" << " x=" << results[i].x << " y=" << results[i].y << " pixdiff=" << results[i].pixDiff << endl; + + if ((results[i].pixDiff >= share->blockThresh) && (results[i].pixDiff > maxPixDiff)) + { + maxPixDiff = results[i].pixDiff; + blockPick = i; + } + } + + if (maxPixDiff >= share->blockThresh) + { + // return true on this function with the block with the + // high pixDiff value which should be at or exceeds + // the block_threshold set by the conf file. + auto res = results[blockPick]; + + block->x = res.x; + block->y = res.y; + block->height = res.ySize; + block->width = res.xSize; + + moInBlock = true; + } + return moInBlock; } @@ -98,8 +128,6 @@ bool moDetect(const string &buffFile, Rect *block, Mat *img, shared_t *share) Mat prev; Mat next; - share->stat = vector(ST_LEN, string("0")); - while (capPair(prev, next, capture, share)) { if (imgDiff(toGray(prev), toGray(next), block, share)) @@ -108,19 +136,6 @@ bool moDetect(const string &buffFile, Rect *block, Mat *img, shared_t *share) mod = true; break; } } - - ofstream file(string(cleanDir(share->buffDir) + "/stat").c_str()); - - file << "pix_diff: " << share->stat[ST_PIXDIFF] << endl; - file << "block_pos_x: " << share->stat[ST_BLOCK_POS_X] << endl; - file << "block_pos_y: " << share->stat[ST_BLOCK_POS_Y] << endl; - file << "block_siz_x: " << share->stat[ST_BLOCK_LEN_X] << endl; - file << "block_siz_y: " << share->stat[ST_BLOCK_LEN_Y] << endl; - file << "--object_dection:" << endl; - file << " confidence: " << share->stat[ST_CONFIDENCE] << endl; - file << " class_score: " << share->stat[ST_CLASS_SCORE] << endl; - - file.close(); } else { diff --git a/src/mo_detect.h b/src/mo_detect.h index 5b24789..884091f 100644 --- a/src/mo_detect.h +++ b/src/mo_detect.h @@ -15,7 +15,16 @@ #include "common.h" -void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, Rect *block, bool *mod, shared_t *share); +struct sec_t +{ + int x; + int y; + int xSize; + int ySize; + int pixDiff; +}; + +void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, vector *results, mutex *secMutex, shared_t *share); bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share); bool imgDiff(Mat prev, Mat next, Rect *block, shared_t *share); bool moDetect(const string &buffFile, Rect *block, Mat *img, shared_t *share); diff --git a/src/obj_detect.cpp b/src/obj_detect.cpp index 01d23fc..2a3b367 100644 --- a/src/obj_detect.cpp +++ b/src/obj_detect.cpp @@ -28,6 +28,9 @@ bool objectInImage(const Mat &src, const Rect &area, shared_t *share) float *data = (float *)outputs[0].data; + share->stat << "object_detection---" << endl; + share->stat << " outputs_size=" << outputs.size() << " looking_for_objects..." << endl; + for (int i = 0; i < 25200; ++i) { // confidence level data[4]; @@ -41,11 +44,13 @@ bool objectInImage(const Mat &src, const Rect &area, shared_t *share) minMaxLoc(scores, 0, &maxClassScore, 0, &classId); - share->stat[ST_CONFIDENCE] = to_string(data[4]); - share->stat[ST_CLASS_SCORE] = to_string(maxClassScore); + share->stat << " potential_object[confidence_level]=" << data[4] << endl; + share->stat << " potential_object[class_score]=" << maxClassScore << endl; if (maxClassScore > 0.2) { + share->stat << " object_confirmed" << endl; + return true; } } @@ -53,5 +58,7 @@ bool objectInImage(const Mat &src, const Rect &area, shared_t *share) data += 85; } + share->stat << " no_objects_found" << endl; + return false; }