// 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" void detectMoInStream(const string &bufPath, shared_t *share) { ifstream fileIn(bufPath); pls_t clip; auto clipPathFilter = genTimeStr("VIDEO_TS/live/%Y/%j/%H/"); for (string line; getline(fileIn, line); ) { if (line.starts_with(clipPathFilter)) { clip.clipPath = line; } else if (line.starts_with("#EXTINF")) { clip.extINF = line; } } if (!clip.clipPath.empty() && !clip.extINF.empty()) { if (moDetect(clip.clipPath, clip.thumbnail, share)) { clip.fileName = genTimeStr("%Y-%j-%H-%M"); clip.dstPath = genEventPath(bufPath); clip.createTime = genEpoch(); share->skipCmd = true; share->recList.push_back(clip); } } } bool imgDiff(Mat &prev, Mat &next, shared_t *share) { auto ret = false; cvtColor(prev, prev, COLOR_BGR2GRAY); cvtColor(next, next, COLOR_BGR2GRAY); Mat diff; absdiff(prev, next, diff); threshold(diff, diff, share->pixThresh, 255, THRESH_BINARY); auto diffScore = countNonZero(diff); detLog("diff_score: " + to_string(diffScore), share); return diffScore >= share->imgThresh; } bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share) { auto mod = false; detLog("stream_clip: " + buffFile, share); VideoCapture capture(buffFile.c_str(), CAP_FFMPEG); if (capture.isOpened()) { Mat prev; Mat next; detLog("capture open successful.", share); while (capture.grab()) { if (prev.empty()) { capture.retrieve(prev); } else { capture.retrieve(next); if (!share->postCmdRunning) { if (imgDiff(prev, next, share)) { resize(next, vidThumb, Size(720, 480), INTER_LINEAR); mod = true; break; } } prev.release(); next.release(); } sleep(1); } } else { detLog("capture open failure, check debug output.", share); } capture.release(); return mod; }