0

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

3
  • 3
    You should try turning on optimisations when you compile. -O2 or -O3. Commented Sep 26, 2024 at 20:10
  • IOW you have not specified an optimization level, and the default is -O0 (i.e. none). Commented Sep 26, 2024 at 20:35
  • There is no value in measuring the performance of non-optimized C++ code. Commented Sep 26, 2024 at 20:45

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.