how to solve problem of low fps in OpenCV
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 ...
as I know CascadeClassifier in opencv is not so fast. You may try some dlib face detection.
That's not error. Do you use debug configuration ? What does it mean low fps? Image size ? pc configuration.?
Use a cnn model with a nvidea gpu.
as I koow,OpenCV is not support GPU yet?
Use a model based on a convolutionary neuronal network - just google it and pick a framework with gpu support (they all have it).
not a single one of these answers really provide a solution ... except the one of @LBerger that is trying to collect more information
All depends on what you call slow. We need more info on that.
no, that is just debug info from the VideoCapture. (and only in DEBUG mode)