Fadwa
Fadwa

Reputation: 1931

opencv 3.1 detectmultiscale() for face detection

I am trying to detect faces using opencv detectmultiscale. The number of faces in the output is HUGE (faces { size=1152921366050660864 }...) although the input image has only one face. I tried to change the minNeighbors value in order to eliminate the redundancy but nothing changed. I also tried to draw only 5 instances from faces instead of looping on faces.size()to see what is going on, and the output was drawing circles over and over on the same face. I have the same problem as in here, they suggested to build the library by myself. Is this the only solution. I am not good at using Make.

drawing 5 instances only

Here is the code, it's copy/paste from the openCV tutorial. I am using VS2015.

#include "stdafx.h"
#include <iostream>
#include <conio.h>

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

using namespace std;
using namespace cv;

String face_cascade_name = "haarcascade_frontalface_default.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

    for (size_t i = 0; i < 5; i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
        Mat faceROI = frame_gray(faces[i]);
        //std::vector<Rect> eyes;

        ////-- In each face, detect eyes
        //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 eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
        //  int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
        //  circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
        //}
    }

    //-- Show what you got
    imshow(window_name, frame);
}



int main()
{
    //Load the cascades
    if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading eyes cascade\n"); return -1; };

    Mat img, gray_img;
    VideoCapture myVideo("head.mpg"); //open default camera

    if (!myVideo.isOpened())
        cout << "The Camera is not open";

    while (myVideo.read(img)) {

        detectAndDisplay(img);

        if (waitKey(30) >= 0) break;

    }

    //  system("pause");
    return 0;
}

Upvotes: 1

Views: 1417

Answers (2)

Fadwa
Fadwa

Reputation: 1931

I rebuilt opencv using cmake. You can find the step by step tutorial here. and it worked.

Result

Upvotes: 1

guinny
guinny

Reputation: 1532

It seems what you need is Non-Maximum-Suppression, which would get rid of all the redundant detection.

I was surprised opencv doesn't seem to have it anywhere. Maybe you can find some inspiration here: http://code.opencv.org/attachments/994/nms.cpp?

Upvotes: 0

Related Questions