How to load .xml file in the Microsoft Visual Studio

For my project, I'm using Microsoft Visual Studio Ultimate 2013, opencv-3.3.1-vc14 and Microsoft Window 10. In my project, I'm going to load an .xml file but it didn't.

The directory of my project is: C:\Users\Muhammad_Anas_Hashmi\Desktop\Face_and_Eye_Detection. But the .xml file that I want to load has the directory: C:\opencv\sources\data\haarcascades\haarcascade_eye.xml. The way that I use to load the file is:

CascadeClassifier eye_cascade;
eye_cascade.load("C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xml")

But I check through a message whether the file has been loaded or not through the following code.

if (!eye_cascade.load("C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xml"))
    {
        printf("Error loading cascade file for eye\n");
    }

Every time it prints out the message. It means file has not been loaded.

But the whole code of my project is:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<vector>

using namespace cv;
using namespace std;

int main()
{
    CascadeClassifier face_cascade, eye_cascade;
    if (!face_cascade.load("C:\\Users\Muhammad Anas Hashmi\Desktop\Face_and_Eye_Detection\haarcascade_frontalface_alt2.xml"))
    {
        printf("Error loading cascade file for face\n");
        /*return 1;*/
    }
    if (!eye_cascade.load("C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xml"))
    {
        printf("Error loading cascade file for eye\n");
        /*return 1;*/
    }
    VideoCapture capture(0);
    if (!capture.isOpened())
    {
        printf("Error to initialize camera");
        return 1;
    }
    Mat cap_img, gray_img;
    vector<Rect> faces, eyes;
    while (1)
    {
        capture >> cap_img;
        waitKey(10);
        cvtColor(cap_img, gray_img, CV_BGR2GRAY);
        cv::equalizeHist(gray_img, gray_img);
        face_cascade.detectMultiScale(gray_img, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0), cvSize(300, 300));
        for (int i = 0; i < faces.size(); i++)
        {
            Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
            Point pt2(faces[i].x, faces[i].y);
            Mat faceROI = gray_img(faces[i]);
            eye_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
            for (size_t j = 0; j < eyes.size(); j++)
            {
                Point center(faces[i].x + eyes[j].x + eyes[j].width * 0.5, faces[i].y + eyes[j].y + eyes[j].width * 0.5);
                int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
                circle(cap_img, center, radius, Scalar(255, 0, 0), 2, 8, 0);
            }
            rectangle(cap_img, pt1, pt2, cvScalar(0, 255, 0), 2, 8, 0);
        }
        imshow("Result", cap_img);
        waitKey(3);
        char c = waitKey(3);
        if (c == 27)
        {
            break;
        }
    }
    system("PAUSE");
    return 0;
}

I found the problem at the start while loading the .xml file.

Upvotes: 0

Views: 1071

Answers (1)

nvoigt
nvoigt

Reputation: 77334

C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xm

You only escaped the first backslash. You need to escape all:

C:\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_eye.xml

Upvotes: 1

Related Questions