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)
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
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)))
labels.append(0) test = [] play = [1, 2, 3] test.append(play) print(type(hist)) # print(hist.type()) # samples = np.array([np.float32(j) for j in samples]) samples = np.float32(test) labels = np.array(labels) svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_RBF) svm.setGamma(5.383) svm.setC(2.67) svm.train(samples, cv2.ml.ROW_SAMPLE, labels) # print(samples) # [l.tolist() for l in samples] # print(samples) # samples = np.float32(samples) # # print("samples", samples) # [print("labels", labels)] # positive_path = "archive/train/ben_afflek" # # img = cv2.imread(positive_path) # img = cv2.imread(glob.glob(os.path.join(positive_path, '*jpg'))[0])