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-26 10:32:56 -04:00
|
|
|
void detectMoInStream(const string &streamFile, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-03-26 10:32:56 -04:00
|
|
|
if (share->procTime >= share->schSec)
|
|
|
|
{
|
2023-04-15 08:23:49 -04:00
|
|
|
share->curEventName.clear();
|
|
|
|
|
|
|
|
share->procTime = 0;
|
|
|
|
share->maxScore = 0;
|
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
if (!share->skipCmd)
|
|
|
|
{
|
|
|
|
detLog("no motion detected, running post command: " + share->postCmd, share);
|
|
|
|
system(share->postCmd.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
share->skipCmd = false;
|
|
|
|
|
|
|
|
detLog("motion detected, skipping the post command.", share);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ifstream fileIn(streamFile);
|
2023-03-12 13:22:10 -04:00
|
|
|
string tsPath;
|
|
|
|
Mat thumbnail;
|
2022-09-22 20:57:46 -04: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-26 10:32:56 -04:00
|
|
|
if (line.starts_with("live/"))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-03-12 13:22:10 -04:00
|
|
|
tsPath = line;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
2022-12-16 18:24:18 -05:00
|
|
|
}
|
|
|
|
|
2023-04-15 08:23:49 -04:00
|
|
|
if (share->curEventName.empty())
|
|
|
|
{
|
|
|
|
share->curEventName = genTimeStr("%Y-%j-%H-%M");
|
|
|
|
}
|
|
|
|
|
2023-03-12 15:27:53 -04:00
|
|
|
if (!tsPath.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-04-15 08:23:49 -04:00
|
|
|
if (share->recList.find(share->curEventName) != share->recList.end())
|
2023-03-12 13:22:10 -04:00
|
|
|
{
|
2023-04-15 08:23:49 -04:00
|
|
|
share->recList[share->curEventName].srcPaths.push_back(tsPath);
|
|
|
|
|
|
|
|
if (!thumbnail.empty())
|
|
|
|
{
|
|
|
|
share->recList[share->curEventName].thumbnail = thumbnail.clone();
|
|
|
|
}
|
2023-03-12 13:22:10 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pls_t event;
|
|
|
|
|
|
|
|
event.srcPaths.push_back(tsPath);
|
|
|
|
|
2023-04-15 08:23:49 -04:00
|
|
|
event.thumbnail = thumbnail.clone();
|
|
|
|
event.evName = share->curEventName;
|
2022-09-27 18:10:04 -04:00
|
|
|
|
2023-04-15 08:23:49 -04:00
|
|
|
share->recList.insert(pair{event.evName, event});
|
2023-03-12 13:22:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
share->skipCmd = true;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
2023-03-26 10:32:56 -04:00
|
|
|
|
2023-04-15 08:23:49 -04:00
|
|
|
share->procTime += 2;
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
bool imgDiff(const Mat &prev, const Mat &next, int &score, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-03-26 10:32:56 -04:00
|
|
|
Mat prevGray;
|
|
|
|
Mat nextGray;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
cvtColor(prev, prevGray, COLOR_BGR2GRAY);
|
|
|
|
cvtColor(next, nextGray, 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-26 10:32:56 -04:00
|
|
|
absdiff(prevGray, nextGray, diff);
|
2023-03-05 16:07:07 -05:00
|
|
|
threshold(diff, diff, share->pixThresh, 255, THRESH_BINARY);
|
2022-12-04 15:13:39 -05:00
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
score = countNonZero(diff);
|
2022-09-29 11:37:10 -04:00
|
|
|
|
2023-04-15 08:23:49 -04:00
|
|
|
detLog("diff_score: " + to_string(score) + " thresh: " + to_string(share->imgThresh), share);
|
2022-12-16 18:24:18 -05:00
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
return score >= 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-04-15 08:23:49 -04:00
|
|
|
auto score = 0;
|
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
|
|
|
{
|
2023-03-26 10:32:56 -04: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);
|
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
if (!next.empty())
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-03-26 10:32:56 -04:00
|
|
|
if (imgDiff(prev, next, score, share))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-04-15 08:23:49 -04:00
|
|
|
if (score > share->maxScore)
|
2023-03-26 10:32:56 -04:00
|
|
|
{
|
2023-04-15 08:23:49 -04:00
|
|
|
share->maxScore = score;
|
2023-03-05 16:07:07 -05:00
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
resize(next, vidThumb, Size(720, 480), INTER_LINEAR);
|
|
|
|
}
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
|
2023-04-15 08:23:49 -04:00
|
|
|
return score > 0;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 15:27:53 -04:00
|
|
|
void wrOutVod(const pls_t &event, shared_t *share)
|
2023-03-10 19:47:36 -05:00
|
|
|
{
|
2023-03-12 15:27:53 -04:00
|
|
|
auto concat = event.evName + ".tmp";
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 15:27:53 -04:00
|
|
|
ofstream file(concat.c_str());
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-03-12 15:27:53 -04:00
|
|
|
for (auto i = 0; i < event.srcPaths.size(); ++i)
|
2023-03-10 19:47:36 -05:00
|
|
|
{
|
2023-03-12 15:27:53 -04:00
|
|
|
file << "file '" << event.srcPaths[i] << "''" << endl;
|
2023-03-10 19:47:36 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 13:22:10 -04:00
|
|
|
file.close();
|
2023-03-12 15:27:53 -04:00
|
|
|
|
2023-03-26 10:32:56 -04:00
|
|
|
system(string("ffmpeg -f concat -safe 0 -i " + concat + " -c copy events/" + event.evName + ".mp4").c_str());
|
2023-03-12 15:27:53 -04:00
|
|
|
remove(concat);
|
2023-03-10 19:47:36 -05:00
|
|
|
}
|