1 | initial version |
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;
for(;;)
{
capture >> frame;
frame_number += 1;
if( frame.empty() )
break;
if( frame_number % 5 == 0 )
{
Mat gray;
cvtColor( frame, gray, COLOR_BGR2GRAY );
resize( gray, gray, Size(), 0.25, 0.25 );
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 * 4;
faces[i].y = faces[i].y * 4;
faces[i].width = faces[i].width * 4;
faces[i].height = faces[i].height * 4;
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;
}
2 | No.2 Revision |
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(), 0.25, 0.25 (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 * 4;
scale;
faces[i].y = faces[i].y * 4;
scale;
faces[i].width = faces[i].width * 4;
scale;
faces[i].height = faces[i].height * 4;
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;
}