Python OpenCV SVM implementation

后端 未结 2 1646
挽巷
挽巷 2021-01-31 23:03

So I have a matrix with my sample images (all turned into vectors) which was run trough PCA/LDA, and a vector which denotes the class each images belongs to. Now I want to use t

2条回答
  •  情书的邮戳
    2021-01-31 23:13

    Adapted from timgluz version, but uses "train_auto" instead of "train". cv2 will find parameters "C", "gamma", ... for us.

    import cv2
    import numpy as np
    
    class Learn:
        def __init__(self, X, y):
            self.est = cv2.SVM()
            params = dict(kernel_type=cv2.SVM_LINEAR, svm_type=cv2.SVM_C_SVC)
            self.est.train_auto(X, y, None, None, params, 3) #kfold=3 (default: 10)
    
        def guess(self, X):
            return np.float32( [self.est.predict(s) for s in X])
    
    X = np.array(np.random.random((6,2)), dtype = np.float32)
    y = np.array([1.,0.,0.,1.,0.,1.], dtype = np.float32)
    g = Learn(X,y).guess(X)
    

提交回复
热议问题