-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_detection.cpp
154 lines (124 loc) · 3.97 KB
/
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp> // OpenCV window I/O
#include <stdio.h>
#include <iostream>
const std::string WindowName("Face Detection");
using namespace std;
using namespace cv;
class CascadeDetectorAdapter : public DetectionBasedTracker::IDetector
{
public:
CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
IDetector(),
Detector(detector)
{
CV_Assert(detector);
}
void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)
{
Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
}
virtual ~CascadeDetectorAdapter()
{}
private:
CascadeDetectorAdapter();
cv::Ptr<cv::CascadeClassifier> Detector;
};
int main(int , char** )
{
namedWindow(WindowName);
VideoCapture capture;
capture.open(0);
if (!capture.set(CAP_PROP_FRAME_WIDTH, 1280))
{
printf("Error: Cannot set output width\n");
}
if (!capture.set(CAP_PROP_FRAME_HEIGHT, 720))
{
printf("Error: Cannot set output width\n");
}
if (!capture.set(CAP_PROP_CONVERT_RGB, false))
{
printf("Error: Cannot set output width\n");
}
if (!capture.isOpened())
{
printf("Error: Cannot open video stream from camera\n");
return 1;
}
std::string cascadeFrontalfilename1 = "/usr/share/opencv/lbpcascades/lbpcascade_frontalface.xml";
cv::Ptr<cv::CascadeClassifier> cascade1 = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename1);
cv::Ptr<DetectionBasedTracker::IDetector> MainDetector1 = makePtr<CascadeDetectorAdapter>(cascade1);
if (cascade1->empty() )
{
printf("Error: Cannot load %s\n", cascadeFrontalfilename1.c_str());
return 2;
}
cascade1 = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename1);
cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector1 = makePtr<CascadeDetectorAdapter>(cascade1);
if (cascade1->empty() )
{
printf("Error: Cannot load %s\n", cascadeFrontalfilename1.c_str());
return 2;
}
DetectionBasedTracker::Parameters params1;
DetectionBasedTracker Detector1(MainDetector1, TrackingDetector1, params1);
if (!Detector1.run())
{
printf("Error: Detector initialization failed\n");
return 2;
}
std::string cascadeFrontalfilename2 = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml";
cv::Ptr<cv::CascadeClassifier> cascade2 = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename2);
cv::Ptr<DetectionBasedTracker::IDetector> MainDetector2 = makePtr<CascadeDetectorAdapter>(cascade2);
if (cascade2->empty() )
{
printf("Error: Cannot load %s\n", cascadeFrontalfilename2.c_str());
return 2;
}
cascade2 = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename2);
cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector2 = makePtr<CascadeDetectorAdapter>(cascade2);
if (cascade2->empty() )
{
printf("Error: Cannot load %s\n", cascadeFrontalfilename2.c_str());
return 2;
}
DetectionBasedTracker::Parameters params2;
DetectionBasedTracker Detector2(MainDetector2, TrackingDetector2, params2);
if (!Detector2.run())
{
printf("Error: Detector initialization failed\n");
return 2;
}
Mat ReferenceFrame;
Mat GrayFrame;
vector<Rect> Faces1;
vector<Rect> Faces2;
while (1)
{
capture >> ReferenceFrame;
cvtColor(ReferenceFrame, GrayFrame, COLOR_BGR2GRAY);
Detector1.process(GrayFrame);
Detector1.getObjects(Faces1);
Detector2.process(GrayFrame);
Detector2.getObjects(Faces2);
for (size_t i = 0; i < Faces1.size(); i++)
{
rectangle(ReferenceFrame, Faces1[i], Scalar(0,255,0));
}
for (size_t i = 0; i < Faces2.size(); i++)
{
rectangle(ReferenceFrame, Faces2[i], Scalar(0,0,255));
}
imshow(WindowName, ReferenceFrame);
char c = static_cast<char>(waitKey(10));
// Press q to exit from window
if (c == 27 || c == 'q' || c == 'Q')
break;
}
Detector1.stop();
Detector2.stop();
return 0;
}