-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceDetection.cpp
98 lines (75 loc) · 2.92 KB
/
FaceDetection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/** g++ FaceID.cpp -o output `pkg-config --cflags --libs opencv` **/
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Print error message **/
void PANIC(char *msg);
#define PANIC(msg){perror(msg); exit(-1);}
/** Function for face detection **/
void detectAndDraw(Mat frame);
/** Gloabal variables **/
String face_cascade_name = "Source/haarcascade_frontalface_default.xml";
String eyes_cascade_name = "Source/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade; // Define the face classifier
CascadeClassifier eyes_cascade; // Define the eyes classifier
String window_name = "Face detection";
int main(int argc, char *argv[]){
// VideoCapture class for playing video for which faces to be detected
VideoCapture capture = VideoCapture(0);
Mat frame, image;
if (!face_cascade.load(face_cascade_name))
PANIC("Error loading face cascade");
if (!eyes_cascade.load(eyes_cascade_name))
PANIC("Error loading eyes cascade");
// After the camera is opened
if(capture.isOpened()){
cout << "Face Detection Started...." << endl;
while(1){
capture >> frame; // Get photos from camera
if(frame.empty())
PANIC("Error capture frame");
detectAndDraw(frame);
// Press q to exit from window
char ch = (char)waitKey(10);
if(ch == 27 || ch == 'q' || ch == 'Q')
break;
}
}
else
PANIC("Error open camera");
return 0;
}
void detectAndDraw(Mat frame){
std::vector<Rect> faces, eyes;
Mat frame_gray, frame_resize;
int radius;
// Convert to Gray Scale
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
// Resize the Grayscale Image
resize(frame_gray, frame_resize, Size(), 1, 1, INTER_LINEAR);
// Histogram equalization
equalizeHist(frame_resize, frame_resize);
// Detect faces of different sizes using cascade classifier
face_cascade.detectMultiScale(frame_resize, faces, 1.1, 5, CV_HAAR_SCALE_IMAGE, Size(30, 30));
// Draw circles around the faces
for (size_t i = 0; i < faces.size(); i++)
{
Point center;
rectangle(frame, faces[i], Scalar(255, 0, 0), 3, 8, 0);
Mat faceROI = frame_resize(faces[i]);
// Detection of eyes int the input image
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 1, CV_HAAR_SCALE_IMAGE, Size(3, 3));
// Draw circles around eyes
for (size_t j = 0; j < eyes.size(); j++)
{
center.x = cvRound((faces[i].x + eyes[j].x + eyes[j].width*0.5));
center.y = cvRound((faces[i].y + eyes[j].y + eyes[j].height*0.5));
radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
circle(frame, center, radius, Scalar(0, 255, 0), 3, 8, 0);
}
}
// Show Processed Image with detected faces
imshow( "Face Detection", frame);
}