Commit 071914fe authored by Benjamin Gallois's avatar Benjamin Gallois

Initial commit

parents
# Standard
CC = g++
CFLAGS = -std=c++11 -Wall -Wextra -fdiagnostics-color=always -O3
IDIR= ./
SRC= $(IDIR)motionSensor.cpp
# compile
all: $(SRC)
$(CC) $(CFLAGS) $(SRC) -o motionSensor -I/usr/local/include/ -lraspicam -lraspicam_cv -lopencv_core -lopencv_highgui -pthread -lopencv_video -lopencv_imgcodecs -lopencv_imgproc -lstdc++fs
$(CC) $(CFLAGS) display.cpp -o display -I/usr/local/include/ -lraspicam -lraspicam_cv -lopencv_core -lopencv_highgui -pthread -lopencv_video -lopencv_imgcodecs -lopencv_imgproc
# remove compilation products
clean:
rm -f *.o
#include <iostream>
#include <unistd.h>
#include <raspicam/raspicam_cv.h>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(){
double FPS = 20;
// Object initialization
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
// Camera setup
Camera.set( cv::CAP_PROP_FORMAT, CV_8UC1 );
Camera.set( cv::CAP_PROP_FRAME_WIDTH , 1280 );
Camera.set( cv::CAP_PROP_FRAME_HEIGHT, 960 );
Camera.set( cv::CAP_PROP_FPS , FPS );
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;}
for(;;){
Camera.grab();
Camera.retrieve (image);
cv::imshow("raspicam_cv_image.jpg", image);
if(waitKey(1) >= 0) break;
}
return 0;
}
#include <ctime>
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>
#include <unistd.h>
#include <experimental/filesystem>
#include <raspicam/raspicam_cv.h>
#include <opencv2/video/background_segm.hpp>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
// Global variables
mutex mtx;
mutex analysisMtx;
Mat fgMaskMOG2;
Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
/**
* Description: struct that contains the frame extract from the camera
* Member image: image openCV Mat type
* Member timestamp: timestamp of the frame std::string
*/
struct frame {
Mat image;
string timestamp;
} ;
/**
* Return the date when it is called
* @return date of the called formated as dd-mm-YYYY-II-MM-SS
* @type std::string
*/
string timestamp ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y-%I-%M-%S",timeinfo);
std::string str(buffer);
return str;
}
/**
* Grab frame from the camera and insert it in a queue
* @param queue FIBO queue
* @type std::queue
*/
void grabFrame(queue<frame>& queue){
// Statistic variables
time_t timer_begin,timer_end;
int imgCount = 0;
double FPS = 20;
// Object initialization
frame frame;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
// Camera setup
Camera.set( cv::CAP_PROP_FORMAT, CV_8UC1 );
Camera.set( cv::CAP_PROP_FRAME_WIDTH , 1280 );
Camera.set( cv::CAP_PROP_FRAME_HEIGHT, 960 );
Camera.set( cv::CAP_PROP_FPS , FPS );
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;}
time ( &timer_begin );
for ( ;; ) { // Infinite loop
Camera.grab();
Camera.retrieve (image);
string time = timestamp();
frame.image = image;
frame.timestamp = time + "-" + to_string(imgCount);
mtx.lock(); // Lock queue that can be shared by multiple thread
queue.push(frame);
mtx.unlock();
imgCount ++;
cout << imgCount << " " << queue.size() << endl;
if (queue.size() > 10000){ // Automatic FPS setting, no mutex here to check if errors
usleep(2000000);
}
}
Camera.release();
//show time statistics
time ( &timer_end ); /* get current time; same as: timer = time(NULL) */
double secondsElapsed = difftime ( timer_end,timer_begin );
cout<< secondsElapsed<<" seconds for "<< imgCount<<" frames : FPS = "<< ( float ) ( ( float ) ( imgCount ) /secondsElapsed ) <<endl;
}
/**
* Grab frame from the camera and insert it in a queue
* @param queue FIBO queue
* @type std::queue
*/
void worker(queue<frame>& queue){
Mat tmp;
frame frame;
bool notEmpty = false;
for(;;){ // Infinite loop
mtx.lock(); // Lock shared queue
if ( !queue.empty() ) {
frame = queue.front();
queue.pop();
notEmpty = true;
}
mtx.unlock();
if ( notEmpty ) {
//cv::imshow("raspicam_cv_image.jpg", frame.image);
//if(waitKey(1) >= 0) break;
analysisMtx.lock();
resize(frame.image, tmp, Size(640, 480)); // Resize analysis image to be faster
pMOG2->apply(tmp , fgMaskMOG2);
analysisMtx.unlock();
double maskSum = sum(fgMaskMOG2)[0];
if (maskSum > 5000000.){ // Condition to detect movement
cout << "Detect" << endl;
string name = "images/" + frame.timestamp + ".pgm";
cout<<name<<endl;
imwrite(name, frame.image);
}
}
notEmpty = false;
}
}
int main(){
experimental::filesystem::create_directories("images/");
int nWorker = 3;
queue<frame> frameQueue;
thread t1(grabFrame, ref(frameQueue));
thread workerPool[nWorker];
for (int i = 0; i < nWorker; i++ ){
workerPool[i] = thread(worker, ref(frameQueue));
}
t1.join();
for (int i = 0; i < nWorker; i++ ){
workerPool[i].join();
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment