Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT OpenCV Play Pause and Stop Video



  • Hi All, I want to add Play Pause and Stop ToolButtons to my current code shown below. Any idea how?

    void MainWindow::DisplayVideo(Ui::MainWindow& ui, std::string& path, MainWindow &parent){
    
        cv::VideoCapture cap(path);
        int frameRate = (int) cap.get(cv::CAP_PROP_FPS);
        
        cv::Mat frame;
        ui.textEdit->append("Display Video Called");
        parent.on_actionPlay_triggered();
        while(!stop)
        {
            ui.textEdit->append("While loop called");
           cap >> frame;
           if(!cap.read(frame))
           {
               stop = true;
           }
           if(frame.channels() == 3)
           {
               cv::Mat dest;
               cv::cvtColor(frame,  dest, cv::COLOR_BGR2RGB);
               cv::resize(dest, dest, cv::Size(900,700), 0, 0, cv::INTER_AREA);
               QImage imdisplay((uchar*)dest.data, dest.cols, dest.rows, dest.step, QImage::Format_RGB888);
               ui.display_image->setPixmap(QPixmap::fromImage(imdisplay));
           }
           else
           {
               QImage imdisplay = QImage((const unsigned char*)(frame.data),
                                                frame.cols,frame.rows,QImage::Format_Indexed8);
               ui.display_image->setPixmap(QPixmap::fromImage(imdisplay));
           }
           QApplication::processEvents();
        }
    
    }
    

    possibly logic controls? to pause the video frames from displaying when the Pause ToolButton is clicked.


  • Lifetime Qt Champion

    @Stevendragoes This is not how event driven applications are implemented.
    Do not implement endless loops!
    If there is no callback in OpenCV which is called when a new frame is available (I'm not an OpenCV expert) I suggest to use a QTimer to call a slot regularly where you fetch the next frame. Then it is easy to implement stop/pause: call stop() on the timer and start when user wants to start play back again.



  • Hi @jsulm

    I am not implementing endless loops. I have a button that makes "stop" to true when the stop button is pressed. Alright. Any examples on the QTimer so that I can try it out in my code?


  • Lifetime Qt Champion

    @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

    I am not implementing endless loops. I have a button that makes "stop" to true when the stop button is pressed

    I know, but this is still not how you should implement event driven applications.

    You can find examples here: https://doc.qt.io/qt-5/qtimer.html

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &getNewFrame);
    timer->start(1000); // Change the interval to a value matching your framerate
    

    Here getNewFrame is a lot where you read one frame.



  • Alright @jsulm, I will try it out and Will update accordingly. Thank you for your assistance



  • @jsulm and I am guessing getNewFrame is the function that I have to implement so that I get the new frame?


  • Lifetime Qt Champion

    @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

    and I am guessing getNewFrame is the function that I have to implement so that I get the new frame?

    Yes



  • Hello @jsulm
    When I implement getNewFrame, I can't even get the path to print on the text edit. Any Reason why? I know it is triggering every 1 second, and I am trying to append every one second as well?



  • @jsulm
    db330cbf-9dbb-474f-982d-59344137290a-image.png

    qpath is loaded into by FileDialog


  • Lifetime Qt Champion

    @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

    qpath

    Do you assign the path selected in the file dialog to it? Please show your code, else I can only guess.

    "I know it is triggering every 1 second, and I am trying to append every one second as well?" - if you did not change the timeout period then yes it is triggered once each second.



  • @jsulm said in QT OpenCV Play Pause and Stop Video:

    Do you assign the path selected in the file dialog to it?

    Hello @jsulm
    6c06a870-02f9-4a11-b478-622f8ca6e627-image.png

    I assighed the qpath after I pressed Load PushButton in the UI


  • Lifetime Qt Champion

    @Stevendragoes Are you sure your timer is actually running? And are you sure you connected the slot for the timer properly?

    Please post your code as text, not as screen shot, then it is easier to others to change it if there is something wrong.



  • cShowVideo::cShowVideo(Ui::MainWindow& ui, std::string& path, QString& qpath, MainWindow &parent) : MainWindow()
    {
        timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(getNextFrame()));
        //ui.textEdit->setText(qpath);
        timer->start(1000);
        DisplayVideo(ui,path,parent);
    

    this is my timer code. It starts properly.
    when i used qDebug, it shows the text in 1 second which means that the Timer function is working?
    However, when I change it to the text edit, as shown below, the text is not appearing as intended
    The code printing is as follows:

    void MainWindow::getNextFrame()
    {
        qDebug("Get Next Frame is working");
        ui->textEdit->append("GetNextFrame");
    }
    
    

    To show its working, the "Get Next Frame is working" is printing every second
    cc2daa6d-75d7-42d1-9596-5e7fe43a2ce7-image.png



  • @jsulm Sorry forgot to tag you


  • Lifetime Qt Champion

    @Stevendragoes Do you still have the loop in DisplayVideo?



  • @jsulm Yes I do have the loop, Should I take it out first?


  • Lifetime Qt Champion

    @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

    Should I take it out first?

    Of course. As I said before this loop is blocking the event loop, so UI does not work properly.



  • @jsulm When I commented out the loop, the timer doesn't run anymore.


  • Lifetime Qt Champion

    @Stevendragoes Please comment out DisplayVideo call in cShowVideo



  • I have commented it out also.
    However, what I want is that when I load the video, the timer to start. So, I initialised the timer in the cShowvideo which is a class inherited from the MainWindow().
    Is that an incorrect way of doing so?


  • Lifetime Qt Champion

    @Stevendragoes As I said already: in the slot connected to the timer you get next frame and show it. You need to implement this to make it work.



  • Yeap. I think I figured it out.
    I shifted the initialisation of the timer in the MainWindow.

    MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
    {
        timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(getNextFrame()));
        ui->setupUi(this);
    }
    
    

    and I used the cShowVideo Class to start the timer when I load the video from the FileDialog

    cShowVideo::cShowVideo(Ui::MainWindow& ui, std::string& path, QString& qpath, MainWindow &parent) : MainWindow()
    {
        parent.timer->start(1000);
        ui.textEdit->append(qpath);
    }
    

    and it works, it is appending every second.



  • @jsulm
    From what I saw in the QTimer Class, there is no such thing as pause the timer?


  • Lifetime Qt Champion

    @Stevendragoes This is not good design - child should not access parent members directly. And this timer does not belong to MainWindow but to cShowVideo. Move timer again to cShowVideo.
    I just noticed that cShowVideo is a local variable in on_actionLoad_Video_triggered(). That means it is destroyed as soon as on_actionLoad_Video_triggered() finishes - that's why your timer is not working. Make sure cShowVideo instance lives as long as it is playing video.
    Also another note regarding design: your cShowVideo should not know anything about the MainWindow UI. This issue is called tight coupling and makes the maintenance of your software harder. In cShowVideo you should only get next frame and send it to MainWindow via a slot, MainWindow knows better how to use its UI to show the frames.



  • Hello @jsulm
    I have FINALLY made it work already. Thank you for your guidance, it was good to drop the cShowVideo Completely. This is the following codes. However, there are a few issues when I try to close the program

    Loading the Video and Starting the Timer so that it gets the next frame.

    void MainWindow::on_actionLoad_Video_triggered()
    {
        QMessageBox::StandardButton loadBtn = QMessageBox::question( this, "MainWindow",tr("Load Video? \n"), QMessageBox::No | QMessageBox::Yes,QMessageBox::Yes);
        if (loadBtn == QMessageBox::Yes)
        {
            qpath = QFileDialog::getOpenFileName(this, "Load Video File"); //Input code to skip the event when cancel is pressed
            if (qpath.isNull())
            {
                ui->textEdit->append("Cannot get file, user cancelled while choosing file");
            }
            else
            {
                path = qpath.toLocal8Bit().constData();\
                ui->textEdit->append("cShowVideo is called");
                //cShowVideo(*ui, path, qpath, *this);                        //For OPENCV analysis and filtering
                cap.open(path);
                vwidth = ui->display_image->height();
                vheight = ui->display_image->width();
                timer->start(30);                                           //this can set the frame rate
                cap >> frame;
            }
        }
        else
        {
            ui->textEdit->setText("User Cancelled");
        }
    }
    

    This is the getNextFrame() function to get the next frame after every timeout() event from the timer

    void MainWindow::getNextFrame()
    {
        cap.read(frame);
        if(frame.empty()==true) return;
        cv::cvtColor(frame,  frame, cv::COLOR_BGR2RGB);
        cv::resize(frame, frame, cv::Size(vheight,vwidth), 0, 0, cv::INTER_AREA);
        QImage imdisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
        ui->display_image->setPixmap(QPixmap::fromImage(imdisplay));
    }
    
    

    I have also implemented 3 pushbuttons to play, pause and stop

    void MainWindow::on_Play_clicked()
    {
        if(timer->isActive() == true){
            return;
        }
        else {
            timer->start(30);
        }
    }
    
    void MainWindow::on_Pause_clicked()
    {
        if(timer->isActive() == true)
        {
            timer->stop();
        }
        else{
            return;
        }
    }
    
    void MainWindow::on_Stop_clicked()
    {
        if(timer->isActive()==true)
        {
            timer->stop();
            cap.release();
        }
    }
    
    

    However, when I try to close it, the program finishes unexpectedly and crashed unexpectedly.
    This is the screenshot when i try to use the debugger to trace where it went wrong.

    ac96b437-ad70-4103-b8b4-efab7b6c279a-image.png



  • maybe @J.Hilk could help me?


  • Lifetime Qt Champion

    @Stevendragoes Can you show the destructor of your MainWindow?
    Also, when closing your app you should first stop the playback if active.



  • @jsulm this is my destructor

    MainWindow::~MainWindow()
    {
        cap.release();
        delete ui;
    }
    
    


  • @jsulm I have also attached the close event captured for reference

    void MainWindow::closeEvent(QCloseEvent *event)    //To confirm that the user really wants to quit the application
    {
        QMessageBox::StandardButton exitBtn = QMessageBox::question( this, "MainWindow",tr("Are you sure you want to exit?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,QMessageBox::Yes);
        if (exitBtn == QMessageBox::Yes) {
            event->accept();
        } else {
            event->ignore();
        }
    }
    

  • Lifetime Qt Champion

    @Stevendragoes I would stop the timer in closeEvent if it is running and user wants to quit.



  • Hi @jsulm I have implemented timer->stop() but it still gives me the same error. I am not too sure why.



  • Hi @jsulm
    I think i have solved the problem

    w.setAttribute(Qt::WA_DeleteOnClose, true);
    

    this was inside the main.cpp. I am not sure why this happens.


  • Lifetime Qt Champion

    @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

    w.setAttribute

    What is w? Maybe it was double delete?


  • Lifetime Qt Champion

    @Stevendragoes said in QT OpenCV Play Pause and Stop Video:

    w.set

    Now I see the problem: w is allocated on the stack and there is no need to delete it explicitly (what Qt::WA_DeleteOnClose does), so it was double delete.



  • @jsulm I suspect is double delete. w is the MainWindow Setup in the main.cpp
    The Code is as follows

    #include "mainwindow.h"
    #include <QApplication>                                         //Widgets Events Handling, Mouse Movements
    
    
    int main(int argc, char *argv[])                                //All Executions are going to begin
    {
        QApplication a(argc, argv);                                 //Create the QApplication Object to Handle Events etc.
        MainWindow w;
        w.showMaximized();
        w.show();
        return a.exec();
    }
    
    
    

    on other hand, do u mind if use my solution to mark it as solved?



  • @jsulm
    Thank you so much for your assistance in these few days. Now I need to move on to interfacing with an inbuilt camera or USB connected camera to do my object tracking.


  • Lifetime Qt Champion

    @Stevendragoes There is no need for Qt::WA_DeleteOnClose, see my previous comment.


Log in to reply