简体   繁体   English

Opencv - Haar级联 - 面部跟踪非常慢

[英]Opencv - Haar cascade - Face tracking is very slow

I have developed a project to tracking face through camera using OpenCV library. 我开发了一个使用OpenCV库通过相机跟踪人脸的项目。 I used haar cascade with haarcascade_frontalface_alt.xml to detect face. 我使用haar cascade和haarcascade_frontalface_alt.xml来检测面部。

My problem is if image capture from webcame doesn't contain any faces, process to detect faces is very slow so images from camera, which are showed continuosly to user, are delayed. 我的问题是,如果从网络摄像头捕获的图像不包含任何面部,则检测面部的过程非常慢,因此相机的图像会连续显示给用户,会延迟。

My source code: 我的源代码:

void camera() 
{
    String face_cascade_name = "haarcascade_frontalface_alt.xml";
    String eye_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;
    String window_name = "Capture - Face detection";
    VideoCapture cap(0);

    if (!face_cascade.load(face_cascade_name))
        printf("--(!)Error loading\n");

    if (!eyes_cascade.load(eye_cascade_name))
        printf("--(!)Error loading\n");

    if (!cap.isOpened()) 
    {
        cerr << "Capture Device ID " << 0 << "cannot be opened." << endl;
    } 
    else 
    {
        Mat frame;
        vector<Rect> faces;
        vector<Rect> eyes;
        Mat original;
        Mat frame_gray;
        Mat face;
        Mat processedFace;

        for (;;) 
        {
            cap.read(frame);
            original = frame.clone();    
            cvtColor(original, frame_gray, CV_BGR2GRAY);
            equalizeHist(frame_gray, frame_gray);
            face_cascade.detectMultiScale(frame_gray, faces, 2, 0,
                    0 | CASCADE_SCALE_IMAGE, Size(200, 200));

            if (faces.size() > 0)
                rectangle(original, faces[0], Scalar(0, 0, 255), 2, 8, 0);

            namedWindow(window_name, CV_WINDOW_AUTOSIZE);
            imshow(window_name, original);
        }

        if (waitKey(30) == 27)
            break;
    }
}

Haar classifier is relatively slow by nature. 哈尔分类器本质上相对较慢。 Furthermore, there is not much of optimization you can do to the algorithm itself because detectMultiScale is parallelized in OpenCV. 此外,您无法对算法本身进行太多优化,因为detectMultiScale在OpenCV中是并行化的。

The only note about your code: do you really get some faces ever detected with minSize which equals to Size(200, 200) ? 关于你的代码的唯一注意事项:你是否真的得到了一些用minSize检测到的minSize ,这等于Size(200, 200) Though surely, the bigger the minSize - the better the performance is. 虽然肯定, minSize越大 - 性能越好。

Try scaling the image before detecting anything: 在检测任何内容之前尝试缩放图像:

const int scale = 3;
cv::Mat resized_frame_gray( cvRound( frame_gray.rows / scale ), cvRound( frame_gray.cols / scale ), CV_8UC1 );
cv::resize( frame_gray, resized_frame_gray, resized_frame_gray.size() );
face_cascade.detectMultiScale(resized_frame_gray, faces, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(20, 20));

(don't forget to change minSize to more reasonable value and to convert detected face locations to real scale) (不要忘记将minSize更改为更合理的值并将检测到的面部位置转换为实际比例)

Image size reducing for 2, 3, 5 times is a great performance relief for any image processing algorithm, especially when it comes to some costly stuff like detection. 对于任何图像处理算法,图像大小减少2,3,5倍是一个很好的性能缓解,特别是当涉及到一些昂贵的东西,如检测。

As it was mentioned before, if resizing won't do the trick, try fetching some other bottlenecks using a profiler. 如前所述,如果调整大小不起作用,请尝试使用分析器获取其他一些瓶颈。

And you can also switch to LBP classifier which is comparably faster though less accurate. 而且你也可以切换到LBP分类器,虽然不太准确,但速度相对较快。

Hope it will help. 希望它会有所帮助。

I use Haar cascade classifiers regularly, and easily get 15 frames/second for face detection on 640x480 images, on an Intel PC/Mac (Windows/Ubuntu/OS X) with 4GB Ram and 2GHz CPU. 我定期使用Haar级联分类器,在640x480图像上,在具有4GB Ram和2GHz CPU的Intel PC / Mac(Windows / Ubuntu / OS X)上轻松获得15帧/秒的面部检测。 What is your configuration? 你的配置是什么?

Here are a few things that you can try. 以下是您可以尝试的一些事项。

  1. You don't have to create the window ( namedWindow(window_name, CV_WINDOW_AUTOSIZE); ) within each frame. 您不必在每个帧中创建窗口( namedWindow(window_name, CV_WINDOW_AUTOSIZE); )。 Just create it first and update the image. 只需先创建它并更新图像。

  2. You can try how fast it runs without histogram equalization. 您可以尝试在没有直方图均衡的情况下运行的速度。 Not always required with a webcam. 网络摄像头并不总是需要。

  3. As suggested by Micka above, you should check whether your program runs in Debug mode or release mode. 正如Micka上面所建议的,您应该检查您的程序是在调试模式还是在发布模式下运行。

  4. Use a profiler to see whether the bottleneck is. 使用分析器来查看瓶颈是否存在。

  5. In case you haven't done it yet, have you measured the frame rate you get if you comment out face detection and drawing rectangles? 如果你还没有完成它,你是否测量了如果你注释掉面部检测和绘制矩形你得到的帧速率?

May be it will useful for you: 可能对你有用:

There is a Simd Library , which has an implementation of HAAR and LBP cascade classifiers. 有一个SIMD库 ,它有一个实现 HAAR和LBP级联分类的。 It can use standard HAAR and LBP casscades from OpenCV. 它可以使用OpenCV的标准HAAR和LBP casscades。 This implementation has SIMD optimizations with using of SSE4.1, AVX2 and NEON(ARM), so it works in 2-3 times faster then original OpenCV implementation. 该实现具有使用SSE4.1,AVX2和NEON(ARM)的SIMD优化,因此其工作速度比原始OpenCV快2-3倍。

You can use LBP Cascade to detect faces. 您可以使用LBP级联来检测面部。 It is much more lightweight. 它更轻巧。 You can find lbpcascade_frontalface.xml in OpenCV source directory. 您可以在OpenCV源目录中找到lbpcascade_frontalface.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM