-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeta_recognizer.cpp
97 lines (85 loc) · 3.38 KB
/
seeta_recognizer.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
#include "seeta_recognizer.hpp"
#include <utility>
Recognizer::Recognizer(const string& knownFacesDir, vector<string> models) {
loadModels(std::move(models));
loadKnownFeatures(this->fd, this->fl, this->fr, knownFacesDir, &this->knownFeatures);
}
void Recognizer::convertImage(const Mat& cvImage, SeetaImageData *seetaImage) {
seetaImage->height = cvImage.rows;
seetaImage->width = cvImage.cols;
seetaImage->channels = cvImage.channels();
seetaImage->data = cvImage.data;
}
void Recognizer::loadModels(vector<string> models) {
ModelSetting fd_settings;
fd_settings.set_device(SEETA_DEVICE_AUTO);
fd_settings.append(models.at(0));
this->fd = new FaceDetector(fd_settings);
ModelSetting fl_settings;
fl_settings.set_device(SEETA_DEVICE_AUTO);
fl_settings.append(models.at(1));
this->fl = new FaceLandmarker(fl_settings);
ModelSetting fr_settings;
fr_settings.set_device(SEETA_DEVICE_AUTO);
fr_settings.append(models.at(2));
this->fr = new FaceRecognizer(fr_settings);
}
void Recognizer::loadKnownFeatures(const FaceDetector *fd,
const FaceLandmarker *fl,
const FaceRecognizer *fr,
const string& knownFacesDir,
map<string, shared_ptr<float>> *features) {
DIR* dp = opendir(knownFacesDir.c_str());
if (!dp) {
cout << "open faces dir failed!" << endl;
return;
}
for (dirent* entry = readdir(dp); entry != nullptr; entry = readdir(dp)) {
string fname = entry->d_name;
if (strcmp(".", fname.c_str()) == 0 || strcmp("..", fname.c_str()) == 0) {
continue;
}
string fpath = knownFacesDir;
fpath.append("/");
fpath.append(fname);
shared_ptr<float> feature(new float[fr->GetExtractFeatureSize()]);
Mat image = imread(fpath);
extract_feature(fd, fl, fr, image, feature.get());
string name = fname.replace(fname.find(".jpg"), 4, "");
features->insert(pair<string, shared_ptr<float>>(name, feature));
}
}
void Recognizer::extract_feature(const FaceDetector *FD,
const FaceLandmarker *FL,
const FaceRecognizer *FR,
const Mat& image,
float *feature){
SeetaImageData img{};
convertImage(image, &img);
auto faces = FD->detect(img);
if (faces.size <= 0) {
cout << "no face detected" << endl;
return;
}
SeetaPointF points[5];
FL->mark(img, faces.data[0].pos, points);
FR->Extract(img, points, feature);
}
bool Recognizer::cmp(const pair<string, float>& p1,
const pair<string, float>& p2) {
return p1.second > p2.second;
}
vector<pair<string, float>> Recognizer::recognize(Mat img) {
shared_ptr<float> inpFeature(new float[fr->GetExtractFeatureSize()]);
extract_feature(this->fd, this->fl, this->fr, img, inpFeature.get());
vector<pair<string, float>> similarities;
map<string, shared_ptr<float>>::iterator knownIt;
for (knownIt = knownFeatures.begin(); knownIt != knownFeatures.end(); knownIt++) {
string name = knownIt->first;
shared_ptr<float> feature = knownIt->second;
float sim = this->fr->CalculateSimilarity(inpFeature.get(), feature.get());
similarities.emplace_back(name, sim);
}
sort(similarities.begin(), similarities.end(), cmp);
return similarities;
}