Ask Your Question
0

how to solve problem of low fps in OpenCV

asked 2019-08-06 19:47:04 -0600

I took this sample code, but i have low fps during code execution

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace std;
using namespace cv;

/** Função principal */
void detectAndDisplay(Mat frame);

/** Global rariaveis */
String face_cascade_name = "C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml";
String nose_cascade_name = "C:\\opencv\\sources\\data\\haarcascades\\haarcascade_mcs_nose.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
CascadeClassifier nose_cascade;
string window_name = "Capturando - Rosto detectado";
RNG rng(12345);

/** @funcão main */
int main(int argc, const char** argv)
{
    VideoCapture vcap(0);
    if (!vcap.isOpened()) {
        cout << "Erro ao abrir o video" << endl;
        return -1;
    }
    Mat frame;

    //-- 1. Carrega as biblioteca Cascade, responsavel por mapear o rosto.
    if (!face_cascade.load(face_cascade_name)) { printf("--(!)Erro ao carregar\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Erro ao carregar\n"); return -1; };
    if (!nose_cascade.load(nose_cascade_name)) { printf("--(!)Erro ao carregar\n"); return -1; };

    //-- 2. Ler o video

    while (true)
    {
        vcap >> frame;
        //-- 3.Aplicação para captura os frames
        if (!frame.empty())
        {
            detectAndDisplay(frame);
        }
        else
        {
            printf(" --(!) No captured frame -- Break!"); break;
        }

        int c = waitKey(1);
        if ((char)c == 'c') { break; }
    }
    return 0;
}

/** @Função para detectar e mostrar */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detecta rosto
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
        rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(255, 0, 0), 3, 8);
        Mat faceROI = frame_gray(faces[i]);
        vector<Rect> eyes;
        vector<Rect> noses;
        //-- detectar nariz e olhos
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        for (size_t j = 0; j < eyes.size(); j++)
        {
            Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
            int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
            circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }
        nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        for (size_t j = 0; j < noses.size(); j++) {
            Point center(faces[i].x + noses[j].x + noses[j].width*0.5, faces[i].y + noses[j].y + noses[j].height*0.5);
            int radius = cvRound((noses[j].width + noses[j].height)*0.25);
            circle(frame, center, radius, Scalar(0, 0, 255), 4, 8, 0);
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}

while running, I get these errors or warnings

[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\videoio_registry.cpp (187) cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(7, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940)
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (340) cv::impl::getPluginCandidates ...
(more)
edit retag flag offensive close merge delete

Comments

as I know CascadeClassifier in opencv is not so fast. You may try some dlib face detection.

gino0717 gravatar imagegino0717 ( 2019-08-07 01:53:43 -0600 )edit

That's not error. Do you use debug configuration ? What does it mean low fps? Image size ? pc configuration.?

LBerger gravatar imageLBerger ( 2019-08-07 03:30:13 -0600 )edit

Use a cnn model with a nvidea gpu.

holger gravatar imageholger ( 2019-08-07 15:17:02 -0600 )edit

as I koow,OpenCV is not support GPU yet?

jsxyhelu gravatar imagejsxyhelu ( 2019-08-10 19:12:14 -0600 )edit

Use a model based on a convolutionary neuronal network - just google it and pick a framework with gpu support (they all have it).

holger gravatar imageholger ( 2019-08-11 10:55:18 -0600 )edit

not a single one of these answers really provide a solution ... except the one of @LBerger that is trying to collect more information

  • If your FPS is low, it can be due to the large image size you are processing. If you use the face detector, do you have an idea of scales and thus are you able to limit the scale ranges? On top of that you could also use a smaller image size. Too much pixel information per object is just not usefull in these cascades.
  • You can use OpenVINO toolkit to run efficient deep learning models on CPU. There are some heavily optimized models that run quite well.

All depends on what you call slow. We need more info on that.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-20 08:18:55 -0600 )edit

I get these errors or warnings

no, that is just debug info from the VideoCapture. (and only in DEBUG mode)

berak gravatar imageberak ( 2020-03-29 07:30:33 -0600 )edit

1 answer

Sort by » oldest newest most voted
-1

answered 2020-03-29 06:05:47 -0600

ComputerVisionary gravatar image

OpenCV standard cascades are terribly slow and inaccurate in real world scenarios. Professional project re-train from the scratch the cascades to reach for better results. Take a look here for an overview on how to retrain a cascade for superior FPS on low cost devices: http://www.vision-ary.net/2015/03/lar...

Regards!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-08-06 19:47:04 -0600

Seen: 1,401 times

Last updated: Mar 29 '20