I currently have the same problem and I saw the following document from OpenCV:
OCR handwritten data using SVM
Where you can find part of your answer:
deskewed = [map(deskew,row) for row in train_cells]
hogdata = [map(hog,row) for row in deskewed]
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis])
svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setType(cv2.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
svm.train(trainData, cv2.ml.ROW_SAMPLE, responses)
svm.save('svm_data.dat')
That is, I work. As soon as I solved it, I will update the answer. But at this point, I hope this helps you.
.................................................. ..........................
opencv digits.py:
\ OpenCV\Sources\\
opencv SVM. opencv 3.1.
svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_RBF)
svm.setGamma(5.383)
svm.setC(2.67)
svm.train(samples_train, cv2.ml.ROW_SAMPLE, labels_train)
resp = svm.predict(samples_test)[1].ravel()
print resp, labels_test
err = (labels_test != resp).mean()
print('error: %.2f %%' % (err*100))
confusion = np.zeros((10, 10), np.int32)
for i, j in zip(labels_test, resp):
confusion[i, j] += 1
print('confusion matrix:')
print(confusion)
print()
............................................................................
, :
samples = []
labels = []
for filename in glob.glob(os.path.join(positive_path, '*.jpg')):
img = cv2.imread(filename, 1)
hist = hog(img)
samples.append(hist)
labels.append(1)
for filename in glob.glob(os.path.join(negative_path, '*.jpg')):
img = cv2.imread(filename, 1)
hist = hog(img)
samples.append(hist)
labels.append(0)
samples = np.float32(samples)
labels = np.array(labels)
rand = np.random.RandomState(321)
shuffle = rand.permutation(len(samples))
samples = samples[shuffle]
labels = labels[shuffle]
svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_RBF)
svm.setGamma(5.383)
svm.setC(2.67)
svm.train(samples, cv2.ml.ROW_SAMPLE, labels)
svm.save('svm_data.dat')
.