我使用Python 2.7和OpenCV构建程序来识别对象。我的数据集是CIFAR-10。我花了两天时间试图理解错误,但我无法找到原因。我的代码是:
import cv2
import cv2.cv as cv
import numpy as np
import os, os.path
import csv
labels = []
with open('labels.csv','rb') as lb:
for row in lb:
labels.append(row)
os.chdir('train')
#counts the total number of pictures in the "train" folder
nFiles = len([name for name in os.listdir('.') if os.path.isfile(name)])
extension = '.png'
images = []
imageLabels = []
trainLabels =[]
trainingDescriptors = []
i = 1
sift = cv2.SIFT()
j = 0
BOW = cv2.BOWKMeansTrainer(10)
with open('../trainLabels.csv', 'rb') as labels:
for row in labels:
imageLabels.extend(row[1])
while i < 5000:
#Load all images
#print "loading file " + str(i) + ".png"
if i%500 == 0:
print "Loading file " + str(i)
image = cv2.imread(str(i)+extension,cv2.CV_LOAD_IMAGE_GRAYSCALE)
images.append(image.flatten())
descriptor = np.array([])
keypoints, descriptor = sift.detectAndCompute(image,None)
if descriptor != None:
BOW.add(descriptor)
trainingDescriptors.extend(descriptor)
i += 1
j += 1
dictionary = BOW.cluster()
print "bow dictionary", np.shape(dictionary)
for label in imageLabels:
if label == 'airplane':
trainLabels.append(1)
elif label == 'automobile':
trainLabels.append(2)
elif label == 'bird':
trainLabels.append(3)
elif label == 'cat':
trainLabels.append(4)
elif label == 'deer':
trainLabels.append(5)
elif label == 'dog':
trainLabels.append(6)
elif label == 'frog':
trainLabels.append(7)
elif label == 'horse':
trainLabels.append(8)
elif label == 'ship':
trainLabels.append(9)
elif label == 'truck':
trainLabels.append(10)
svm = cv2.SVM()
svm.train(np.array(trainingDescriptors),np.array(trainLabels))
错误在这一行:
svm.train(np.array(trainingDescriptors),np.array(trainLabels))
,错误是:
错误:( - 5)函数cvPreprocessCategoricalResponses中的响应数组无效