-
Notifications
You must be signed in to change notification settings - Fork 2
MainEvent.cpp
Q-engineering edited this page Mar 9, 2022
·
3 revisions
MainEvents while loop checks for the presents of SWAP_RDY_FILE
, indication Qstreamer has saved a new frame SWAP_IMG_FILE
.
It reads the frame and immediately deletes both files. Qstreamer notices this and starts collecting a new image.
Notifying Qstreamer after MainEvent's analysis is complete makes sense, but before Qstreamer has its next frame ready to read, MainEvent becomes inactive.
It lowers the FPS. Better to get Qstreamer working while MotionEvent is also running. That's why we use different processes instead of threading.
#include <stdio.h>
#include <chrono>
#include <thread>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include "General.h"
#include "Settings.h"
#include "Email.h"
#include "OkFile.h"
#include "Background.h"
#include "Usb.h"
#include "Pid.h"
//----------------------------------------------------------------------------------
using namespace cv;
using namespace std;
//---------------------------------------------------------------------------
int Tag;
TEmail MyEmail;
TSettings MySettings(SETTING_FILE); //the settings (they are loaded here for the first time)
TBackground MyBackGrnd;
TUsb MyUSB;
//----------------------------------------------------------------------------------
int main(int argc,char ** argv)
{
Mat frame;
int t, Qpid, Tag=0;
float fM;
bool Trig=false;
bool Trig_t1=false;
bool Blocked=false;
bool UsbRec=false;
string Str;
chrono::steady_clock::time_point Tbegin, Tend;
//start Qstreamer
Qpid = getProcIdByName("Qstreamer");
if(Qpid < 0) system("/usr/local/bin/Qstreamer &");
MyEmail.Start();
cout << "Entering main loop by sending email to : " << MySettings.MailAddress << endl;
//main loop
while(1){
if(FileExists(SWAP_RDY_FILE) ){ //check if new frame is writen
frame=imread(SWAP_IMG_FILE); //load the new frame
//notify Qstreamer we got the frame
unlink(SWAP_IMG_FILE);
unlink(SWAP_RDY_FILE);
//get movements
fM= MyBackGrnd.Execute(frame);
if(fM <= MySettings.ResetTrig) Trig=false;
if(fM >= MySettings.SetTrig) Trig=true;
if(!Trig_t1 && Trig){
//start of event
if(MySettings.USBrecord){
if(!UsbRec){
MyUSB.StartRecord();
UsbRec=true;
}
}
if(!Blocked){
Str="Raspberry Pi camera ";
Str+=MySettings.CamID;
Str+="\n";
if(MySettings.USBrecord){
Str+="File : ";
Str+=MyUSB.Fname;
Str+="\n";
}
Str+=format("Detected motion : %0.3f %%",fM);
MyEmail.Send("RPi motion",Str.c_str());
}
}
if(Trig){
Blocked = true; //set mail Blocked
if(MySettings.USBrecord) UsbRec = true;
Tbegin = chrono::steady_clock::now();
}
if(Blocked){
Tend = chrono::steady_clock::now();
t = chrono::duration_cast <chrono::seconds> (Tend - Tbegin).count();
if(t > MySettings.NoSecMail) Blocked = false; //reset mail Blocked
}
if(UsbRec){
Tend = chrono::steady_clock::now();
t = chrono::duration_cast <chrono::seconds> (Tend - Tbegin).count();
if(t > MySettings.USBprolong){
MyUSB.StopRecord();
UsbRec = false;
//see if we need to upload the file to Gdrive
if(MySettings.GDrive){
Str ="/usr/local/bin/Google.sh ";
Str+=MyUSB.Fdir;
Str+=MyUSB.Fname;
if(MySettings.DelUpload) Str+=" 1 &";
else Str+=" 0 &";
system(Str.c_str());
}
}
}
Trig_t1=Trig;
//show result if you like
putText(MyBackGrnd.finalImage, format("%0.3f %%",fM),Point(10,20),FONT_HERSHEY_SIMPLEX,0.6, Scalar(255, 255, 255));
imshow("Result", MyBackGrnd.finalImage);
char esc = cv::waitKey(5); //you need to use waitKey to
if(esc == 27) break; //get imshow shown
//print result
cout << fixed;
cout.precision(2);
cout << "Analized : " << Tag++ << " - motion : " << fM;
if(Trig) cout << " | ";
else cout << " - ";
if(UsbRec) cout << " R ";
else cout << " - ";
if(Blocked) cout << " B " << endl;
else cout << endl;
}
}
//kill Qstreamer
Qpid = getProcIdByName("Qstreamer");
Str = "kill ";
Str += to_string(Qpid);
system(Str.c_str());
return 0;
}
//----------------------------------------------------------------------------------