ValueError:找到带有暗淡的数组3.预期的估计量< = 2.使用自己的数据集

时间:2016-07-27 07:00:49

标签: python-2.7 image-processing machine-learning scikit-learn

我试图为识别问题生成自己的训练数据。我有两个文件夹s0和s1,包含的文件夹是数据。图片,标签是两个列表,其中标签包含文件夹的名称。

data
  |—- s0
  |    |—- 1.pgm
  |    |—- 2.pgm
  |    |—- ...
  |—- s1
  |    |—- 1.pgm
  |    |—- 2.pgm
  |    |—- ...

以下是代码,它在classifier.fit(images, lables)行显示以下错误

 Traceback (most recent call last):
 File "mint.py", line 34, in <module>
   classifier.fit(images, lables)
 File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py",  line 150, in fit
  X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
 File "/usr/local/lib/python2.7/dist-         packages/sklearn/utils/validation.py", line 396, in check_array
 % (array.ndim, estimator_name)) 

ValueError: Found array with dim 3. Estimator expected <= 2. here

代码:

import os,sys
import cv2
import numpy as np
from sklearn.svm import SVC
fn_dir ='/home/aquib/Desktop/Natural/data'

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(fn_dir):
  for subdir in dirs:
     names[id] = subdir
     mypath = os.path.join(fn_dir, subdir)
    for item in os.listdir(mypath):
      if '.png' in item:  
      label=id
      image = cv2.imread(os.path.join(mypath, item),0)
      r_image = np.resize(image,(30,30))
      if image is not None:
         images.append(r_image)
         lables.append(int(label))
    id += 1  
# Create a Numpy array from the two lists above
(images, lables) = [np.array(lis) for lis in [images, lables]]
classifier = SVC(verbose=0, kernel='poly', degree=3)
classifier.fit(images, lables)

我真的不明白如何在二维中纠正它。我正在尝试以下方法,但错误是一样的:

images = np.array(images)
im_sq = np.squeeze(images).shape
images = images.reshape(images.shape[:2]) 

0 个答案:

没有答案