问题
I am using opencv3.0,My IDE is pycharm
I have two lists one list of training_set and one list of trainig_labels. training_set is a list of lists like
[array([119, 122, 91, ..., 185, 80, 255], dtype=uint8), array([112, 106, 120, ..., 121, 138, 255], dtype=uint8), ....... ]
training_labels is list of labels for each list in training_set.training_labels looks like
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
But when i try to pass this to svm.train()
svm = cv2.ml.SVM_create()
trainingdata = map(int, training_set)
responses = map(int, training_labels)
svm.train(trainingdata,responses,params=svm_params)
svm.save('svm_data.dat')
I am getting this error
trainingdata = map(int, training_set)
TypeError: only length-1 arrays can be converted to Python scalars
How to format my data correctly before giving it as input to svm.train()
回答1:
Finaly i got the answer.I switched to opencv2.4.I modifed my code as
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')
Now every thing is perfect.
来源:https://stackoverflow.com/questions/36110924/how-can-i-format-my-list-to-give-it-as-input-to-svm-train-in-opencv3-0