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;
}
what kind of objects do you detect.IMHO you don't need to proceess every frame. you can skip frames.
@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.
@sturkmen I have even done re-sizing of image to 50% 75% and 25%.. did all possible thinngs to get good result.
take a look at my other answer and here
Where is your program bottleneck? Did you perform profiling?
@Vit, I did not do profiling, I don't know what it is. Could you please help me to know that
@sturkmen@Vit Does Background subtraction helps to speedup the program?
profiling
@Vit in my previous comment i gave two link based on Background subtraction
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!