[英]Dlib face detection terrible performance on C++, good in python, why?
I am trying to write a simple face detection algorithm using OpenCV for camera capture and Dlib for face detection (using Histogram of Oriented Gradients algorithm). 我正在尝试编写一个简单的人脸检测算法,使用OpenCV进行摄像头捕获,使用Dlib进行人脸检测(使用直方图梯度算法)。
Using Python, I get a decent performance with around 20 fps. 使用Python,我获得了大约20 fps的不错表现。 However, the same-ish code in C++ has very poor performance, with each dlib's detection process taking around 4 seconds.
但是,C ++中的相同代码具有非常差的性能,每个dlib的检测过程大约需要4秒。
Does anyone know what's happening ? 有谁知道发生了什么?
I did some optimization, but nothing really improve performances : 我做了一些优化,但没有真正提高性能:
Thanks for the help. 谢谢您的帮助。
EDIT : Solution found ! 编辑:找到解决方案! I managed to reach similarly good performance under C++ by compiling using the commands at the end of this message.
通过使用此消息末尾的命令进行编译,我设法在C ++下达到了同样出色的性能。 Before that, I used the compiler from Jetbrain's CLion IDE and it was not working properly, even if the compiler send a positive "AVX intructions enabled" message.
在此之前,我使用了Jetbrain的CLion IDE中的编译器并且它无法正常工作,即使编译器发送了正“AVX intructions enabled”消息。 AVX Instructions was the answer to my problem.
AVX说明是我的问题的答案。
Here are the codes: 以下是代码:
In C++: 在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();
}
}
In Python : 在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()
For C++ compilation, I use cmake, this is my CMakeLists.txt 对于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)
I run the compilation with the following commands : 我使用以下命令运行编译:
cmake . -DUSE_AVX_INSTRUCTIONS=ON
cmake --build . --config Release
The issue came from the CMakeLists.txt. 问题来自CMakeLists.txt。 AVX optimizations need to be set in the CMakeLists.txt this way :
需要以这种方式在CMakeLists.txt中设置AVX优化:
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)
The accepted solution wasn't a solution for me. 接受的解决方案对我来说不是解决方案。
I was building dlib separately (using the option: -DUSE_AVX_INSTRUCTIONS=ON
) and then attempting to build my project with this in my CMakeLists.txt file: 我正在单独构建dlib(使用选项:
-DUSE_AVX_INSTRUCTIONS=ON
),然后尝试使用我的CMakeLists.txt文件构建我的项目:
find_package(dlib REQUIRED)
It sort of worked. 它有点奏效。 It was linking to dlib, but for some reason it ran super slow.
它链接到dlib,但由于某种原因它运行速度超慢。
To get the most out of dlib I had to: 为了充分利用dlib,我不得不:
add_subdirectory(../dlib dlib_build)
in my CMakeLists.txt file and build my project as I would have built dlib: 在我的CMakeLists.txt文件中构建我的项目,因为我将构建dlib:
cmake -DUSE_AVX_INSTRUCTIONS=ON ../
cmake --build . --config Release
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.