def compute(self, X, y): [D, self.W, self.mu] = fisherfaces(asRowMatrix(X),y, self.num_components) # store labels self.y = y # store projections for xi in X: self.projections.append(project(self.W, xi.reshape(1,-1), self.mu))
def compute(self, X, y): [D, self.W, self.mu] = fisherfaces(asRowMatrix(X), y, self.num_components) # store labels self.y = y # store projections for xi in X: self.projections.append(project(self.W, xi.reshape(1, -1), self.mu))
def predict ( self , X): minDist = np . finfo (’float ’) . max minClass = -1 Q = project ( self .W , X. reshape (1 , -1) , self . mu ) for i in xrange ( len ( self . projections )): dist = self . dist_metric ( self . projections [ i], Q) if dist < minDist : minDist = dist minClass = self . y[i] return minClass
def predict(self, X): minDist = np.finfo('float').max minClass = -1 Q = project(self.W, X.reshape(1,-1), self.mu) for i in xrange(len(self.projections)): dist = self.dist_metric(self.projections[i], Q) if dist < minDist: minDist = dist minClass = self.y[i] return minClass
def predict(self, X, minDist=0.1): ## minDist = np.finfo('float').max ## minDist = 0.1 ## print "min dist: %f"%minDist minClass = -1 Q = project(self.W, X.reshape(1, -1), self.mu) for i in xrange(len(self.projections)): dist = self.dist_metric(self.projections[i], Q) if dist < minDist: minDist = dist ## print "min dist final: %f"%minDist minClass = self.y[i] ## break return minClass, minDist
def visualize(self): # Using PCA (Principal Components Analysis) to recognize faces # Face recognizer just in openCV version 2.4x, not in version 3.0 (alpha + beta) # cv2.createEigenFaceRecognizer() [X, y] = read_images(self._facePath) # get k principal components [D, W, mu] = pca(as_row_matrix(X), y) # to check how many principal components (eigenfaces) - which has the same dimension with observed vector # print(len(W[1, :])) # print(len(W[:, 1])) # Visualize eigenfaces E = [] # take at most 16 people for i in xrange(min(len(X), 16)): e = W[:, i].reshape(X[0].shape) # normalize value of eigenvectors in [0, 255] to display on image E.append(normalize(e, 0, 255)) # display k principal components (k eigenvectors) # eigenfaces encode facial features + illumination of image subplot(title="Eigencefaces AT&T", images=E, rows=4, cols=4, sptitle="Eigenface", colormap=cm.gray, filename="pca_eigenface.png") # Reconstructor image from eigenfaces # array of vector number [10, 30, 50 ..., min(len(X), 320)] steps = [i for i in xrange(10, min(len(X), 320), 20)] E = [] # take at most 16 eigenvectors of first person for i in xrange(min(len(steps), 16)): num_eigenvectors = steps[i] # project each vector x1, x2, ... xn to num_eigenvectors P = project(W[:, 0:num_eigenvectors], X[0].reshape(1, -1), mu) # reconstruct image from num_eigenvectors R = reconstruct(W[:, 0:num_eigenvectors], P, mu) R = R.reshape(X[0].shape) E.append(normalize(R, 0, 255)) # reconstruct image from k (10, 30, ...) eigenvector subplot(title="Reconstruction AT&T", images=E, rows=4, cols=4, sptitle="EigenVectors", sptitles=steps, colormap=cm.gray, filename="pca_reconstruction.png")
# images ( note : eigenvectors are stored by column !) E = [] for i in xrange(min(W.shape[1], 16)): e = W[:, i].reshape(X[0].shape) E.append(normalize(e, 0, 255)) # plot them and store the plot to " python_fisherfaces_fisherfaces . pdf " subplot(title=" Fisherfaces AT&T Facedatabase ", images=E, rows=4, cols=4, sptitle="Fisherface ", colormap=cm.jet, filename=" python_fisherfaces_fisherfaces.jpg") EE = [] for i in xrange(min(W.shape[1], 20)): e = W[:, i].reshape(-1, 1) P = project(e, X[0].reshape(1, -1), mu) R = reconstruct(e, P, mu) R = R.reshape(X[0].shape) EE.append(normalize(R, 0, 255)) subplot(title=" Fisherfaces Reconstruction Yale FDB ", images=EE, rows=4, cols=5, sptitle=" Fisherface", colormap=cm.gray, filename="python_fisherfaces_reconstruction.jpg")
def compute(self, X, y): [D, self.W, self.mu] = pca(as_row_matrix(X), y, self.num_components) self.y = y for xi in X: self.projections.append(project(self.W, xi.reshape(1, -1), self.mu))