gpt4 book ai didi

python - 属性错误 : 'LinearSVC' object has no attribute 'classes_'

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:30 26 4
gpt4 key购买 nike

此代码用于检测字符和绘制矩形,然后预测字符,但每次都会给我以下错误!

for rect in rects:
# Draw the rectangles
cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
# Make the rectangular region around the digit
leng = int(rect[3] * 1.6)
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
# Resize the image
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = cv2.dilate(roi, (3, 3))
# Calculate the HOG features
roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
ar=np.array([roi_hog_fd], 'float64')
nbr = clf.predict(ar)

Traceback (most recent call last):
File "performRecognition.py", line 43, in <module>
nbr = clf.predict(np.array([roi_hog_fd], 'float64'))
File "/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 341, in predict
return self.classes_[indices]
AttributeError: 'LinearSVC' object has no attribute 'classes_'

最佳答案

@Wineartist 显然出现此错误是因为我没有执行下面的生成分类器代码:

# Import the modules
from sklearn.externals import joblib
from sklearn import datasets
from skimage.feature import hog
from sklearn.svm import LinearSVC
import numpy as np
from collections import Counter

# Load the dataset
dataset = datasets.fetch_mldata("MNIST Original")

# Extract the features and labels
features = np.array(dataset.data, 'int16')
labels = np.array(dataset.target, 'int')

# Extract the hog features
list_hog_fd = []
for feature in features:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, 'float64')

print "Count of digits in dataset", Counter(labels)

# Create a linear SVM object
clf = LinearSVC()

# Perform the training
clf.fit(hog_features, labels)

# Save the classifier
joblib.dump(clf, "digits_cls.pkl", compress=3)

关于python - 属性错误 : 'LinearSVC' object has no attribute 'classes_' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42472362/

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