1. By capturing hand gestures in the air, image feature extraction and PCA feature dimensionality reduction are carried out by convolution, and finally classification is carried out by SVM. The specific code is as follows, through
sklearn, numpy and other related libraries.
#!usr/bin/python3 # -*- coding:utf-8 -*- # author:SingWeek import cv2 import numpy as np import os from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix from sklearn.decomposition import PCA from sklearn.svm import SVC from sklearn.externals import joblib class SVM(object): def __init__(self,svmfile="svm.model",pcafile="pca.model"): """ //Initialization parameters :param svmfile: SVM Name of the model save"svm.model" :param pcafile: PCA The name of the model save,"pca.model" """ self.svmfile=svmfile self.pcafile=pcafile def Load_Svm(self): self.clf=joblib.load(self.svmfile)#Loading model def Load_Pca(self): self.pca=joblib.load(self.pcafile)#Loading model def Train_Pca(self,X_Train,n_components = 100): """ PCA model training :param X_Train:Training data :param n_components: Characteristic number PCA Number of principal components to be retained in the algorithm n,That is, the number of features retained n :return: None """ self.pca = PCA(svd_solver='auto', n_components=n_components, whiten=True).fit(X_Train) # Training a pca model joblib.dump(self.pca, self.pcafile) # Preservation model def Train(self,X_train, Y_train): """ SVM model training :param X_train:Training data :param Y_train: Training label :return: None """ X_train=self.Pca_X(X_train)#data conversion param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5], # C is the punishment for mistakes 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], } # gamma How many feature points in kernel function will be used}#Try different values for parameters self.clf = GridSearchCV(SVC(kernel='rbf'), param_grid) self.clf.fit(X_train, Y_train) joblib.dump(self.clf, self.svmfile) # Preservation model def Split_Data(self,X,Y,test_ratio=0.3): """ //Training data segmentation :param X:data :param Y:Data labels :param test_ratio:Test set share :return:X_train, X_test, Y_train, Y_test: Training data, test data, training data label, test data label """ X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=test_ratio) # Select test set of test ratio return X_train, X_test, Y_train, Y_test def Print_Score(self,X_Test,Y_Test): """ //Model score :param X_Test: test data :param Y_Test: Test data label :return: Matrix scoring """ target_names = ['0', '1', '2', '3', '4', '5', '6'] n_classes = 7 X_Test=self.Pca_X(X_Test) Y_pred=self.clf.predict(X_Test) print(classification_report(Y_Test, Y_pred, target_names=target_names))#Test report matrix=confusion_matrix(Y_Test, Y_pred, labels=range(n_classes)) print(matrix)#Matrix scoring return matrix def Pca_X(self,X_Test): """ PCA Dimensionality reduction :param X_Test: Input dimension reduction data :return: Data after dimension reduction """ return self.pca.transform(X_Test) def Conv_Pooling(self,frame): """ //Image feature extraction :param frame:Convolution pool image :return:Image 20 after convolution pooling*20 """ # fil = np.array([[1, 1, 1], [1, -2, 1], [-1, -1, -1]]) fil1 = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]) # fil2 = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) fil3 = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) frame = cv2.filter2D(frame, -1, fil1) # Convolution, high pass filtering frame = cv2.resize(frame, (140, 140)) # Equivalent to pooling frame = cv2.resize(frame, (80, 80)) # Equivalent to pooling frame = cv2.filter2D(frame, -1, fil3) # Convolution, edge feature extraction frame = cv2.resize(frame, (40, 40)) frame = cv2.resize(frame, (20, 20)) return frame def Prect(self,img): """ //Image classification and prediction :param img: Input the image to be classified :return:Recognition result,0,1,2,3,4,5,6 """ X_Test=self.Conv_Pooling(img).reshape(400) X_Test=np.array([X_Test],dtype='float32') X_Test=self.Pca_X(X_Test) return self.clf.predict(X_Test)[0] def Load_Data(self,file): """ //Read image from folder and transform feature extraction :param file:Total file name of training data :return: Converted one-dimensional image features """ listsx = [] listsy = [] for i in os.listdir(file):#Directory hierarchy, category classfile = file + i + '/' for j in os.listdir(classfile):#Locate a specific data read filename = classfile + j img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) Conv_out = self.Conv_Pooling(img)#Convolution feature extraction, scaling and dimensionality reduction Conv_out = Conv_out.reshape(400)#Two to one listsx.append(Conv_out) listsy.append(int(i)) listsx = np.array(listsx, dtype='float32') return listsx, listsy
2. After dividing training set and test set by 3 / 7, relevant printing parameters are as follows:
3. The gesture data set collected by myself contains 0,1,2,3,4,5, and the number of 6 is 46895428128234644885. The sixth type is negative sample https://download.csdn.net/download/zx520113/12116992
4. Reference: https://www.cnblogs.com/albert-yzp/p/9525162.html
5. Summary: in practical application, different speed of hand movement in the air will cause ghosting, which will affect the recognition between 1 and 2, 2 and 3, 3 and 4, and the recognition between 1 and 0 will also be affected if 0 scale is faster. More will be improved in the follow-up, increase the data set, and control the feature relationship between different categories.
6. Simple test training code needs to be modified according to its own file path.
# !usr/bin/python3 # -*- coding:utf-8 -*- # author:SingWeek from package import * svm=SVM()#Model definition x,y=svm.Load_Data("./TrainData/")#Data loading x_train,x_test,y_train,y_test=svm.Split_Data(x,y,0.3)#Data segmentation svm.Train_Pca(x)#train svm.Train(x_train,y_train)#train # svm.Load_Svm() # svm.Load_Pca() svm.Print_Score(x_test,y_test)#score #Forecast img=cv2.imread("E:\\Write\\2\\879.jpg",cv2.IMREAD_GRAYSCALE) img=svm.Conv_Pooling(img) result=svm.Prect(img) print(result)