我已经对特征向量进行了重塑,但仍然遇到了以下错误:
ValueError: Expected 2D array, got 1D array instead: array=[].Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
我在预测前使用了reshape,如下所示:
features = features.reshape(1, -1)
但仍然没有解决问题。
这是我的代码:
import cv2import numpy as npimport osimport globimport mahotas as mtfrom sklearn.svm import LinearSVC# function to extract haralick textures from an imagedef extract_features(image): # calculate haralick texture features for 4 types of adjacency textures = mt.features.haralick(image) # take the mean of it and return it ht_mean = textures.mean(axis = 0).reshape(1, -1) return ht_mean# load the training datasettrain_path = "C:/dataset/train"train_names = os.listdir(train_path)# empty list to hold feature vectors and train labelstrain_features = []train_labels = []# loop over the training datasetprint ("[STATUS] Started extracting haralick textures..")for train_name in train_names: cur_path = train_path + "/" + train_name cur_label = train_name i = 1 for file in glob.glob(cur_path + "/*.jpg"): print ("Processing Image - {} in {}".format(i, cur_label)) # read the training image image = cv2.imread(file) # convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # extract haralick texture from the image features = extract_features(gray) # append the feature vector and label train_features.append(features.reshape(1, -1))[0] train_labels.append(cur_label) # show loop update i += 1# have a look at the size of our feature vector and labelsprint ("Training features: {}".format(np.array(train_features).shape))print ("Training labels: {}".format(np.array(train_labels).shape))# create the classifierprint ("[STATUS] Creating the classifier..")clf_svm = LinearSVC(random_state = 9)# fit the training data and labelsprint ("[STATUS] Fitting data/label to model..")clf_svm.fit(train_features, train_labels)# loop over the test imagestest_path = "C:/dataset/test"for file in glob.glob(test_path + "/*.jpg"): # read the input image image = cv2.imread(file) # convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # extract haralick texture from the image features = extract_features(gray) # evaluate the model and predict label prediction = clf_svm.predict(features) # show the label cv2.putText(image, prediction, (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3) print ("Prediction - {}".format(prediction)) # display the output image cv2.imshow("Test_Image", image) cv2.waitKey(0)
我不知道我是否错误地使用了reshape()函数,还是遗漏了什么。
ValueError: Expected 2D array, got 1D array instead: array=[]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
回答:
请考虑以下几点:
-
您遇到上述错误是因为
train_features
在clf_svm.fit(train_features, train_labels)
这一行是一个空列表([])。它应该至少包含1个数据。这是因为train_path
指向一个只包含图像文件的文件夹,但上述代码假设train_path
指向一个至少包含1个子文件夹(不包含文件)的文件夹。train - class1_folder[class11.jpg, class12.jpg, ...] - class2_folder[class21.jpg, class22.jpg, ...] - and so on ...
在这里,您的训练数据的类名将是
[class1, class2, ...]
-
将
train_features.append(features.reshape(1, -1))[0]
这一行改正为train_features.append(features.reshape(1, -1)[0])
clf_svm.predict(features)
的输出是一个numpy数组。因此,在cv2.putText
函数中,将prediction
替换为str(prediction)
。您也可以将其替换为prediction[0]
。
尝试以下代码:
(此处省略了代码内容,因为代码不需要翻译)