Skip to content

Commit 77ea5af

Browse files
Merge pull request spmallick#487 from asmorkalov/as/fd_dlib
Made dlib dependency optional as it conflicts with modern OpenCV
2 parents 23a440f + e6f6875 commit 77ea5af

File tree

3 files changed

+71
-19
lines changed

3 files changed

+71
-19
lines changed

FaceDetectionComparison/CMakeLists.txt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,32 @@ message(STATUS " libraries: ${OpenCV_LIBS}")
1313
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
1414

1515
include_directories( ${OpenCV_INCLUDE_DIRS})
16-
include(./dlib/dlib/cmake)
16+
17+
find_package(dlib QUIET)
18+
if(dlib_FOUND)
19+
message(STATUS "Found system linstance of DLib")
20+
else()
21+
message(STATUS "Use own instance of DLib")
22+
set(dlib_FOUND 1)
23+
include(./dlib/dlib/cmake)
24+
endif()
1725

1826
MACRO(add_example name)
1927
ADD_EXECUTABLE(${name} ${name}.cpp)
20-
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS} dlib::dlib)
28+
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS})
29+
IF(dlib_FOUND)
30+
TARGET_LINK_LIBRARIES(${name} dlib::dlib)
31+
ENDIF()
2132
ENDMACRO()
2233

2334
add_example(face_detection_opencv_haar)
24-
add_example(face_detection_opencv_dnn)
25-
add_example(face_detection_dlib_hog)
26-
add_example(face_detection_dlib_mmod)
35+
if(${OpenCV_VERSION} VERSION_GREATER 3.4)
36+
add_example(face_detection_opencv_dnn)
37+
endif()
38+
39+
if(dlib_FOUND AND ${OpenCV_VERSION} VERSION_LESS 3.0)
40+
add_example(face_detection_dlib_hog)
41+
add_example(face_detection_dlib_mmod)
42+
endif()
43+
2744
add_example(run-all)

FaceDetectionComparison/face_detection_opencv_dnn.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ int main( int argc, const char** argv )
8080
else if (argc == 2)
8181
{
8282
videoFileName = argv[1];
83-
device = "gpu";
83+
device = "cpu";
8484
framework = "caffe";
8585
}
8686
else
8787
{
8888
videoFileName = "";
89-
device = "gpu";
89+
device = "cpu";
9090
framework = "caffe";
9191
}
9292

@@ -109,6 +109,7 @@ int main( int argc, const char** argv )
109109
else
110110
net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
111111

112+
#if (CV_MAJOR_VERSION >= 4)
112113
if (device == "CPU")
113114
{
114115
net.setPreferableBackend(DNN_TARGET_CPU);
@@ -118,6 +119,11 @@ int main( int argc, const char** argv )
118119
net.setPreferableBackend(DNN_BACKEND_CUDA);
119120
net.setPreferableTarget(DNN_TARGET_CUDA);
120121
}
122+
#else
123+
// force CPU backend for OpenCV 3.x as CUDA backend is not supported there
124+
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
125+
device = "cpu";
126+
#endif
121127

122128
cv::VideoCapture source;
123129
if (videoFileName != "")

FaceDetectionComparison/run-all.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@
44
#include <stdlib.h>
55

66
#include <opencv2/core.hpp>
7+
#include <opencv2/core/version.hpp>
78
#include <opencv2/imgproc.hpp>
89
#include <opencv2/highgui.hpp>
910
#include <opencv2/objdetect.hpp>
1011
#include <opencv2/videoio.hpp>
11-
#include <opencv2/dnn.hpp>
1212

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
1828

1929
#include <boost/algorithm/string.hpp>
2030

2131
using namespace cv;
22-
using namespace cv::dnn;
2332
using namespace std;
24-
using namespace dlib;
2533

2634
/** Global variables */
2735
String faceCascadePath;
@@ -54,6 +62,7 @@ void detectFaceOpenCVHaar(CascadeClassifier faceCascade, Mat &frameOpenCVHaar, i
5462
}
5563
}
5664

