-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbasic_face_detection.cpp
63 lines (45 loc) · 1.34 KB
/
basic_face_detection.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
#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;
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);
// 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;
// Load preconstructed classifier
face_cascade.load("haarcascade_frontalface_alt.xml");
while(cap.read(frame)) {
detectFaces(frame); // Call function to detect faces
if( waitKey(30) >= 0) // pause
break;
}
return 0;
}