Gomi Odabaşıoğlu
Gomi Odabaşıoğlu

Reputation: 176

C++ OPENCV 4.3.0 x64 Why face_cascade throws error?

I am using OpenCV to add a face recognition feature to my C++ program. I have never used it before and I cant seem to get the cascade feature for facial recognition to work. I am wondering if they have made some changes for the FLAGS in the new version. I can display an image but when that comes to the cascade it always throws an error. Can anyone tell me what am I missing?

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc.hpp>
    #include <stdio.h>
    #include <iostream>
    #include <opencv2\objdetect.hpp>

    using namespace std;
    using namespace cv;

    String face_cascade_name  = "sources/data/haarcascades/haarcascade_frontalface_default.xml";
    String eyes_cascade_name  = "sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
    String smile_cascade_name = "sources/data/haarcascades/haarcascade_smile.xml";

    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;
    CascadeClassifier smile_cascade;

    string window_name = "Capture - Face detection";
    RNG rng(12345);

    int main()
    {
        //-- 1. Load the cascades
        if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading file 1\n"); return -1; };
        if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading file 2\n"); return -1; };
        if (!smile_cascade.load(smile_cascade_name)) { printf("--(!)Error loading file 3\n"); return -1; };

        std::string image_path = samples::findFile("test.jpg");
        Mat img = imread(image_path, IMREAD_COLOR);
        Mat img_gry;

        if (img.empty())
        {
            std::cout << "Could not read the image: " << image_path << std::endl;
            return 1;
        }

        imshow("Display window", img);

        // Detect faces
        std::vector<Rect> faces;
        cvtColor(img, img_gry, COLOR_BGR2GRAY);
        equalizeHist(img, img_gry);

        //I GET ERROR HERE
        face_cascade.detectMultiScale(img_gry, faces, 1.1, 2, CASCADE_SCALE_IMAGE, Size(30, 30), Size(130, 130)); //I GET ERROR HERE

/*...REST WILL BE PARSING faces...*/

        int g_key = waitKey(0); // Wait for a keystroke in the window

        if (g_key == 's')
        {
            imwrite("starry_night.png", img); //save image in same path
        }

        return 0;
    }

Upvotes: 1

Views: 389

Answers (2)

Gomi Odabaşıoğlu
Gomi Odabaşıoğlu

Reputation: 176

I modified the code as:

/*...
OTHER CODE
...*/
std::vector<Rect> faces;
cvtColor(img, img_gry, COLOR_BGR2GRAY);
equalizeHist(img_gry, img_gry);
face_cascade.detectMultiScale(img_gry, faces);

for (size_t i = 0; i < faces.size(); i++)
{
       /*...PARSE...*/
}

imshow("Display window", img);

As @SourceCode mentioned :

equalizeHist( smallImg, smallImg); //my variable is img_gry instead.

But also, I modified :

face_cascade.detectMultiScale(img_gry, faces, 1.1, 2, CASCADE_SCALE_IMAGE, Size(30, 30), Size(130, 130));

To

face_cascade.detectMultiScale(img_gry, faces);

Otherwise it does not work in my case.

Source if that helps: https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html

Upvotes: 1

Source Code
Source Code

Reputation: 221

Have you tried using something like:

face_cascade.detectMultiScale(img_gry, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));

Upvotes: 0

Related Questions