Monday, December 30, 2013

Programming A Webcam Recorder using OpenCV

There are 4 important steps in capturing video / images from webcam programmatically.
  1. Initialize webcam
  2. Capture frames from webcam for a particular duration
  3. Stop capturing frames from Webcam
  4. Release resources for escaping from memory leaks
Above mentioned 4 functionalities are implemented in the following class

/* File Name: WCRecorder.hpp */

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
static int argc;
static char **argv;

class WebcamVideoRecorder
{
 public:
 CvCapture *capture;
 IplImage* rgb_image;
 bool initialized;
public:
 WebcamVideoRecorder(); /* constructor of  WebcamVideoRecorder calss */
 void Initialize(); /* Function to initialize webcam */
 void ProcessFrame(); /* Function to Capture frames from webcam */
 void StopProcessFrame(); /* Function to unCapture frames from webcam */
 void ReleaseResources(); /* function to release all resources */
};

/* File Name: WCRecorder.cpp */

#include <stdio.h>
#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "WCRecorder_GUI.hpp"
/* Pre-requisite OpenCV-2.1.0 http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.1/ */
/* Command to Run: g++ WCRecorder.cpp `pkg-config --cflags --libs opencv` */
using namespace std;

WebcamVideoRecorder::WebcamVideoRecorder() {
    capture = 0;
    rgb_image = 0;
    initialized = false;
}

void WebcamVideoRecorder::Initialize() {
    printf("OpenCV version %s (%d.%d.%d)\n", CV_VERSION, CV_MAJOR_VERSION,
            CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
    capture = cvCaptureFromCAM(0);
    if (!capture) {
        fprintf(stderr, "Error in Capture...!!!\n");
        initialized = false;
    } else {
        cvNamedWindow("WebCam-Video-Recorder", 0);
        initialized = true;
    }
}

void WebcamVideoRecorder::ProcessFrame() {
    if (initialized) {
        string p_path(pfile_path);
        string p0_path(pfile_path);
        char str[10];
        CvVideoWriter *writer;
        IplImage *bgr_frame = cvQueryFrame(capture);
        CvSize size = cvGetSize(bgr_frame);
        rgb_image = cvCreateImage(cvGetSize(bgr_frame), bgr_frame->depth,
                bgr_frame->nChannels);
        if (vflag || pflag) {
            strcat((char*) vfile_path, ".avi");
            if (vflag)
                writer = cvCreateVideoWriter(vfile_path,
                        CV_FOURCC('I', 'Y', 'U', 'V'), 10.0, size);
            while (((bgr_frame = cvQueryFrame(capture)) != NULL)) {
                if (vflag)
                    cvWriteFrame(writer, bgr_frame);
                if (!bgr_frame)
                    break;
                cvCopy(bgr_frame, rgb_image);
                cvShowImage("WebCam-Video-Recorder", rgb_image);
                if (pflag) {
                    sprintf(str, "%d", ++nFrame);
                    p_path.append(str);
                    p_path.append(".jpeg");
                    cvSaveImage(p_path.c_str(), rgb_image);
                    p_path = p0_path;
                }
                char c = cvWaitKey(10);
                if (c == 27)
                    break;
            }
            cvReleaseVideoWriter(&writer);
        }
    }
    ReleaseResources();
}

void WebcamVideoRecorder::StopProcessFrame() {
    cvReleaseCapture (&capture);
}

void WebcamVideoRecorder::ReleaseResources() {
    cvReleaseImage (&rgb_image);
    cvReleaseCapture (&capture);
    cvDestroyWindow("WebCam-Video-Recorder");
}

int main(int argc_, char **argv_) {
    argc = argc_;
    argv = argv_;
    WebcamVideoRecorder WVR;
    WVR.Initialize();
    for(int i=0 ; i < 200 ; i++) /* here you can use timer to record that much of time */
    WVR.ProcessFrame();

    WVR.StopProcessFrame();
    WVR.ReleaseResources();
    return 0;
}

No comments:

Post a Comment