Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT OpenCV Play Pause and Stop Video
Forum Updated to NodeBB v4.3 + New Features

QT OpenCV Play Pause and Stop Video

Scheduled Pinned Locked Moved Solved General and Desktop
37 Posts 2 Posters 7.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stevendragoes
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • S Stevendragoes

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Stevendragoes
        wrote on last edited by
        #3

        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?

        jsulmJ 1 Reply Last reply
        0
        • S Stevendragoes

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Stevendragoes
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Stevendragoes
              wrote on last edited by
              #6

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

              jsulmJ 1 Reply Last reply
              0
              • S Stevendragoes

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  Stevendragoes
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Stevendragoes
                    wrote on last edited by
                    #9

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

                    qpath is loaded into by FileDialog

                    jsulmJ 1 Reply Last reply
                    0
                    • S Stevendragoes

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

                      qpath is loaded into by FileDialog

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Stevendragoes
                        wrote on last edited by
                        #11

                        @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

                        jsulmJ 1 Reply Last reply
                        0
                        • S Stevendragoes

                          @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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Stevendragoes
                            wrote on last edited by
                            #13
                            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

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Stevendragoes
                              wrote on last edited by
                              #14

                              @jsulm Sorry forgot to tag you

                              jsulmJ 1 Reply Last reply
                              0
                              • S Stevendragoes

                                @jsulm Sorry forgot to tag you

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Stevendragoes Do you still have the loop in DisplayVideo?

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Stevendragoes
                                  wrote on last edited by
                                  #16

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

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • S Stevendragoes

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

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @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.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      Stevendragoes
                                      wrote on last edited by
                                      #18

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

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • S Stevendragoes

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

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @Stevendragoes Please comment out DisplayVideo call in cShowVideo

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          Stevendragoes
                                          wrote on last edited by
                                          #20

                                          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?

                                          jsulmJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved