问题描述
我目前想在一个 python 文件中创建 SIFT 方法和 KNN 分类器。这是我的代码。
from sklearn.model_selection import train_test_split
from imutils import paths
import numpy as np
import imutils
import cv2
import os
import pickle
def image_preprocessing(image,size = (32,32)):
image = cv2.resize(image,size,interpolation = cv2.INTER_AREA)
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image,(5,5),0)
return image.flatten()
def extract_sift_descriptors(image):
sift = cv2.xfeatures2d.SIFT_create()
kp,des = sift.detectAndCompute(image,None)
return des
dataset = "C:\\cats_breeds_classification_oxford_dataset\\images1"
imagePaths = list(paths.list_images(dataset))
rawImages = []
des_list = []
labels = []
for (i,imagePath) in enumerate(imagePaths):
image = cv2.imread(imagePath)
label = imagePath.split(os.path.sep)[1]
pixels = image_preprocessing(image)
des = extract_sift_descriptors(image)
rawImages.append(pixels)
des_list.append(des)
labels.append(label)
if i > 0 and i % 10 == 0:
print("[INFO] processed {}/{}".format(i,len(imagePaths)))
rawImages = np.array(rawImages)
des_list = np.array(des_list,dtype = object)
labels = np.array(labels)
(trainRI,testRI,trainRL,testRL) = train_test_split(
rawImages,labels,test_size=0.30,random_state=42)
(trainFeat,testFeat,trainLabels,testLabels) = train_test_split(
des_list,test_size=0.30)
print("[INFO] evaluating raw pixel accuracy...")
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(trainRI,trainRL)
acc = knn.score(testRI,testRL)
print("[INFO] raw pixel accuracy: {:.2f}%".format(acc * 100))
print("[INFO] evaluating histogram accuracy...")
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(trainFeat,trainLabels)
acc = knn.score(testFeat,testLabels)
print("[INFO] histogram accuracy: {:.2f}%".format(acc * 100))
pkl_filename = "pickle_knn.pkl"
with open(pkl_filename,'wb') as file:
pickle.dump(knn,file)
with open(pkl_filename,'rb') as file:
pickle_model = pickle.load(file)
predict = pickle_model.predict(testFeat)
但是我收到了这个错误。
ValueError: setting an array element with a sequence.
我明白这个错误,但我不知道如何解决它。我一直在寻找解决方案,但没有一个成功。
[INFO] processed 10/100
[INFO] processed 20/100
[INFO] processed 30/100
[INFO] processed 40/100
[INFO] processed 50/100
[INFO] processed 60/100
[INFO] processed 70/100
[INFO] processed 80/100
[INFO] processed 90/100
[INFO] evaluating raw pixel accuracy...
[INFO] raw pixel accuracy: 100.00%
[INFO] evaluating histogram accuracy...
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\AndroidStudioProjects\DesignInterface\app\src\main\python\sift_python.py",line 63,in <module>
knn.fit(trainFeat,trainLabels)
File "C:\Users\user\AppData\Roaming\Python\python39\site-packages\sklearn\neighbors\_base.py",line 1131,in fit
X,y = self._validate_data(X,y,accept_sparse="csr",File "C:\Users\user\AppData\Roaming\Python\python39\site-packages\sklearn\base.py",line 432,in _validate_data
X,y = check_X_y(X,**check_params)
File "C:\Users\user\AppData\Roaming\Python\python39\site-packages\sklearn\utils\validation.py",line 72,in inner_f
return f(**kwargs)
File "C:\Users\user\AppData\Roaming\Python\python39\site-packages\sklearn\utils\validation.py",line 795,in check_X_y
X = check_array(X,accept_sparse=accept_sparse,File "C:\Users\user\AppData\Roaming\Python\python39\site-packages\sklearn\utils\validation.py",line 598,in check_array
array = np.asarray(array,order=order,dtype=dtype)
File "C:\Users\user\AppData\Roaming\Python\python39\site-packages\numpy\core\_asarray.py",line 83,in asarray
return array(a,dtype,copy=False,order=order)
ValueError: setting an array element with a sequence.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)