OpenCV HOG+SVM: assertion failed checkDetectorSize()

3.1k views Asked by At

I have a problem with openCV in c++ when I try to set my own SVM detector into the method hog::setSVMDetector(Detector) in openCV.

I followed the following procedure SVM classifier based on HOG features for "object detection" in OpenCV and I'm stuck at step 3.

I'm using openCV 3.0 and it's currently built in SVM. This is how I'm training and building my detector:

TRAIN

svm = SVM::create();
svm->setType(type);
svm->setKernel(kernel);
svm->setC(C);

if (kernel == SVM::LINEAR) {
    svm->setDegree(1);
} else if (kernel == SVM::POLY) {
    svm->setDegree(3);
}
svm->train(trainingSamples, ml::ROW_SAMPLE, labels);

BUILDING DETECTOR

vector<float> alpha;
vector<float> svidx;
vector<float> model;

// Getting Support Vectors
Mat svs = svm->getSupportVectors();

double rho = svm->getDecisionFunction(0, alpha, svidx);

    // Computing w in primal form
for (int i = 0; i < svidx.size(); i++) {
    model.push_back(svs.at<float>(i, 0) * alpha.at(i));
    for (int j = 1; j < svs.cols; j++) {
        model.at(i) += svs.at<float>(i, j) * alpha.at(i);
    }
}

//   Adding rho
    model.push_back(rho);
    return model;

The error occur when I try to feed the above model to:

hog.setDetector(model); 

OpenCV Error: Assertion failed (checkDetectorSize()) in setSVMDetector, file /home/dario/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/hog.cpp, line 115 terminate called after throwing an instance of 'cv::Exception' what(): /home/dario/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/hog.cpp:115: error: (-215) checkDetectorSize() in function setSVMDetector

Any idea of what I'm doing wrong?

1

There are 1 answers

1
Dario On BEST ANSWER

I solved the problem by rewriting the above code as

    Ptr<SVM> svm;
    HOGDescriptor my_hog;
    ...
    // Load the trained SVM.
    svm = StatModel::load<SVM>( "model.yml" );
    // Set the trained svm to my_hog
    vector< float > hog_detector;
    get_svm_detector( svm, hog_detector );
    my_hog.setSVMDetector( hog_detector );