"错误的参数(列车数据必须是浮点矩阵)"错误

时间:2016-03-25 08:57:01

标签: python opencv machine-learning svm

我正在开发一个图像分类器。我将功能提取为pca。我的示例代码是

for file in listing1:
  img = cv2.imread(path1 + file)
  res=cv2.resize(img,(250,250))
  gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
  xarr=np.squeeze(np.array(gray_image).astype(np.float32))
  m,v=cv2.PCACompute(xarr)
  training_set.append(v)
  training_labels.append(1)

trainData=np.float32(np.float32(xi) for xi in training_set)
responses=np.float32(training_labels)
svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')

但在训练期间,我收到了这个错误:

  

OpenCV错误:错误参数(列车数据必须是浮点矩阵)   在cvCheckTrainData中,文件   .. \ .. \ .. \ .. \ opencv \ modules \ ml \ src \ inner_functions.cpp,第857行

1 个答案:

答案 0 :(得分:1)

解决方案这对我有用 您需要将其转换为np数组,并且需要展平该数组。

for file in listing1:
 img = cv2.imread(path1 + file)
 res=cv2.resize(img,(250,250))
 gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
 xarr=np.squeeze(np.array(gray_image).astype(np.float32))
 m,v=cv2.PCACompute(xarr)
 arr= np.array(v)
 flat_arr= arr.ravel()
 training_set.append(flat_arr)
 training_labels.append(1)

培训

trainData=np.float32(training_set)
responses=np.float32(training_labels)
svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')