-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataset.py
78 lines (64 loc) · 2.53 KB
/
Dataset.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
#!/usr/bin/env python
# Author: TOSSOU Aristide
import os;
from PIL import Image;
import numpy as np;
# Helper to load the Extended Yale Database
# data_home is the directory containing the list of directories corresponding to faces
# num_faces number of fdifferent people to take
# num_train number of faces per personn for training
# num_test number of faces per personn for testing
# h,w dimension for reducing the images
def loadD(data_home='.',num_faces=10,num_train=10,num_test=3,h=64,w=64,extension=('.jpeg','.jpg','.png','.pgm')):
X_train=[];
X_test=[];
y_train=[];
y_test=[];
rep=[];
target_names=[];
k=0;
for dirname, dirnames, filenames in os.walk(data_home):
for subdirname in dirnames:
rep.append(os.path.join(dirname,subdirname));
l=len(rep);
for i in range(l):
if(i>num_faces):
break;
target_names.append(os.path.basename(rep[i]));
k=0;
for dirname, dirnames, filenames in os.walk(rep[i]):
# Iterating over files in a given directory
for filename in filenames:
k+=1;
if(k<=num_train+num_test):
if(k<=num_train):
str1=os.path.join(dirname,filename);
if((str1.endswith(extension))==False):
k-=1;
continue;
im=Image.open(str1);
im=im.resize((h,w));
X_train.append(np.asarray(im));
y_train.append(i);
else:
str1=os.path.join(dirname,filename);
if((str1.endswith(extension))==False):
k-=1;
continue;
im=Image.open(str1);
im=im.resize((h,w));
X_test.append(np.asarray(im));
y_test.append(i);
# Transformation of the images
l=len(X_train);
X_train=np.array(X_train);
X_train=np.reshape(X_train,(l,h*w));
X_train=np.float32(X_train);
l=len(X_test);
X_test=np.array(X_test);
X_test=np.reshape(X_test,(l,h*w));
X_test=np.float32(X_test);
y_test=np.array(y_test);
y_train=np.array(y_train);
target_names=np.array(target_names);
return X_train,y_train,X_test,y_test,target_names;