-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_detection.cpp
More file actions
73 lines (58 loc) · 1.91 KB
/
Copy pathface_detection.cpp
File metadata and controls
73 lines (58 loc) · 1.91 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
71
72
73
#include <iostream>
#include <opencv2/opencv.hpp>
void detectAndDraw(cv::Mat& img, cv::CascadeClassifier& classifier, double scale = 1.3) {
std::vector<cv::Scalar> colors = {
cv::Scalar(255, 0, 0), // Blue
cv::Scalar(255, 128, 0), // Orange
cv::Scalar(255, 255, 0), // Yellow
cv::Scalar(0, 255, 0) // Green
};
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::resize(gray, gray, cv::Size(), 1.0 / scale, 1.0 / scale);
cv::equalizeHist(gray, gray);
// DETECT OBJECTS IF ANY
std::vector<cv::Rect> objects;
classifier.detectMultiScale(
gray,
objects,
1.1,
5, //ngbh
0,
cv::Size(30, 30)
);
for (size_t i = 0; i < objects.size(); ++i) {
cv::Rect r = objects[i];
//filter out small detections
if (r.width * r.height < 1000) //adjust this threshold as needed
continue;
cv::rectangle(img, cv::Point(cvRound(r.x * scale), cvRound(r.y * scale)),
cv::Point(cvRound((r.x + r.width - 1) * scale), cvRound((r.y + r.height - 1) * scale)),
colors[i % colors.size()], 2);
}
}
int main() {
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
std::cerr << "Error: Cannot open webcam." << std::endl;
return -1;
}
cv::CascadeClassifier classifier;
if (!classifier.load("/Users/michael/Desktop/mars/opn/haarcascade_frontalface_default.xml")) {
std::cerr << "Error: Cannot load classifier." << std::endl;
return -1;
}
cv::Mat frame;
std::string window = "Face Recognition";
while (true) {
cap >> frame;
if (frame.empty())
break;
detectAndDraw(frame, classifier);
cv::imshow(window, frame);
int key = cv::waitKey(1);
if (key == 'q')
break;
}
return 0;
}