-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict-image-pca.py
95 lines (74 loc) · 2.79 KB
/
predict-image-pca.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 sys
from helpers import image_util
import numpy as np
from numpy import linalg as la
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def predict(data, threshold):
# Step 1: Get normalize face and info
mean_face = get_mean_face('pca-model/mean-face.txt')
height, width = get_info('pca-model/info.txt')
# Step 2: Resize and convert to vector
resize_data = image_util.resize_image(data, width, height)
vector, _, _ = image_util.matrix2vector(resize_data)
# Plot mean face
plt.figure(2)
plt.axis("off")
plt.title('Mean Image')
plt.imshow(mean_face.reshape((height, width)), cmap=plt.get_cmap('gray'))
normalize_face = np.subtract(vector, mean_face) # N^2 x 1
# Step 3: Get eigenvectors from pca-model then calculate simplified face
u = get_eigenvectors('pca-model/list-eigenvectors.txt') # N^2 x k
w = np.dot(u.transpose(), normalize_face) # (k x N^2) x (N^2 x 1) = k x 1
simplified_normalize_face = np.dot(u, w) # (N^2 x k) x (k x 1) = N^2 x 1
simplified_face = np.add(simplified_normalize_face, mean_face) # N^2 x 1
# Plot simplified face:
plt.figure(3)
plt.axis("off")
plt.title('Simplified Image')
plt.imshow(simplified_face.reshape((height, width)), cmap=plt.get_cmap('gray'))
# Step 4: Calculate error detection
error = la.norm(np.subtract(normalize_face, simplified_normalize_face), axis=0)
print error / (width * height)
return error / (width * height) < threshold
def get_mean_face(file_path='pca-model/mean-face.txt'):
f = open(file_path, 'r')
data = f.read().strip(';')
return np.matrix(data)
def get_info(file_path='pca-model/info.txt'):
f = open(file_path, 'r')
data = list(f.readlines())
height = int(data[0].strip())
width = int(data[1].strip())
return height, width
def get_eigenvectors(file_path='pca-model/list-eigenvectors.txt'):
f = open(file_path, 'r')
samples = list(f.readlines())
f.close()
u = []
samples = [sample.strip() for sample in samples if len(sample.strip()) > 0]
for sample in samples:
sample = sample.strip(';')
u = append_column(u, np.matrix(sample).transpose())
return u
def append_column(arr, values):
if len(arr) == 0:
return values
return np.append(arr, values, axis=1)
if __name__ == '__main__':
filename = sys.argv[1]
try:
THRESHOLD = float(sys.argv[2])
except IndexError:
THRESHOLD = 0.55
data = image_util.load_image(filename)
if not image_util.is_grayscale(filename):
data = image_util.rgb2gray(data)
# Plot gray scale image
plt.figure(1)
plt.axis("off")
plt.title('Gray Scale Image')
plt.imshow(data, cmap=plt.get_cmap('gray'))
print predict(data, threshold=THRESHOLD)
# Show Plot
plt.show()