Exemplo n.º 1
0
def create_svm(svm_options):
    svm = cv2.ml.SVM_create()
    svm.setType(svm_options.svm_type)
    svm.setKernel(svm_options.kernel_type)
    svm.setDegree(svm_options.degree)
    svm.setGamma(svm_options.gamma)
    svm.setC(svm_options.C)
    svm.setP(svm_options.e)
    svm.setTermCriteria((cv2.TERM_CRITERIA_COUNT + cv2.TERM_CRITERIA_EPS, svm_options.max_iter, 1e-09))
    return svm
 def __init__(self):
     svm = cv2.ml.SVM_create()
     svm.setType(cv2.ml.SVM_C_SVC)
     svm.setKernel(cv2.ml.SVM_LINEAR)  # (cv2.ml.SVM_RBF)
     # svm.setDegree(0.0)
     svm.setGamma(5.383)
     # svm.setCoef0(0.0)
     svm.setC(2.67)
     # svm.setNu(0.0)
     # svm.setP(0.0)
     # svm.setClassWeights(None)
     super().__init__(svm)
Exemplo n.º 3
0
def create_svm(svm_options):
    """
    Create an instance of SVM with svm_options.
    
    :param svm_options: SVM options.
    :return: An instance of OpenCV SVM model.
    """
    svm = cv2.ml.SVM_create()
    svm.setType(svm_options.svm_type)
    svm.setKernel(svm_options.kernel_type)
    svm.setDegree(svm_options.degree)
    svm.setGamma(svm_options.gamma)
    svm.setC(svm_options.C)
    svm.setP(svm_options.e)
    svm.setTermCriteria((cv2.TERM_CRITERIA_COUNT + cv2.TERM_CRITERIA_EPS, svm_options.max_iter, 1e-09))
    return svm
Exemplo n.º 4
0
def main():

    hog = cv2.HOGDescriptor()

    #GET IMAGES AND THEIR LABEL
    print("Loading pictures...")
    train_img, train_labels = get_images("train", "train_labels.csv")
    test_img, test_labels = get_images("test", "test_labels.csv")
    print("Loaded...")
    #RESIZE
    print("Resizing images...")
    train_img = resize_images(train_img)
    test_img = resize_images(test_img)
    print("Resized...")
    #MAP LABEL TO INT
    train_labels = list(map(strToNumberLabels, train_labels))
    test_labels = list(map(strToNumberLabels, test_labels))
    #EXTRACT FEATURES
    print("Before hog extraction")
    features_train = hog_compute(hog, train_img)
    features_test = hog_compute(hog, test_img)

    print("passed hog extraction")
    #
    trainingDataMat = np.array(features_train)
    labelsMat = np.array(train_labels)

    svm = cv2.ml.SVM_create()
    svm.setType(cv2.ml.SVM_C_SVC)
    svm.setKernel(cv2.ml.SVM_LINEAR)

    svm.setTermCriteria((cv2.TERM_CRITERIA_COUNT, 100, 1.e-10))

    svm.train(trainingDataMat, cv2.ml.ROW_SAMPLE, labelsMat)
    sample_data = np.array(features_test, np.float32)

    svm.setC(100)
    #svm.setGamma(0.1)
    print("Training model...")
    svm.train(trainingDataMat, cv2.ml.ROW_SAMPLE, labelsMat)
    response = svm.predict(sample_data)
    final = []
    for y in response[1]:
        final.append(int(y[0]))
    countAccuracy(final, test_labels)
Exemplo n.º 5
0
def OpenCvSVM(X,y):
    X_train,X_test,y_train,y_test=train_test_split(X, y, test_size=0.2,random_state=1)
    X_train=np.array(X_train,dtype=np.float32)
    X_test=np.array(X_test,dtype=np.float32)
    y_test=np.array(y_test,dtype=np.int32)
    sc=StandardScaler()
    sc.fit(X_train)
    X_train=sc.transform(X_train)
    X_test=sc.transform(X_test)
    svm = cv2.ml.SVM_create()
    svm.setType(cv2.ml.SVM_C_SVC)
    svm.setKernel(cv2.ml.SVM_LINEAR)
    svm.train(X_train, cv2.ml.ROW_SAMPLE, y_train)
    y_predict = svm.predict(X_test)
    y_pred=np.zeros(len(y_predict[1]))
    for i in range(len(y_predict[1])):
        y_pred[i]=y_predict[1][i][0]
    return y_pred,y_test
Exemplo n.º 6
0
    if 'sad' in j:
        L.append(1)
    if 'angry' in j:
        L.append(2)
    if 'surprise' in j:
        L.append(3)
    if 'natural' in j:
        L.append(4)
    if 'fear' in j:
        L.append(5)
    if 'disgust' in j:
        L.append(6)

    h = hog.compute(resized_image)
    Big.append(h)

svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)

BN = np.array(Big, np.float32)
LN = np.array(L, np.int)
print(BN.shape)

print(LN.shape)
svm.train(BN, cv2.ml.ROW_SAMPLE, LN)

svm.save('emotions.dat')
#filename = 'SVModel.sav'
#pickle.dump(svm, open(filename, 'wb'))
Exemplo n.º 7
0
def create_svm():
    svm = cv2.ml.SVM_create()
    svm.setType(cv2.ml.SVM_C_SVC)
    svm.setKernel(cv2.ml.SVM_LINEAR)
    svm.setTermCriteria((cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6))
    return svm