2023-05-15 15:29:47 -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 "camera.h"
|
|
|
|
|
2023-07-31 11:16:07 -04:00
|
|
|
Camera::Camera(QObject *parent) : QObject(parent) {}
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
int Camera::start(const QStringList &args)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-07-31 11:16:07 -04:00
|
|
|
if (rdConf(getParam("-c", args), &shared))
|
2023-10-19 15:04:39 -04:00
|
|
|
{
|
|
|
|
auto thr1 = new QThread(nullptr);
|
|
|
|
auto thr2 = new QThread(nullptr);
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
new EventLoop(&shared, thr1, nullptr);
|
|
|
|
new DetectLoop(&shared, thr2, nullptr);
|
|
|
|
|
2023-10-19 15:04:39 -04:00
|
|
|
thr1->start();
|
|
|
|
thr2->start();
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
return shared.retCode;
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
2023-05-26 16:12:53 -04:00
|
|
|
Loop::Loop(shared_t *sharedRes, QThread *thr, QObject *parent) : QObject(parent)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
|
|
|
shared = sharedRes;
|
|
|
|
heartBeat = 10;
|
2023-05-26 16:12:53 -04:00
|
|
|
loopTimer = 0;
|
2023-05-17 15:06:58 -04:00
|
|
|
|
2023-05-27 09:33:14 -04:00
|
|
|
connect(thr, &QThread::started, this, &Loop::init);
|
2023-05-17 15:06:58 -04:00
|
|
|
|
|
|
|
moveToThread(thr);
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
void Loop::init()
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-05-27 09:33:14 -04:00
|
|
|
loopTimer = new QTimer(this);
|
2023-05-26 16:12:53 -04:00
|
|
|
|
|
|
|
connect(loopTimer, &QTimer::timeout, this, &Loop::loopSlot);
|
|
|
|
|
|
|
|
loopTimer->setSingleShot(false);
|
2023-05-21 09:34:57 -04:00
|
|
|
loopTimer->start(heartBeat * 1000);
|
2023-06-09 16:24:32 -04:00
|
|
|
|
|
|
|
loopSlot();
|
2023-05-17 15:06:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Loop::loopSlot()
|
|
|
|
{
|
2023-05-21 09:34:57 -04:00
|
|
|
if (!exec())
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-05-21 09:34:57 -04:00
|
|
|
loopTimer->stop(); QCoreApplication::exit(shared->retCode);
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Loop::exec()
|
|
|
|
{
|
2023-05-26 16:12:53 -04:00
|
|
|
if (loopTimer->interval() != heartBeat * 1000)
|
|
|
|
{
|
|
|
|
loopTimer->start(heartBeat * 1000);
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
return shared->retCode == 0;
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
EventLoop::EventLoop(shared_t *sharedRes, QThread *thr, QObject *parent) : Loop(sharedRes, thr, parent)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-06-09 16:24:32 -04:00
|
|
|
heartBeat = 2;
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
bool EventLoop::wrOutVod(const evt_t &event)
|
2023-06-10 09:45:26 -04:00
|
|
|
{
|
2023-06-10 22:44:38 -04:00
|
|
|
auto ret = false;
|
2023-11-05 18:44:50 -05:00
|
|
|
auto cnt = 0;
|
|
|
|
auto concat = shared->buffPath + "/live/" + event.timeStamp + ".ctmp";
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
QFile file(concat, this);
|
2023-05-15 15:29:47 -04:00
|
|
|
|
|
|
|
file.open(QFile::WriteOnly);
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
for (auto &&vid : event.vidList)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-10-27 15:43:17 -04:00
|
|
|
QTextStream(stdout) << "event_src: " << vid << Qt::endl;
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-05-26 16:12:53 -04:00
|
|
|
if (QFile::exists(vid))
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-05-26 16:12:53 -04:00
|
|
|
file.write(QString("file '" + vid + "'\n").toUtf8()); cnt++;
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
2023-10-27 15:43:17 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QTextStream(stdout) << "warning: the event hls clip does not exists." << Qt::endl;
|
|
|
|
}
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (cnt == 0)
|
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
QTextStream(stderr) << "err: none of the event hls clips exists, cancelling write out." << Qt::endl;
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-06-10 22:44:38 -04:00
|
|
|
QFile::remove(concat);
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QStringList args;
|
|
|
|
|
|
|
|
args << "-f";
|
|
|
|
args << "concat";
|
|
|
|
args << "-safe" << "0";
|
|
|
|
args << "-i" << concat;
|
|
|
|
args << "-c" << "copy";
|
2023-11-05 18:44:50 -05:00
|
|
|
args << shared->recPath + "/" + event.timeStamp + shared->recExt;
|
2023-10-27 15:43:17 -04:00
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
if (QProcess::execute("ffmpeg", args) == 0)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QFile::remove(concat);
|
|
|
|
}
|
2023-06-10 22:44:38 -04:00
|
|
|
|
|
|
|
return ret;
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
bool EventLoop::exec()
|
|
|
|
{
|
|
|
|
enforceMaxEvents(shared);
|
|
|
|
enforceMaxImages(shared);
|
|
|
|
enforceMaxClips(shared);
|
|
|
|
|
|
|
|
if (!shared->recList.isEmpty())
|
|
|
|
{
|
|
|
|
auto event = shared->recList.takeFirst();
|
|
|
|
|
|
|
|
QTextStream(stdout) << "attempting write out of event: " << event.timeStamp << Qt::endl;
|
|
|
|
|
|
|
|
if (wrOutVod(event))
|
|
|
|
{
|
|
|
|
QStringList args;
|
|
|
|
|
|
|
|
args << "convert";
|
|
|
|
args << event.imgPath;
|
|
|
|
args << shared->recPath + "/" + event.timeStamp + shared->thumbExt;
|
|
|
|
|
|
|
|
QProcess::execute("magick", args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Loop::exec();
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
DetectLoop::DetectLoop(shared_t *sharedRes, QThread *thr, QObject *parent) : Loop(sharedRes, thr, parent)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
pcTimer = 0;
|
|
|
|
heartBeat = 2;
|
2023-05-17 15:06:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DetectLoop::init()
|
|
|
|
{
|
2023-05-27 09:33:14 -04:00
|
|
|
pcTimer = new QTimer(this);
|
2023-11-05 18:44:50 -05:00
|
|
|
|
|
|
|
eventQue.queAge = 0;
|
|
|
|
eventQue.score = 0;
|
|
|
|
eventQue.inQue = false;
|
2023-05-26 16:12:53 -04:00
|
|
|
|
|
|
|
connect(pcTimer, &QTimer::timeout, this, &DetectLoop::pcBreak);
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-05-20 19:18:55 -04:00
|
|
|
resetTimers();
|
2023-05-21 09:34:57 -04:00
|
|
|
|
|
|
|
Loop::init();
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DetectLoop::resetTimers()
|
|
|
|
{
|
2023-05-26 16:12:53 -04:00
|
|
|
pcTimer->start(shared->postSecs * 1000);
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
2023-05-26 16:12:53 -04:00
|
|
|
void DetectLoop::pcBreak()
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-05-26 16:12:53 -04:00
|
|
|
if (!shared->postCmd.isEmpty())
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-10-27 15:43:17 -04:00
|
|
|
QTextStream(stdout) << "---POST_BREAK---" << Qt::endl;
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
if (eventQue.inQue)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-10-27 15:43:17 -04:00
|
|
|
QTextStream(stdout) << "motion detected, skipping the post command." << Qt::endl;
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-27 15:43:17 -04:00
|
|
|
QTextStream(stdout) << "no motion detected, running post command: " << shared->postCmd << Qt::endl;
|
2023-08-06 10:17:10 -04:00
|
|
|
|
|
|
|
auto args = parseArgs(shared->postCmd.toUtf8(), -1);
|
|
|
|
|
|
|
|
if (args.isEmpty())
|
|
|
|
{
|
2023-10-27 15:43:17 -04:00
|
|
|
QTextStream(stderr) << "err: did not parse an executable from the post command line." << Qt::endl;
|
2023-08-06 10:17:10 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QProcess::execute(args[0], args.mid(1));
|
|
|
|
}
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
}
|
2023-05-20 19:18:55 -04:00
|
|
|
}
|
|
|
|
|
2023-08-06 10:17:10 -04:00
|
|
|
float DetectLoop::getFloatFromExe(const QByteArray &line)
|
|
|
|
{
|
|
|
|
QString strLine(line);
|
|
|
|
QString strNum;
|
|
|
|
|
|
|
|
for (auto chr : strLine)
|
|
|
|
{
|
|
|
|
if (chr.isDigit() || (chr == '.'))
|
|
|
|
{
|
|
|
|
strNum.append(chr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
auto ok = false;
|
|
|
|
auto res = strNum.toFloat(&ok);
|
|
|
|
|
|
|
|
if (!ok || strNum.isEmpty())
|
|
|
|
{
|
|
|
|
QTextStream(stderr) << "err: the image comp command returned unexpected output and couldn't be converted to float." << Qt::endl;
|
|
|
|
QTextStream(stderr) << " raw output: " << line << Qt::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2023-08-06 10:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DetectLoop::buildArgs(const QString &prev, const QString &next)
|
|
|
|
{
|
|
|
|
auto args = parseArgs(shared->compCmd.toUtf8(), -1);
|
|
|
|
|
|
|
|
for (auto i = 0; i < args.size(); ++i)
|
|
|
|
{
|
|
|
|
if (args[i] == PREV_IMG) args[i] = prev;
|
|
|
|
if (args[i] == NEXT_IMG) args[i] = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2023-10-27 15:43:17 -04:00
|
|
|
QStringList DetectLoop::buildSnapArgs(const QString &vidSrc, const QString &imgPath)
|
|
|
|
{
|
|
|
|
QStringList ret;
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
ret.append("-hide_banner");
|
2023-10-27 15:43:17 -04:00
|
|
|
ret.append("-y");
|
|
|
|
ret.append("-i");
|
|
|
|
ret.append(vidSrc);
|
|
|
|
ret.append("-frames:v");
|
|
|
|
ret.append("1");
|
|
|
|
ret.append(imgPath);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-26 16:12:53 -04:00
|
|
|
bool DetectLoop::exec()
|
2023-05-20 19:18:55 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
if (eventQue.inQue)
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
eventQue.queAge += heartBeat;
|
|
|
|
}
|
2023-05-29 17:43:31 -04:00
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
auto clips = lsFilesInDir(shared->buffPath + "/live", shared->streamExt);
|
|
|
|
|
|
|
|
if (clips.size() < 2)
|
|
|
|
{
|
|
|
|
QTextStream(stdout) << "warning: didn't pick up enough clips files from the video stream. number of files: " << QString::number(clips.size()) << Qt::endl;
|
|
|
|
QTextStream(stdout) << " will try again on the next loop." << Qt::endl;
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
2023-05-26 16:12:53 -04:00
|
|
|
else
|
2023-05-15 15:29:47 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
auto vidAPath = shared->buffPath + "/live/" + clips[clips.size() - 2];
|
|
|
|
auto vidBPath = shared->buffPath + "/live/" + clips[clips.size() - 1];
|
|
|
|
auto imgAPath = shared->buffPath + "/img/" + QFileInfo(vidAPath).baseName() + ".bmp";
|
|
|
|
auto imgBPath = shared->buffPath + "/img/" + QFileInfo(vidBPath).baseName() + ".bmp";
|
|
|
|
auto snapArgsA = buildSnapArgs(vidAPath, imgAPath);
|
|
|
|
auto snapArgsB = buildSnapArgs(vidBPath, imgBPath);
|
|
|
|
auto compArgs = buildArgs(imgAPath, imgBPath);
|
|
|
|
|
|
|
|
if (compArgs.isEmpty())
|
2023-05-27 09:33:14 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
QTextStream(stderr) << "err: could not parse a executable name from img_comp_cmd: " << shared->compCmd << Qt::endl;
|
2023-05-27 09:33:14 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
QProcess::execute("ffmpeg", snapArgsA);
|
|
|
|
QProcess::execute("ffmpeg", snapArgsB);
|
2023-05-15 15:29:47 -04:00
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
if (QFile::exists(imgAPath) && QFile::exists(imgBPath))
|
2023-05-27 09:33:14 -04:00
|
|
|
{
|
2023-08-06 10:17:10 -04:00
|
|
|
QProcess extComp;
|
|
|
|
|
2023-10-27 15:43:17 -04:00
|
|
|
extComp.start(compArgs[0], compArgs.mid(1));
|
2023-08-06 10:17:10 -04:00
|
|
|
extComp.waitForFinished();
|
|
|
|
|
|
|
|
float score = 0;
|
|
|
|
|
|
|
|
if (shared->outputType == "stdout")
|
|
|
|
{
|
|
|
|
score = getFloatFromExe(extComp.readAllStandardOutput());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
score = getFloatFromExe(extComp.readAllStandardError());
|
2023-08-06 10:17:10 -04:00
|
|
|
}
|
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
QTextStream(stdout) << compArgs.join(" ") << " --result: " << QString::number(score) << Qt::endl;
|
|
|
|
|
|
|
|
if (eventQue.inQue)
|
2023-08-06 10:17:10 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
if (eventQue.score < score)
|
|
|
|
{
|
|
|
|
eventQue.score = score;
|
|
|
|
eventQue.imgPath = imgBPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventQue.vidList.append(vidAPath);
|
|
|
|
eventQue.vidList.append(vidBPath);
|
2023-08-06 10:17:10 -04:00
|
|
|
|
2023-11-05 18:44:50 -05:00
|
|
|
if (eventQue.queAge >= shared->evMaxSecs)
|
2023-08-06 10:17:10 -04:00
|
|
|
{
|
2023-11-05 18:44:50 -05:00
|
|
|
eventQue.inQue = false;
|
|
|
|
|
|
|
|
shared->recList.append(eventQue);
|
|
|
|
|
|
|
|
eventQue.imgPath.clear();
|
|
|
|
eventQue.vidList.clear();
|
|
|
|
eventQue.timeStamp.clear();
|
|
|
|
|
|
|
|
eventQue.score = 0;
|
|
|
|
eventQue.queAge = 0;
|
2023-08-06 10:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
2023-11-05 18:44:50 -05:00
|
|
|
else if (score >= shared->imgThresh)
|
|
|
|
{
|
|
|
|
QTextStream(stdout) << "--threshold_breached: " << QString::number(shared->imgThresh) << Qt::endl;
|
|
|
|
|
|
|
|
eventQue.score = score;
|
|
|
|
eventQue.imgPath = imgBPath;
|
|
|
|
eventQue.inQue = true;
|
|
|
|
eventQue.queAge = 0;
|
|
|
|
eventQue.timeStamp = QDateTime::currentDateTime().toString(DATETIME_FMT);
|
|
|
|
|
|
|
|
eventQue.vidList.clear();
|
|
|
|
eventQue.vidList.append(vidAPath);
|
|
|
|
eventQue.vidList.append(vidBPath);
|
|
|
|
}
|
2023-05-27 09:33:14 -04:00
|
|
|
}
|
2023-05-21 09:34:57 -04:00
|
|
|
}
|
2023-05-15 15:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Loop::exec();
|
|
|
|
}
|