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!
@Vit, this is manual profiling that I have done. All the results are in milliseconds.
@Vit@StevenPuttemans I have gone through the profiling, I got to know we need a tool to check the which function is taking much time. But there are lot of tools available. Which is the best tool to use for Opencv, Raspberry pi, C++?
how about this
@pradeep_kb please update you question with profiling information. Based on your manual profiling one can see the most calculation-intensive part is morphology, it takes ~80% of time. The common advise for morphology is to avoid large kernels, emulation for large kernel is simplest resize and small kernel or "small kernel N-times passing".
@Vit, From the above result that I have posted I found morphology process is taking more time. I think background subtraction process will take less time then morphology process, What do you say? I am new to Opencv, this program was written by someone else. I have to optimize it now.
@Vit@StevenPuttemans@sturkmen, I have done profiling for my program using gprof tool here is the details. I am new to this, not able to analyze properly. Thanks for the help.
This profiling i have done in my PC not on raspberry pi. On raspberry pi compiler is not generating gmon.out I am working on that. I have followed theset steps. But not working for raspbian OS.
@Vit@StevenPuttemans@sturkmen, I have done profiling for the program on raspberry pi 2 with raspbian OS. here are the details. Need your help to analyse it in depth.
"You need permission. Ask the owner for access, or switch to an account with permission."