Skip to content

Commit

Permalink
Made dlib dependency optional as it conflicts with modern OpenCV.
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorkalov committed Sep 9, 2020
1 parent 0e91e09 commit e6f6875
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
27 changes: 22 additions & 5 deletions FaceDetectionComparison/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@ message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")

include_directories( ${OpenCV_INCLUDE_DIRS})
include(./dlib/dlib/cmake)

find_package(dlib QUIET)
if(dlib_FOUND)
message(STATUS "Found system linstance of DLib")
else()
message(STATUS "Use own instance of DLib")
set(dlib_FOUND 1)
include(./dlib/dlib/cmake)
endif()

MACRO(add_example name)
ADD_EXECUTABLE(${name} ${name}.cpp)
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS} dlib::dlib)
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS})
IF(dlib_FOUND)
TARGET_LINK_LIBRARIES(${name} dlib::dlib)
ENDIF()
ENDMACRO()

add_example(face_detection_opencv_haar)
add_example(face_detection_opencv_dnn)
add_example(face_detection_dlib_hog)
add_example(face_detection_dlib_mmod)
if(${OpenCV_VERSION} VERSION_GREATER 3.4)
add_example(face_detection_opencv_dnn)
endif()

if(dlib_FOUND AND ${OpenCV_VERSION} VERSION_LESS 3.0)
add_example(face_detection_dlib_hog)
add_example(face_detection_dlib_mmod)
endif()

add_example(run-all)
10 changes: 8 additions & 2 deletions FaceDetectionComparison/face_detection_opencv_dnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ int main( int argc, const char** argv )
else if (argc == 2)
{
videoFileName = argv[1];
device = "gpu";
device = "cpu";
framework = "caffe";
}
else
{
videoFileName = "";
device = "gpu";
device = "cpu";
framework = "caffe";
}

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

#if (CV_MAJOR_VERSION >= 4)
if (device == "CPU")
{
net.setPreferableBackend(DNN_TARGET_CPU);
Expand All @@ -118,6 +119,11 @@ int main( int argc, const char** argv )
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
}
#else
// force CPU backend for OpenCV 3.x as CUDA backend is not supported there
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
device = "cpu";
#endif

cv::VideoCapture source;
if (videoFileName != "")
Expand Down
53 changes: 41 additions & 12 deletions FaceDetectionComparison/run-all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@
#include <stdlib.h>

#include <opencv2/core.hpp>
#include <opencv2/core/version.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/dnn.hpp>

#include <dlib/opencv.h>
#include <dlib/image_processing.h>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
#include <dlib/image_processing/frontal_face_detector.h>
#if(CV_MAJOR_VERSION >= 3)
# include <opencv2/dnn.hpp>

using namespace cv::dnn;
#endif

#if(CV_MAJOR_VERSION < 3)
# include <dlib/opencv.h>
# include <dlib/image_processing.h>
# include <dlib/dnn.h>
# include <dlib/data_io.h>
# include <dlib/image_processing/frontal_face_detector.h>

using namespace dlib;
#endif

#include <boost/algorithm/string.hpp>

using namespace cv;
using namespace cv::dnn;
using namespace std;
using namespace dlib;

/** Global variables */
String faceCascadePath;
Expand Down Expand Up @@ -54,6 +62,7 @@ void detectFaceOpenCVHaar(CascadeClassifier faceCascade, Mat &frameOpenCVHaar, i
}
}

#if(CV_MAJOR_VERSION >= 3)
const size_t inWidth = 300;
const size_t inHeight = 300;
const double inScaleFactor = 1.0;
Expand Down Expand Up @@ -96,9 +105,10 @@ void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN, string framework="caffe")
cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),(int)(frameHeight/150.0), 4);
}
}

}
#endif

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

Expand Down Expand Up @@ -171,7 +181,7 @@ void detectFaceDlibMMOD(net_type mmodFaceDetector, Mat &frameDlibMmod, int inHei
cv::rectangle(frameDlibMmod, Point(x1, y1), Point(x2, y2), Scalar(0,255,0), (int)(frameHeight/150.0), 4);
}
}

#endif

int main( int argc, const char** argv )
{
Expand All @@ -183,11 +193,12 @@ int main( int argc, const char** argv )
return -1;
}

#if(CV_MAJOR_VERSION < 3)
frontal_face_detector hogFaceDetector = get_frontal_face_detector();

String mmodModelPath = "models/mmod_human_face_detector.dat";
net_type mmodFaceDetector;
deserialize(mmodModelPath) >> mmodFaceDetector;
#endif

string videoFileName;
string device;
Expand All @@ -208,7 +219,7 @@ int main( int argc, const char** argv )
else if (argc == 2)
{
videoFileName = argv[1];
device = "gpu";
device = "cpu";
framework = "caffe";
}
else
Expand Down Expand Up @@ -237,6 +248,7 @@ int main( int argc, const char** argv )
else
net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);

#if (CV_MAJOR_VERSION >= 4)
if (device == "CPU")
{
net.setPreferableBackend(DNN_TARGET_CPU);
Expand All @@ -248,6 +260,12 @@ int main( int argc, const char** argv )
net.setPreferableTarget(DNN_TARGET_CUDA);
cout << "Device - "<< device << endl;
}
#elif(CV_MAJOR_VERSION == 3)
// OpenCV 3.4.x does not support GPU backend
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
device = "cpu";
cout << "Device - "<< device << endl;
#endif

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

Mat frameOpenCVDNN = frame.clone();
#if(CV_MAJOR_VERSION >= 3)
t = cv::getTickCount();
detectFaceOpenCVDNN (net, frameOpenCVDNN, framework);
tt_opencvDNN += ((double)cv::getTickCount() - t)/cv::getTickFrequency();
double fpsOpencvDNN = frame_count/tt_opencvDNN;
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);
#else
putText(frameOpenCVDNN, "OpenCV DNN NOT SUPPORTED", Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);
#endif

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

Mat top, bottom, combined;
hconcat(frameOpenCVHaar, frameOpenCVDNN, top);
#if(CV_MAJOR_VERSION < 3)
hconcat(frameDlibHog, frameDlibMmod, bottom);
vconcat(top, bottom, combined);
#else
combined = top;
#endif

cv::resize(combined, combined, Size(), .5, .5);
imshow("Face Detection Comparison", combined);

Expand Down

1 comment on commit e6f6875

@3sa100
Copy link

@3sa100 3sa100 commented on e6f6875 Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could anyone help me please. I can not solve the problem of the (dlib).

Please sign in to comment.