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

我正在尝试编写一个简单的人脸检测算法,使用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 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




莫回无
浏览 191回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python