-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEigenFace.py
66 lines (49 loc) · 1.73 KB
/
EigenFace.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
# Necessary modules imported
import numpy as np
import sys
import cv2
import os
# Constants
dim = (32, 32) # cv2.imread(trainList[0], 0).shape
def loadListFromFile(filePath):
return map(lambda path: path.strip(), open(filePath, 'r').readlines())
def train(trainList):
trainset = [ np.ravel(cv2.imread(path, 0)) for path in trainList ]
N = len(trainset)
# Computing mean image
mean = np.mean(trainset, 0)
# print N, np.array(trainset).shape, mean.shape
cov = lambda mat: np.dot(np.reshape(mat-mean, (-1, 1)), np.reshape(mat-mean, (1, -1)))
C = (1.0/N)*np.array(np.sum((map(cov, trainset)), 0))
w, v = np.linalg.eig(C)
return mean, w, v
def computeCoeff(img, mean, eiVecs):
imgR = np.ravel(cv2.resize(img, dim)) - mean
coeff = map(lambda v: np.dot(v, imgR), eiVecs)
return coeff
def computeLoss(tmpl, pred):
# L2 Loss
loss = float(np.real(np.sqrt(np.sum((pred-tmpl)*np.conj(pred-tmpl)))))
return loss
# Incomplete
def test(testList, eiVecs):
temp = map(lambda line: line.split(' '), testList)
testset = [ np.ravel(cv2.resize(cv2.imread(path, 0), dim)) for path in temp[:, 0]]
testLabels = temp[:, 1]
output = [ computeCoeff(img, eiVecs) for img in testset ]
data = load(tmplDir)
# TODO : Compute loss and give a match
return output
def saveTemplate(tmplDir, label, coeff):
np.save(tmplDir + os.sep + label, coeff)
def loadTemplates(tmplDir):
data = {}
for name in os.listdir(tmplDir):
if name.endswith(".npy"):
data[name.replace(".npy", "")] = np.load(tmplDir + os.sep + name)
return data
def loadModel(dataDir):
return np.load(dataDir + os.sep + "mean.npy"), np.load(dataDir + os.sep + "model.npy")
def saveModel(dataDir, mean, eiVecs):
np.save(dataDir + os.sep + "mean", mean)
np.save(dataDir + os.sep + "model", eiVecs)