25 static int checkSystem(
void)
33 static void convertBytes2Feature(
unsigned char* bytes,
int length, std::vector<float> &feature) {
34 int feathreLen = length / 4;
35 for (
int i = 0; i<feathreLen; i++) {
38 Number.
IntNum = bytes[4 * i + 3];
47 static unsigned char* convertFeature2Bytes(std::vector<float> &feature,
int &length){
48 if (feature.size() == 0) {
52 length = 4 * feature.size();
53 unsigned char * DataBuf =
new unsigned char[length];
54 for (
int i = 0; i < feature.size(); i++) {
57 DataBuf[4 * i + 0] = (
unsigned char)Number.
IntNum;
58 DataBuf[4 * i + 1] = (
unsigned char)(Number.
IntNum >> 8);
59 DataBuf[4 * i + 2] = (
unsigned char)(Number.
IntNum >> 16);
60 DataBuf[4 * i + 3] = (
unsigned char)(Number.
IntNum >> 24);
66 static inline void convertFeature2Str(std::vector<float> &feature, std::string& str) {
67 if (feature.size() == 0) {
70 std::stringstream stmstr;
71 for (
int i = 0; i< feature.size(); i++) {
72 stmstr << feature[i] <<
',';
74 std::string restr = stmstr.str();
75 str = restr.substr(0, restr.length() - 1);
79 static inline std::vector<std::string> explodeStr(
const std::string &s,
const char &c) {
81 std::vector<std::string> v;
83 for (
int i = 0; i<s.size(); i++)
90 if (tmp == c && buff !=
"") {
104 static inline void convertStr2Feature(std::string& str, std::vector<float> &feature){
105 std::vector<std::string> fstrs = explodeStr(str,
',');
106 if (fstrs.size() == 0) {
109 for (
int i = 0; i<fstrs.size(); i++) {
110 float f = atof(fstrs[i].c_str());
111 feature.push_back(f);
116 static inline float norm(
const std::vector<float>& vec){
119 for (
int i = 0; i<n; ++i)
120 sum += vec[i] * vec[i];
132 static inline float similarityByFeature(std::vector<unsigned char>& feature1, std::vector<unsigned char>& feature2){
133 std::vector<float> feature_arr1;
134 std::vector<float> feature_arr2;
135 convertBytes2Feature(feature1.data(), feature1.size(), feature_arr1);
136 convertBytes2Feature(feature2.data(), feature2.size(), feature_arr2);
138 int n = feature_arr1.size();
140 for (
int i = 0; i<n; ++i){
141 tmp += feature_arr1[i] * feature_arr2[i];
143 float cosine = tmp / (norm(feature_arr1)*norm(feature_arr2));
144 float norm_cosine = 0.5 + 0.5 * cosine;
150 static inline std::string getPathName(
const std::string& s) {
155 size_t i = s.rfind(sep, s.length());
156 if (i != std::string::npos) {
157 return(s.substr(0, i));
163 static inline std::vector<std::string> split_str(
const std::string& str,
const std::string& delim)
165 std::vector<std::string> tokens;
166 size_t prev = 0, pos = 0;
169 pos = str.find(delim, prev);
170 if (pos == std::string::npos) pos = str.length();
171 std::string token = str.substr(prev, pos-prev);
172 if (!token.empty()) tokens.push_back(token);
173 prev = pos + delim.length();
175 while (pos < str.length() && prev < str.length());
179 static inline void replace_str(std::string& str,
const std::string& before,
const std::string& after)
181 for (std::string::size_type pos(0); pos != std::string::npos; pos += after.length())
183 pos = str.find(before, pos);
184 if (pos != std::string::npos)
185 str.replace(pos, before.length(), after);
192 #endif //DFACE_UTILS_H
int i
Definition: utils.h:20
float FloatNum
Definition: utils.h:14
char c
Definition: utils.h:21
int IntNum
Definition: utils.h:15