我正在尝试编写一个简单的人脸检测算法,使用OpenCV进行相机捕获,并使用Dlib进行人脸检测(使用“定向梯度直方图”算法)。使用Python,我获得了大约20 fps的不错表现。但是,C ++中的相同代码的性能非常差,每个dlib的检测过程大约需要4秒钟。有人知道发生了什么吗?我做了一些优化,但是并没有真正提高性能:图片缩小为640x480我在启用AVX指令的情况下编译了dlib我也试图用-0fast标志进行编译...谢谢您的帮助。编辑:找到解决方案!通过使用此消息末尾的命令进行编译,我设法在C ++下达到了类似的良好性能。在此之前,我使用了Jetbrain的CLion IDE中的编译器,即使编译器发送肯定的“ AVX指令已启用”消息,它也无法正常工作。AVX指令可以解决我的问题。以下是代码:在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 argparseimport cv2import dlib#initialize face detectordetector = dlib.get_frontal_face_detector()#initialize video sourcecam = 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
添加回答
举报
0/150
提交
取消