JustMotion/src/mo_detect.cpp
Maurice ONeal a065b7a1d3 v2.0.t1
Completely reformed the internal workings of the application code. I
brought back multi-threaded functions so there is now 5 separate threads
for different tasks.

recLoop() - this function calls ffmpeg to begin recording footage from
the defined camera and stores the footage in hls format. It is designed
to keep running for as long as the application is running and if it does
stop for whatever reason, it will attempt to auto re-start.

upkeep() - this function does regular cleanup and enforcement of maxDays
maxLogSize and maxEvents without the need to stop recording or detecting
motion.

detectMo() - this function reads directly from recLoop's hls output and
list all footage that has motion in it. motion detection no longer has
to wait for the clip to finish recording thanks to the use of .ts
containers for the video clips. this makes the motion detection for less
cpu intensive now that it will now operate at the camera's fps (slower).

eventLoop() - this function reads the motion list from detectMo and
copies the footage pointed out by the list to an events folder, also in
hls format.

schLoop() - this function runs an optional user defined external command
every amount of seconds defined in sch_sec. this command temporary stops
motion detection without actually terminating the thread. It will also
not run the command at the scheduled time if motion was detected.

Benefits to this reform:

- far less cpu intensive operation
- multi-threaded architecture for better asynchronous operation
- it has support for live streaming now that hls is being used
- a buff_dir is no longer necessary
2023-03-05 16:07:07 -05:00

119 lines
2.9 KiB
C++

// This file is part of Motion Watch.
// Motion Watch is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Motion Watch is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
#include "mo_detect.h"
void detectMoInStream(const string &bufPath, shared_t *share)
{
ifstream fileIn(bufPath);
pls_t clip;
auto clipPathFilter = genTimeStr("VIDEO_TS/live/%Y/%j/%H/");
for (string line; getline(fileIn, line); )
{
if (line.starts_with(clipPathFilter))
{
clip.clipPath = line;
}
else if (line.starts_with("#EXTINF"))
{
clip.extINF = line;
}
}
if (!clip.clipPath.empty() && !clip.extINF.empty())
{
if (moDetect(clip.clipPath, clip.thumbnail, share))
{
clip.fileName = genTimeStr("%Y-%j-%H-%M");
clip.dstPath = genEventPath(bufPath);
clip.createTime = genEpoch();
share->skipCmd = true;
share->recList.push_back(clip);
}
}
}
bool imgDiff(Mat &prev, Mat &next, shared_t *share)
{
auto ret = false;
cvtColor(prev, prev, COLOR_BGR2GRAY);
cvtColor(next, next, COLOR_BGR2GRAY);
Mat diff;
absdiff(prev, next, diff);
threshold(diff, diff, share->pixThresh, 255, THRESH_BINARY);
auto diffScore = countNonZero(diff);
detLog("diff_score: " + to_string(diffScore), share);
return diffScore >= share->imgThresh;
}
bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share)
{
auto mod = false;
detLog("stream_clip: " + buffFile, share);
VideoCapture capture(buffFile.c_str(), CAP_FFMPEG);
if (capture.isOpened())
{
Mat prev;
Mat next;
detLog("capture open successful.", share);
while (capture.grab())
{
if (prev.empty())
{
capture.retrieve(prev);
}
else
{
capture.retrieve(next);
if (!share->postCmdRunning)
{
if (imgDiff(prev, next, share))
{
resize(next, vidThumb, Size(720, 480), INTER_LINEAR);
mod = true; break;
}
}
prev.release();
next.release();
}
sleep(1);
}
}
else
{
detLog("capture open failure, check debug output.", share);
}
capture.release();
return mod;
}