DFACE C++  5.0.0
utils.h
Go to the documentation of this file.
1 #ifndef DFACE_UTILS_H
2 #define DFACE_UTILS_H
3 
4 #include <stdio.h>
5 #include <string>
6 #include <iostream>
7 #include <sstream>
8 #include <fstream>
9 #include <vector>
10 #include <math.h>
11 
12 
13 union FloatType {
14  float FloatNum;
15  int IntNum;
16 };
17 
18 
19 typedef union {
20  int i;
21  char c;
22 }my_union;
23 
24 
25 static int checkSystem(void)
26 {
27  my_union u;
28  u.i = 1;
29  return (u.i == u.c);
30 }
31 
32 
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++) {
36  FloatType Number;
37  Number.IntNum = 0;
38  Number.IntNum = bytes[4 * i + 3];
39  Number.IntNum = (Number.IntNum << 8) | bytes[4 * i + 2];
40  Number.IntNum = (Number.IntNum << 8) | bytes[4 * i + 1];
41  Number.IntNum = (Number.IntNum << 8) | bytes[4 * i + 0];
42  feature.push_back(Number.FloatNum);
43  }
44 }
45 
46 
47 static unsigned char* convertFeature2Bytes(std::vector<float> &feature, int &length){
48  if (feature.size() == 0) {
49  length = 0;
50  return NULL;
51  }
52  length = 4 * feature.size();
53  unsigned char * DataBuf = new unsigned char[length];
54  for (int i = 0; i < feature.size(); i++) {
55  FloatType Number;
56  Number.FloatNum = feature[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);
61  }
62  return DataBuf;
63 }
64 
65 
66 static inline void convertFeature2Str(std::vector<float> &feature, std::string& str) {
67  if (feature.size() == 0) {
68  return;
69  }
70  std::stringstream stmstr;
71  for (int i = 0; i< feature.size(); i++) {
72  stmstr << feature[i] << ',';
73  }
74  std::string restr = stmstr.str();
75  str = restr.substr(0, restr.length() - 1);
76 }
77 
78 
79 static inline std::vector<std::string> explodeStr(const std::string &s, const char &c) {
80  std::string buff;
81  std::vector<std::string> v;
82  char tmp;
83  for (int i = 0; i<s.size(); i++)
84  {
85  tmp = s[i];
86  if (tmp != c) {
87  buff += tmp;
88  }
89  else {
90  if (tmp == c && buff != "") {
91  v.push_back(buff);
92  buff = "";
93  }
94  }//endif
95  }
96  if (buff != "")
97  {
98  v.push_back(buff);
99  }
100  return v;
101 }
102 
103 
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) {
107  return;
108  }
109  for (int i = 0; i<fstrs.size(); i++) {
110  float f = atof(fstrs[i].c_str());
111  feature.push_back(f);
112  }
113 }
114 
115 //vector norm
116 static inline float norm(const std::vector<float>& vec){
117  int n = vec.size();
118  float sum = 0.0;
119  for (int i = 0; i<n; ++i)
120  sum += vec[i] * vec[i];
121  return sqrt(sum);
122 }
123 
124 
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);
137 
138  int n = feature_arr1.size();
139  float tmp = 0.0;
140  for (int i = 0; i<n; ++i){
141  tmp += feature_arr1[i] * feature_arr2[i];
142  }
143  float cosine = tmp / (norm(feature_arr1)*norm(feature_arr2));
144  float norm_cosine = 0.5 + 0.5 * cosine;
145  return norm_cosine;
146 }
147 
148 
149 
150 static inline std::string getPathName(const std::string& s) {
151  char sep = '/';
152 #ifdef _WIN32
153  sep = '\\';
154 #endif
155  size_t i = s.rfind(sep, s.length());
156  if (i != std::string::npos) {
157  return(s.substr(0, i));
158  }
159  return("");
160 }
161 
162 
163 static inline std::vector<std::string> split_str(const std::string& str, const std::string& delim)
164 {
165  std::vector<std::string> tokens;
166  size_t prev = 0, pos = 0;
167  do
168  {
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();
174  }
175  while (pos < str.length() && prev < str.length());
176  return tokens;
177 }
178 
179 static inline void replace_str(std::string& str, const std::string& before, const std::string& after)
180 {
181  for (std::string::size_type pos(0); pos != std::string::npos; pos += after.length())
182  {
183  pos = str.find(before, pos);
184  if (pos != std::string::npos)
185  str.replace(pos, before.length(), after);
186  else
187  break;
188  }
189 }
190 
191 
192 #endif //DFACE_UTILS_H
Definition: utils.h:13
int i
Definition: utils.h:20
float FloatNum
Definition: utils.h:14
char c
Definition: utils.h:21
Definition: utils.h:19
int IntNum
Definition: utils.h:15