Disappointing Dlib face detection performance in C++ while in python it is very good, How to fix it?
The face detection performance is quite disappointing in C++ code compared to python where for detection in 1 frame in C++ it reaches around 1-2 seconds while in python it is only less than 500ms. I have used DUSE_AVX_INSTRUCTIONS both in creating libraries and when compiling my code (adding argument '-mavx2') as stated in the following link this link. However that solution helping it better than before that tanks around 3-4 second. In c++ i use same code but with slightly modified like this:
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing.h>
#include <chrono>
using namespace dlib;
using namespace std;
int main()
{
cv::VideoCapture cap(0);
std::vector<cv::Rect> facesCV;
std::vector<rectangle> faces;
frontal_face_detector detector = get_frontal_face_detector();
cv::namedWindow("test");
cv::Mat frame, small;
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
while (true)
{
// Grab a frame
if (!cap.read(frame))
{
break;
}
cv::resize(frame, small, {640, 480});
cv_image<rgb_pixel> cimg(small);
auto start = std::chrono::high_resolution_clock::now();
// Detect faces
faces = detector(cimg);
for (auto &f : faces)
{
facesCV.emplace_back(cv::Point((int)f.left(), (int)f.top()), cv::Point((int)f.right(), (int)f.bottom()));
}
for (auto &r : facesCV)
{
cv::rectangle(small, r, {0, 255, 0}, 2);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
cout << "Time taken for processing: " << elapsed.count() << " ms" << endl;
cv::imshow("test", small);
cv::waitKey(1);
faces.clear();
facesCV.clear();
}
}
and here is how i compiled it:
sudo g++ -g test.cpp -o dlib_test `pkg-config --cflags --libs opencv4` -ldlib -lX11 -lblas -llapack -mavx2
Thare is any solution for this problem?
Because this is my choice to migrate to c++ to make it faster and more lightweight for mini computer like Rasberry PI 4B.
this is my specification about my development computer:
OS: Ubntu 22.04
Processsor: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
RAM: 8 GB
GPU: NVIDIA930MX (but i not included while instaling openCV and DLIB)
I think this is should be better or minimal same to Python
-O2or-O3.-O0(i.e. none).