Ask Your Question

kuntoro's profile - activity

2017-05-24 22:06:28 -0600 commented answer Python SVM Function svm.predict(testData) Returns Tuple instead of Numpy Array
2017-05-24 21:54:48 -0600 commented answer Python SVM Function svm.predict(testData) Returns Tuple instead of Numpy Array

I'm not so familiar with the environment. However, very well, I'll try to post there.

2017-05-19 20:54:11 -0600 received badge  Editor (source)
2017-05-19 19:49:59 -0600 asked a question Python SVM Function svm.predict(testData) Returns Tuple instead of Numpy Array

Hi,

I have searched through this forum about this problem and I found no result. I wish that this question has never been asked before. I am very sorry if it has.

I test machine learning sample code from http://docs.opencv.org/3.2.0/dd/d3b/tutorial_py_svm_opencv.html and it returns an error:

cv2.error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\  src\svm.cpp:1618: error: (-5) in the case of classification problem the response  s must be categorical; either specify varType when creating TrainData, or pass i  nteger responses in function cv::ml::SVMImpl::train

I tried to fixed it by changing the response type from float to int, from

responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis])

to

responses = np.repeat(np.arange(10),250)[:,np.newaxis]

However, the outcome of the classification is still not shown properly.

After some investigation, I found a solution. This function call

result = svm.predict(testData)

does not return an array of result but it returns a tuple instead. I believe this is the output of print(result)

(0.0, array([[ 0.],
       [ 0.],
       [ 0.],
       ...,
       [ 9.],
       [ 9.],
       [ 9.]], dtype=float32))

So, instead of writing

mask = result==responses

the solution that worked for me is

mask = result[1] == responses

I don't know, but maybe the tutorial needs update. My question is, what value is the first element of the tuple which returned by svm.predict(testData) ? I have tried to find documentation but unfortunately I couldn't find any information about this function.

Thank you very much for your kind help.

2016-08-29 21:16:14 -0600 received badge  Scholar (source)
2016-08-29 10:03:02 -0600 asked a question CV_8UC3 returns grayscale

Excuse me, I'm new to opencv. I learned about CV_8UC3 that it defines an image in 8 bit unsigned integers using 3 channels (BGR). When a color image is loaded using this constant as parameter,

cv::Mat image = cv::imread("image_file.jpg", CV_8UC3);

does that mean that it instructs to load the image as color image? I tried that to load and display using cv::namedWindow, as a result, the image is displayed in grayscale instead. But when the parameter CV_8UC3 is removed, the color image is loaded and displayed normally (as color image). Do I misunderstand the concept or is there (probably) a bug in my code?

Thank you very much. (I'm sorry if my English is not good)