Commit 5a7d08de authored by Benjamin Gallois's avatar Benjamin Gallois

Add running time

parent 805dd24e
......@@ -17,8 +17,8 @@ using namespace cv;
// Global variables
mutex mtx;
mutex analysisMtx;
mutex mtx; // Shared queue mutex
mutex analysisMtx; // Shared background substractor object
Mat fgMaskMOG2;
Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
time_t timeBegin, timeNow;
......@@ -37,7 +37,7 @@ struct frame {
/**
* Return the date when it is called
* @return date of the called formated as dd-mm-YYYY-II-MM-SS
* @return date when it is called formated as dd-mm-YYYY-II-MM-SS
* @type std::string
*/
string timestamp ()
......@@ -50,7 +50,7 @@ string timestamp ()
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y-%I-%M-%S",timeinfo);
std::string str(buffer);
string str(buffer);
return str;
......@@ -61,8 +61,12 @@ string timestamp ()
* Grab frame from the camera and insert it in a queue
* @param queue FIBO queue
* @type std::queue
* @param FPS goal framerate
* @type double
* @param duration duration of the detection in seconds
* @type double
*/
void grabFrame(queue<frame>& queue, double FPS){
void grabFrame(queue<frame>& queue, double FPS, double duration){
// Object initialization
......@@ -81,20 +85,20 @@ void grabFrame(queue<frame>& queue, double FPS){
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;}
for ( ;; ) { // Infinite loop
while( difftime(timeNow, timeBegin) < duration ) {
Camera.grab();
Camera.retrieve (image);
string time = timestamp();
string timeStamp = timestamp();
time ( &timeNow );
imgCount ++;
frame.image = image;
frame.timestamp = time + "-" + to_string(imgCount);
frame.timestamp = timeStamp + "-" + to_string(imgCount);
mtx.lock(); // Lock queue that can be shared by multiple thread
queue.push(frame);
mtx.unlock();
//cout << imgCount << " " << queue.size() << endl; // For debugging
if (difftime ( timeNow, timeBegin ) < 120 && queue.size() > 500){ // Automatic FPS setting
if (difftime ( timeNow, timeBegin ) < 120 && queue.size() > 500){ // Automatic FPS adjustment during 2 minutes
FPS -= 1;
cout << "Set fps at: " << FPS << endl;
Camera.release();
......@@ -104,9 +108,9 @@ void grabFrame(queue<frame>& queue, double FPS){
queue.pop();
}
}
else if (difftime ( timeNow, timeBegin ) > 120 && setup == true) {
else if ( setup == true && difftime(timeNow, timeBegin) > 120) { // End of adjustment time
mtx.lock();
for(int i = 0; i < queue.size(); i++){
for(int i = 0; i < queue.size(); i++){ // Clear queue to begin the recording
queue.pop();
}
mtx.unlock();
......@@ -127,14 +131,14 @@ void grabFrame(queue<frame>& queue, double FPS){
* @param threshold
* @type double
*/
void worker(queue<frame>& queue, double threshold, string path){
void worker(queue<frame>& queue, double threshold, string path, double duration){
Mat tmp;
frame frame;
bool notEmpty = false;
vector<vector<Point>> contours;
for(;;){ // Infinite loop
while( difftime(timeNow, timeBegin) < duration || notEmpty) {
mtx.lock(); // Lock shared queue a minimum of time
if ( !queue.empty() ) {
frame = queue.front();
......@@ -149,10 +153,9 @@ void worker(queue<frame>& queue, double threshold, string path){
pMOG2->apply(tmp , fgMaskMOG2);
analysisMtx.unlock();
time ( &timeNow ); /* get current time; same as: timer = time(NULL) */
time ( &timeNow );
findContours( fgMaskMOG2, contours, RETR_TREE, CHAIN_APPROX_SIMPLE ); // Find contours in the image
for (unsigned int i = 0; i < contours.size(); i++){
// double secondsElapsed = difftime ( timer_end,timer_begin );
if (difftime ( timeNow, timeBegin ) > 125 && contourArea(contours[i]) > threshold) {
cout << "Detect" << endl;
string name = path + frame.timestamp + ".pgm";
......@@ -172,15 +175,17 @@ int main(int argc, char *argv[]){
int c;
time (&timeBegin);
time ( &timeNow );
// Default values
int nWorker = 3;
double threshold = 50000;
double FPS = 60;
string path = "images/";
double stopAfter = 1.7E301; // Running time of the program in hours
queue<frame> frameQueue;
while ((c = getopt(argc, argv, "t:n:f:p:")) != -1){ // Command line options parsing
while ((c = getopt(argc, argv, "t:n:f:p:s:")) != -1){ // Command line options parsing
switch(c){
case 't':
threshold = stod(optarg);
......@@ -194,16 +199,19 @@ int main(int argc, char *argv[]){
case 'p':
path = optarg;
break;
case 's':
stopAfter = stod(optarg)*3600;
break;
}
}
cout << "Parameters: " << ", threshold: " << threshold << ", worker threads:" <<nWorker << ", fps: "<< FPS << ", path: "<< path << endl;
cout << "Parameters: " << ", threshold: " << threshold << ", worker threads:" <<nWorker << ", fps: "<< FPS << ", path: "<< path << ", duration (seconds)" << stopAfter << 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 t1(grabFrame, ref(frameQueue), FPS, stopAfter);
thread workerPool[nWorker];
for (int i = 0; i < nWorker; i++ ){
workerPool[i] = thread(worker, ref(frameQueue), threshold, path);
workerPool[i] = thread(worker, ref(frameQueue), threshold, path, stopAfter);
}
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