Face recognition using harr cascades fails every time b/c the program can not load cascades

I have tried absolute path (C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml) directly into the code. Also, I tried copying the xml files into the folder where my program is, I also tried running the code in different platforms and configuration(Off course matching the version), but still nothing works for me. I have also tried visual studio 2015,2017, open CV 3.2 and 3.3 but again, no success Please help me find what I am doing wrong and thanks. Here is on of the codes I have tried.The program crashes in the if statement, but I have no idea why.

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

using namespace std;
using namespace cv;

// Function Headers
void detectAndDisplay(Mat frame);

// Global variables
string face_cascade_name = 
"C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml";
CascadeClassifier face_cascade;

// Function main
int main(void)
{
    // Load the cascade

    if (!face_cascade.load(face_cascade_name)) {
        printf("--(!)Error on cascade loading\n");
        return (-1);
     }

     // Read the image file
    Mat frame = imread("preview-obama-10.jpg");

    // Apply the classifier to the frame
    if (!frame.empty())
        detectAndDisplay(frame);
    waitKey(0);
    return 0;
 }

// Function detectAndDisplay
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, 2, 0 | 
CASCADE_SCALE_IMAGE, Size(30, 30));

    for (int ic = 0; ic < faces.size(); ic++) // Iterate through all current 
elements (detected faces)
    {
        Point pt1(faces[ic].x, faces[ic].y); // Display detected faces on 
main window - live stream from camera
        Point pt2((faces[ic].x + faces[ic].height), (faces[ic].y + 
faces[ic].width));
        rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
    }

    imshow("original", frame);
}

Upvotes: 0

Views: 67

Answers (1)

user0042
user0042

Reputation: 8018

Please help me find what I am doing wrong ...

Escape \ appearing in string literals: \\!

This

 string face_cascade_name = 
 "C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml";

should be

string face_cascade_name = 
"C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";
 // ^       ^        ^     ^             ^

or1

 string face_cascade_name = 
    R"x(C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml)x";
 // ^^^^                                                                    ^^

BTW: The compiler should have spit out warnings about unknown character escape sequences like '\o', '\s', '\d' and '\h'.


1)See raw string literals

Upvotes: 2

Related Questions