def trainSVMInMyWay(target, labels): print(len(target)) print(len(target[0])) # First half is trainData, remaining is testData train_cells = target ###### Now training ######################## trainData = np.float32(train_cells).reshape(-1, 400) #print trainData.shape responses = np.float32(labels)[:, np.newaxis] svm = cv2.SVM() svm.train(trainData, responses, params=svm_params) svm.save('svm_data.dat') return svm
def trainSVM(target, labels): print(len(target)) print(len(target[0])) # First half is trainData, remaining is testData train_cells = target ###### Now training ######################## deskewed = [map(deskew, row) for row in train_cells] hogdata = [map(hog, row) for row in deskewed] trainData = np.float32(hogdata).reshape(-1, 64) #print trainData.shape responses = np.float32(labels)[:, np.newaxis] svm = cv2.SVM() svm.train(trainData, responses, params=svm_params) svm.save('svm_data.dat') return svm
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'))
def store_svm(svm, path): svm.save(path)
# Set up SVM for OpenCV 3 svm = cv2.ml.SVM_create() # Set SVM type svm.setType(cv2.ml.SVM_C_SVC) # Set SVM Kernel to Radial Basis Function (RBF) #svm.setKernel(cv2.ml.SVM_RBF) # Set parameter C #svm.setC(C) # Set parameter Gamma #svm.setGamma(gamma) # Train SVM on training data svm.trainAuto(X_tr, cv2.ml.ROW_SAMPLE, y_tr) # Save trained model svm.save("apples_svm_model.yml") ''' svm = cv2.ml.SVM_load('apples_svm_model.yml') pred = svm.predict(X_te)[1] ''' a = [] for i in range(len(pred)): a.append(int(pred[i][0])) mask = a == y_te correct = np.count_nonzero(mask) print("svm model accuracy with HOG descriptor:", end=" ") print(correct * 100.0 / pred.size) validation_fruit_images = []
Cropped=image[y:y + h, x:x + w] resized_image = cv2.resize(Cropped, (64, 128)) # c=0 # while(c!=''): # if c"" if 'man' in j: L.append(0) if 'bnat' in j: L.append(1) h = hog.compute(resized_image) Big.append(h) print (h.mean(), L[-1]) BN = np.array(Big, np.float32) LN = np.array(L, np.int) print(BN.shape) print(LN.shape) svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_LINEAR) svm.train(BN, cv2.ml.ROW_SAMPLE, LN) svm.save('gender.dat') #filename = 'SVModel.sav' #pickle.dump(svm, open(filename, 'wb'))