BackupGGCode / pyopencv

Python bindings for OpenCV 2.x using Boost.Python and NumPy
1 stars 0 forks source link

SVM issues #41

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Try to replicate the C++ SVM example in pyopencv
2. Refactor C++ example to match PyOpenCV syntax
(See attached file, and code with notes at bottom)

What is the expected output? What do you see instead?
A match to the class '10', i.e. a matrix with the correct prediction(s)

What version of the product are you using? On what operating system?
Opencv2.1 on Ubuntu (Maverick).

Please provide any additional information below.

Below is the code example.  There really aren't enough tutorials for PyOpencv.  
Syntactic changes from the python library to the pyopen can be confusing.  If 
you wanted help creating a tutorial to aid the transition for some users, i 
would love to help out.

from pyopencv import *

if __name__ == "__main__":
    value=1  #Value starts at 1

    train_update = False #Set training parameters
    train_sample_count = 100
    train_sample_size = 10
        #Create the matrices for class and data.
    class_mat = Mat(train_sample_count, 1, CV_32FC1);
    data_mat= Mat(train_sample_count, train_sample_size, CV_32FC1);
    i=0
    j=0
    val=1
        #Add values to the class mat, and fill the data with the same values
    while i<train_sample_count:
        class_mat[i] = value
        while j<train_sample_size:
            data_mat[i*train_sample_size+ j] = val
            j+=1
        val+=1
        value+=1
        i+=1
        #Create a param model for the SVM
    x=CvSVMParams
    x.svm_type = 104;
    x.kernel_type = 2;
    x.gamma = 1./train_sample_size;
    x.nu = 0.5;
    x.C = 8;
    #Term criteria in C++ has child parameters (epsilon/count etc)
    x.term_crit =TermCriteria(1,50,0.001)
        #Changed to suit python TermCriteria format
        #Create the training SVM
    tSvm = CvSVM()
    tSvm.train(data_mat, class_mat, 0, 0, x);
    tSvm.save("filename.xml", 0);

    #Create predictive SVM which will classify the value '1' (val)
    val = 1
    pSvm = CvSVM();
    pSvm.load("filename.xml", 0);
        #Create Sample Matrix
    sample = Mat((1, train_sample_size), CV_32FC1);
    while i<train_sample_size:
        sample[i] = val
    pred = pSvm.predict(sample);
    print("\n %.3f", pred);

Original issue reported on code.google.com by john.mrd...@googlemail.com on 8 Mar 2011 at 12:36

Attachments:

GoogleCodeExporter commented 9 years ago
The error produced from this is......

Traceback (most recent call last):
  File "svm2.py", line 31, in <module>
    tSvm.train(data_mat, class_mat, 0, 0, x);
Boost.Python.ArgumentError: Python argument types in
    CvSVM.train(CvSVM, Mat, Mat, int, int, Boost.Python.class)
did not match C++ signature:
    train(CvSVM_wrapper {lvalue}, cv::Mat _train_data, cv::Mat _responses, cv::Mat _var_idx=Mat(), cv::Mat _sample_idx=Mat(), CvSVMParams _params=<pyopencv.ml_ext.CvSVMParams object at 0x12033f0>)
    train(CvSVM {lvalue}, cv::Mat _train_data, cv::Mat _responses, cv::Mat _var_idx=Mat(), cv::Mat _sample_idx=Mat(), CvSVMParams _params=<pyopencv.ml_ext.CvSVMParams object at 0x12033f0>)
    train(CvSVM {lvalue} inst, cv::Mat {lvalue} _train_data, cv::Mat {lvalue} _responses, cv::Mat _var_idx=Mat(), cv::Mat _sample_idx=Mat(), CvSVMParams _params=<pyopencv.ml_ext.CvSVMParams object at 0x1203190>)

Original comment by john.mrd...@googlemail.com on 8 Mar 2011 at 12:37

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Changing the CvSVMParams line to....

x=CvSVMParams()

Circumvents this issue.

In the C++ example each value of the Term Criteria is set independently as an 
attribute of the CvSVMParams.

However the attribute for epsilon etc, aren't attributes in Pyopencv?

This code produces this error.

Produces the error......

File "svm2.py", line 29, in <module>
    x.term_crit =TermCriteria(1,50,0.001)
AttributeError: can't set attribute

Are the values supposed to be passed to the SVM upon instantiation? Or does the 
CvSVMParams class need to be configured first?

Kind regards,

Daniel

Original comment by john.mrd...@googlemail.com on 8 Mar 2011 at 1:31