Пример #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
Пример #2
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
Пример #3
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)
Пример #4
0

if __name__ == '__main__':
    train_img, train_labels = load_images("train", "train_labels.csv")
    test_img, test_labels = load_images("test", "test_labels.csv")

    train_labels_int = encode_labels(train_labels)
    test_labels_int = encode_labels(test_labels)

    train_img = resize_images(train_img, (128, 128))
    test_img = resize_images(test_img, (128, 128))

    features_train = extract_features(train_img)
    features_test = extract_features(test_img)

    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.setC(100)
    svm.setGamma(0.1)
    svm.train(np.array(features_train), cv2.ml.ROW_SAMPLE,
              np.array(train_labels_int))

    predicted = svm.predict(np.array(features_test, np.float32))

    result = []
    for p in predicted[1]:
        result.append(int(p[0]))

    print("Acurracy: " + str(count_accuracy(result, test_labels_int)))
Пример #5
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