-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (47 loc) · 1.5 KB
/
main.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
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main() {
double scale = 2.0;
CascadeClassifier faceCascade;
// Train CascadeClassifier based on default data
faceCascade.load("..\\..\\opencv\\build\\etc\\haarcascades\\haarcascade_"
"frontalface_default.xml");
VideoCapture capture(0);
if (!capture.isOpened()) {
cout << "Error: No Camera Found" << endl;
return -1;
}
Mat frame, grayscale;
vector<Rect> faces;
Scalar drawColor = Scalar(0, 0, 255);
while (true) {
capture >> frame;
// Converts fram to grayscale
cvtColor(frame, grayscale, COLOR_BGR2GRAY);
resize(
grayscale, grayscale,
Size(grayscale.size().width / scale, grayscale.size().height / scale));
// Detect Faces
faceCascade.detectMultiScale(grayscale, faces, 1.1, 3, 0, Size(30, 30));
for (Rect area : faces) {
rectangle(frame,
// Top Left Point of Rectangle
Point(cvRound(area.x * scale), cvRound(area.y * scale)),
// Bottom Right Point of Rectangle
Point(cvRound((area.x + area.width - 1) * scale),
cvRound((area.y + area.height - 1) * scale)),
drawColor);
}
// Shows frame
imshow("Webcam", frame);
// If a key is pressed from the keyboard, then stop capture
if (waitKey(30) >= 0) {
break;
}
}
return 0;
}