-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.cpp
94 lines (65 loc) · 2.59 KB
/
utils.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
#include "utils.h"
Mat detect_face (Mat& frame) {
// grayscale
Mat frame_gray = frame;
if (frame.channels() > 1){
cvtColor (frame, frame_gray, CV_BGR2GRAY);
}
equalizeHist (frame_gray, frame_gray);
// detect
CascadeClassifier face_cascade;
if (!face_cascade.load (face_cascade_file)) {
THROW_ERROR (-1, "cannot load classifier: " + face_cascade_file);
}
std::vector<Rect> faces;
face_cascade.detectMultiScale (frame_gray, faces,
1.1, 2, 0|CV_HAAR_SCALE_IMAGE,
Size(30, 30) );
Mat ROI;
for( int i = 0; i < faces.size(); i++ )
{
Point center (faces[i].x + faces[i].width*0.5,
faces[i].y + faces[i].height*0.5 );
// extrace face area
ROI = frame_gray (Rect (faces[i].tl().x,
faces[i].tl().y,
faces[i].width,
faces[i].height));
resize (ROI, ROI, Size(21,18), 0, 0, CV_INTER_LINEAR);
// draw rectangle on original image
rectangle (frame,
faces[i].tl(), faces[i].br(),
Scalar(0, 255, 0), 3);
}
return ROI; //this casue return of last face onlym, change to vector later
}
float detect_gender_per_face (Mat& ROI){
// gabor feature
normalize (ROI, ROI, 1, 0, CV_MINMAX, CV_32F);
Mat feature_Gabor;
for (int i=0;i<THETA_BOUND;i++) {
for (int j=0;j<LAMBDA_BOUND;j++) {
Mat M_Magnitude = myGabor::getGaborImg (ROI,
SIGMA, //sigma
i, //theta
j, //lambda
GAMMA, //gamma
Size(kSizeX, kSizeY)); //kernel size
normalize (M_Magnitude, M_Magnitude, 0, 1, CV_MINMAX,CV_32F);
Mat feature_line= M_Magnitude.reshape (0,1);
feature_Gabor.push_back (feature_line);
}
}
// pca
pcaImpl pca = pcaImpl::pcaImpl (feature_Gabor, PCA_FEATURE_NO, PCA_RATE);
Mat dst = pca.pcaProject (feature_Gabor) ;
//cout << "dst:" << dst.size() <<endl;
Mat feature_line = dst.reshape (0,1);
//cout << "feature_line: " << line.size() <<endl;
// svm classification
CvSVM mySVM;
mySVM.load ("SVM_PCA.xml");
float response = mySVM.predict (feature_line );
feature_Gabor.release();
return response;
}