0

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?

Community
  • 1
  • 1
Dario
  • 331
  • 4
  • 16
  • 1
    It might be that you different window size and in that case it will different number of values that are fed into `hog.setDetector(model); ` – shah Sep 18 '15 at 20:23
  • I've got a similar problem. I'd like to manually set the detector size to something without a 0.5 aspect ratio. My reasoning is that I want to detect people that aren't always in the perfect stance that they fit into a 0.5 aspect ratio. Consider the case of a boxer with arms extended in a punch and knees bent. I guess this isn't possible due to the use of the pretraine person detection classifier. Any recommendations? Perhaps I need to manually train an SVM for this purpose? – Joey Carson May 05 '16 at 17:15

1 Answers1

0

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 );
Dario
  • 331
  • 4
  • 16
  • 1
    Hi, What version of opencv do you use ? Does function get_svm_detector is in opencv? – Ocxs Mar 21 '16 at 10:14