-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceDetector.hpp
106 lines (86 loc) · 2.92 KB
/
FaceDetector.hpp
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
99
100
101
102
103
104
105
106
#ifndef FACE_DETECTOR
#define FACE_DETECTOR
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/objdetect.hpp>
#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
using namespace std;
using namespace cv;
class FaceDetector {
public:
virtual vector<Rect> facesFromMat(Mat frame, bool showTime = false) {}
};
class HaarCascadeDetector : public FaceDetector {
private:
CascadeClassifier clf;
Size size;
float scaleFactor;
int minNeighbors;
int flags;
public:
HaarCascadeDetector(
float scaleFactor = 1.1,
int minNeighbors = 3,
int flags = 0 | CV_HAAR_SCALE_IMAGE,
int length = 30
) {
this->scaleFactor = scaleFactor;
this->minNeighbors = minNeighbors;
this->flags = flags;
this->size = Size(length, length);
this->clf.load("haarcascade_frontalface_alt2.xml");
}
vector<Rect> facesFromImage(char * imgPath) {
Mat img = imread(imgPath);
return this->facesFromMat(img);
}
vector<Rect> facesFromMat(Mat frame, bool showTime = false) {
vector<Rect> faces;
clock_t start, end;
if (showTime)
start = clock();
this->clf.detectMultiScale(
frame,
faces,
this->scaleFactor,
this->minNeighbors,
this->flags,
this->size
);
if (showTime) {
end = clock();
double elapsed = (double)(end - start) * 1000.0 / CLOCKS_PER_SEC;
cout << "Time (ms): " << elapsed << endl;
}
return faces;
}
};
class DlibDetector : public FaceDetector {
private:
dlib::frontal_face_detector clf;
public:
DlibDetector() {
this->clf = dlib::get_frontal_face_detector();
}
vector<Rect> facesFromImage(char * imgPath) {
Mat img = imread(imgPath);
return this->facesFromMat(img);
}
vector<Rect> facesFromMat(Mat frame, bool showTime = false) {
vector<Rect> faces;
dlib::array2d<unsigned char> img;
dlib::assign_image(img, dlib::cv_image<dlib::bgr_pixel>(frame));
dlib::pyramid_up(img);
vector<dlib::rectangle> dets = this->clf(img);
for (int i = 0; i < dets.size(); ++i) {
cout << endl << dets[i].left() << " " << dets[i].top() << " " << dets[i].right() << " " << dets[i].bottom() << endl;
Point topLeft = Point(dets[i].left(), dets[i].top());
Point bottomRight = Point(dets[i].right(), dets[i].bottom());
Rect r(topLeft, bottomRight);
faces.push_back(r);
}
return faces;
}
};
#endif