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
|
|
|
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-05-15 15:29:47 -04:00
|
|
|
detLog("diff_score: " + QString::number(score) + " thresh: " + QString::number(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-05-15 15:29:47 -04:00
|
|
|
bool moDetect(const QString &buffFile, QThread *thr, 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;
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (!capture.open(buffFile.toUtf8().data(), CAP_FFMPEG))
|
2023-04-20 14:52:59 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
thr->usleep(500);
|
2023-04-20 14:52:59 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
capture.open(buffFile.toUtf8().data(), CAP_FFMPEG);
|
2023-04-20 14:52:59 -04:00
|
|
|
}
|
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-05-15 15:29:47 -04:00
|
|
|
if (frm == fps) thr->sleep(1); frm = 1;
|
2023-04-21 16:01:14 -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-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
|
|
|
}
|