Commit 805dd24e authored by Benjamin Gallois's avatar Benjamin Gallois

Add setup time

parent e9af1d9a
......@@ -21,6 +21,7 @@ mutex mtx;
mutex analysisMtx;
Mat fgMaskMOG2;
Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
time_t timeBegin, timeNow;
/**
......@@ -63,9 +64,6 @@ string timestamp ()
*/
void grabFrame(queue<frame>& queue, double FPS){
// Statistic variables
time_t timer_begin,timer_end;
int imgCount = 0;
// Object initialization
frame frame;
......@@ -78,33 +76,45 @@ void grabFrame(queue<frame>& queue, double FPS){
Camera.set( cv::CAP_PROP_FRAME_HEIGHT, 960 );
Camera.set( cv::CAP_PROP_FPS , FPS );
int imgCount = 0;
bool setup = true;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;}
time ( &timer_begin );
for ( ;; ) { // Infinite loop
Camera.grab();
Camera.retrieve (image);
string time = timestamp();
imgCount ++;
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);
//cout << imgCount << " " << queue.size() << endl; // For debugging
if (difftime ( timeNow, timeBegin ) < 120 && queue.size() > 500){ // Automatic FPS setting
FPS -= 1;
cout << "Set fps at: " << FPS << endl;
Camera.release();
Camera.set( cv::CAP_PROP_FPS , FPS );
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;}
for(int i = 0; i < 25; i++){
queue.pop();
}
}
else if (difftime ( timeNow, timeBegin ) > 120 && setup == true) {
mtx.lock();
for(int i = 0; i < queue.size(); i++){
queue.pop();
}
mtx.unlock();
cout << "End of the setup time" << endl;
setup = false;
}
}
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;
}
......@@ -117,7 +127,7 @@ void grabFrame(queue<frame>& queue, double FPS){
* @param threshold
* @type double
*/
void worker(queue<frame>& queue, double threshold){
void worker(queue<frame>& queue, double threshold, string path){
Mat tmp;
frame frame;
......@@ -139,11 +149,13 @@ void worker(queue<frame>& queue, double threshold){
pMOG2->apply(tmp , fgMaskMOG2);
analysisMtx.unlock();
time ( &timeNow ); /* get current time; same as: timer = time(NULL) */
findContours( fgMaskMOG2, contours, RETR_TREE, CHAIN_APPROX_SIMPLE ); // Find contours in the image
for (unsigned int i = 0; i < contours.size(); i++){
if (contourArea(contours[i]) > threshold) {
// double secondsElapsed = difftime ( timer_end,timer_begin );
if (difftime ( timeNow, timeBegin ) > 125 && contourArea(contours[i]) > threshold) {
cout << "Detect" << endl;
string name = "images/" + frame.timestamp + ".pgm";
string name = path + frame.timestamp + ".pgm";
imwrite(name, frame.image);
}
}
......@@ -158,30 +170,40 @@ void worker(queue<frame>& queue, double threshold){
int main(int argc, char *argv[]){
experimental::filesystem::create_directories("images/");
int c;
time (&timeBegin);
// Default values
int nWorker = 3;
double threshold = 50000;
double FPS = 15;
double FPS = 60;
string path = "images/";
queue<frame> frameQueue;
while ((c = getopt(argc, argv, "t:n:f:")) != -1){ // Command line options parsing
while ((c = getopt(argc, argv, "t:n:f:p:")) != -1){ // Command line options parsing
switch(c){
case 't':
threshold = atoi(optarg);
threshold = stod(optarg);
break;
case 'n':
nWorker = atoi(optarg);
break;
case 'f':
FPS = atoi(optarg);
FPS = stod(optarg);
break;
case 'p':
path = optarg;
break;
}
}
cout << "Parameters: " << ", threshold: " << threshold << ", worker threads:" <<nWorker << ", fps: "<< FPS << ", path: "<< path << endl;
cout << "Beginning of setup time (2 minutes)" << endl;
const std::experimental::filesystem::path folderPath = path;
experimental::filesystem::create_directories(folderPath);
thread t1(grabFrame, ref(frameQueue), FPS);
thread workerPool[nWorker];
for (int i = 0; i < nWorker; i++ ){
workerPool[i] = thread(worker, ref(frameQueue), threshold);
workerPool[i] = thread(worker, ref(frameQueue), threshold, path);
}
t1.join();
for (int i = 0; i < nWorker; i++ ){
......
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