I'm trying to generate my own training data for recognition problem. I have two folders s0 and s1 and the folder containing is data. Images, lables are the two list in which the labels contains the names of the folder.
data
|—- s0
| |—- 1.pgm
| |—- 2.pgm
| |—- ...
|—- s1
| |—- 1.pgm
| |—- 2.pgm
| |—- ...
Below is the code, it's showing me the following error on line 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
Code:
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)
I really don't understand how to correct it in 2 dimension. I am trying the following, but the error is same:
images = np.array(images)
im_sq = np.squeeze(images).shape
images = images.reshape(images.shape[:2])