svm python opencv_使用OpenCV-3.0.1 / python 2.7创建svm

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)

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值