Dlib 人脸检测在 C++ 上表现糟糕,在 python 中表现良好,为什么?

Dlib face detection terrible performance on C++, good in python, why?

我正在尝试编写一个简单的人脸检测算法,使用 OpenCV 进行相机捕获,使用 Dlib 进行人脸检测(使用定向梯度直方图算法)。

使用 Python,我获得了大约 20 fps 的不错性能。 然而,C++ 中的相同代码性能非常差,每个 dlib 的检测过程大约需要 4 秒。

有谁知道发生了什么事吗?

我做了一些优化,但没有真正提高性能:

感谢您的帮助。

编辑:找到解决方案! 通过使用此消息末尾的命令进行编译,我设法在 C++ 下达到了类似的良好性能。在此之前,我使用了 Jetbrain 的 CLion IDE 的编译器,但它无法正常工作,即使编译器发送了肯定的 "AVX intructions enabled" 消息。 AVX Instructions 是我问题的答案。

代码如下:

在 C++ 中:

#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>

using namespace dlib;
using namespace std;

int main(){

cv::VideoCapture cap(0);
vector<cv::Rect> facesCV;
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);

    // 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);
    }
    cv::imshow("test", small);
    cv::waitKey(1);
    faces.clear();
    facesCV.clear();
}
}

在 Python 中:

import argparse
import cv2
import dlib

#initialize face detector
detector = dlib.get_frontal_face_detector()

#initialize video source
cam = cv2.VideoCapture(0)
window = cv2.namedWindow("camera")

while True:
    ret, image = cam.read()
    if ret is True:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        gray =cv2.resize(gray, (640, 480))

        for r in detector(gray, 0):
            cv2.rectangle(image, (r.left(), r.top()), (r.right(), r.bottom()), (0, 255, 0), 2)

        cv2.imshow(window, image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    else:
        break

cam.release()
cv2.destroyAllWindows()

C++编译,我用的是cmake,这是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(FaceDetection)

set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS " -Ofast")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )

add_subdirectory(/path/to/dlib/dlib-19.14/dlib dlib_build)
find_package( OpenCV REQUIRED)

add_executable(FaceDetection main.cpp)
target_link_libraries( FaceDetection ${OpenCV_LIBS} dlib::dlib)

我运行用以下命令编译:

cmake . -DUSE_AVX_INSTRUCTIONS=ON
cmake --build . --config Release

问题来自 CMakeLists.txt。 AVX 优化需要在 CMakeLists.txt 中这样设置:

set(USE_AVX_INSTRUCTIONS ON CACHE BOOL "Use AVX instructions")
add_subdirectory("path/to/dlib" dlib_build)

add_executable(myProject main.cpp)
target_link_libraries( myProject dlib::dlib)

公认的解决方案不适合我。

我正在单独构建 dlib(使用选项:-DUSE_AVX_INSTRUCTIONS=ON),然后尝试在我的 CMakeLists.txt 文件中使用它构建我的项目:

find_package(dlib REQUIRED)

有点奏效了。它正在链接到 dlib,但由于某种原因,它 运行 超级慢。

为了充分利用 dlib,我必须:

add_subdirectory(../dlib dlib_build)

在我的 CMakeLists.txt 文件中构建 my 项目,就像我构建 dlib:

cmake -DUSE_AVX_INSTRUCTIONS=ON  ../
cmake --build . --config Release