问题描述
这是一个多么奇怪的系统.我有一个与此问题相同的问题: AttributeError:模块"对象没有属性"SVM_LINEAR" 但是我无法在该问题上添加任何其他问题或评论,因此我被迫提出几乎相同的问题.无论如何,请在以下方面提供帮助:
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:
所以我只是注意到CV-3.0.1同时具有卡方和交叉核,而我以前的2.4.9没有,因此我进行了升级(gentoo btw).一切都在2.4.9中运行,我只想选择磨碎的内核(Yang等人2009年所说的相交效果很好).
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.
除了我的平常:
import cv2
我尝试添加:
import cv2.ml
和/或
from cv2 import ml
它们什么也没解决(我也是python的新手,所以不确定我要使用的是什么.)
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).
我的电话:
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'
或:
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:半小时前,除了尝试使用新的内核类型之外,其他所有功能在2.4.9中都可以正常工作,因此纯粹是3.0.1-r2中的一些新语法已更改.
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.
我还将在此处的文档中注意到它们的示例: http ://docs.opencv.org/3.1.0/dd/d3b/tutorial_py_svm_opencv.html 也未添加任何".ml",因此即使尚未更新(我复制了svm =他们示例btw的第48行中的cv2.SVM()语法).
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).
我注意到,如果我只是删除该行,它将通过代码进一步处理,使用上一个问题中的.ml修复程序,它可以很好地接受我的参数:
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)
但是当我去训练时却找不到svm:
but then when I go to train it can't find the svm:
svm.train(traindata,trainnames,params=svm_params)
(显然是因为我还没有创建'svm'对象)
(obviously because I haven't created the 'svm' object yet)
推荐答案
那应该是这样的:
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)
这篇关于使用OpenCV-3.0.1/python 2.7创建svm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!