Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To use OpenCV machine learning algorithms, you have to write some wrapper classes:</p> <p><strong>1. First parent class</strong></p> <pre><code>class StatModel(object): '''parent class - starting point to add abstraction''' def load(self, fn): self.model.load(fn) def save(self, fn): self.model.save(fn) </code></pre> <p><strong>2. Finally SvM wrapper:</strong></p> <pre><code>class SVM(StatModel): '''wrapper for OpenCV SimpleVectorMachine algorithm''' def __init__(self): self.model = cv2.SVM() def train(self, samples, responses): #setting algorithm parameters params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_C_SVC, C = 1 ) self.model.train(samples, responses, params = params) def predict(self, samples): return np.float32( [self.model.predict(s) for s in samples]) </code></pre> <p><strong>3.Example usage:</strong></p> <pre><code>import numpy as np import cv2 samples = np.array(np.random.random((4,2)), dtype = np.float32) y_train = np.array([1.,0.,0.,1.], dtype = np.float32) clf = SVM() clf.train(samples, y_train) y_val = clf.predict(samples) </code></pre> <p><strong>Setting parameters</strong></p> <p>Setting parameters is simple - just write a dictionary that holds the parameters as keys. You should look original documentation to see all possible parameters and allowed values: <a href="http://opencv.itseez.com/modules/ml/doc/support_vector_machines.html#cvsvmparams">http://opencv.itseez.com/modules/ml/doc/support_vector_machines.html#cvsvmparams</a></p> <p>Yes, possible values for svm_type and kernel_type are in C++, but there is easy way to convert those constants into Python representation, for example CvSVM::C_SVC is written as cv2.SVM_C_SVC in Python.</p> <p><strong>Prelude</strong> To get more wrappers for machine learning algorithms, look into <em>letter-recog.py</em> example in your opencv examples on disk or open url of OpenCV repository: <a href="https://github.com/Itseez/opencv/tree/master/samples/python2">https://github.com/Itseez/opencv/tree/master/samples/python2</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload