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);
|
2023-03-12 13:22:10 -04:00
|
|
|
string tsPath;
|
|
|
|
string extINF;
|
|
|
|
Mat thumbnail;
|
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))
|
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
tsPath = line;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
|
|
|
else if (line.starts_with("#EXTINF"))
|
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
extINF = line;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
2022-12-16 18:24:18 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
if (!tsPath.empty() && !extINF.empty())
|
2022-12-04 15:13:39 -05:00
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
if (moDetect(tsPath, thumbnail, share))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
auto eventName = genTimeStr("%Y-%j-%H-%M");
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
if (share->recList.find(eventName) != share->recList.end())
|
|
|
|
{
|
|
|
|
share->recList[eventName].srcPaths.push_back(tsPath);
|
|
|
|
share->recList[eventName].dstPaths.push_back(genEventPath(tsPath));
|
|
|
|
share->recList[eventName].extINFs.push_back(extINF);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pls_t event;
|
|
|
|
|
|
|
|
event.srcPaths.push_back(tsPath);
|
|
|
|
event.dstPaths.push_back(genEventPath(tsPath));
|
|
|
|
event.extINFs.push_back(extINF);
|
|
|
|
|
|
|
|
event.createTime = genEpoch();
|
|
|
|
event.thumbnail = thumbnail.clone();
|
|
|
|
event.evName = eventName;
|
2022-09-27 18:10:04 -04:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
share->recList.insert(pair{eventName, event});
|
|
|
|
}
|
|
|
|
|
|
|
|
share->skipCmd = true;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
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;
|
|
|
|
}
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
void wrOutm3u8(const pls_t &event, shared_t *share)
|
2023-03-10 19:47:36 -05:00
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
auto plsFile = event.evName + ".m3u8";
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
ofstream file(plsFile.c_str());
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
file << "#EXTM3U" << endl;
|
|
|
|
file << "#EXT-X-VERSION:3" << endl;
|
|
|
|
file << "#EXT-X-TARGETDURATION:10" << endl;
|
|
|
|
file << "#EXT-X-MEDIA-SEQUENCE:0" << endl;
|
|
|
|
file << "#EXT-X-START:TIME-OFFSET=0" << endl;
|
|
|
|
file << "#EXT-X-PLAYLIST-TYPE:VOD" << endl;
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
for (auto i = 0; i < event.extINFs.size(); ++i)
|
2023-03-10 19:47:36 -05:00
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
auto src = event.srcPaths[i];
|
|
|
|
auto dst = event.dstPaths[i];
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
createDirTree(path(dst).parent_path().string());
|
|
|
|
copy_file(src, dst);
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
file << event.extINFs[i] << endl;
|
|
|
|
file << dst << endl;
|
2023-03-10 19:47:36 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
file << "#EXT-X-ENDLIST" << endl;
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
file.close();
|
2023-03-10 19:47:36 -05:00
|
|
|
}
|