- reduced the hls size to 2 seconds.
- motion clips are not being combined. fixed it by making event
  loop track the size of shared_t::recList instead of system time.
- maxScore is now global inside of shared_t instead of locally
  inside detectMoInStream().
This commit is contained in:
Maurice ONeal 2023-04-15 08:23:49 -04:00
parent 19872b3ff5
commit bafd2bf727
4 changed files with 46 additions and 50 deletions

View File

@ -350,8 +350,3 @@ string genVidNameFromLive(const string &tsPath)
return string(); return string();
} }
} }
uint64_t genEpoch()
{
return duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
}

View File

@ -34,7 +34,7 @@ using namespace std;
using namespace std::filesystem; using namespace std::filesystem;
using namespace std::chrono; using namespace std::chrono;
#define APP_VER "2.0" #define APP_VER "2.1.t1"
#define APP_NAME "Motion Watch" #define APP_NAME "Motion Watch"
#define REC_LOG_NAME "rec_log_lines.html" #define REC_LOG_NAME "rec_log_lines.html"
#define DET_LOG_NAME "det_log_lines.html" #define DET_LOG_NAME "det_log_lines.html"
@ -44,13 +44,13 @@ struct pls_t
{ {
string evName; string evName;
vector<string> srcPaths; vector<string> srcPaths;
uint64_t createTime;
Mat thumbnail; Mat thumbnail;
}; };
struct shared_t struct shared_t
{ {
map<string, pls_t> recList; map<string, pls_t> recList;
string curEventName;
string conf; string conf;
string recLog; string recLog;
string detLog; string detLog;
@ -64,6 +64,7 @@ struct shared_t
string webFont; string webFont;
string webRoot; string webRoot;
bool skipCmd; bool skipCmd;
int maxScore;
int procTime; int procTime;
int schSec; int schSec;
int pixThresh; int pixThresh;
@ -90,6 +91,5 @@ void enforceMaxEvents(shared_t *share);
bool rdConf(shared_t *share); bool rdConf(shared_t *share);
vector<string> lsFilesInDir(const string &path, const string &ext = string()); vector<string> lsFilesInDir(const string &path, const string &ext = string());
vector<string> lsDirsInDir(const string &path); vector<string> lsDirsInDir(const string &path);
uint64_t genEpoch();
#endif // COMMON_H #endif // COMMON_H

View File

@ -27,40 +27,30 @@ void eventLoop(shared_t *share)
{ {
while (share->retCode == 0) while (share->retCode == 0)
{ {
while (!share->recList.empty()) while (share->recList.size() > 1)
{ {
auto it = share->recList.begin(); auto it = share->recList.begin();
auto evName = it->first; auto evName = it->first;
auto event = it->second; auto event = it->second;
auto timeDiff = genEpoch() - event.createTime;
// wait at least 62 seconds before processing the event in try
// queue.
if ((timeDiff > 0) && (timeDiff > 62))
{ {
try createDirTree("events");
{ wrOutVod(event, share);
createDirTree("events"); genHTMLvod(evName);
wrOutVod(event, share);
genHTMLvod(evName);
if (!exists("events/" + evName + ".jpg")) if (!exists("events/" + evName + ".jpg"))
{
imwrite(string("events/" + evName + ".jpg").c_str(), event.thumbnail);
}
}
catch (filesystem_error &ex)
{ {
recLog(string("err: ") + ex.what(), share); imwrite(string("events/" + evName + ".jpg").c_str(), event.thumbnail);
} }
share->recList.erase(it);
} }
catch (filesystem_error &ex)
sleep(5); {
recLog(string("err: ") + ex.what(), share);
}
} }
sleep(5); sleep(10);
} }
} }
@ -126,7 +116,7 @@ void recLoop(shared_t *share)
" -hls_segment_filename 'live/%Y-%j-%H-%M-%S.ts'" + " -hls_segment_filename 'live/%Y-%j-%H-%M-%S.ts'" +
" -hls_flags delete_segments" + " -hls_flags delete_segments" +
" -y -vcodec copy" + " -y -vcodec copy" +
" -f hls -hls_time 10 -hls_list_size 400" + " -f hls -hls_time 2 -hls_list_size 1000" +
" stream.m3u8"; " stream.m3u8";
recLog("ffmpeg_run: " + cmd, share); recLog("ffmpeg_run: " + cmd, share);
@ -170,6 +160,7 @@ int main(int argc, char** argv)
{ {
sharedRes.retCode = 0; sharedRes.retCode = 0;
sharedRes.procTime = 0; sharedRes.procTime = 0;
sharedRes.maxScore = 0;
sharedRes.skipCmd = false; sharedRes.skipCmd = false;
rdConf(&sharedRes); rdConf(&sharedRes);

View File

@ -16,6 +16,11 @@ void detectMoInStream(const string &streamFile, shared_t *share)
{ {
if (share->procTime >= share->schSec) if (share->procTime >= share->schSec)
{ {
share->curEventName.clear();
share->procTime = 0;
share->maxScore = 0;
if (!share->skipCmd) if (!share->skipCmd)
{ {
detLog("no motion detected, running post command: " + share->postCmd, share); detLog("no motion detected, running post command: " + share->postCmd, share);
@ -24,7 +29,6 @@ void detectMoInStream(const string &streamFile, shared_t *share)
else else
{ {
share->skipCmd = false; share->skipCmd = false;
share->procTime = 0;
detLog("motion detected, skipping the post command.", share); detLog("motion detected, skipping the post command.", share);
} }
@ -42,15 +46,23 @@ void detectMoInStream(const string &streamFile, shared_t *share)
} }
} }
if (share->curEventName.empty())
{
share->curEventName = genTimeStr("%Y-%j-%H-%M");
}
if (!tsPath.empty()) if (!tsPath.empty())
{ {
if (moDetect(tsPath, thumbnail, share)) if (moDetect(tsPath, thumbnail, share))
{ {
auto eventName = genTimeStr("%Y-%j-%H-%M"); if (share->recList.find(share->curEventName) != share->recList.end())
if (share->recList.find(eventName) != share->recList.end())
{ {
share->recList[eventName].srcPaths.push_back(tsPath); share->recList[share->curEventName].srcPaths.push_back(tsPath);
if (!thumbnail.empty())
{
share->recList[share->curEventName].thumbnail = thumbnail.clone();
}
} }
else else
{ {
@ -58,17 +70,16 @@ void detectMoInStream(const string &streamFile, shared_t *share)
event.srcPaths.push_back(tsPath); event.srcPaths.push_back(tsPath);
event.createTime = genEpoch(); event.thumbnail = thumbnail.clone();
event.thumbnail = thumbnail.clone(); event.evName = share->curEventName;
event.evName = eventName;
share->recList.insert(pair{eventName, event}); share->recList.insert(pair{event.evName, event});
} }
share->skipCmd = true; share->skipCmd = true;
} }
share->procTime += 10; share->procTime += 2;
} }
} }
@ -87,15 +98,14 @@ bool imgDiff(const Mat &prev, const Mat &next, int &score, shared_t *share)
score = countNonZero(diff); score = countNonZero(diff);
detLog("diff_score: " + to_string(score) + " tresh: " + to_string(share->imgThresh), share); detLog("diff_score: " + to_string(score) + " thresh: " + to_string(share->imgThresh), share);
return score >= share->imgThresh; return score >= share->imgThresh;
} }
bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share) bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share)
{ {
auto maxScore = 0; auto score = 0;
auto score = 0;
detLog("stream_clip: " + buffFile, share); detLog("stream_clip: " + buffFile, share);
@ -122,9 +132,9 @@ bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share)
{ {
if (imgDiff(prev, next, score, share)) if (imgDiff(prev, next, score, share))
{ {
if (score > maxScore) if (score > share->maxScore)
{ {
maxScore = score; share->maxScore = score;
resize(next, vidThumb, Size(720, 480), INTER_LINEAR); resize(next, vidThumb, Size(720, 480), INTER_LINEAR);
} }
@ -145,7 +155,7 @@ bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share)
capture.release(); capture.release();
return maxScore > 0; return score > 0;
} }
void wrOutVod(const pls_t &event, shared_t *share) void wrOutVod(const pls_t &event, shared_t *share)