Ask Your Question

Revision history [back]

OpenCV Error: Null pointer...Invalid classifier cascade in function oclHaarDetectObjects

Hi everyone! I have search this problem on this site. But the answer is not correct to solve my situation...

I did actually add the *.xml file, so I test the version which didn't use the ocl library.

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;

vector<Rect> detectFaces(Mat gray){

vector<Rect> faces;
CascadeClassifier face_detector;

bool r;
r = face_detector.load("/home/swpc/haarcascade_frontalface_default.xml");
if(!r)cout<<"Can not load the xml!!"<<endl;

face_detector.detectMultiScale(gray, faces,  1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0));
return faces;
}

int main(){

    vector<Rect> f;
    Mat Image;

    Image = imread("wang.jpg");
    if (Image.cols == 0) {
            cout << "Error reading file " << endl;
            return -1;
    }

    resize(Image,Image,Size(640,480),0,0,1);
    Mat gray(Image);
    cvtColor(gray,gray,CV_BGR2GRAY);
   equalizeHist(gray,gray);

    f = detectFaces(gray);

    for(unsigned int i=0;i<f.size();i++){
            Rect f_rect = f[i];
            rectangle(Image,f_rect,CV_RGB(255,0,0),1,8,0);
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.

    imshow("face",Image);

    waitKey(0);
    return(0);
}

It is work correctly for me.. But this version got a error !!

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/ocl/ocl.hpp>
#include <iostream>
using namespace std;
using namespace cv;

vector<Rect> detectFaces(Mat gray){

    ocl::oclMat oclGray;
    vector<Rect> faces;
    ocl::OclCascadeClassifier face_detector;
    oclGray.upload(gray);

    bool r;
    r = face_detector.load("/home/swpc/haarcascade_frontalface_default.xml");
    if(!r)cout<<"Can not load the xml!!"<<endl;

    face_detector.detectMultiScale(oclGray, faces,  1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0));
    return faces;
}


int main(){

    vector<Rect> f;
    Mat Image;

    Image = imread("wang.jpg");
    if (Image.cols == 0) {
            cout << "Error reading file " << endl;
            return -1;
    }

    resize(Image,Image,Size(640,480),0,0,1);
    Mat gray(Image);
    cvtColor(gray,gray,CV_BGR2GRAY);
    equalizeHist(gray,gray);

    f = detectFaces(gray);

    for(unsigned int i=0;i<f.size();i++){
            Rect f_rect = f[i];
            rectangle(Image,f_rect,CV_RGB(255,0,0),1,8,0);
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.

    imshow("face",Image);

    waitKey(0);
    return(0);
}

The error is >>>

OpenCV Error: Null pointer (Invalid classifier cascade) in oclHaarDetectObjects, file /home/swpc/opencv-2.4.11/modules/ocl/src/haar.cpp, line 686 terminate called after throwing an instance of 'cv::Exception' what(): /home/swpc/opencv-2.4.11/modules/ocl/src/haar.cpp:686: error: (-27) Invalid classifier cascade in function oclHaarDetectObjects

Aborted (core dumped)

sorry for my poor English. Does anyone know this problem??