c++ - Opencv - Haar cascade - Face tracking is very slow -


i have developed project tracking face through camera using opencv library. used haar cascade haarcascade_frontalface_alt.xml detect face.

my problem if image capture webcame doesn't contain faces, process detect faces slow images camera, showed continuosly user, 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 opened." << endl;     }      else      {         mat frame;         vector<rect> faces;         vector<rect> eyes;         mat original;         mat frame_gray;         mat face;         mat processedface;          (;;)          {             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 relatively slow nature. furthermore, there not of optimization can algorithm because detectmultiscale parallelized in opencv.

the note code: faces ever detected minsize equals size(200, 200)? though surely, bigger minsize - better performance is.

try scaling 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 change minsize more reasonable value , convert detected face locations real scale)

image size reducing 2, 3, 5 times great performance relief image processing algorithm, when comes costly stuff detection.

as mentioned before, if resizing won't trick, try fetching other bottlenecks using profiler.

and can switch lbp classifier comparably faster though less accurate.

hope help.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -