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.
This commit is contained in:
Maurice ONeal 2022-09-29 11:37:10 -04:00
parent 5d12855fad
commit 7ab51226b2
6 changed files with 83 additions and 50 deletions

View File

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

View File

@ -25,6 +25,8 @@
#include <thread>
#include <dirent.h>
#include <filesystem>
#include <sstream>
#include <mutex>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/videoio.hpp>
@ -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<string> stat;
stringstream stat;
vector<string> classNames;
dnn::Net network;
string statData;
string recordUrl;
string outDir;
string postCmd;

View File

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

View File

@ -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<sec_t> *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<mutex> 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<thread> threads;
vector<sec_t> 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<string>(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
{

View File

@ -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<sec_t> *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);

View File

@ -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;
}