我的代码有问题,这是我的代码列表
import os
os.getcwd()
from SimpleCV import EdgeHistogramFeatureExtractor, HueHistogramFeatureExtractor, MorphologyFeatureExtractor
from SimpleCV.MachineLearning import *
from SimpleCV.ImageClass import Image, ImageSet, ColorSpace
edgeExtractor = EdgeHistogramFeatureExtractor(20)
hueExtractor = HueHistogramFeatureExtractor(10)
morphoExtractor = MorphologyFeatureExtractor()
extractorsList = [edgeExtractor, hueExtractor, morphoExtractor]
svm = SVMClassifier(extractorsList)
tree = TreeClassifier(extractorsList, flavor='Boosted')
naiveBayes = NaiveBayesClassifier(extractorsList)
trainPaths = ['C:\\Users\\abi\\Documents\\program coba\\ImageTrain', 'C:\\Users\\abi\\Documents\\program coba\\ImageTest']
trainPaths = ['C:\\Users\\abi\\Documents\\program coba\\ImageTrain\\Positive1', 'C:\\Users\\abi\\Documents\\program coba\\ImageTrain\\Negative1']
classes = ['Negative', 'Positive']
svm.train(trainPaths, classes, savedata="mydata.txt", verbose=False)
testPaths = ['C:\\Users\\abi\\Documents\\program coba\\ImageTest\\Positive1', 'C:\\Users\\abi\\Documents\\program coba\\ImageTest\\Negative1']
print "SVM:", svm.test(testPaths, classes, verbose=False) # [good, bad, confusion]
edgeExtractor = EdgeHistogramFeatureExtractor(20)
svm = SVMClassifier(extractorsList)
listImages = ImageSet()
for p in ['C:\\Users\\abi\\Documents\\program coba\\NewImages\\coba']:
listImages += ImageSet(p)
for image in listImages:
className = svm.classify(image)
image.drawText(className,25,25,fontsize=60)
listImages.show()
之后我遇到了一些像这样的问题
Traceback (most recent call last):
File "C:\Users\abi\Documents\program coba\imnew", line 28, in <module>
className = svm.classify(image)
File "C:\Python27\lib\site-packages\SimpleCV\MachineLearning\SVMClassifier.py", line 153, in classify
featureVector.extend([self.mClassNames[0]])
IndexError: list index out of range
有IndexError按摩,但我不知道代码是什么错误。现在我使用Python 2.7.3。请帮帮我
答案 0 :(得分:0)
svm
初始化两次。第二次,您跳过svm.train()
,这就是self.mClassNames[0]
为空的原因。
mClassNames
由classes
参数填充:
svm.train(trainPaths, classes, savedata="mydata.txt", verbose=False)`
只需删除第二个svm = SVMClassifier(extractorsList)
即可解析IndexError
。
但请注意,根据documentation,您的classes
订单与trainPaths
中的图片路径顺序不一致:
图像 - 路径或图像集的顺序必须与类类型的顺序相同 - 请注意,所有图像类必须位于单独的目录中 - 类名也必须与目录对齐。
这表明classes
应更改为关注trainPaths
:
trainPaths = ['C:\\Users\\abi\\Documents\\program coba\\ImageTrain\\Positive1',
'C:\\Users\\abi\\Documents\\program coba\\ImageTrain\\Negative1']
classes = ['Positive', 'Negative']
您可能还需要让classes
字符串与图像目录字符串完全匹配。源代码中的注释有点含糊不清。您还出于某种原因定义了trainPaths
两次。