Python implementation of PCA face recognition

Source: Internet
Author: User

These days to see the PCA and face recognition of the process, and search the corresponding Python code on the network, there is, but the code quality is not good, so they re-write the next, for the att_faces data set recognition rate can reach 92.5%~98.0% (40 types, each randomly selected 5 training, 5 recognition), all code below, less than 50 lines oh.

#-*-Coding:utf-8-*-import numpy as Npimport os, glob, Random, Cv2def PCA (data,k): data = Np.float32 (Np.mat (data))    Rows,cols = data.shape #取大小 Data_mean = Np.mean (data,0) #求均值                                       Z = Data-np.tile (Data_mean, (rows,1)) D,v = Np.linalg.eig (z*z.t) #特征值与特征向量 V1 = v[:,: K] #取前k个特征向量 V1 = Z.t*v1 for i in Xrange (k): #特征向 Volume normalization V1[:,i]/= np.linalg.norm (V1[:,i]) return Np.array (Z*V1), Data_mean,v1def loadimageset (folder=u ' e:/Thunderbolt download/face Process/att_faces ', samplecount=5): #加载图像集, randomly select Samplecount images for training traindata = []; TestData = []; Ytrain=[];    Ytest = []; For k in range: Folder2 = os.path.join (folder, ' s%d '% (k+1)) data = [Cv2.imread (D.encode (' GBK '), 0) for D in Glob.glob (Os.path.join (Folder2, ' *.PGM '))] Sample = Random.sample (range (ten), Samplecount) Traindata.ext End ([Data[i].ravel () For I in range (ten) if I in Sample]) Testdata.extend ([Data[i].ravel ()-I in range () If I is not in sample]) Ytest.extend ([k]* (10-samplecount)) ytrain.extend ([k]* Samplecount) return Np.array (Traindata), Np.array (YTra IN), Np.array (TestData), Np.array (Ytest) def main (): Xtrain_, Ytrain, xtest_, ytest = Loadimageset () Num_train, num_ Test = Xtrain_.shape[0], xtest_.shape[0] xtrain,data_mean,v = PCA (Xtrain_,) xTest = Np.array ((Xtest_-np.tile ( Data_mean, (num_test,1))) * V) #得到测试脸在特征向量下的数据 ypredict =[ytrain[np.sum ((Xtrain-np.tile (d, (num_train,1)) **2, 1). Argmin ()] for D in xTest] print U ' European distance method recognition rate:%.2f%% '% ((ypredict = = Np.array (ytest)). Mean () *100) SVM = Cv2. SVM () #支持向量机方法 Svm.train (Np.float32 (Xtrain), Np.float32 (ytrain), params = {' Kernel_type ': C V2. Svm_linear}) ypredict = [Svm.predict (d) for D in Np.float32 (xTest)] #yPredict = Svm.predict_all (Xtest.astype (Np.floa T64)) print U ' support vector machine recognition rate:%.2f%% '% (Ypredict = = Np.array (ytest)). Mean () *100) if __name__ = = ' __main__ ': Main () 

  

Python implementation of PCA face recognition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.