ValueError:找到带有dim的数组3.估计的估计值< = 2

时间:2016-07-25 04:30:09

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

我正在尝试为识别问题生成自己的训练数据。我有两个文件夹s0s1,文件夹包含dataimageslableslabels包含文件夹名称的两个列表。

|—- data
|    |—- s0
|    |    |—- 1.pgm
|    |    |—- 2.pgm
|    |    |—- 3.pgm
|    |    |—- 4.pgm
|    |    |—- ...
|    |—- s1
|    |    |—- 1.pgm
|    |    |—- 2.pgm
|    |    |—- 3.pgm
|    |    |—- 4.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:找到带有暗淡的数组3.估计的估计量&lt; = 2。  这里

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])

1 个答案:

答案 0 :(得分:0)

代码中while ((r = read(fd, buf, BUFF_SIZE)) > 0) { if ((write(STDOUT_FILENO, buf, r)) == -1) { // the fixed line perror(argv[i]); return EXIT_FAILURE; } // if } // while 最后一行有语法错误。括号未正确关闭。所以它应该像这样images.append(cv2.imread((path, 0))。发布错误的回溯总是一件好事,这样任何人都可以轻松回答。