Hi Manish,
Here is the app code below, how to make this piece of code run on gpu ? will it be faster if it runs on gpu?
// CPP program to detects face in a video
// Include required header files from OpenCV directory
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
// Function for Face Detection
void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale);
//void detectAndDisplay( Mat& frame, CascadeClassifier& fullbodycascade, CascadeClassifier& bodycascade);
int main( int argc, const char** argv )
{
// VideoCapture class for playing video for which faces to be detected
VideoCapture capture;
Mat frame, image;
// PreDefined trained XML classifiers with facial features
// CascadeClassifier upperbodycascade, fullbodycascade;
CascadeClassifier facecascade, eyecascade;
double scale=1;
// Load classifiers from "opencv/data/haarcascades" directory
//nestedCascade.load("haarcascade_eye_tree_eyeglasses.xml") ;
// Change path before execution
//fullbodycascade.load( "haarcascade_fullbody.xml" ) ;
//upperbodycascade.load( "haarcascade_upperbody.xml" ) ;
facecascade.load( "haarcascade_frontalface_alt.xml" ) ;
eyecascade.load( "haarcascade_eye.xml" ) ;
// Start Video..1) 0 for WebCam 2) "Path to Video" for a Local Video
capture.open(0);
if( !capture.isOpened() )
{
cout << "Camera port opening failed!" << endl;
exit(1);
}
else
{
cout << "Camera port opened successfully!" << endl;
}
// set resolution & frame rate (FPS)
capture.set(CAP_PROP_FRAME_WIDTH, 640);
capture.set(CAP_PROP_FRAME_HEIGHT,480);
//capture.set(CAP_PROP_FPS, 30);
while(1)
{
cout << "####reading frame" << endl;
capture >> frame;
if( frame.empty() )
break;
Mat frame1 = frame.clone();
//detectAndDisplay( frame1, fullbodycascade, upperbodycascade);
detectAndDraw(frame1, facecascade, eyecascade, scale);
waitKey(1);
}
capture.release();
return 0;
}
/*void detectAndDisplay( Mat& frame, CascadeClassifier& fullbodycascade, CascadeClassifier& upperbodycascade)
{
std::vector<Rect> upperbodies, fullbodies;
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect upperbody
upperbodycascade.detectMultiScale(frame_gray, upperbodies, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
cout << "upperbody size: " << upperbodies.size() << endl;
for (size_t i = 0; i < upperbodies.size(); i++)
{
rectangle(frame, upperbodies[i], Scalar(255, 0, 255), 1, 8, 0);
//-- Detect fullbody
fullbodycascade.detectMultiScale(frame_gray, fullbodies, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
cout << "full body size: " << fullbodies.size() << endl;
for (size_t i = 0; i < fullbodies.size(); i++)
{
rectangle(frame, fullbodies[i], Scalar(255, 0, 255), 1, 8, 0);
}
}
//-- Show what you got
imshow("pedestrian detection", frame);
}*/
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
CascadeClassifier& nestedCascade, double scale)
{
vector<Rect> faces;
Mat gray; //smallImg;
cvtColor( img, gray, COLOR_BGR2GRAY ); // Convert to Gray Scale
//double fx = 1 / scale;
// Resize the Grayscale Image
//resize( gray, smallImg, Size(), fx, fx, INTER_LINEAR );
equalizeHist( gray, gray );
// Detect faces of different sizes using cascade classifier
cascade.detectMultiScale(gray, faces, 1.1,
3, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
cout << "face size: " << faces.size() << endl;
// Draw circles around the faces
for ( size_t i = 0; i < faces.size(); i++ )
{
Rect r = faces[i];
Mat smallImgROI;
vector<Rect> nestedObjects;
Point center;
Scalar color = Scalar(255, 0, 0); // Color for Drawing tool
int radius;
double aspect_ratio = (double)r.width/r.height;
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
{
center.x = cvRound((r.x + r.width*0.5)*scale);
center.y = cvRound((r.y + r.height*0.5)*scale);
radius = cvRound((r.width + r.height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
else
rectangle( img, cv::Point(cvRound(r.x*scale), cvRound(r.y*scale)),
cv::Point(cvRound((r.x + r.width-1)*scale),
cvRound((r.y + r.height-1)*scale)), color, 3, 8, 0);
if( nestedCascade.empty() )
continue;
smallImgROI = gray( r );
// Detection of eyes int the input image
nestedCascade.detectMultiScale( smallImgROI, nestedObjects, 1.1, 3,
0|CASCADE_SCALE_IMAGE, Size(30, 30) );
// Draw circles around eyes
for ( size_t j = 0; j < nestedObjects.size(); j++ )
{
Rect nr = nestedObjects[j];
center.x = cvRound((r.x + nr.x + nr.width*0.5)*scale);
center.y = cvRound((r.y + nr.y + nr.height*0.5)*scale);
radius = cvRound((nr.width + nr.height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
}
// Show Processed Image with detected faces
imshow( "Face Detection", img );
}
/*void detectAndDraw( Mat& image, CascadeClassifier& face_cascade)
{
// Detect faces
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
cout << "body size: " << faces.size() << endl;
// Draw circles on the detected faces
for( unsigned int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( image, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow( "Detected Face", image );
}*/