OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector
I'm trying to train SVM with my own images and feed that to the HOGDescriptor in OpenCV.
The training looks to be going well, but when I try to give the model to the HOGDescriptor with the setSVMDetector
function it fails.
Code for training
HOGDescriptor hog = new HOGDescriptor(new Size(96, 160), new Size(8,8), new Size(4,4), new Size(4,4), 9);
SVM svm = SVM.create();
svm.setKernel(SVM.LINEAR);
MatOfFloat traindata = new MatOfFloat();
Mat trainlabel = new Mat();
String personsPath = "C:\\_small\\pos";
for (File file : new File(personsPath).listFiles()) {
if(file.isFile()){
Mat img = Imgcodecs.imread(file.getPath());
MatOfFloat descriptors = new MatOfFloat();
Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
hog.compute(gray, descriptors);
traindata.push_back(descriptors);
trainlabel.push_back(Mat.ones(descriptors.rows(), 1, CvType.CV_32SC1));
}
}
String personsPathneg = "C:\\_small\\neg";
for (File file : new File(personsPathneg).listFiles()) {
if(file.isFile()){
Mat img = Imgcodecs.imread(file.getPath());
MatOfFloat descriptors = new MatOfFloat();
Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.resize(gray, gray, new Size(96 , 160));
hog.compute(gray, descriptors);
traindata.push_back(descriptors);
trainlabel.push_back(Mat.zeros(descriptors.rows(), 1, CvType.CV_32SC1));
}
}
svm.train(traindata, Ml.ROW_SAMPLE, trainlabel);
Mat supportVector = svm.getSupportVectors();
Mat alpha = new Mat();
Mat svidx = new Mat();
double rho = svm.getDecisionFunction(0, alpha, svidx);
int size = (int)(supportVector.total() + 1) * supportVector.channels();
float[] temp = new float[size];
supportVector.get(0, 0, temp);
temp[temp.length - 1] = (float)-rho;
MatOfFloat floatvector = new MatOfFloat(temp);
if(hog.checkDetectorSize()){
hog.setSVMDetector(floatvector);
}
The error message
OpenCV Error: Assertion failed (checkDetectorSize()) in cv::HOGDescriptor::setSVMDetector, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\hog.cpp, line 117
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\hog.cpp:117: error: (-215) checkDetectorSize() in function cv::HOGDescriptor::setSVMDetector
]
at org.opencv.objdetect.HOGDescriptor.setSVMDetector_0(Native Method)
at org.opencv.objdetect.HOGDescriptor.setSVMDetector(HOGDescriptor.java:302)
at feature.extraction.App.main(App.java:86)
It's driving me crazy does anybody have an idea?
Edit3:
Changed the code to train the features (thanks to berak) but still I get the same error message.
The error message points to hog.cpp line 117 the assert that is being called is CV_Assert(checkDetectorSize());
I call the java variant of this function at the end to check if it's true
and it always returns true
So what is the difference between the c++ checkDetectorSize
and the java one.
i guess, you forgot to add -rho to the support vector
(and the libpng warning is unrelated)
I've edited the question with added -rho but still the same error, I don't know if I did it right though.
hmm, imho, you have to create a new array, 1 larger than the original, and then put -rho at the end.
I did that (See edit) but still the same error. It's driving me nuts I don't know what i'm doing wrong
oh, wait, i missed the obvious : you have to train the SVM on hog features, not images
Yeah that's true, I'm just playing with the trainer now just trying to get it to work. But that shouldn't matter that much right? Anyways it may be better to do it the right way and train the features. I'll report back when I edited the code.
it does matter !