1

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

  • What do you expect to get after `map(int, training_set)`? For now you are trying to call `int` on `array([119, ..., 255], dtype=uint8)`. Of course it is wrong. – Aleksandr Kovalev Mar 20 '16 at 08:33
  • @AleksandrKovalev i am expecting that the list will be converted to int.What is the right method?i googled and i ended up in http://stackoverflow.com/questions/7368789/convert-all-strings-in-a-list-to-int –  Mar 20 '16 at 08:38
  • You have list of arrays. What do you want to get after conversion? Plain list of integers? Or list of lists of integers? – Aleksandr Kovalev Mar 20 '16 at 08:40
  • And btw, do you use python2 or python3? – Aleksandr Kovalev Mar 20 '16 at 08:44
  • @AleksandrKovalev I want trainining_set=list of lists,training_label=label of each list in training_set.when i directly give this svm.train(training_set,cv2.ml.ROW_SAMPLE,training_labels) i am getting error:TypeError: samples is not a numpy array, neither a scalar –  Mar 20 '16 at 08:45
  • @AleksandrKovalev python 2.7 –  Mar 20 '16 at 08:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106833/discussion-between-emmanu-varghese-and-aleksandr-kovalev). –  Mar 20 '16 at 08:46

1 Answers1

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.