As i also have uploaded the “detection of face and eye” tutorial but now at this time, i am only discussing the “Detection of Face”. The code given below is well commented. So just copy it your project, and it will work 100%.
/*
This program will detect the face of each person from the picture. For this you also have to upload the “haarcascade xml file”
because in this xml file all the data is available about different faces, so you will got a rectangel drawn at accurate face
DATED: 22/01/2013 (N.W.F.P UET MARDAN CAMPUS) 7TH SEMESTER
*/
#include<cv.h>
#include<highgui.h>
using namespace cv;
using namespace std;
int main()
{
CascadeClassifier cascade;
// this will upload the “haarcascade xml file”…
if (!cascade.load(“C:\\opencv/data/haarcascades/haarcascade_frontalface_alt.xml”))
printf(“XML file loading is missing !\nSo, you will not be able to detect the faces in the pic”);
//uploading the pic
Mat src = imread(“C:\\images/batch8.jpg”,1);
if (src.empty())
printf(“Image is not upload successfully!”);
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
equalizeHist(gray, gray);
vector<Rect> faces;
cascade.detectMultiScale(gray, faces, 1.2, 3);
for (int i = 0; i < faces.size(); i++)//size will show that how many faces have been detecte, if there is an image with two person size =2
{
//printf(“X & Y position of Rect are: %d,%d\n”,faces[i].x,faces[i].y);
Rect r = faces[i];
rectangle(src, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), CV_RGB(0,0,255));
printf(“Area of Rect %d is %d\n”,i,(r.width*r.height));
}
imshow(“src”, src);
waitKey(0);
return 0;
}
The Result of this code should be like this:



