Sid411
Sid411

Reputation: 703

Video not playing continuously

I am playing video in Qt using opencv. I am having 6 tiled view cameras from which I am playing video. The problem is if one of the videos is not playing i.e finishes then the GUI freezes and exits. The error I get is you must reimplement QApplication::notify() and catch the exceptions there. How to do this? The code I am using is as follows.

Somewhere in a function

 void MainWindow::ActivateWindow()
{
  //Some part of code to set Index for stacked widget

   if(stackWidget->currentIndex()==9)
   {
      const int imagePeriod == 1000/25;
      imageTimer->setInterval(imagePeriod);
      connect(imageTimer,SIGNAL(timeout()),this,SLOT(demoSlot());
      imageTimer->start();
   }
}

In slot demoSlot

 void MainWindow::demoSlot()
{
   captureCamera1 cvCaptureFromFile("/root/mp.mp4");
   captureCamera2 cvCaptureFromFile("/root/mp.mp4");
   captureCamera3 cvCaptureFromFile("/root/mp.mp4");

   while(imageTimer->isActive())
   {
      frameCamera1 = cvQueryFrame(captureCamera1);
      frameCamera2 = cvQueryFrame(captureCamera2);
      frameCamera3 = cvQueryFrame(captureCamera2);

      sourceImageCam1 = frameCamera1;
      sourceImageCam2 = frameCamera2;
      sourceImageCam3 = frameCamera3;

      cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
      cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
      cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);

      cv::cvtColor(sourceImageCam1,sourceImageCam2,CV_BGR2RGB);
      cv::cvtColor(sourceImageCam2,sourceImageCam2,CV_BGR2RGB);
      cv::cvtColor(sourceImageCam2,sourceImageCam2,CV_BGR2RGB);

      QImage tempImage1 = QImage((const unsigned char* sourceImageCam1.data,sourceImageCam1.cols,sourceImageCam2.rows,QImage::Format_RG888);
      QImage tempImage2 = QImage((const unsigned char* sourceImageCam2.data,sourceImageCam2.cols,sourceImageCam2.rows,QImage::Format_RG888);
      QImage tempImage3 = QImage((const unsigned char* sourceImageCam3.data,sourceImageCam3.cols,sourceImageCam3.rows,QImage::Format_RG888);

      labelCameraCapture1->setPixmap(QPixmap::fromImage(tempImage1));     //label to display video
      labelCameraCapture2->setPixmap(QPixmap::fromImage(tempImage2));
      labelCameraCapture3->setPixmap(QPixmap::fromImage(tempImage3));

      lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
      lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
      lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());

      cvWaitkey(20);
      qApp->processEvents();
    }
 if(imageTimer->isActive())
 {
   imageTimer->stop();
 }
 else
 {
   imageTimer->start();
 }
}

In header file

   cvCapture *captureCamera1;
   cvCapture *captureCamera1;
   cvCapture *captureCamera1;

   IplImage frameCamera1;
   IplImage frameCamera2;
   IplImage frameCamera3;

   cv::Mat sourceImageCam1;
   cv::Mat sourceImageCam2;
   cv::Mat sourceImageCam3;

Upvotes: 0

Views: 378

Answers (2)

Marek R
Marek R

Reputation: 37512

This will do the trick changing that to 3 movies is simple.

class MainWindow : public QMainWindow {
Q_OBJECT

   explicit QMainWindow(QWidget *parent) ....
   // prepare timer and so on

public slots:
   void startVideo() {
      vid1.close();
      vid1.open("/root/mp.mp4");
      imageTimer->start();
   }

   void demoSlot() {
      cv::Mat frame;
      vid1 >> frame;
      cv::cvtColor(frame,frame,CV_BGR2RGB);
      QImage img((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
      label1->setPixmap(QPixmap::fromImage(img));
   }

private:
   ...
   QTimer *imageTimer;
   cv::VideoCapture vid1;
};

Upvotes: 1

Andrey  Smorodov
Andrey Smorodov

Reputation: 10852

Check if frame captured from camera is NULL. Then simply skip processing steps for this camera.

And it'll be better to not mix C++ and C interfaces (I mean cv::Mat and IplImage).

Upvotes: 0

Related Questions