-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFaceRecognition.h
More file actions
170 lines (152 loc) · 5.85 KB
/
FaceRecognition.h
File metadata and controls
170 lines (152 loc) · 5.85 KB
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
//
// Created by eipi10 on 1/30/21.
//
#ifndef FACERECOGNITION_FACERECOGNITION_H
#define FACERECOGNITION_FACERECOGNITION_H
#include <string>
#include <vector>
#include <dirent.h>
#include "priorbox.hpp"
#include "utils.hpp"
#include "opencv2/opencv.hpp"
void read_directory(const std::string& name, std::vector<std::string> &v);
class Detector {
public:
Detector(const std::string& model_path, cv::Size input_shape,
float conf_thresh=0.6, float nms_thresh=0.3, int keep_top_k=750,
cv::dnn::Backend backend=cv::dnn::DNN_BACKEND_OPENCV,
cv::dnn::Target target=cv::dnn::DNN_TARGET_CPU);
std::vector<Face>& detect(cv::Mat& img);
size_t size() const;
std::vector<Face>& faces();
cv::Size& shape();
private:
cv::dnn::Net model;
cv::Size input_shape;
float conf_thresh, nms_thresh;
int keep_top_k;
PriorBox pb;
std::vector<cv::String> output_names = {"loc", "conf", "iou"};
std::vector<cv::Mat> output_blobs;
std::vector<Face> dets;
size_t detected_nums;
};
void face_alignment(const cv::Mat& img, cv::Mat& faceImagAligned,
const cv::Point& left_eye, const cv::Point& right_eye,
const cv::Point& left_eye_ref, const cv::Point& right_eye_ref);
class FeatureExtractor {
public:
FeatureExtractor(const std::string& model_path, cv::Size input_shape,
cv::dnn::Backend backend=cv::dnn::DNN_BACKEND_OPENCV,
cv::dnn::Target target=cv::dnn::DNN_TARGET_CPU);
cv::Mat extract(const cv::Mat& img);
private:
cv::dnn::Net model;
cv::Size input_shape;
};
bool features_from_img(cv::Mat& img, Detector& detector, FeatureExtractor& extractor,
std::vector<cv::Mat>& features, const cv::Size& display_shape, int rfactor);
class Similarity {
public:
virtual double similarity(const cv::Mat& feature, const cv::Mat& ref_feature)=0;
virtual bool feature_fusion(std::vector<cv::Mat> &features, cv::Mat &feature)=0;
};
class NormSimilarity: public Similarity {
public:
double similarity(const cv::Mat& feature, const cv::Mat& ref_feature) override;
bool feature_fusion(std::vector<cv::Mat> &features, cv::Mat &feature) override;
};
class CosSimilarity: public Similarity {
public:
double similarity(const cv::Mat& feature, const cv::Mat& ref_feature) override;
bool feature_fusion(std::vector<cv::Mat> &features, cv::Mat &feature) override;
};
template<typename T>
class FeatureDatabase {
public:
FeatureDatabase(std::string data_path, Detector& detector,
FeatureExtractor& extractor, const cv::Size& display_shape,
int rfactor, float threshold=0.8):
path(std::move(data_path)), threshold(threshold), similar_calculator(new T()) {
read_directory(path, names);
std::vector<std::string> photos;
cv::Mat k;
std::vector<cv::Mat> features;
for (auto i = 2; i < names.size(); ++i) {
cv::glob(path + names[i], photos);
std::vector<cv::Mat> temp;
for (const auto& j: photos) {
k = cv::imread(j);
if (features_from_img(k, detector, extractor, features,
display_shape, rfactor)) {
std::cout << "\t" << detector.size() << " faces loaded from: " << j << std::endl;
} else {
std::cout << "\tNo faces loaded from: " << j << std::endl;
continue;
}
cv::Mat t;
features[0].copyTo(t);
temp.emplace_back(std::move(t));
}
cv::Mat fused_feature;
similar_calculator->feature_fusion(temp, fused_feature);
feature_database.emplace_back(std::move(fused_feature));
}
data_len = feature_database.size();
}
~FeatureDatabase() { free(similar_calculator); }
bool select_max_mean_similarity(const cv::Mat& feature, float& similarity,
std::string& name) {
double total;
similarity = -1.;
if (feature.empty()) { return false; }
for (int i = 0; i < data_len; ++i) {
const std::vector<cv::Mat> &ref_features = feature_database[i];
if (ref_features.empty()) { continue; }
total = 0.;
for (const cv::Mat &ref_feature : ref_features) {
total += similar_calculator->similarity(feature, ref_feature);
}
total /= double(ref_features.size());
if (similarity < total) {
similarity = total;
name = names[i + 2];
}
}
return similarity >= threshold;
}
bool select_max_similarity(const cv::Mat& feature, float& similarity,
std::string& name) {
double total;
similarity = -1.;
if (feature.empty()) { return false; }
for (int i = 0; i < data_len; ++i) {
const cv::Mat &ref_feature = feature_database[i];
if (ref_feature.empty()) { continue; }
total = similar_calculator->similarity(feature, ref_feature);
if (similarity < total) {
similarity = total;
name = names[i + 2];
}
}
return similarity >= threshold;
}
size_t size() const { return data_len; }
friend void read_directory(const std::string& name, std::vector<std::string> &v);
private:
std::string path;
Similarity *similar_calculator;
std::vector<cv::Mat> feature_database;
std::vector<std::string> names;
size_t data_len;
float threshold;
};
class FaceRecognition {
public:
FaceRecognition(float recogn_thresh=0.9);
private:
cv::dnn::Net detector, feature_extractor;
float conf_thresh, nms_thresh, recogn_thresh;
int keep_top_k;
};
#endif //FACERECOGNITION_FACERECOGNITION_H