Ask Your Question
0

optimization techniques for opencv, C++

asked 2016-01-09 12:55:22 -0600

pradeep_kb gravatar image

I have written a program in which the program has to detect objects from the video. the actual duration of video is 5 hours but it is taking nearly 10 hours on raspberry pi 2. It is taking very long time, doesn't work in real time. Is there any technique to utilize all the 4 cores of raspberry pi to run the program? Are there any optimization techniques available for opencv to make my program run faster?

edit retag flag offensive close merge delete

Comments

1

what kind of objects do you detect.IMHO you don't need to proceess every frame. you can skip frames.

sturkmen gravatar imagesturkmen ( 2016-01-09 13:04:10 -0600 )edit

@sturkmen I am detecting the cars present in the video and counting no. of cars entering in certain region. I am not processing every frame. I am have tested the program with 4, 6 & 8 frame skips. But still it is taking more time and not processing in required time. The video that have is of 5 hours and when I run the program it is taking 6.5 to 7 hours for skip 8 but it is not giving accurate results. The number of cars count is reducing.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-09 22:23:44 -0600 )edit

@sturkmen I have even done re-sizing of image to 50% 75% and 25%.. did all possible thinngs to get good result.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-09 22:33:34 -0600 )edit

take a look at my other answer and here

sturkmen gravatar imagesturkmen ( 2016-01-10 04:18:06 -0600 )edit
1

Where is your program bottleneck? Did you perform profiling?

Vit gravatar imageVit ( 2016-01-10 18:45:50 -0600 )edit

@Vit, I did not do profiling, I don't know what it is. Could you please help me to know that

pradeep_kb gravatar imagepradeep_kb ( 2016-01-10 19:11:04 -0600 )edit

@sturkmen@Vit Does Background subtraction helps to speedup the program?

pradeep_kb gravatar imagepradeep_kb ( 2016-01-10 19:12:44 -0600 )edit
1
Vit gravatar imageVit ( 2016-01-10 19:18:54 -0600 )edit

@Vit in my previous comment i gave two link based on Background subtraction

sturkmen gravatar imagesturkmen ( 2016-01-10 20:22:52 -0600 )edit

I am sorry @sturkmen but @Vit is right, profiling is the only decent solution here. We need to identify which part of his code takes long on raspberry pi first!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-11 06:46:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-09 14:31:53 -0600

updated 2016-01-09 14:50:16 -0600

in the sample code below i tried to show how we can speed up the face detection on video.

by combination of skipping frames and resizing frame you can speed up the process. you can change skip value or resize ratio according to your expectation.

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    string cascadeName = "lbpcascade_frontalface.xml";
    VideoCapture capture;
    Mat frame;
    CascadeClassifier cascade;

    if( !cascade.load( cascadeName ) )
    {
        printf("ERROR: Could not load classifier cascade\n");
        return -1;
    }

    if(!capture.open( argv[1] ))
    {
        printf("ERROR: Video capturing has not been started\n");
        return -1;
    }

    int frame_number = 0;
    int scale = 2; // scale value could be 4 on HD videos
    for(;;)
    {
        capture >> frame;
        frame_number += 1;

        if( frame.empty() )
            break;

        if( frame_number % 5 == 0 ) // indicates skipped frame count
        {
            Mat gray;
            cvtColor( frame, gray, COLOR_BGR2GRAY );
            resize( gray, gray, Size(), (float)1/scale, (float)1/scale );
            vector<Rect> faces;
            cascade.detectMultiScale( gray, faces, 1.3, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

            for ( size_t i = 0; i < faces.size(); i++ )
            {
                faces[i].x = faces[i].x * scale;
                faces[i].y = faces[i].y * scale;
                faces[i].width = faces[i].width * scale;
                faces[i].height = faces[i].height * scale;
                rectangle( frame, faces[i], Scalar(0,255,0), 2 );
            }
        }

        imshow("Face Detection Demo", frame );
        waitKey(1);
        int c = waitKey(10);
        if( c == 27 || c == 'q' || c == 'Q' )
            break;
    }
    return 0;
}
edit flag offensive delete link more

Comments

I am detecting the cars present in the video and counting no. of cars entering in certain region. I am not processing every frame. I am have tested the program with 4, 6 & 8 frame skips. But still it is taking more time and not processing in required time. The video that have is of 5 hours and when I run the program it is taking 6.5 to 7 hours for skip 8 but it is not giving accurate results. The number of cars count is reducing.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-09 22:24:21 -0600 )edit

You need to focus on profiling your program as @Vit stated. As long as you do not know how long it takes for each processing step, we cannot help you out with decent tips!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-11 06:45:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-09 12:55:22 -0600

Seen: 1,890 times

Last updated: Jan 09 '16