Python 属性错误:';LinearSVC';对象没有属性';类别';

Python 属性错误:';LinearSVC';对象没有属性';类别';,python,opencv,numpy,Python,Opencv,Numpy,这段代码用于检测字符并绘制矩形,然后预测字符,但每次都会出现下面的错误 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 =

这段代码用于检测字符并绘制矩形,然后预测字符,但每次都会出现下面的错误

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)

回溯(最近一次呼叫最后一次):
文件“performRecognition.py”,第43行,在
nbr=clf.predict(np.array([roi\u hog\u fd],'float64'))
文件“/usr/local/lib/python2.7/dist packages/sklearn/linear_model/base.py”,第341行,在predict中
返回self.classes_u2;[索引]
AttributeError:“LinearSVC”对象没有属性“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)      

@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)      

请格式化您的代码,添加缩进您应该首先调用
clf.fit()
。谢谢!!我忘了先运行分类器代码!!!它起作用了now@AbdouElMesnaoui你能分享一下你使用的clf.fit()代码吗?我也在处理类似的代码库。@Wineartist我刚刚做了,下面请格式化您的代码,添加缩进。您应该首先调用
clf.fit()
。谢谢!!我忘了先运行分类器代码!!!它起作用了now@AbdouElMesnaoui你能分享一下你使用的clf.fit()代码吗?我也在处理类似的代码库。@Wineartist我刚刚做了,下面我尝试执行生成分类器代码,但出现了一些错误。从这行中得到第一个错误:dataset=datasets.fetch_mldata(“MNIST Original”)错误是urllib2.HTTPError:HTTP错误500:INTERNAL SERVER Error,因此我下载了mnist mat文件,并使用以下代码将其加载到数据集:dataset=datasets.load_digits('mnist-original.mat')。在执行此代码时,在此修复之后,我遇到了另一个错误。(下面复制)回溯(最后一次调用):文件“generateClassifier.py”,第22行,fd=hog(feature.reformate((32,32)),方向=9,每单元像素=(14,14),每块单元=(1,1),Visualize=False)值错误:无法将大小为64的数组重塑为形状(32,32)我也做了同样的操作,但是代码功能。重塑((32,32))生成一个值错误,表示无法将大小为64的数组重塑为形状(32,32)。我尝试执行生成分类器代码,但出现了一些错误。从这一行获得第一个错误:dataset=datasets.fetch_mldata(“MNIST Original”)错误是urllib2.HTTPError:HTTP错误500:INTERNAL SERVER Error,因此我下载了mnist mat文件,并使用以下代码将其加载到数据集:dataset=datasets.load_digits('mnist-original.mat')。在执行此代码时,在此修复之后,我遇到了另一个错误。(下面复制)回溯(最后一次调用):文件“generateClassifier.py”,第22行,fd=hog(feature.reformate((32,32)),方向=9,每单元像素=(14,14),每块单元=(1,1),Visualize=False)值错误:无法将大小为64的数组重塑为形状(32,32)我也做了同样的操作,但是代码特性.reformate((32,32))生成一个值错误,表示无法将大小为64的数组重新整形为形状(32,32)