4
4
#include < stdlib.h>
5
5
6
6
#include < opencv2/core.hpp>
7
+ #include < opencv2/core/version.hpp>
7
8
#include < opencv2/imgproc.hpp>
8
9
#include < opencv2/highgui.hpp>
9
10
#include < opencv2/objdetect.hpp>
10
11
#include < opencv2/videoio.hpp>
11
- #include < opencv2/dnn.hpp>
12
12
13
- #include < dlib/opencv.h>
14
- #include < dlib/image_processing.h>
15
- #include < dlib/dnn.h>
16
- #include < dlib/data_io.h>
17
- #include < dlib/image_processing/frontal_face_detector.h>
13
+ #if (CV_MAJOR_VERSION >= 3)
14
+ # include < opencv2/dnn.hpp>
15
+
16
+ using namespace cv ::dnn;
17
+ #endif
18
+
19
+ #if (CV_MAJOR_VERSION < 3)
20
+ # include < dlib/opencv.h>
21
+ # include < dlib/image_processing.h>
22
+ # include < dlib/dnn.h>
23
+ # include < dlib/data_io.h>
24
+ # include < dlib/image_processing/frontal_face_detector.h>
25
+
26
+ using namespace dlib ;
27
+ #endif
18
28
19
29
#include < boost/algorithm/string.hpp>
20
30
21
31
using namespace cv ;
22
- using namespace cv ::dnn;
23
32
using namespace std ;
24
- using namespace dlib ;
25
33
26
34
/* * Global variables */
27
35
String faceCascadePath;
@@ -54,6 +62,7 @@ void detectFaceOpenCVHaar(CascadeClassifier faceCascade, Mat &frameOpenCVHaar, i
54
62
}
55
63
}
56
64
65
+ #if (CV_MAJOR_VERSION >= 3)
57
66
const size_t inWidth = 300 ;
58
67
const size_t inHeight = 300 ;
59
68
const double inScaleFactor = 1.0 ;
@@ -96,9 +105,10 @@ void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN, string framework="caffe")
96
105
cv::rectangle (frameOpenCVDNN, cv::Point (x1, y1), cv::Point (x2, y2), cv::Scalar (0 , 255 , 0 ),(int )(frameHeight/150.0 ), 4 );
97
106
}
98
107
}
99
-
100
108
}
109
+ #endif
101
110
111
+ #if (CV_MAJOR_VERSION < 3)
102
112
void detectFaceDlibHog (frontal_face_detector hogFaceDetector, Mat &frameDlibHog, int inHeight=300 , int inWidth=0 )
103
113
{
104
114
@@ -171,7 +181,7 @@ void detectFaceDlibMMOD(net_type mmodFaceDetector, Mat &frameDlibMmod, int inHei
171
181
cv::rectangle (frameDlibMmod, Point (x1, y1), Point (x2, y2), Scalar (0 ,255 ,0 ), (int )(frameHeight/150.0 ), 4 );
172
182
}
173
183
}
174
-
184
+ # endif
175
185
176
186
int main ( int argc, const char ** argv )
177
187
{
@@ -183,11 +193,12 @@ int main( int argc, const char** argv )
183
193
return -1 ;
184
194
}
185
195
196
+ #if (CV_MAJOR_VERSION < 3)
186
197
frontal_face_detector hogFaceDetector = get_frontal_face_detector ();
187
-
188
198
String mmodModelPath = " models/mmod_human_face_detector.dat" ;
189
199
net_type mmodFaceDetector;
190
200
deserialize (mmodModelPath) >> mmodFaceDetector;
201
+ #endif
191
202
192
203
string videoFileName;
193
204
string device;
@@ -208,7 +219,7 @@ int main( int argc, const char** argv )
208
219
else if (argc == 2 )
209
220
{
210
221
videoFileName = argv[1 ];
211
- device = " gpu " ;
222
+ device = " cpu " ;
212
223
framework = " caffe" ;
213
224
}
214
225
else
@@ -237,6 +248,7 @@ int main( int argc, const char** argv )
237
248
else
238
249
net = cv::dnn::readNetFromTensorflow (tensorflowWeightFile, tensorflowConfigFile);
239
250
251
+ #if (CV_MAJOR_VERSION >= 4)
240
252
if (device == " CPU" )
241
253
{
242
254
net.setPreferableBackend (DNN_TARGET_CPU);
@@ -248,6 +260,12 @@ int main( int argc, const char** argv )
248
260
net.setPreferableTarget (DNN_TARGET_CUDA);
249
261
cout << " Device - " << device << endl;
250
262
}
263
+ #elif (CV_MAJOR_VERSION == 3)
264
+ // OpenCV 3.4.x does not support GPU backend
265
+ net.setPreferableBackend (DNN_BACKEND_DEFAULT);
266
+ device = " cpu" ;
267
+ cout << " Device - " << device << endl;
268
+ #endif
251
269
252
270
cv::VideoCapture source;
253
271
if (videoFileName != " " )
@@ -281,12 +299,17 @@ int main( int argc, const char** argv )
281
299
putText (frameOpenCVHaar, format (" OpenCV HAAR; FPS = %.2f" ,fpsOpencvHaar), Point (10 , 50 ), FONT_HERSHEY_SIMPLEX, 1.3 , Scalar (0 , 0 , 255 ), 4 );
282
300
283
301
Mat frameOpenCVDNN = frame.clone ();
302
+ #if (CV_MAJOR_VERSION >= 3)
284
303
t = cv::getTickCount ();
285
304
detectFaceOpenCVDNN (net, frameOpenCVDNN, framework);
286
305
tt_opencvDNN += ((double )cv::getTickCount () - t)/cv::getTickFrequency ();
287
306
double fpsOpencvDNN = frame_count/tt_opencvDNN;
288
307
putText (frameOpenCVDNN, format (" OpenCV DNN %s FPS = %.2f" , device.c_str (), fpsOpencvDNN), Point (10 , 50 ), FONT_HERSHEY_SIMPLEX, 1.3 , Scalar (0 , 0 , 255 ), 4 );
308
+ #else
309
+ putText (frameOpenCVDNN, " OpenCV DNN NOT SUPPORTED" , Point (10 , 50 ), FONT_HERSHEY_SIMPLEX, 1.3 , Scalar (0 , 0 , 255 ), 4 );
310
+ #endif
289
311
312
+ #if (CV_MAJOR_VERSION < 3)
290
313
t = cv::getTickCount ();
291
314
Mat frameDlibHog = frame.clone ();
292
315
detectFaceDlibHog ( hogFaceDetector, frameDlibHog );
@@ -300,11 +323,17 @@ int main( int argc, const char** argv )
300
323
tt_dlibMmod += ((double )cv::getTickCount () - t)/cv::getTickFrequency ();
301
324
double fpsDlibMmod = frame_count/tt_dlibMmod;
302
325
putText (frameDlibMmod, format (" DLIB MMOD; FPS = %.2f" ,fpsDlibMmod), Point (10 , 50 ), FONT_HERSHEY_SIMPLEX, 1.3 , Scalar (0 , 0 , 255 ), 4 );
326
+ #endif
303
327
304
328
Mat top, bottom, combined;
305
329
hconcat (frameOpenCVHaar, frameOpenCVDNN, top);
330
+ #if (CV_MAJOR_VERSION < 3)
306
331
hconcat (frameDlibHog, frameDlibMmod, bottom);
307
332
vconcat (top, bottom, combined);
333
+ #else
334
+ combined = top;
335
+ #endif
336
+
308
337
cv::resize (combined, combined, Size (), .5 , .5 );
309
338
imshow (" Face Detection Comparison" , combined);
310
339
0 commit comments