Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating svm with OpenCV-3.0.1 / python 2.7

Tags:

python

opencv

What a weird system it is here. I had the same problem as in this question here: AttributeError: 'module' object has no attribute 'SVM_LINEAR' But I can't add any more questions or comments to that question so I'm forced to ask almost the same question. Anyway, please help with the below:

So I just noticed that CV-3.0.1 has both Chi-squared and intersection kernels, whereas my previous 2.4.9 did not, so I upgraded (gentoo btw). Everything was working in 2.4.9, I just wanted moar kernel choices (and intersection works well with what I'd doing says Yang et al 2009).

But following the above hasn't worked for me.

Besides my usual:

import cv2

I've tried adding:

import cv2.ml

and/or

from cv2 import ml

They don't fix anything (I'm kind of new to python too, so not sure which is what I'm meant to be using).

My line:

svm = cv2.SVM()

is what's causing the problem, I've tried changing it to:

svm = cv2.ml.SVM()

And that doesn't fix it, all I get is still:

Traceback (most recent call last):
File "05traintestsift.py", line 12, in svm = cv2.SVM()
AttributeError: 'module' object has no attribute 'SVM'

or:

Traceback (most recent call last):
File "05traintestsift.py", line 12, in svm = cv2.ml.SVM()
AttributeError: 'module' object has no attribute 'SVM'

Surely there's some basic way to get stuff working again that I'm missing?

nb: everything except trying the new kernel type was working half an hour ago in 2.4.9, so it's purely some new syntax in 3.0.1-r2 that's changed.

I'll also note that their example in the documentation here: http://docs.opencv.org/3.1.0/dd/d3b/tutorial_py_svm_opencv.html also hasn't put in any '.ml', so even that hasn't been updated (I copied the svm = cv2.SVM() syntax from line 48 of their example btw).

I've noticed that if I just delete that line it gets further through the code, with the .ml fix from the previous question it accepts my parameters fine:

svm_params = dict(kernel_type = cv2.ml.SVM_CHI2,svm_type = cv2.ml.SVM_C_SVC,C=7,gamma=3)

but then when I go to train it can't find the svm:

svm.train(traindata,trainnames,params=svm_params)

(obviously because I haven't created the 'svm' object yet)

like image 782
DrCroubie Avatar asked Dec 07 '22 22:12

DrCroubie


1 Answers

That how it should look like:

trainingDataMat = np.array(*train_data*, np.float32)
labelsMat = np.array([*label_data*], np.int32)

svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
# svm.setDegree(0.0)
# svm.setGamma(0.0)
# svm.setCoef0(0.0)
# svm.setC(0)
# svm.setNu(0.0)
# svm.setP(0.0)
# svm.setClassWeights(None)
svm.setTermCriteria((cv2.TERM_CRITERIA_COUNT, 100, 1.e-06))
svm.train(trainingDataMat, cv2.ml.ROW_SAMPLE, labelsMat)

sample_data = np.array([*your_data*], np.float32)
response = svm.predict(sample_data)
like image 116
Alex Glinsky Avatar answered Dec 11 '22 10:12

Alex Glinsky