Prashant Kumar
Prashant Kumar

Reputation: 11

Video record using opencv and Qt

I can't seem to find error in the code. The code compile perfectly i am able to start video but i am not able to record video.

image output window

the video file is created in the desired directory but it is only 10 kb. I have tried changing four_cc to MPEG still no effect.

#include "camerafeed.h"
#include "ui_camerafeed.h"
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>

#include <QtCore>

using namespace cv;
using namespace std;
bool recording = false;



camerafeed::camerafeed(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::camerafeed)
{
    ui->setupUi(this);
    cap1.open(0);

    Starttrigger = new QTimer(this);
//    connect(Starttrigger,SIGNAL(timeout()),this,SLOT(processGui()));


}

camerafeed::~camerafeed()
{
    delete ui;
}

void camerafeed::processGui()
{
    int fcc =   CV_FOURCC('D','I','V','X');
    int fps =   20;

    cap1.read(frame1);

    Size frameSize(cap1.get(CV_CAP_PROP_FRAME_WIDTH),cap1.get(CV_CAP_PROP_FRAME_HEIGHT));
    string filename1 = "D:\\Camera1\\video1.avi";
    writer1 = VideoWriter(filename1,fcc,fps,frameSize);

    bool bSuccess = cap1.read(frame1);

    if(recording==true)
    {
        writer1.write(frame1);
        putText(frame1,"[REC]",Point(0,30),5,1,Scalar(0,0,225));

    }

    cv::cvtColor(frame1,frame1,CV_BGR2RGB);

    QImage qframe1((uchar*)frame1.data,frame1.cols,frame1.rows,frame1.step,QImage::Format_RGB888);

    ui->camera1->setPixmap(QPixmap::fromImage(qframe1));
}

void camerafeed::on_Start_clicked()
{
    Starttrigger->start(20);
    connect(Starttrigger,SIGNAL(timeout()),this,SLOT(processGui()));
}


void camerafeed::on_stop_clicked()
{
    Starttrigger->stop();

    recording =  false;
    disconnect(Starttrigger,SIGNAL(timeout()),this,SLOT(processGui()));

}

void camerafeed::on_record_clicked()
{
    recording   =   !recording;

}

Upvotes: 0

Views: 1916

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

One of the problems of your code is that you are restarting the video object with the function, I recommend you run it only when the start button is pressed. For this, it is better to create a function that performs this action.

CameraFeed::CameraFeed(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    isRecorded = false;
    trigger = new QTimer(this);
    connect(trigger, &QTimer::timeout, this, &CameraFeed::processGui);
}

void CameraFeed::initCamera()
{
    cap.open(0);
    if(cap.isOpened())
    {
        qDebug() << "Camera successfully connected.";
    }
}

void CameraFeed::initVideo()
{
    std::string filename =  "video.avi";
    int fcc =   CV_FOURCC('D','I','V','X');
    int fps =   20;
    cv::Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH),cap.get(CV_CAP_PROP_FRAME_HEIGHT));
    writer = cv::VideoWriter(filename,fcc,fps,frameSize);
}

void CameraFeed::processGui()
{
    if(cap.read(frame)){
        cv::cvtColor(frame,frame,CV_BGR2RGB);
        if(isRecorded)
        {
            writer.write(frame);
            cv::putText(frame,"[REC]",cv::Point(0,30),5,1,cv::Scalar(0,0,225));
        }

        QImage qframe((uchar*)frame.data,frame.cols,frame.rows,frame.step,QImage::Format_RGB888);

        ui->camera1->setPixmap(QPixmap::fromImage(qframe));
    }
}


void CameraFeed::on_start_clicked()
{
    initCamera();
    trigger->start(100);
}

void CameraFeed::on_stop_clicked()
{
    if(cap.isOpened())
    {
        // Disconnect camera
        cap.release();
        qDebug() << "Camera successfully disconnected.";
    }
    trigger->stop();
}

void CameraFeed::on_record_clicked()
{
    isRecorded = !isRecorded;
    if(isRecorded)
        initVideo();
}

complete code

Upvotes: 1

Related Questions