2022-09-22 20:57:46 -04:00
|
|
|
// 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"
|
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
void detectMoInStream(const string &bufPath, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
ifstream fileIn(bufPath);
|
|
|
|
pls_t clip;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
auto clipPathFilter = genTimeStr("VIDEO_TS/live/%Y/%j/%H/");
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
for (string line; getline(fileIn, line); )
|
2022-12-16 18:24:18 -05:00
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
if (line.starts_with(clipPathFilter))
|
|
|
|
{
|
|
|
|
clip.clipPath = line;
|
|
|
|
}
|
|
|
|
else if (line.starts_with("#EXTINF"))
|
|
|
|
{
|
|
|
|
clip.extINF = line;
|
|
|
|
}
|
2022-12-16 18:24:18 -05:00
|
|
|
}
|
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
if (!clip.clipPath.empty() && !clip.extINF.empty())
|
2022-12-04 15:13:39 -05:00
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
if (moDetect(clip.clipPath, clip.thumbnail, share))
|
|
|
|
{
|
|
|
|
clip.fileName = genTimeStr("%Y-%j-%H-%M");
|
|
|
|
clip.dstPath = genEventPath(bufPath);
|
|
|
|
clip.createTime = genEpoch();
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
share->skipCmd = true;
|
2022-09-27 18:10:04 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
share->recList.push_back(clip);
|
|
|
|
}
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
bool imgDiff(Mat &prev, Mat &next, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
auto ret = false;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
cvtColor(prev, prev, COLOR_BGR2GRAY);
|
|
|
|
cvtColor(next, next, COLOR_BGR2GRAY);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
Mat diff;
|
2022-09-29 11:37:10 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
absdiff(prev, next, diff);
|
|
|
|
threshold(diff, diff, share->pixThresh, 255, THRESH_BINARY);
|
2022-12-04 15:13:39 -05:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
auto diffScore = countNonZero(diff);
|
2022-09-29 11:37:10 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
detLog("diff_score: " + to_string(diffScore), share);
|
2022-12-16 18:24:18 -05:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
return diffScore >= share->imgThresh;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-02-18 21:20:24 -05:00
|
|
|
auto mod = false;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
detLog("stream_clip: " + buffFile, share);
|
|
|
|
|
2023-02-18 21:20:24 -05:00
|
|
|
VideoCapture capture(buffFile.c_str(), CAP_FFMPEG);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-02-18 21:20:24 -05:00
|
|
|
if (capture.isOpened())
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2022-12-17 10:34:40 -05:00
|
|
|
Mat prev;
|
|
|
|
Mat next;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
detLog("capture open successful.", share);
|
2022-12-17 10:34:40 -05:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
while (capture.grab())
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
if (prev.empty())
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
capture.retrieve(prev);
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
2023-03-05 16:07:07 -05:00
|
|
|
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);
|
2022-12-16 18:24:18 -05:00
|
|
|
}
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
detLog("capture open failure, check debug output.", share);
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2022-12-17 10:34:40 -05:00
|
|
|
capture.release();
|
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
return mod;
|
|
|
|
}
|