-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_faces.py
58 lines (52 loc) · 1.81 KB
/
visualize_faces.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
import matplotlib
matplotlib.use("TkAgg")
#from keras.preprocessing.image import load_img, img_to_array
from keras.utils import load_img, img_to_array
import os
import numpy as np
from matplotlib import pyplot as plt
faces_dir = 'att_faces/'
X_train, Y_train = [], []
X_test, Y_test = [], []
subfolders = sorted([file.path for file in os.scandir(faces_dir) if file.is_dir()])
for idx, folder in enumerate(subfolders):
for file in sorted(os.listdir(folder)):
img = load_img(folder+"/"+file, color_mode='grayscale')
img = img_to_array(img).astype('float32')/255
img = img.reshape(img.shape[0], img.shape[1],1)
if idx < 35:
X_train.append(img)
Y_train.append(idx)
else:
X_test.append(img)
Y_test.append(idx-35)
X_train = np.array(X_train)
X_test = np.array(X_test)
Y_train = np.array(Y_train)
Y_test = np.array(Y_test)
subject_idx = 4
fig, ((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = plt.subplots(3,3,figsize=(10,10))
subject_img_idx = np.where(Y_train==subject_idx)[0].tolist()
for i, ax in enumerate([ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]):
img = X_train[subject_img_idx[i]]
img = img.reshape(img.shape[0], img.shape[1])
ax.imshow(img, cmap='gray')
ax.grid(False)
ax.set_xticks([])
ax.set_yticks([])
plt.tight_layout()
plt.show()
subjects = range(10)
fig, ((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = plt.subplots(3,3,
figsize=(10,12))
subject_img_idx = [np.where(Y_train==i)[0].tolist()[0] for i in subjects]
for i, ax in enumerate([ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]):
img = X_train[subject_img_idx[i]]
img = img.reshape(img.shape[0], img.shape[1])
ax.imshow(img, cmap='gray')
ax.grid(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("Subject {}".format(i))
plt.tight_layout()
plt.show()