forked from galeone/face-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarianceclassifier.cpp
182 lines (152 loc) · 5.17 KB
/
varianceclassifier.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
/*
Face Miner: data mining applied to face detection
Copyright (C) 2016 Paolo Galeone <nessuno@nerdz.eu>
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Exhibit B is not attached; this software is compatible with the
licenses expressed under Section 1.12 of the MPL v2.
*/
#include "varianceclassifier.h"
VarianceClassifier::VarianceClassifier(const cv::Size windowSize) {
auto cols = windowSize.width, rows = windowSize.height;
auto aThirdRows = std::floor(rows / 3), // 6
aThirdCols = std::floor(cols / 3); // 6
// Mouth region
_E = cv::Rect(0, rows - aThirdRows, cols, aThirdRows);
// Nose region
_D = cv::Rect(0, rows - 2 * aThirdRows + 1, cols, aThirdRows - 1);
// Left eye region
auto ac_cols = aThirdCols + 1;
auto b_cols = cols - 2 * ac_cols;
// Top section
if (rows % 2 != 0) {
++aThirdRows;
}
int topHeight = std::floor(aThirdRows / 3);
_A = cv::Rect(0, 2 * topHeight, ac_cols, topHeight);
// Between eye region
_B = cv::Rect(ac_cols, 2 * topHeight, b_cols, topHeight);
// Right eye region
_C = cv::Rect(cols - ac_cols, 2 * topHeight, ac_cols, topHeight);
}
void VarianceClassifier::_getMForABC(const cv::Mat1b& window,
IntegralImage& ii,
float* ma,
float* mb,
float* mc) {
cv::Mat1b roi_a = window(_A), roi_b = window(_B), roi_c = window(_C);
float mu_a = ii.calcMean(_A), mu_b = ii.calcMean(_B), mu_c = ii.calcMean(_C);
*ma = 0, *mb = 0, *mc = 0;
uint32_t validPx = 0;
// *ma is the average intensity of those pixels that are
// darker than the average intensity in region A
cv::Point coord;
for (auto x = 0; x < roi_a.cols; ++x) {
for (auto y = 0; y < roi_a.rows; ++y) {
coord.x = x;
coord.y = y;
auto pxBrightness = roi_a.at<uchar>(coord);
if (pxBrightness < mu_a) {
*ma += pxBrightness;
++validPx;
}
}
}
*ma /= (validPx > 0 ? validPx : 1);
validPx = 0;
// *mc is the average intensity of those pixels that are
// darker than the average intensity in region C
for (auto x = 0; x < roi_c.cols; ++x) {
for (auto y = 0; y < roi_c.rows; ++y) {
coord.x = x;
coord.y = y;
auto pxBrightness = roi_c.at<uchar>(coord);
if (pxBrightness < mu_c) {
*mc += pxBrightness;
++validPx;
}
}
}
*mc /= (validPx > 0 ? validPx : 1);
validPx = 0;
// *mb is the average intensity of those pixels that are
// birghter than the average intensity in region B
for (auto x = 0; x < roi_b.cols; ++x) {
for (auto y = 0; y < roi_b.rows; ++y) {
coord.x = x;
coord.y = y;
auto pxBrightness = roi_b.at<uchar>(coord);
if (pxBrightness > mu_b) {
*mb += pxBrightness;
++validPx;
}
}
}
*mb /= (validPx > 0 ? validPx : 1);
}
// Adjust the thresholds untile the face is marked as a valid face
// we suppose that face has the same dimension of _positiveMFI / _negativeMFI
void VarianceClassifier::train(std::vector<cv::Mat1b>& positive,
std::vector<cv::Mat1b>& negative) {
std::vector<double> positiveT, negativeT;
positiveT.reserve(positive.size());
negativeT.reserve(negative.size());
IntegralImage ii;
for (const auto& raw : positive) {
cv::Mat1b face = Preprocessor::equalize(raw);
ii.setImage(face);
positiveT.push_back(ii.calcVariance(_D));
positiveT.push_back(ii.calcVariance(_E));
}
for (const auto& raw : negative) {
cv::Mat1b face = Preprocessor::equalize(raw);
ii.setImage(face);
negativeT.push_back(ii.calcVariance(_D));
negativeT.push_back(ii.calcVariance(_E));
}
_t = _equalErrorRate(positiveT, negativeT).second / 21.5;
_k = 1.65;
std::cout << "T: << " << _t << "\nK: " << _k << std::endl;
}
void VarianceClassifier::train(QString positiveTrainingSet,
QString negativeTrainingSet) {
std::vector<cv::Mat1b> positive, negative;
QDirIterator* it = new QDirIterator(positiveTrainingSet);
while (it->hasNext()) {
auto fileName = it->next();
if (!Preprocessor::validMime(fileName)) {
continue;
}
cv::Mat raw = cv::imread(fileName.toStdString());
cv::Mat1b gray = Preprocessor::gray(raw);
positive.push_back(gray);
}
delete it;
it = new QDirIterator(negativeTrainingSet);
while (it->hasNext()) {
auto fileName = it->next();
if (!Preprocessor::validMime(fileName)) {
continue;
}
cv::Mat raw = cv::imread(fileName.toStdString());
cv::Mat1b gray = Preprocessor::gray(raw);
negative.push_back(gray);
}
delete it;
return train(positive, negative);
}
bool VarianceClassifier::classify(const cv::Mat1b& window) {
cv::Mat1b face = Preprocessor::equalize(window);
IntegralImage ii;
ii.setImage(window);
if (ii.calcVariance(_D) < _t || ii.calcVariance(_E) < _t) {
return false;
}
float ma, mb, mc;
_getMForABC(face, ii, &ma, &mb, &mc);
if (mb < _k * ma || mb < _k * mc) {
return false;
}
return true;
}