-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFeatureExtractor.cpp
109 lines (97 loc) · 2.93 KB
/
FeatureExtractor.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
#include "FeatureExtractor.h"
FeatureExtractor::FeatureExtractor()
{
try
{
// Load face detection and pose estimation models.
detector = get_frontal_face_detector();
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
}
catch (serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
FeatureExtractor::~FeatureExtractor()
{
}
std::vector<dlib::rectangle> FeatureExtractor::detectFaces(cv_image<bgr_pixel> cimg)
{
return detector(cimg);
}
std::vector<dlib::rectangle> FeatureExtractor::detectFaces(array2d<rgb_pixel> *cimg)
{
return detector(*cimg);
}
std::vector<full_object_detection> FeatureExtractor::detectFeatures(cv_image<bgr_pixel> cimg, std::vector<dlib::rectangle> faces)
{
std::vector<full_object_detection> shapes;
if (faces.size() < 1)
{
cout << endl << "No face detected in detectFeatures()" << endl;
faces.push_back(dlib::rectangle(cimg.nc(), cimg.nr()));
}
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
return shapes;
}
std::vector<full_object_detection> FeatureExtractor::detectFeatures(array2d<rgb_pixel> *cimg, std::vector<dlib::rectangle> faces)
{
std::vector<full_object_detection> shapes;
if (faces.size() < 1)
{
cout << endl << "faces.size() < 1 in detectFeatures()" << endl;
faces.push_back(dlib::rectangle(cimg->nc(), cimg->nr()));
}
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(*cimg, faces[i]));
return shapes;
}
//converts landmarks from std::vector<dlib::full_object_detection> to std::vector<float>
std::vector<float> FeatureExtractor::getFlattened(std::vector<dlib::full_object_detection> shapes)
{
std::vector<float> flattened;
for (unsigned long i = 0; i < shapes.at(0).num_parts(); i++)
{
flattened.push_back(shapes.at(0).part(i).x());
flattened.push_back(shapes.at(0).part(i).y());
}
return flattened;
}
//returns a vector of differences between 2 vectors
std::vector<float> FeatureExtractor::getDifference(std::vector<float> shapes1, std::vector<float> shapes2)
{
std::vector<float> difference;
for (int i = 0; i < shapes1.size(); i++)
{
difference.push_back(shapes1[i]-shapes2[i]);
}
return difference;
}
//converts std::vector<float> to string, used when saving data to file
string FeatureExtractor::getFlattenedStr(std::vector<float> flattened)
{
stringstream flt;
for (int i = 0; i < flattened.size(); i ++)
{
flt << flattened.at(i);
flt << " ";
}
return flt.str();
}
//debug function, prints vector<float> to console
void FeatureExtractor::showFlattned(std::vector<float> flattened)
{
for (int i = 0; i < flattened.size(); i++)
{
cout << flattened[i] << " ";
}
cout << "\n";
}