65+
#if(CV_MAJOR_VERSION >= 3)
5766
const size_t inWidth = 300;
5867
const size_t inHeight = 300;
5968
const double inScaleFactor = 1.0;
@@ -96,9 +105,10 @@ void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN, string framework="caffe")
96105
cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),(int)(frameHeight/150.0), 4);
97106
}
98107
}
99-
100108
}
109+
#endif
101110

111+
#if(CV_MAJOR_VERSION < 3)
102112
void detectFaceDlibHog(frontal_face_detector hogFaceDetector, Mat &frameDlibHog, int inHeight=300, int inWidth=0)
103113
{
104114

@@ -171,7 +181,7 @@ void detectFaceDlibMMOD(net_type mmodFaceDetector, Mat &frameDlibMmod, int inHei
171181
cv::rectangle(frameDlibMmod, Point(x1, y1), Point(x2, y2), Scalar(0,255,0), (int)(frameHeight/150.0), 4);
172182
}
173183
}
174-
184+
#endif
175185

176186
int main( int argc, const char** argv )
177187
{
@@ -183,11 +193,12 @@ int main( int argc, const char** argv )
183193
return -1;
184194
}
185195

196+
#if(CV_MAJOR_VERSION < 3)
186197
frontal_face_detector hogFaceDetector = get_frontal_face_detector();
187-
188198
String mmodModelPath = "models/mmod_human_face_detector.dat";
189199
net_type mmodFaceDetector;
190200
deserialize(mmodModelPath) >> mmodFaceDetector;
201+
#endif
191202

192203
string videoFileName;
193204
string device;
@@ -208,7 +219,7 @@ int main( int argc, const char** argv )
208219
else if (argc == 2)
209220
{
210221
videoFileName = argv[1];
211-
device = "gpu";
222+
device = "cpu";
212223
framework = "caffe";
213224
}
214225
else
@@ -237,6 +248,7 @@ int main( int argc, const char** argv )
237248
else
238249
net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
239250

251+
#if (CV_MAJOR_VERSION >= 4)
240252
if (device == "CPU")
241253
{
242254
net.setPreferableBackend(DNN_TARGET_CPU);
@@ -248,6 +260,12 @@ int main( int argc, const char** argv )
248260
net.setPreferableTarget(DNN_TARGET_CUDA);
249261
cout << "Device - "<< device << endl;
250262
}
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
251269

252270
cv::VideoCapture source;
253271
if (videoFileName != "")
@@ -281,12 +299,17 @@ int main( int argc, const char** argv )
281299
putText(frameOpenCVHaar, format("OpenCV HAAR; FPS = %.2f",fpsOpencvHaar), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);
282300

283301
Mat frameOpenCVDNN = frame.clone();
302+
#if(CV_MAJOR_VERSION >= 3)
284303
t = cv::getTickCount();
285304
detectFaceOpenCVDNN (net, frameOpenCVDNN, framework);
286305
tt_opencvDNN += ((double)cv::getTickCount() - t)/cv::getTickFrequency();
287306
double fpsOpencvDNN = frame_count/tt_opencvDNN;
288307
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
289311

312+
#if(CV_MAJOR_VERSION < 3)
290313
t = cv::getTickCount();
291314
Mat frameDlibHog = frame.clone();
292315
detectFaceDlibHog ( hogFaceDetector, frameDlibHog );
@@ -300,11 +323,17 @@ int main( int argc, const char** argv )
300323
tt_dlibMmod += ((double)cv::getTickCount() - t)/cv::getTickFrequency();
301324
double fpsDlibMmod = frame_count/tt_dlibMmod;
302325
putText(frameDlibMmod, format("DLIB MMOD; FPS = %.2f",fpsDlibMmod), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);
326+
#endif
303327

304328
Mat top, bottom, combined;
305329
hconcat(frameOpenCVHaar, frameOpenCVDNN, top);
330+
#if(CV_MAJOR_VERSION < 3)
306331
hconcat(frameDlibHog, frameDlibMmod, bottom);
307332
vconcat(top, bottom, combined);
333+
#else
334+
combined = top;
335+
#endif
336+
308337
cv::resize(combined, combined, Size(), .5, .5);
309338
imshow("Face Detection Comparison", combined);
310339

0 commit comments

Comments
 (0)