-
Notifications
You must be signed in to change notification settings - Fork 0
/
alttraining.py
95 lines (72 loc) · 2.29 KB
/
alttraining.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import numpy.linalg as linalg
import Image
import os
class Faces:
def __init__(self):
self.listfaces = []
self.faces = None
self.meanface = None
self.differencefaces = None
self.covmatrix = None
self.eigenfaces = None
self.weights = None
def get_frame_vector(self, video_frame, flatten=True):
im = Image.open(video_frame)
# convert to grayscale
im_gray = im.convert('L')
# convert to a matrix
im_matrix = np.matrix(im_gray)
if flatten == True:
return im_matrix.flatten('F')
else:
return im_matrix
def get_face_images(self):
# get all files of image directory
images = os.listdir(os.getcwd() + "/picturesofjames")
# get rid of the .DS_Store file
images.pop(0)
# add each vector to the list
for i in images:
self.listfaces.append(self.get_frame_vector("picturesofjames/" + i).T)
# compress vector list into one matrix
self.faces = np.concatenate(self.listfaces, axis = 1)
def mean_face(self):
self.meanface = np.mean(self.faces, axis = 1)
def difference_faces(self):
self.differencefaces = self.faces - self.meanface
def covariance(self):
self.covmatrix = self.differencefaces.T * self.differencefaces
def get_eigenfaces(self):
# get eigenvalues and eigenvectors
eigenvalues, eigenvectors = linalg.eig(self.covmatrix)
# find the order of the eigenvalues in descending order
order = eigenvalues.argsort()[::-1]
# sort eigenvalues and eigenvectors according to most significant eigenvalues
eigenvalues = eigenvalues[order]
eigenvectors = eigenvectors[:,order]
# get only the 10 most significant eigenvectors
eigenvectors = eigenvectors[:,:10]
# create the eigenfaces
eigenfaces = self.differencefaces * eigenvectors
# normalize each eigenface
# get the tranpose
temp = eigenfaces.T
vectors = []
for face in temp:
# get the length of the row and divide each row by it
length = linalg.norm(face)
nface = face / length
vectors.append(nface)
# get the transpose of the normalized eigenfaces
neigenfaces = np.concatenate(vectors).T
self.eigenfaces = neigenfaces
def get_weights(self):
self.weights = self.eigenfaces.T * self.faces
def main2(self):
self.get_face_images()
self.mean_face()
self.difference_faces()
self.covariance()
self.get_eigenfaces()
self.get_weights()