gpt4 book ai didi

python - "Bad argument (train data must be floating-point matrix)"错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:52:33 25 4
gpt4 key购买 nike

我正在开发图像分类器。我将特征提取为 pca。我的示例代码是

for file in listing1:
img = cv2.imread(path1 + file)
res=cv2.resize(img,(250,250))
gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
xarr=np.squeeze(np.array(gray_image).astype(np.float32))
m,v=cv2.PCACompute(xarr)
training_set.append(v)
training_labels.append(1)

trainData=np.float32(np.float32(xi) for xi in training_set)
responses=np.float32(training_labels)
svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')

但是在训练期间我收到了这个错误:

OpenCV Error: Bad argument (train data must be floating-point matrix) in cvCheckTrainData, file ..\..\..\..\opencv\modules\ml\src\inner_functions.cpp, line 857

最佳答案

解决方案这对我有用您需要将其转换为 np 数组并展平该数组。

for file in listing1:
img = cv2.imread(path1 + file)
res=cv2.resize(img,(250,250))
gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
xarr=np.squeeze(np.array(gray_image).astype(np.float32))
m,v=cv2.PCACompute(xarr)
arr= np.array(v)
flat_arr= arr.ravel()
training_set.append(flat_arr)
training_labels.append(1)

训练

trainData=np.float32(training_set)
responses=np.float32(training_labels)
svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')

关于python - "Bad argument (train data must be floating-point matrix)"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36216887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com