Search code examples
opencvface-detectionhaar-classifier

Accuracy tuning for Haar-Cascade Classifier


I'm using Haar-Cascade Classifier in order to detect faces.

I'm currently facing some problems with the following function:

void ImageManager::detectAndDisplay(Mat frame, CascadeClassifier face_cascade){


    string window_name = "Capture - Face detection";
    string filename;

    std::vector<Rect> faces;
    std::vector<Rect> eyes;
    Mat frame_gray;
    Mat crop;
    Mat res;
    Mat gray;
    string text;
    stringstream sstm;


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

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

    // Set Region of Interest
    cv::Rect roi_b;
    cv::Rect roi_c;

    size_t ic = 0; // ic is index of current element


    for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)  
    {

        roi_c.x = faces[ic].x;
        roi_c.y = faces[ic].y;
        roi_c.width = (faces[ic].width);
        roi_c.height = (faces[ic].height);



        crop = frame_gray(roi_c);

        faces_img.push_back(crop);

        rectangle(frame, Point(roi_c.x, roi_c.y), Point(roi_c.x + roi_c.width, roi_c.y + roi_c.height), Scalar(0,0,255), 2);


    }

    imshow("test", frame);
    waitKey(0);

    cout << faces_img.size();


}

The frame is the photo I'm trying to scan.

The face_cascade is the classifier.


Solution

  • internally, the CascadeClassifier does several detections, and groups those.

    minNeighbours (in the detectMultiScale call) is the amount of detections in about the same place nessecary to count as a valid detection, so increase that from your current 2 to maybe 5 or so, until you start to miss positives.