Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Slow Processing on Rasberry Pi

I'm building an application that takes two video sources, pulls their frames, and places them one after another in a third video, essentially "interweaving" the frames. I had planned on using the Raspberry Pi for this, but the processing speed is unreasonable; it takes five minutes to produce 5 seconds of video.

Is there something I could do to (significantly) speed up the processing time of the application, beyond switching hardware?

Here's the basics of the code:

int framePerFile = 300; /*Put 300 frames in one video file (about 5sec )*/
int videoFile = 0; /*Current video count.*/

/*Get input videos*/
CvCapture *video1;
if( (video1  = cvCreateFileCapture( argv[1] )) == NULL )
{
    cout << argv[1] << "could not be opened." << endl;
    return( -2 );
}

CvCapture *video2;
if( (video2  = cvCreateFileCapture( argv[2] )) == NULL )
{
    cout << argv[2] << "could not be opened." << endl;
    return( -2 );
}

/*Get the first frame, and video properties from that frame.*/
IplImage *frame = cvQueryFrame( video1 );
double framePerSec = cvGetCaptureProperty( video1, CV_CAP_PROP_FPS );
double framePerSec2 = cvGetCaptureProperty( video2, CV_CAP_PROP_FPS );
CvSize vidSize = cvSize( (int) cvGetCaptureProperty(video1, CV_CAP_PROP_FRAME_WIDTH),
                         (int) cvGetCaptureProperty(video1, CV_CAP_PROP_FRAME_HEIGHT)
);

/*Create output file*/
char fileName[50];
int fileNameLength;
fileNameLength = sprintf( fileName, "INTERLACE%03d.h263", videoFile );
cout << "Using OpenCV version: " << CV_VERSION << endl;

/*Create video writer with vidSize of original video.*/
cout << "Frames per sec: " << framePerSec << endl
     << "Frames per sec: " << framePerSec2 << endl;

     CvVideoWriter *writer;
if( (writer = cvCreateVideoWriter( fileName, CV_FOURCC( 'U', '2', '6', '3' ), 60, vidSize )) == NULL )
{
    cout << "An error occured whilst making the video file." << endl;
    return( -2 );
}
else
{
    cout << "Video creation successful!" << endl;
}

/*Write frames of input files to ouptut file*/
int currFrame = 0;
while( currFrame < framePerFile )
{
    frame = cvQueryFrame( video1 );
    if( !frame ) break;
    cvWriteFrame( writer, frame );
    frame = cvQueryFrame( video2 );
    if( !frame ) break;
    cvWriteFrame( writer, frame );

    currFrame += 2;
}

cout << "Video creation complete." << endl;

/*Cleanup*/
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &video1 );
cvReleaseCapture( &video2 );

I've tried a few different codecs, with no noticeable difference. I've also run this on a Core 2 Duo running Ubuntu 12.10 with a processing time of approximately 20 seconds; I realize the Raspberry Pi is a single core processor, but I'm hoping to increase performance to under a minute, regardless.

Written in C++, running on the Raspberry Pi (model B, processor overclocked to 1GHz), using OpenCV 2.4.9.