-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathimproved_face_detection.cpp
More file actions
70 lines (51 loc) · 1.63 KB
/
improved_face_detection.cpp
File metadata and controls
70 lines (51 loc) · 1.63 KB
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
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
CascadeClassifier face_cascade, eyes_cascade;
String window_name = "Face Detection";
/**
* Detects faces and draws an ellipse around them
*/
void detectFaces(Mat frame) {
std::vector<Rect> faces;
Mat frame_gray;
// Convert to gray scale
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
// Equalize histogram
equalizeHist(frame_gray, frame_gray);
// Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3,
0|CASCADE_SCALE_IMAGE, Size(30, 30));
// Iterate over all of the faces
for(size_t i = 0; i < faces.size(); i++) {
// Find center of faces
Point center(faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2);
Mat face = frame_gray(faces[i]);
std::vector<Rect> eyes;
// Try to detect eyes, inside each face
eyes_cascade.detectMultiScale(face, eyes, 1.1, 2,
0 |CASCADE_SCALE_IMAGE, Size(30, 30) );
if(eyes.size() > 0)
// Draw ellipse around face
ellipse(frame, center, Size(faces[i].width/2, faces[i].height/2),
0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
}
// Display frame
imshow( window_name, frame );
}
int main() {
VideoCapture cap(0); // Open default camera
Mat frame;
face_cascade.load("haarcascade_frontalface_alt.xml"); // load faces
eyes_cascade.load("haarcascade_eye_tree_eyeglasses.xml"); // load eyes
while(cap.read(frame)) {
detectFaces(frame); // Call function to detect faces
if( waitKey(30) >= 0) // pause
break;
}
return 0;
}