Ask Your Question

just_trying_stuff's profile - activity

2018-02-08 03:39:15 -0600 received badge  Notable Question (source)
2018-02-08 03:39:15 -0600 received badge  Popular Question (source)
2016-04-17 15:22:04 -0600 commented question mat.hpp assert error at line 554

what kind of assert error?

2016-04-12 02:06:20 -0600 received badge  Enthusiast
2016-04-11 14:47:22 -0600 commented answer svm load (still?) missing in java

omg I finally got it compiled. And now I get errors on smv.save. Exception in thread "main" java.lang.Exception: unknown exception Yeah that's a clear error message /s

2016-04-11 14:44:29 -0600 commented question How to store and load train data of KNearest with Java API

Ever found a solution for this?

2016-04-11 05:05:18 -0600 commented answer svm load (still?) missing in java

Hmm yeah that's something I'm going to do another day then. Never done that before but I found this tutorial. So it shouldn't be to hard I guess.

2016-04-11 04:47:57 -0600 commented answer svm load (still?) missing in java

That's what I also though but I downloaded the latest release from opencv.org (3.1.0) and used the prebuilt jar file. I just redownloaded for a sanity check but still no load function. The load function is a function from the algorithm class. The Algorithm class in java doesn't contain a load function.

2016-04-10 07:49:30 -0600 asked a question svm load (still?) missing in java

So after training and saving my model. I tried to load it again only to find that there's no load function in the java wrappers. After some googling I found that this is a bug. It looks like the python wrappers are fixed but no word on the java ones.

So is there a work around for this? Because training the model each run is a bit too much. I'm using OpenCV 3.1.0

2016-04-07 08:33:49 -0600 received badge  Supporter (source)
2016-04-07 08:26:25 -0600 received badge  Self-Learner (source)
2016-04-07 08:20:01 -0600 received badge  Scholar (source)
2016-04-07 08:12:20 -0600 answered a question OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector

After a lot of hours and help from berak I figured it out. Instead of using a Mat and using the push_back function. I made a 2d float array and put the descriptor output from the compute function in there.

//for loop each image
float[] vector = descriptors.toArray();
trainVector[i] = vector;

Afterwards I made an empty Mat and put the values of the float array in there. And used that for training. Also don't forget to put the rho at the end of the supportvectorfrom the svm.

2016-04-06 05:44:38 -0600 commented answer OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector

the 160x96 was indeed wrong but I changed that already, it's now 96*160. The floatvector object is 2*1*CV_32FC1. And I made sure that the descriptor size matches the expectation. I also added hog.checkDetectorSize() before training with svm and it returns true.

2016-04-06 04:53:35 -0600 commented answer OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector

I've edited the code to train the features but still I get the same error message. (i've edited the main question with the new code)

2016-04-06 03:47:36 -0600 commented question OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector

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.

2016-04-06 02:59:32 -0600 commented question OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector

I did that (See edit) but still the same error. It's driving me nuts I don't know what i'm doing wrong

2016-04-05 06:26:58 -0600 commented question OpenCV 3.1.0 Assertion failed checkDetectorSize in cv::HOGDescriptor::setSVMDetector

I've edited the question with added -rho but still the same error, I don't know if I did it right though.

2016-04-05 06:25:18 -0600 received badge  Editor (source)
2016-04-05 04:45:47 -0600 asked a question 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.