Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2/objdetect.hpp"
- #include "opencv2/highgui.hpp"
- #include "opencv2/imgproc.hpp"
- #include <iostream>
- #include <direct.h>
- #include <ctime>
- #include "SideCode.cpp";
- using namespace std;
- using namespace cv;
- int main(int argc, const char** argv)
- {
- VideoCapture vc;
- VideoWriter vw;
- Mat mask, frameNow, frame, grayFrame, oldGrayFrame, diffFrame;
- string _pathToCam, _output, _maskFile;
- int _fps, _threshold, _delay;
- bool useMask = false, _showDiff, _showNormal, _showSmall, _consoleNormed;
- /* input */
- cv::CommandLineParser parser(argc, argv,
- "{pathtocam|rtsp://192.168.2.2:554/user=admin&password=&channel=1&stream=0.sdp?real_stream--rtp-caching=100|}"
- //"{pathtocam||}"
- "{output|records/|}"
- "{maskfile| |}"
- "{fps|25|}"
- "{threshold|2000|}"
- "{diff||}"
- "{normal||}"
- "{small|1|}"
- "{delay|5|}"
- "{normed||}"
- );
- _pathToCam = parser.get<string>("pathtocam");
- cerr << "_pathToCam = " + _pathToCam << endl;
- _output = parser.get<string>("output");
- cerr << "output = " + _output << endl;
- _maskFile = parser.get<string>("maskfile");
- cerr << "maskfile = " + _maskFile << endl;
- _fps = parser.get<int>("fps");
- cerr << "fps = " + to_string(_fps) << endl;
- _threshold = parser.get<int>("threshold");
- cerr << "threshold = " + to_string(_threshold) << endl;
- _delay = parser.get<int>("delay");
- cerr << "delay = " + to_string(_delay) << endl;
- _showDiff = parser.has("diff");
- cerr << "diff = " + to_string(_showDiff) << endl;
- _showNormal = parser.has("normal");
- cerr << "normal = " + to_string(_showNormal) << endl;
- _showSmall = parser.has("small");
- cerr << "small = " + to_string(_showSmall) << endl;
- _consoleNormed = parser.has("normed");
- cerr << "normed = " + to_string(_consoleNormed) << endl;
- if (!parser.check())
- {
- parser.printErrors();
- return 0;
- }
- /* end input */
- my_mkdir(_output.c_str());
- if (_maskFile.length() > 1)
- {
- useMask = true;
- mask = imread(_maskFile);
- if (!mask.empty())
- cerr << "Mask loaded" << endl;
- }
- if (_pathToCam.length() > 1)
- vc.open(_pathToCam);
- else
- vc.open(0);
- if (!vc.isOpened())
- {
- cerr << "Can't open stream!";
- return 1;
- }
- vc.set(CAP_PROP_FPS, _fps);
- int frame_width = (int)vc.get(CV_CAP_PROP_FRAME_WIDTH);
- int frame_height = (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT);
- int timeForSave = 0;
- int delayConsoleThreshold = 10;
- int normed;
- string normedStr = "";
- string fn;
- char c;
- while (1)
- {
- vc >> frameNow;
- /* mask */
- if (useMask)
- frameNow.copyTo(frame, mask);
- else
- frameNow.copyTo(frame);
- /* end mask */
- // show normal size
- if (_showNormal)
- imshow("normal", frame);
- //resize
- resize(frame, frame, Size(300, 200));
- // show small
- if (_showSmall)
- imshow("small", frame);
- // set rgb color
- cvtColor(frame, grayFrame, CV_BGR2GRAY);
- // preload old-gray
- if (oldGrayFrame.empty())
- grayFrame.copyTo(oldGrayFrame);
- /* get visual diff */
- if (_showDiff)
- {
- absdiff(oldGrayFrame, grayFrame, diffFrame);
- threshold(diffFrame, diffFrame, 25, 255, cv::THRESH_BINARY);
- //imshow("gray", grayFrame);
- imshow("diff", diffFrame);
- }
- /* end get visual diff */
- /* record */
- normed = norm(oldGrayFrame, grayFrame);
- /* console normed output */
- if (_consoleNormed)
- {
- normedStr += to_string(normed) + " | ";
- delayConsoleThreshold--;
- if (delayConsoleThreshold == 0)
- {
- delayConsoleThreshold = _fps;
- cerr << "normed = " + normedStr << endl;
- normedStr = "";
- }
- }
- /* end console normed output */
- if (normed > _threshold)
- {
- if (timeForSave == 0)
- {
- fn = _output + to_string(time(0)) + ".avi";
- vw.open(fn, CV_FOURCC('X', '2', '6', '4'), _fps, Size(frame_width, frame_height), true);
- cerr << "Start recording into " + fn << endl;
- }
- timeForSave = _fps * _delay;
- }
- if (timeForSave > 0)
- {
- timeForSave--;
- vw.write(frameNow);
- if (timeForSave == 0)
- {
- vw.release();
- cerr << "Recorded" << endl;
- }
- }
- /* end record */
- grayFrame.copyTo(oldGrayFrame);
- c = (char)waitKey(1);
- if (c == 'q')
- break;
- }
- vc.release();
- vw.release();
- cv::destroyAllWindows();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement