forked from hrastnik/face_detect_n_track
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoFaceDetector.cpp
279 lines (226 loc) · 8.29 KB
/
VideoFaceDetector.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "VideoFaceDetector.h"
#include <iostream>
#include <opencv2\imgproc.hpp>
const double VideoFaceDetector::TICK_FREQUENCY = cv::getTickFrequency();
VideoFaceDetector::VideoFaceDetector(const std::string cascadeFilePath, cv::VideoCapture &videoCapture)
{
setFaceCascade(cascadeFilePath);
setVideoCapture(videoCapture);
}
void VideoFaceDetector::setVideoCapture(cv::VideoCapture &videoCapture)
{
m_videoCapture = &videoCapture;
}
cv::VideoCapture *VideoFaceDetector::videoCapture() const
{
return m_videoCapture;
}
void VideoFaceDetector::setFaceCascade(const std::string cascadeFilePath)
{
if (m_faceCascade == NULL) {
m_faceCascade = new cv::CascadeClassifier(cascadeFilePath);
}
else {
m_faceCascade->load(cascadeFilePath);
}
if (m_faceCascade->empty()) {
std::cerr << "Error creating cascade classifier. Make sure the file" << std::endl
<< cascadeFilePath << " exists." << std::endl;
}
}
cv::CascadeClassifier *VideoFaceDetector::faceCascade() const
{
return m_faceCascade;
}
void VideoFaceDetector::setResizedWidth(const int width)
{
m_resizedWidth = std::max(width, 1);
}
int VideoFaceDetector::resizedWidth() const
{
return m_resizedWidth;
}
cv::Rect VideoFaceDetector::face() const
{
cv::Rect faceRect = m_trackedFace;
faceRect.x = (int)(faceRect.x / m_scale);
faceRect.y = (int)(faceRect.y / m_scale);
faceRect.width = (int)(faceRect.width / m_scale);
faceRect.height = (int)(faceRect.height / m_scale);
return faceRect;
}
cv::Point VideoFaceDetector::facePosition() const
{
cv::Point facePos;
facePos.x = (int)(m_facePosition.x / m_scale);
facePos.y = (int)(m_facePosition.y / m_scale);
return facePos;
}
void VideoFaceDetector::setTemplateMatchingMaxDuration(const double s)
{
m_templateMatchingMaxDuration = s;
}
double VideoFaceDetector::templateMatchingMaxDuration() const
{
return m_templateMatchingMaxDuration;
}
VideoFaceDetector::~VideoFaceDetector()
{
if (m_faceCascade != NULL) {
delete m_faceCascade;
}
}
cv::Rect VideoFaceDetector::doubleRectSize(const cv::Rect &inputRect, const cv::Rect &frameSize) const
{
cv::Rect outputRect;
// Double rect size
outputRect.width = inputRect.width * 2;
outputRect.height = inputRect.height * 2;
// Center rect around original center
outputRect.x = inputRect.x - inputRect.width / 2;
outputRect.y = inputRect.y - inputRect.height / 2;
// Handle edge cases
if (outputRect.x < frameSize.x) {
outputRect.width += outputRect.x;
outputRect.x = frameSize.x;
}
if (outputRect.y < frameSize.y) {
outputRect.height += outputRect.y;
outputRect.y = frameSize.y;
}
if (outputRect.x + outputRect.width > frameSize.width) {
outputRect.width = frameSize.width - outputRect.x;
}
if (outputRect.y + outputRect.height > frameSize.height) {
outputRect.height = frameSize.height - outputRect.y;
}
return outputRect;
}
cv::Point VideoFaceDetector::centerOfRect(const cv::Rect &rect) const
{
return cv::Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
}
cv::Rect VideoFaceDetector::biggestFace(std::vector<cv::Rect> &faces) const
{
assert(!faces.empty());
cv::Rect *biggest = &faces[0];
for (auto &face : faces) {
if (face.area() < biggest->area())
biggest = &face;
}
return *biggest;
}
/*
* Face template is small patch in the middle of detected face.
*/
cv::Mat VideoFaceDetector::getFaceTemplate(const cv::Mat &frame, cv::Rect face)
{
face.x += face.width / 4;
face.y += face.height / 4;
face.width /= 2;
face.height /= 2;
cv::Mat faceTemplate = frame(face).clone();
return faceTemplate;
}
void VideoFaceDetector::detectFaceAllSizes(const cv::Mat &frame)
{
// Minimum face size is 1/5th of screen height
// Maximum face size is 2/3rds of screen height
m_faceCascade->detectMultiScale(frame, m_allFaces, 1.1, 3, 0,
cv::Size(frame.rows / 5, frame.rows / 5),
cv::Size(frame.rows * 2 / 3, frame.rows * 2 / 3));
if (m_allFaces.empty()) return;
m_foundFace = true;
// Locate biggest face
m_trackedFace = biggestFace(m_allFaces);
// Copy face template
m_faceTemplate = getFaceTemplate(frame, m_trackedFace);
// Calculate roi
m_faceRoi = doubleRectSize(m_trackedFace, cv::Rect(0, 0, frame.cols, frame.rows));
// Update face position
m_facePosition = centerOfRect(m_trackedFace);
}
void VideoFaceDetector::detectFaceAroundRoi(const cv::Mat &frame)
{
// Detect faces sized +/-20% off biggest face in previous search
m_faceCascade->detectMultiScale(frame(m_faceRoi), m_allFaces, 1.1, 3, 0,
cv::Size(m_trackedFace.width * 8 / 10, m_trackedFace.height * 8 / 10),
cv::Size(m_trackedFace.width * 12 / 10, m_trackedFace.width * 12 / 10));
if (m_allFaces.empty())
{
// Activate template matching if not already started and start timer
m_templateMatchingRunning = true;
if (m_templateMatchingStartTime == 0)
m_templateMatchingStartTime = cv::getTickCount();
return;
}
// Turn off template matching if running and reset timer
m_templateMatchingRunning = false;
m_templateMatchingCurrentTime = m_templateMatchingStartTime = 0;
// Get detected face
m_trackedFace = biggestFace(m_allFaces);
// Add roi offset to face
m_trackedFace.x += m_faceRoi.x;
m_trackedFace.y += m_faceRoi.y;
// Get face template
m_faceTemplate = getFaceTemplate(frame, m_trackedFace);
// Calculate roi
m_faceRoi = doubleRectSize(m_trackedFace, cv::Rect(0, 0, frame.cols, frame.rows));
// Update face position
m_facePosition = centerOfRect(m_trackedFace);
}
void VideoFaceDetector::detectFacesTemplateMatching(const cv::Mat &frame)
{
// Calculate duration of template matching
m_templateMatchingCurrentTime = cv::getTickCount();
double duration = (double)(m_templateMatchingCurrentTime - m_templateMatchingStartTime) / TICK_FREQUENCY;
// If template matching lasts for more than 2 seconds face is possibly lost
// so disable it and redetect using cascades
if (duration > m_templateMatchingMaxDuration) {
m_foundFace = false;
m_templateMatchingRunning = false;
m_templateMatchingStartTime = m_templateMatchingCurrentTime = 0;
}
// Template matching with last known face
//cv::matchTemplate(frame(m_faceRoi), m_faceTemplate, m_matchingResult, CV_TM_CCOEFF);
cv::matchTemplate(frame(m_faceRoi), m_faceTemplate, m_matchingResult, CV_TM_SQDIFF_NORMED);
cv::normalize(m_matchingResult, m_matchingResult, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double min, max;
cv::Point minLoc, maxLoc;
cv::minMaxLoc(m_matchingResult, &min, &max, &minLoc, &maxLoc);
// Add roi offset to face position
minLoc.x += m_faceRoi.x;
minLoc.y += m_faceRoi.y;
// Get detected face
//m_trackedFace = cv::Rect(maxLoc.x, maxLoc.y, m_trackedFace.width, m_trackedFace.height);
m_trackedFace = cv::Rect(minLoc.x, minLoc.y, m_faceTemplate.cols, m_faceTemplate.rows);
m_trackedFace = doubleRectSize(m_trackedFace, cv::Rect(0, 0, frame.cols, frame.rows));
// Get new face template
m_faceTemplate = getFaceTemplate(frame, m_trackedFace);
// Calculate face roi
m_faceRoi = doubleRectSize(m_trackedFace, cv::Rect(0, 0, frame.cols, frame.rows));
// Update face position
m_facePosition = centerOfRect(m_trackedFace);
}
cv::Point VideoFaceDetector::getFrameAndDetect(cv::Mat &frame)
{
*m_videoCapture >> frame;
// Downscale frame to m_resizedWidth width - keep aspect ratio
m_scale = (double) std::min(m_resizedWidth, frame.cols) / frame.cols;
cv::Size resizedFrameSize = cv::Size((int)(m_scale*frame.cols), (int)(m_scale*frame.rows));
cv::Mat resizedFrame;
cv::resize(frame, resizedFrame, resizedFrameSize);
if (!m_foundFace)
detectFaceAllSizes(resizedFrame); // Detect using cascades over whole image
else {
detectFaceAroundRoi(resizedFrame); // Detect using cascades only in ROI
if (m_templateMatchingRunning) {
detectFacesTemplateMatching(resizedFrame); // Detect using template matching
}
}
return m_facePosition;
}
cv::Point VideoFaceDetector::operator>>(cv::Mat &frame)
{
return this->getFrameAndDetect(frame);
}