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-04-20 14:52:59 -04:00
|
|
|
if (share->postInd >= share->postSecs)
|
2023-03-26 10:32:56 -04:00
|
|
|
{
|
2023-04-20 14:52:59 -04:00
|
|
|
if (!share->postCmd.empty())
|
|
|
|
{
|
|
|
|
detLog("---POST_BREAK---", share);
|
2023-04-15 08:23:49 -04:00
|
|
|
|
2023-04-20 14:52:59 -04:00
|
|
|
if (!share->skipCmd)
|
|
|
|
{
|
|
|
|
detLog("no motion detected, running post command: " + share->postCmd, share);
|
|
|
|
system(share->postCmd.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
share->skipCmd = false;
|
2023-04-15 08:23:49 -04:00
|
|
|
|
2023-04-20 14:52:59 -04:00
|
|
|
detLog("motion detected, skipping the post command.", share);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
share->postInd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (share->evInd >= share->evMaxSecs)
|
|
|
|
{
|
|
|
|
detLog("---EVENT_BREAK---", share);
|
|
|
|
|
|
|
|
if (!share->curEvent.srcPaths.empty())
|
2023-03-26 10:32:56 -04:00
|
|
|
{
|
2023-04-21 16:01:14 -04:00
|
|
|
share->curEvent.evName = genEventName(share->maxScore);
|
2023-04-20 14:52:59 -04:00
|
|
|
share->recList.push_back(share->curEvent);
|
|
|
|
|
|
|
|
detLog("motion detected in " + to_string(share->curEvent.srcPaths.size()) + " file(s) in " + to_string(share->evMaxSecs) + " secs", share);
|
|
|
|
detLog("all video clips queued for event generation under event name: " + share->curEvent.evName, share);
|
2023-03-26 10:32:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-20 14:52:59 -04:00
|
|
|
detLog("no motion detected in all files. none queued for event generation.", share);
|
2023-03-26 10:32:56 -04:00
|
|
|
}
|
2023-04-20 14:52:59 -04:00
|
|
|
|
|
|
|
share->curEvent.srcPaths.clear();
|
|
|
|
share->curEvent.evName.clear();
|
|
|
|
share->curEvent.thumbnail.release();
|
|
|
|
|
|
|
|
share->evInd = 0;
|
|
|
|
share->maxScore = 0;
|
2023-03-26 10:32:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ifstream fileIn(streamFile);
|
2023-03-12 13:22:10 -04:00
|
|
|
string tsPath;
|
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-03-12 15:27:53 -04:00
|
|
|
if (!tsPath.empty())
|
2022-12-04 15:13:39 -05:00
|
|
|
{
|
2023-04-20 14:52:59 -04:00
|
|
|
if (moDetect(tsPath, share))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-04-20 14:52:59 -04:00
|
|
|
share->curEvent.srcPaths.push_back(tsPath);
|
2023-03-12 13:22:10 -04:00
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2023-04-20 14:52:59 -04:00
|
|
|
bool moDetect(const string &buffFile, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-04-15 08:23:49 -04:00
|
|
|
auto score = 0;
|
2023-04-20 14:52:59 -04: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-04-20 14:52:59 -04:00
|
|
|
VideoCapture capture;
|
|
|
|
|
|
|
|
if (!capture.open(buffFile.c_str(), CAP_FFMPEG))
|
|
|
|
{
|
|
|
|
usleep(500);
|
|
|
|
|
|
|
|
capture.open(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-04-21 16:01:14 -04:00
|
|
|
int fps = capture.get(cv::CAP_PROP_FPS);
|
|
|
|
|
|
|
|
for (auto gap = 0, frm = fps; capture.grab(); ++gap, ++frm)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-04-21 16:01:14 -04:00
|
|
|
if (frm == fps) sleep(1); frm = 1;
|
|
|
|
|
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-04-20 14:52:59 -04:00
|
|
|
else if (gap == (share->frameGap - 1))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
|
|
|
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-20 14:52:59 -04:00
|
|
|
mod = true;
|
|
|
|
|
|
|
|
if (share->maxScore <= score)
|
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-04-20 14:52:59 -04:00
|
|
|
resize(next, share->curEvent.thumbnail, Size(720, 480), INTER_LINEAR);
|
2023-03-26 10:32:56 -04:00
|
|
|
}
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-04-20 14:52:59 -04:00
|
|
|
prev = next.clone();
|
|
|
|
gap = 0;
|
|
|
|
|
2023-03-05 16:07:07 -05:00
|
|
|
next.release();
|
|
|
|
}
|
2023-04-20 14:52:59 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
capture.grab();
|
|
|
|
}
|
2022-12-16 18:24:18 -05:00
|
|
|
}
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-20 14:52:59 -04:00
|
|
|
detLog("err: failed to open: " + buffFile + " after 500 msecs. giving up.", share);
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2022-12-17 10:34:40 -05:00
|
|
|
capture.release();
|
|
|
|
|
2023-04-20 14:52:59 -04:00
|
|
|
return mod;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
2023-03-10 19:47:36 -05:00
|
|
|
|
2023-04-20 14:52:59 -04:00
|
|
|
bool wrOutVod(const evt_t &event, shared_t *share)
|
2023-03-10 19:47:36 -05:00
|
|
|
{
|
2023-04-20 14:52:59 -04:00
|
|
|
auto cnt = 0;
|
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-04-20 14:52:59 -04:00
|
|
|
recLog("event_src: " + event.srcPaths[i], share);
|
|
|
|
|
|
|
|
if (exists(event.srcPaths[i]))
|
|
|
|
{
|
|
|
|
file << "file '" << event.srcPaths[i] << "''" << endl; cnt++;
|
|
|
|
}
|
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-04-20 14:52:59 -04:00
|
|
|
if (cnt == 0)
|
|
|
|
{
|
|
|
|
recLog("err: none of the event hls clips exists, canceling write out.", share);
|
|
|
|
|
|
|
|
if (exists(concat)) remove(concat);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto ret = system(string("ffmpeg -f concat -safe 0 -i " + concat + " -c copy events/" + event.evName + ".mp4").c_str());
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
recLog("err: ffmpeg concat failure, canceling write out.", share);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists(concat)) remove(concat);
|
|
|
|
|
|
|
|
return ret == 0;
|
|
|
|
}
|
2023-03-10 19:47:36 -05:00
|
|
|
}
|