Results 1 to 5 of 5

Thread: Pausing a Thread running an infinite loop (Qt+OpenCV)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Pausing a Thread running an infinite loop (Qt+OpenCV)

    Good morning community,
    I am developing an OpenCV and Qt based app where I detect the genre of the person looking to a camera, and I fire a script if the detected gender is "Male" and another script if "Female".
    To detect face and gender I use a caffe model and the OpenCV inference engine. To prevent the GUI from freezing, i decided to use a separate thread for camera frames processing but my poor experience in multithreading is giving me some problems. Reading some articles seems that the correct way to use QThreads is using moveToThread function.

    This is the basic structure of my program:

    GDWidget.h
    Qt Code:
    1. ..code..
    2. private:
    3. Ui::GDWidget *ui;
    4. cv::VideoCapture m_capture;
    5.  
    6. cv::Mat m_currentFrame;
    7.  
    8. // for capture thread
    9. QMutex *m_dataLock;
    10. GDDetector *m_worker;
    11. QThread *m_thread;
    12.  
    13. QString m_maleUrl = "https://app.onsign.tv/play/u0mAM5ju0R9cP4R5wr6L?repeat=1";
    14. QString m_femaleUrl = "https://app.onsign.tv/play/Q0CoHhopjpr8l1IaxC7v?repeat=1";
    15. };
    To copy to clipboard, switch view to plain text mode 

    GDWidget.cpp
    Qt Code:
    1. void GDWidget::openCamera()
    2. {
    3. int camIdx = 0;
    4. m_worker = new GDDetector(this, camID, m_dataLock);
    5. m_thread = new QThread;
    6. m_worker->moveToThread(m_thread);
    7. connect(m_worker, &GDDetector::frameCaptured, this, &GDWidget::updateFrame);
    8.  
    9. connect(m_thread, SIGNAL(started()), m_worker, SLOT(processFrame()));
    10. connect(m_worker, SIGNAL(finished()), m_thread, SLOT(quit()));
    11. connect(m_worker, SIGNAL(finished()), m_worker, SLOT(deleteLater()));
    12. connect(m_thread, SIGNAL(finished()), m_thread, SLOT(deleteLater()));
    13. thread->start();
    14. m_worker->start();
    15. }
    To copy to clipboard, switch view to plain text mode 

    In my processFrame() i have an infinite loop (that stops when I set to true a variable). My problem is that I would get the thread paused ( for 10 seconds ) when I detect the person gender.

    Qt Code:
    1. void GDDetector::processFrame()
    2. {
    3. m_running = true;
    4. m_capture.open(m_cameraIdx);
    5. cv::Mat tmp_frame;
    6.  
    7. ..code ..
    8.  
    9. while(m_running)
    10. {
    11. m_capture >> tmp_frame;
    12. if (!tmp_frame.empty())
    13. {
    14. detectFace(tmp_frame);
    15.  
    16. m_dataLock->lock();
    17. cvtColor(tmp_frame, m_sourceFrame, cv::COLOR_BGR2RGB);
    18. m_dataLock->unlock();
    19.  
    20. emit frameCaptured(&m_sourceFrame);
    21. }
    22.  
    23. ///// I WOULD PAUSE THIS LOOP WHEN I GET A SPECIFIC RESULT FROM THE detectFace ROUTINE ///
    24. }
    25.  
    26. emit processFinished();
    27.  
    28. m_capture.release();
    29. m_running = false;
    30. }
    To copy to clipboard, switch view to plain text mode 

    I would have a help in pausing the infinite loop when i detect the gender. Also I would know how to move my code to QtConcurrent module but I don't know how.
    Any help will be appreciated

    Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pausing a Thread running an infinite loop (Qt+OpenCV)

    Hi,

    If you only want to wait for 10 seconds you can call "msleep" method from a QThread. So, add this code on line 23:
    Qt Code:
    1. QThread::currentThread()->msleep(10000);
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    franco.amato (28th October 2019)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pausing a Thread running an infinite loop (Qt+OpenCV)

    If you only want to wait for 10 seconds you can call "msleep" method from a QThread. So, add this code on line 23:
    It is generally a bad idea (and a sign of a design that needs some more thinking) to add pauses of this sort to any program. Not only does it lock the thread during the sleep call, the choice of the delay period is usually arbitrary and dependent on particular hardware.

    If the OP's issue is that he does not want to perform any more face detection while the current face is being processed, then the solution should not be to pause the loop, but probably not to use an infinite loop at all. Instead, it would be better to start and stop the frame capture using some kind of handshaking mechanism between capturing and processing, or use a QTimer to trigger capture every "n" seconds, or some similar method.

    It might be helpful to draw something like a UML timing diagram with the various steps in the process.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (28th October 2019)

  6. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pausing a Thread running an infinite loop (Qt+OpenCV)

    It is generally a bad idea (and a sign of a design that needs some more thinking) to add pauses of this sort to any program. Not only does it lock the thread during the sleep call, the choice of the delay period is usually arbitrary and dependent on particular hardware.

    If the OP's issue is that he does not want to perform any more face detection while the current face is being processed, then the solution should not be to pause the loop, but probably not to use an infinite loop at all. Instead, it would be better to start and stop the frame capture using some kind of handshaking mechanism between capturing and processing, or use a QTimer to trigger capture every "n" seconds, or some similar method.

    It might be helpful to draw something like a UML timing diagram with the various steps in the process.
    Totally agree. But he asked to wait for 10 seconds.
    I used to use other mechanisms like QWaitCondition to let the thread go sleep and wake up when some condition is triggered and synchronize multiple threads.
    Òscar Llarch i Galán

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pausing a Thread running an infinite loop (Qt+OpenCV)

    Totally agree. But he asked to wait for 10 seconds.
    Yes, true, you did answer the question. QWaitCondition might be a good solution for this, or some kind of state machine.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Qt window resize without infinite loop
    By rpvpqq in forum Newbie
    Replies: 1
    Last Post: 15th April 2019, 16:34
  2. Problem pausing the main thread
    By franco.amato in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2011, 21:47
  3. Pausing a thread while waiting that phonon play a sound file
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2011, 21:41
  4. Infinite loop in QXmlSchemaValidator::validate()?
    By TropicalPenguin in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2010, 15:09
  5. infinite loop
    By zakis in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 17:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.